Using an Infrared Sensor to Detect Obstructions with Arduino
Infrared sensors are a simple and effective way to detect the presence of an object. In simple terms, they work by emitting infrared light and detecting the reflection of that light. If the IR sensor doesn’t detect any reflected IR light, then there is no obstacle, however if that light is reflected and detected by the sensor, then the sensor knows there is an obstacle in front of it. We will be using the EK1254 IR Sensor as it is cheap and easily available. It is also very easy to configure and use with Arduino. There are other more advanced options that allow you to control the IR light output. These would help if you want to estimate the distance of an obstacle, but we are simply going to learn how to detect the presence of an obstacle.
Let’s start by figuring out the parts and materials we will need, then learn how to wire the EK1254 to the Arduino and finally write some code to control it and read back the sensor data.
Step 1: Gather Materials:
EK1254 Sensor
Arduino UNO
Breadboard
Breadboard jumper wires
Computer with Arduino IDE installed
I will be using an Arduino UNO in this tutorial, however, other Arduino boards such as the Nano will work. The breadboard makes setting this project up much quicker and easier. It also lets you re-use the components after finishing this project. If you are building this as a permanent solution feel free to solder it all together! We will need 3x Male to Male jumper wires to connect the pins on the EK1254 infrared sensor to the Arduino.
Step 2: Wire Everything Together
Let’s start by putting the EK1254 into the breadboard. You want to put it in the breadboard so that each pin is in its own row, or component rail. See Figure 1 below as a reference.
Now lets connect the wires:
Connect the Ground Pin (Gnd) from the EK1254 to a Gnd pin on the Arduino board
Connect the Voltage In pin (Vcc) from the EK1254 to the 5v out on the Arduino
Connect the Out pin from the EK1254 to pin 12 on the Arduino
Step 3: Write and Upload the Code
First make sure you have the Arduino IDE installed: https://www.arduino.cc/en/software
Now that everything is wired and ready, all we need to do is write the code to control the EK1254. We will also set up serial monitoring so we can read back the data from the sensor to see what’s going on. Go ahead and follow the code snippet below:
const int outputPin = 12; //Define the OUT Pin void setup() { pinMode(outputPin, INPUT); //Set Output Pin Pin as INPUT Serial.begin(9600); } void loop() { if(digitalRead(outputPin) > 0) { //If no obstacle is detected Serial.println("No obstacle detected!"); } else { //Otherwise (Obstacle Detected) Serial.println("Obstacle detected!"); } }
Alright! Now let’s save and upload the code to the board. Make sure your Arduino board is plugged into your computer with the USB cable.
Go to Tools > Board: > Arduino AVR Boards > Arduino Uno
2. Go to Tools > Port: > select the correct port for your Arduino… It will usually have be the usbmodem port, but this will vary between operating systems.
3. If you haven’t already, go ahead and save your Arduino sketch. You can do this from the file menu, or by pressing the Verify button. If you haven’t already saved your file, clicking the Verify button will prompt you to do so. It will then proceed to “verify” your code and check it for any errors.
4. Click the upload button to upload the code to your Arduino board
Step 4: Power Up and Test!
Now that everything is good to go, keep your Arduino plugged into your computer so that we can monitor the serial data and keep it powered.
When you upload the code to your Arduino, the board automatically resets and begins executing your code. We can now open the Serial Monitor to monitor the serial data from the EK1254 sensor.
When you put an object in front of the sensor it reports object detected! You can use this for all sorts of applications like object avoidance, triggering events, light up a warning LED when an object is detected, and an infinite number of other possibilities!