Skip to content

Reflectance Sensor

Overview

Read a reflectance sensor, use a conditional to control an LED, and plan motor responses for a line-following robot.

Builds on

Before You Start

Use your Arduino upload and wiring skills from the first program.

Materials

  • Arduino Uno R3, USB cable, and Arduino IDE
  • Reflectance sensor module and jumper wires
  • Black and white surfaces

Safety

Disconnect power before rewiring

Unplug Arduino USB and disconnect or switch off every other supply before changing wires. Check polarity and connections before restoring power.

You've got it when…

  • Your code reads the sensor and controls the built-in LED; you have tested black and white surfaces.
  • Your notebook contains the wiring table, final code, and completed line-following truth table.

Collaboration & AI

Compare observations and troubleshoot with classmates. Each student must understand the wiring or design and prepare their own submission.

AI — AIAS Level 1, No AI: Complete the code or design yourself without AI assistance. AI Assessment Scale


Use the Reflectance Sensor

  1. Disconnect power. Wire the reflectance sensor using Table 1 and the Arduino Pinout. This sensor uses reflected light to distinguish nearby surfaces.

    Reflectance sensor wiring: Arduino 5V to VCC, GND to GND, and D2 to OUT

    Open the wiring diagram full-size. The FC-51 sensor and Uno R3 are shown with wires attached at their physical pin locations. The boards are drawn at different scales for readability. The Arduino is powered by USB. Wire crossings are not electrical junctions. Match the printed labels on your hardware. Component artwork: Fritzing; diagram licensed CC BY-SA 3.0.

    Table 1. Wire connections

    Reflectance sensor pin Arduino pin
    VCC 5V
    GND GND
    OUT 2
  2. Start an Arduino sketch. Add these constants above setup() and loop() so the pin numbers have readable names. The const keyword prevents a value from changing after initialization.

    const int pinReflect = 2;
    const int pinLed = 13;
    
  3. Add a variable below the constants to hold each sensor reading. It is not constant because its value changes as the program runs.

    int value;
    
  4. In setup(), set pinReflect to INPUT and pinLed to OUTPUT with pinMode().

  5. In loop(), read the sensor and copy its value to the built-in LED. Reconnect power after checking your wiring, upload, and test with an object in front of the sensor.

    value = digitalRead(pinReflect);
    digitalWrite(pinLed, value);
    
  6. Compare the Arduino LED with the sensor's onboard indicator. Are both on at the same time? Discuss what the reading means before changing the program.

  7. Replace loop() with this if/else statement and upload again. It reverses the LED response, turning the LED on when the sensor reads LOW.

    void loop() {
      value = digitalRead(pinReflect);
    
      if (value) {
        digitalWrite(pinLed, LOW);
      } else {
        digitalWrite(pinLed, HIGH);
      }
    }
    
  8. Hold the sensor over black and white surfaces at the same distance. Check whether your program distinguishes them. The example assumes detection gives a LOW reading; verify your sensor's actual behavior.

Simple Line Following Logic

  1. During the class activity, complete Table 2 in your notebook. Use the class's track layout and sensor placement to decide what each motor should do for each pair of readings.

    Table 2. Truth table for a line-following robot

    Left sensor Right sensor Left motor Right motor
    Black Black
    Black White
    White Black
    White White
  2. Copy Table 1 and your final Arduino code into the same notebook entry, alongside your completed truth table.

Turn It In

  1. Submit the following notebook entry:
  • Notebook entry: Table 1, final Arduino code, and completed Table 2

How It's Graded

One score from 0–4 covers the required work and evidence. Verification may be shown during the lab; the required uploads are listed above.

Score Evidence
4 Excellent The wiring table and complete final code agree; the sensor/LED response is verified on black and white surfaces, and all four truth-table rows match the class track arrangement.
3 Above Average The sensor code, wiring table, and truth table are complete and working, with only minor errors or omissions in clarity or verification.
2 Average The sensor code, wiring table, and truth table have significant gaps, errors, or missing evidence.
1 Below Average The required work is largely incomplete or nonfunctional, with little usable evidence.
0 Failing No submission or no evidence of the required work.

Converted from the original lab.