Section outline

  • Coding Lab – Multi-Sensor Data Display

    Now that we understand how multiple sensors can work together, it’s time to bring it all into a visual format. In this coding lab, we will learn how to read sensor data and display it on the Serial Monitor for debugging, analysis, and interaction.

    • 🎯 Objective:

      Build an Arduino sketch that reads values from multiple sensors (IR, Ultrasonic, Temperature, etc.) and prints them clearly on the Serial Monitor.

      📦 What You’ll Need:

      • 1 x Ultrasonic sensor (HC-SR04)
      • 2 x IR sensors (line tracking type)
      • 1 x Temperature sensor (e.g., LM35 or DHT11)
      • Arduino board + USB cable
      • Jumper wires and breadboard

      🔌 Circuit Setup Overview:

      • Connect ultrasonic sensor to digital pins (e.g., 7 for Trigger, 6 for Echo)
      • Connect IR sensors to digital pins (e.g., 2 and 3)
      • Connect temperature sensor to analog pin (e.g., A0)
    • 💻 Sample Code:

      long readUltrasonicDistance() {
        digitalWrite(7, LOW);
        delayMicroseconds(2);
        digitalWrite(7, HIGH);
        delayMicroseconds(10);
        digitalWrite(7, LOW);
        return pulseIn(6, HIGH) / 58;
      }
      
      void setup() {
        Serial.begin(9600);
        pinMode(2, INPUT);
        pinMode(3, INPUT);
        pinMode(7, OUTPUT); // Trigger
        pinMode(6, INPUT);  // Echo
      }
      
      void loop() {
        int irLeft = digitalRead(2);
        int irRight = digitalRead(3);
        long distance = readUltrasonicDistance();
        int tempReading = analogRead(A0);
        float voltage = tempReading * 5.0 / 1024.0;
        float temperatureC = voltage * 100;
      
        Serial.print("IR Left: ");
        Serial.print(irLeft);
        Serial.print(" | IR Right: ");
        Serial.print(irRight);
        Serial.print(" | Distance: ");
        Serial.print(distance);
        Serial.print(" cm | Temp: ");
        Serial.print(temperatureC);
        Serial.println(" C");
      
        delay(1000);
      }

      📊 What to Observe:

      • Watch how the data updates every second
      • Try placing your hand in front of the ultrasonic sensor and see the change
      • Warm up the temperature sensor gently and observe value increase
      • Move the robot over dark/white surfaces to test IR values

      🔍 Debugging Tips:

      • If no values are showing, check sensor connections and power
      • Use Serial.println() to isolate specific values
      • Use LEDs or buzzers to provide physical feedback for sensor ranges

      This lab sets the foundation for real-world monitoring. In later courses, we’ll use LCD screens or Bluetooth apps to view sensor data remotely!