Controlling The Robot Using The IR Sensor

Now that we have added the motors and have the robot moving around, it’s time to use the Infrared sensor to make it a bit more intelligent.

Using the IR sensor, we can program the robot to stay on a table like this one:

As you can see, the table is black in the middle and has a white border. Since the IR sensor is on the bottom of the robot facing the floor, we will be able to tell whether we are currently over black or over white. Based on this information, we can make decisions on how the robot should behave.

For example to stay on the table a simple strategy could be:

This strategy is a starting point which you can build from.


Example Code:

In the driving motors blog post you wrote functions for going forward and reverse and turning. Now we will need to use those functions. The only difference this time is that when those functions are called is dependent on what the IR sensor value is.

You will first need to know roughly what value the IR sensor returns when the robot is on black and when it is on white.

This example is missing some parts that you will have to figure out:


The goal is program the robot so that it can drive around on the table for 60 seconds without falling off.

Try playing around with the timing so the robot will reverse for a bit longer or turn for longer etc. can you make the robot better at staying on the table?


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

1 Response to Controlling The Robot Using The IR Sensor

  1. Pingback: Blanchardstown Library 3 Day Workshop | RoboJAM!

Leave a Reply

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