Built-in project ยท Arduino Uno R3

Obstacle sensor on pin 3 lights the LED on 13, on an Arduino Uno

An infrared obstacle sensor on pin 3: something in front of it pulls its output LOW, and the LED on pin 13 lights. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Infrared reflection sensing
  • A sensor output that goes LOW when triggered
  • Reading a digital input
  • Printing only on a change of state
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The FC-51 obstacle sensor has an infrared LED and a receiver side by side. When an object is close, it reflects the light back to the receiver, and the OUT pin goes LOW. OUT connects to pin 3.

The sketch reads pin 3 every 10 ms. When it reads LOW, it turns on the board's LED on pin 13. It prints obstacle or clear only when the state changes. Drag the obstacle closer in the simulator.

Parts list

3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Obstacle sensor, FC-51 pin GNDArduino Uno R3 pin GND
Obstacle sensor, FC-51 pin OUTArduino Uno R3 pin 3
Obstacle sensor, FC-51 pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino ยท Arduino Uno R3
// Obstacle sensor on pin 3 lights the LED on 13.
//
//   sensor OUT -> pin 3   VCC -> 5V   GND -> GND
//
// An infrared LED and a receiver side by side: a nearby object reflects the light back and the output goes LOW.
//
// Drag the obstacle closer.

const int SENSOR = 3;
const int LED_PIN = 13;
int last = -1;

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: obstacle sensor on pin 3, the LED on pin 13");
}

void loop() {
  int now = digitalRead(SENSOR) == LOW ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "obstacle" : "clear");
  }
  delay(10);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Makes SENSOR (pin 3) an input and LED_PIN (pin 13) an output. It starts the serial port at 9600 baud and prints a hello message.

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: obstacle sensor on pin 3, the LED on pin 13");
}

loop sketch.ino

Sets now to 1 when the sensor reads LOW, meaning an obstacle is near. It writes that to the LED. If now is different from last, it prints obstacle or clear. A 10 ms delay ends each pass.

void loop() {
  int now = digitalRead(SENSOR) == LOW ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "obstacle" : "clear");
  }
  delay(10);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED is on when nothing is there. This project
    The sensor reads LOW for an obstacle. Keep the test as == LOW.
  • Nothing happens when you move the obstacle. This project
    Check OUT on pin 3, VCC on 5V and GND on GND.
  • It detects nothing or always detects. Range depends on the pot and the surface, and black or shiny things reflect poorly. Obstacle sensor
    Turn the pot until the OUT LED switches at your distance. Test with a pale, matte surface. OUT is LOW when something is seen, so read it as active low.
  • Bright sunlight or an incandescent lamp makes it fire at random, because they give off infrared light. Obstacle sensor
    Shade the sensor or use it indoors. Power it from 3.3 V to 5 V, join the grounds, and keep it from facing other IR parts.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.