Build a light barrier with an Arduino

FunduinoPublished on

Unterbrechungen erkennen und im Seriellen Monitor anzeigen

In dieser Anleitung nimmst du die Lichtschranke für Arduino-Mikrocontroller in Betrieb und lässt dir ihr Signal im Seriellen Monitor anzeigen.

Light barrier on Arduino with illuminated LED, light beam unobstructed
The light barrier LED is on - no interruption

You need this

U-shaped photointerrupter - 10 mm slot, digital output, 3.3-5 V U-shaped photointerrupter - 10 mm slot, digital output, 3.3-5 V 1,42 € *Item no. F23107474About the product — U-shaped photointerrupter - 10 mm slot, digital output, 3.3-5 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 4 parts together 17,18 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

How a light barrier works

A light barrier consists of a light source and a sensor for that radiation. The light source constantly emits either infrared or red light, which the sensor receives and converts into a digital signal.

If the signal is undisturbed, the microcontroller outputs the digital value 0 in the Serial Monitor. The light barrier’s LED also lights up red.

If an obstruction occurs - that is, an object enters the light barrier’s optical range - the digital value 1 appears in the Serial Monitor. At the same time, the light barrier’s LED goes out.

Serial monitor displaying light barrier signal 0 and 1
This is what an interruption looks like in the Serial Monitor.

Connect the light barrier

The light barrier has three pins:

  • VCC – Pin for the power supply, connected to the microcontroller’s 5V pin
  • GND – Ground pin, connected to the microcontroller’s GND pin
  • OUT – Digital signal output, connected to pin D8 of the microcontroller

Read the photointerrupter

With this sketch, you can read the signal from the photoelectric sensor and display it in the Serial Monitor. The two images show the setup once with the light beam unobstructed and once with it interrupted.

Light barrier on Arduino with illuminated LED, light beam unobstructed
The light barrier LED is on - no interruption
Light barrier on Arduino with the LED off, object interrupts the light beam
The LED is off – the light beam is interrupted
1 / 2
Program code – read the light barrier
int messwert = 0; // Unter der Variablen "messwert" wird später der Messwert der Lichtschranke gespeichert.
void setup()
{ // Hier beginnt das Setup.
  Serial.begin(9600); // Die Kommunikation mit dem seriellen Port wird gestartet. Das brauchst du, um dir den ausgelesenen Wert im Seriellen Monitor anzeigen zu lassen.
}
void loop()
{ // Hier beginnt der Hauptteil.
  messwert = digitalRead(8); // Die Signalausgabe der Lichtschranke wird ausgelesen und unter der Variablen "messwert" gespeichert. Wenn die Lichtschranke unterbrochen wird, speichert der Mikrocontroller den Wert "1", ansonsten "0".
  Serial.print("Lichtschrankensignal="); // Ausgabe im Seriellen Monitor: "Lichtschrankensignal="
  Serial.println(messwert); // ... und im Anschluss der Messwert: "1" bei unterbrochener Lichtschranke, ansonsten "0".
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.