This post may contain affiliate links. See the affiliate disclosure for more information.
If you liked my article on Controlling an Arduino Mobile Robot using a Bluetooth Keyboard, then you’re in for a real treat! This week’s article will be focused on using an Infrared (IR) sensor to control the Arduino Robot using a TV remote.
Before we get started, let’s learn about Infrared signals and sensors, and how the robot will use them to make decisions.
All things Infrared
He forgot to mention robots in his video, but that’s what this tutorial is for!
Determine what code is being sent to the IR receiver, then command the robot to move according to the signal.
Step 1 – Download and Add the IR Remote Library from Github
Rafi Khan was kind enough to write an IR Remote Library, which will simplify our development for this project. Go ahead and download a copy of his library, here.
Next, you’ll need to add the .ZIP library to Arduino using the Library Importer. Navigate to Sketch > Include Library > Add .ZIP Library and select the .ZIP folder you just downloaded.
Step 2 – Run the Test Remote Code
Part A. Open up the Example Program, IRrecvDemo.ino (File > Examples > IRremote > IRrecvDemo)
Part B. Connect the IR Sensor to Pin 11 on your Arduino (any available PWM pin is fine, just make sure you change the value of
int RECV_PIN = 11; to the pin you have your sensor plugged into.)
Part C. Upload the code to your Arduino. Open up the Serial Monitor. Then press some buttons on your remote. You should see a different HEX value displayed in the serial monitor for each button you press. Here’s a visual of what it should look like. (Your HEX Values may be different). The row of F’s signifies that a different HEX code has been received.
Step 3 – Map Each Button to a Control Function
We’re going to modify this IRrecvDemo code to receive and return the
results.value as an unsigned long. This will make it slightly easier to compare readings rather than converting to HEX. Once we have a reading, we will then initiate the
robot.go() or move command on the robot.
Can you move?
If you haven’t written the code to initiate your motors (i.e. you cannot move forward, back, left, right, or stop), then YOU MUST write that code first. If your robot uses an L298N motor controller, then feel free to use my Create Robot library. Install this library like we did for the IR Remote library and be sure to follow the README.md for wiring and programming instructions. Once you have your move code configured and working, proceed to the following steps.
The thought process. Make sure you read this paragraph!
Using either if-statements, we can check each
results.value reading and then initiate the robot movement. For example, if the “Up Arrow” on my TV remote is pressed, I may want the robot to move forward. We’ll first obtain the remote value for Up Arrow, then we’ll call move forward to make the robot move in the forward direction. Then we’ll repeat this for every direction. Sound simple enough?
Step 3 – Test Out the Code
Go ahead and watch the recap video. Sometimes a picture (or video) is worth a thousand words.
From the video you’ll see that I like to print out a String that says what I’m going to do when the remote button is pressed. That way, I can test to see if the code works before putting in the motor control. If it works, it’s just a matter of writing the line of code that controls the direction of the motor. If not, I can troubleshoot to see what needs to happen.
While you don’t need to do it that way, it’s an easy way to test your program while tethered to your computer. No need to worry about the robot flying off the table!
IR Remote + Arduino Robot = Simple RC Control
As you can see, it’s not too difficult to use a TV remote to control your Arduino Robot. All you need is an IR Receiver, the IR Remote Library, and a little time to develop your movement code. Using if-statements and logic comparisons, we can click a button on our remote to command the robot in a direction.
While I provided the skeleton for using the IR remote to control the robot’s movement, I challenge you to use the other buttons to control speed, sensors, and modes. And if you do take me up on that challenge, PLEASE share your project with me on Facebook or Instagram (tag @learnrobotics)! I enjoy seeing what my readers are up to!
And that’s a wrap!
Thanks for checking out this week’s article! If you enjoyed this tutorial, and want to see more like it, please leave a comment below.