The RedBot has three "line following" sensors (left, center, and right) which can be used to detect lines on a surface.
The bottom of each sensor has an LED that transmits infrared (IR) light, which is invisible to the human eye. The bottom of each sensor also has an IR detector, which measures how much of the IR light is reflected back by the surface that the robot is driving on.
The amount of reflected IR light that is detected depends on several factors, including the color of the surface, as well as the distance between the sensor and the surface:
A light-colored surface will reflect more IR light, while a dark-colored surface will reflect less IR light.
If the surface is farther away, the IR light will become more scattered, and less IR light will be reflected back to the detector.
Each IR line sensor is connected to the RedBot circuit board by a 3-wire jumper cable (white, red, and black wires for data, power, and ground):
The left line sensor data wire should be connected to I/O pin A3
The center line sensor data wire should be connected to I/O pin A6
The right line sensor data wire should be connected to I/O pin A7
The IR sensors measurements can be used to perform several useful robot behaviors:
The robot can follow a line by adjusting the left and right motor powers to steer the robot and keep it centered on the line as it drives.
The robot can avoid a line by turning away from a line that it detects. In this case, the line acts as a border to keep the robot inside (or outside) a certain path or area.
The robot can count lines that it crosses while driving and then stop once it reaches a desired line number. This allows the robot to navigate using a pattern of line markers.
The robot can follow a line while counting lines that it crosses and then stop once it reaches a desired line number. This allows the robot to navigate using a pattern of intersecting line paths.
The robot can detect a surface drop-off (such as: stair step leading down, hole, etc.) and take actions to protect itself (brake, reverse, change direction, etc.).
To use the IR sensors in your robot app, you will need to:
Create a RedBotSensor
object for each IR sensor (left, center, and right)
Use each IR sensor object's read()
method to get a measurement
Add code statement(s) to perform action(s) based on the IR sensor measurements
The SparkFun RedBot
library has a class named RedBotSensor
which defines methods (functions) to control analog sensors, such as the IR line following sensors.
Before the setup()
function, create a RedBotSensor
object for each IR sensor by assigning each object a variable name and indicating its pin number within parentheses:
REDBOT LIBRARY: Be sure your robot app has an #include
statement for the SparkFun RedBot library. Here's how to include the RedBot library.
To check the measurements from the IR line following sensors, use the RedBotSensor
object's read()
method to get a measurement from each sensor:
leftLine.read()
centerLine.read()
rightLine.read()
The read()
method will return an int
value (integer) between 0-1023 that represents a measurement of how much reflected IR light was detected:
Lower values indicate more IR light was reflected back. This indicates a lighter-colored surface.
Higher values indicate less IR light was reflected back. This indicates a darker-colored surface.
If the values are very high, this probably indicates a surface drop-off (such as: a stair step leading down, the edge of a table, a hole in the surface, etc.).
Since you will typically want to compare the readings from all 3 sensors at the same time, your code could assign the sensor readings to local variables, and then perform actions based on the values stored in those variables:
You will need to add code to do something based on the sensor readings. For example, you might use if
statements to perform certain actions if one or more sensor readings are greater than (or less than) a specific value.
To test out your IR sensors, you can view the sensor measurements using the serial monitor in the Arduino code editor.
Add this code statement within the setup()
function:
This starts a serial data connection between your robot and your computer and sets the data transfer rate to 9600 bits per second.
A custom function named testLineSensors()
can be used to read each IR sensor and send (print
) the measurements to your computer as serial data.
Add the testLineSensors()
function after the loop()
function:
Add this code statement within the loop()
function to call the custom function:
This should be only code statement listed within the loop()
function.
After uploading the app to your robot, do not unplug the USB cable. You have to keep the robot connected to your computer to allow the serial data communication.
In your Arduino code editor, open the serial monitor, so you can view the serial data:
Arduino Create (Web Editor): Click the Monitor menu link in the left navigation to display the serial monitor in the middle panel.
Arduino IDE (Desktop Editor): Under the Tools menu, select "Serial Monitor." A new window will appear displaying the serial monitor.
It may take a few seconds for the serial connection to be detected by the editor. Then you should see the sensor measurements being displayed in the serial monitor window.
You can try the following tests to see how the sensor measurements change:
Create a dark line on a sheet of white paper. Position the robot's IR sensors on the dark line to view the measurements. Then position the IR sensors on the white paper to compare the measurements. Try testing other surface colors.
Try slowly lifting the front edge of the robot off the table to see how the sensor measurements change with distance from the surface.
Manually roll the robot towards the edge of a table to see how the measurements change when the sensors are hanging over a surface drop-off.