Adding an Infrared Sensor to the Arduino Robot

The TRCT5000 IR Sensor:

Adding an infrared sensor to the bottom of a robot  facing towards the ground is a common way to do things like line tracking and edge detection. In this post we will take a look at how to add a TCRT5000 IR Sensor to the Arduino robot.

This is the sensor we will be using:

 

 

 

 

 

 

If we look a bit closer at the back of the board you can see the sensor has 4 pins:

 

 

 

Wiring The Sensor:

These pins are Vcc, Gnd, Do, and Ao.

  • Vcc is your positive voltage input and should be connected to the positive power rail of the breadboard.
  • Gnd is the ground or negative connection and should be connected to the negative power rail of the breadboard.
  • Do can be left unconnected, we won’t be using it.
  • Ao is an analogue output. This means that the voltage at this pin will vary between 0-5 Volts. In order for our Arduino to read this analog voltage, it needs to be connected to an analog input pin. Connect it to A0 on the Arduino.

Take a look at the wiring diagram for a visual representation:

Example Code 1 – Looking At Sensor Values:

Now that the sensor is hooked up to the Arduino, it’s time to check if it is working correctly. You can copy and paste the example code below in the Arduino IDE and upload it to your Arduino:

Once this code has been uploaded to your Arduino, you can open the Arduino Serial Monitor to take a look at the values getting returned.

 

Experiments:

The best way to figure out how the sensor works is to have a play around with it. I recommend using the Arduino Plotter tool found in ‘Tools’> ‘Serial Plotter’. This will show a graph of the sensor values. You can then move your hand in front of the sensor or move the sensor and you should see the values change. This is great for figuring out how the sensor works.

 

Answer these questions:

What is the range of the sensor?

What is the value returned when something is right in front of the sensor?

What is the value returned when there is nothing in front of the sensor?

Find a piece of paper that is dark coloured (black ideally) and another that is white. Hold the black paper in in front of the sensor and record the value. Then place the white paper in front of the sensor at the same distance and record the value. Which returns a higher value ? Darker objects or brighter objects ?

 

Blink An LED:

The whole point of adding the IR sensor to the robot is so the robot can make decisions based on the sensors input. Eventually you will program to robot to drive around a table like this one:

The table is black with a white border. Using the IR sensor, the robot will be able to tell whether it is on black or white and make decisions based on that.

For now, let’s simplify this idea and have an LED turn on when the sensor is a certain value. Here is some sample code to get the ball rolling. You can add it to the end of the void loop() function in the program above.

 

 

 

 

This entry was posted in Tutorial and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *