10

Lesson 10: Analog Sensors and Data Collection

Learn to read analog sensors for robot environmental awareness

Learning Objectives

Knowledge Goals:

  • Understanding analogRead() function
  • Converting sensor values to voltage
  • Sensor calibration techniques

Skill Goals:

  • Reading multiple analog sensors
  • Data processing and filtering
  • Robot environmental sensing

Lesson Content

1 Analog Sensors and analogRead() (20 minutes)

Analog Sensor Basics:

  • • analogRead() returns values from 0-1023 (10-bit ADC)
  • • Represents voltage from 0V to 5V
  • • Perfect for reading light, distance, and temperature
  • • Essential for robot environmental awareness
// Basic analog sensor reading
int sensorPin = A0;
int sensorValue;
float voltage;

void setup() {
    Serial.begin(9600);
    Serial.println("🤖 Analog Sensor Reader Ready!");
}

void loop() {
    sensorValue = analogRead(sensorPin);
    // Convert to voltage (0-5V)
    voltage = (sensorValue / 1023.0) * 5.0;
    Serial.print("Raw: ");
    Serial.print(sensorValue);
    Serial.print("  Voltage: ");
    Serial.println(voltage);
    delay(500);
}

2 Common Robot Sensors (20 minutes)

Sensor Types for miniAuto:

  • • Light sensors (photoresistors) for line following
  • • Distance sensors for obstacle avoidance
  • • Temperature sensors for environmental monitoring
  • • Potentiometers for user input and calibration
// Multi-sensor robot system
int lightSensor = A0;
int distanceSensor = A1;
int tempSensor = A2;

void setup() {
    Serial.begin(9600);
    Serial.println("🤖 Multi-Sensor Robot Online!");
}

void loop() {
    int lightLevel = analogRead(lightSensor);
    int distance = analogRead(distanceSensor);
    int temperature = analogRead(tempSensor);
    
    Serial.print("Light: ");
    Serial.print(lightLevel);
    Serial.print("  Distance: ");
    Serial.print(distance);
    Serial.print("  Temp: ");
    Serial.println(temperature);
    
    delay(1000);
}

3 Data Processing and Calibration (15 minutes)

Why Process Sensor Data:

  • • Raw sensor values need conversion to meaningful units
  • • Calibration improves accuracy and reliability
  • • Filtering reduces noise for stable readings
  • • Essential for consistent robot behavior
// Sensor calibration and filtering
int lightMin = 1023;  // Start with max
int lightMax = 0;     // Start with min

void calibrateSensor() {
    Serial.println("🔧 Calibrating light sensor...");
    Serial.println("Cover and uncover sensor for 5 seconds");
    
    unsigned long startTime = millis();
    while (millis() - startTime < 5000) {
        int reading = analogRead(A0);
        if (reading < lightMin) lightMin = reading;
        if (reading > lightMax) lightMax = reading;
        delay(10);
    }
    
    Serial.print("✅ Calibrated! Min: ");
    Serial.print(lightMin);
    Serial.print("  Max: ");
    Serial.println(lightMax);
}

int getCalibratedLight() {
    int raw = analogRead(A0);
    // Map to 0-100 percentage
    return map(raw, lightMin, lightMax, 0, 100);
}

Assessment & Homework

Quick Check (In Class):

  • • Can student read analog sensors correctly?
  • • Do they understand voltage conversion?
  • • Can they implement basic sensor calibration?

Homework Assignment:

Environmental Monitor

Create a sensor system that monitors light and temperature, calibrates the sensors, and provides meaningful readings for robot decision making.

← Previous: Lesson 9
📝 Take Lesson 10 Quiz Next: Lesson 11 →