Arduino: build light barrier - how it works
This practical tip shows you how you can easily build a light barrier with your Arduino.
Build a light barrier with Arduino: the required hardware
To build a light barrier with the Arduino, you need a fork light barrier, as well as an Arduino and various cables.
- First, with a 220 Ω resistor in between, connect an LED to a ground on the Arduino and to pin 13.
- Then flip the fork light barrier over and make sure the emitter (B) is on top and the photoresistor (D) is on the bottom.
- Now connect a cable to the 5V pin of the Arduino, attach a 220 Ω resistor to it and connect it to the cable at the top right of the light barrier.
- The pin on the light barrier on the right is connected to a ground. In the same way, the pin at the bottom left of the light barrier is connected to a ground.
- Now it gets a little complicated: Also connect another cable to the 5V pin of the Arduino and connect a 10 kΩ resistor to it. Then this cable is connected to the pin at the bottom right of the light barrier. However, you have to insert another cable between the connection of the 10 kΩ resistor and the light barrier, which is connected to the pin Analog 0 (A0) on the Arduino. The best way to do this is on a breadboard.
Arduino: program light barrier
After you have connected everything correctly, you can now write a suitable program for the Arduino. We have given you a first example here:
- int sensorPin = A0;
- int sensorValue = 0;
- int ledPin = 13;
- void setup () {
- pinMode (ledPin, OUTPUT);
- Serial.begin (9600);
- }
- void loop () {
- sensorValue = analogRead (sensorPin);
- Serial.println (sensor value);
- if (sensorValue> 512) {
- digitalWrite (ledPin, HIGH);
- } else {
- digitalWrite (ledPin, LOW);
- } delay (1000);
- }
In the next practical tip we will show you how to set up a NAS server with your Raspberry Pi.