RoverBot: IR

by makveen in Circuits > Arduino

269 Views, 0 Favorites, 0 Comments

RoverBot: IR

IMG_0201.jpeg

I built an IR remote-controlled car using an Arduino board, but it’s far more than just a simple toy. This car boasts advanced features like an autopilot single-object avoidance mode and serves as a versatile platform for creating intricate projects, such as a home-cleaning robot or a pet entertainer. Here’s a breakdown of how it works:

The Arduino

The Arduino UNO serves as the “brain” of the project. It stores and executes the code we write, even when powered by a battery instead of a computer. Its versatility makes it an excellent choice for this project.

Powering the Motors

While the Arduino board controls the system, it can’t directly power the motors. That’s where the L293D Motor Driver Shield comes in. This shield, which mounts on top of the Arduino, provides the necessary power to the motors. Many motor driver shields are limited to motor-related functions, but the Moonpreneur model offers additional pins, allowing us to connect other components easily.

IR Sensors for Remote Control

The IR sensors enable remote control functionality. By identifying the unique values associated with each button on the remote, we can program the car to respond accordingly. Each button does a specific action, such as moving the car or activating autopilot mode.

Supplies


NOTE: THE KIT WITH THE MOTOR DRIVER SHIELD HAS ALL THE COMPONENTS YOU NEED INCLUDING SCREWS

1 Moonprenur L293D Motor Driver Shield for Arduino Uno (comes in a kit) Link

2 Single Shaft Hobby Gear-motors with Wheels (may need to solder wires) Link

Shorting Caps Link

1 Ultrasonic Sensor (need a plastic head that attaches to servo motor, but the one in kit works) Link

1 microServo Motor Link

1 16x2 LCD Screen with I2C Link

1 Battery Set Link

1 Battery Box (varies, always double check)

1 IR Receiver & Remote Link

F2F Jumper Wires Link

Spacers, Screws, Castor Wheel, and Chassis included in kit.


Laptop with Arduino IDE

download link Link

Gather Your Materials

IMG_0131.jpeg

Make sure you have all your materials. While building, you might face challenges with compatibility, so always make sure to double check before your order anything. Finding the right components is NOT EASY, so don't get discouraged if you can't find what your looking for right away.

In the supplies list, I've linked the products, but again, double check before you buy anything.

Install the Arduino IDE

Screenshot 2024-12-21 at 4.57.28 PM.png
Screenshot 2024-12-21 at 5.08.20 PM.png

To code the robot, we need the Arduino IDE, which uploads the code to the Arduino board.


First, go to this LINK, or search up "Arduino IDE Download," and click on the first link, which will open the Arduino website. You'll see a list of all the IDEs, for each OS. Download the one that's for your computer.

Assembling the Arduino on the Chassis

IMG_0137.jpeg
IMG_0136.jpeg
IMG_0135.jpeg
IMG_0138.jpeg

Adding the parts is time consuming. Make sure you have all the spacers and screws for Arduino.

To add the Arduino Uno on in the back of the chassis, first add the spacers and screw it in from underneath the chassis, then put the board on top and screw it into place. Then, add the motor driver shield, and connect the motor's wires.

Assembling the Battery Box on Chassis

IMG_0139.jpeg

The battery box is our main source of power. Add the screws, and secure it with the nut from underneath the chassis. Them connect the wire the comes out from the battery box into the motor driver shield. Make sure the screws you are using are flat, so that you can put the batteries in securely.

Installing LCD Display on Chassis

IMG_0142.jpeg
IMG_0141.jpeg
IMG_0140.jpeg

The LCD can provide important information about our vehicle status. Instead of the usual setup, I've used a I2C integrated LCD board.

First, add the spacers and secure them from the back of the car. Then, put the board in place then screw it in, and connect the jumper wires into the motor driver shield.

Installing the Castor Wheel

FullSizeRender.jpeg
IMG_0164.jpeg

Instead of 4 motors, I used two and added a castor wheel in the front for ease of precise movements. This way, we can also improve the battery life of the robot, since the castor wheel doesn't require any power.

Screw the first screw to get the wheel in place. Then, screw in the other two screws. Make sure you tighten it well, because we don't want it to jerk out of place when the car turns.

Installing the IR Sensor

IMG_0175.jpeg
IMG_0182.jpeg

First, put the IR receiver in the back, and attach it the the motor driver shield. Make sure + and - are in the right place and the signal pin will be in place. I've attached a video on how to attach the receiver.

Downloads

Installing the Ultrasonic Sensor

IMG_0178.jpeg
IMG_0177.jpeg
IMG_0176.jpeg

This is the last step in assembling the robot!! After this, we can proceed to coding the robot!

The ultrasonic sensor is essential in making our autopilot. Place it on the servo motor, which we attach to the chassis using screws and bolts. Then, we place the ultrasonic sensor in place and screw it as well. Connect the jumper wires for the ultrasonic sensor and servo motor to the motor driver shield.

Adding the Shorting Caps

IMG_0185.jpeg

Add shorting caps to the Inverter functions pins, because this will help us move our car easily. Make sure you put it on Inverter and not "non-inverter" because that will make the code longer.

Prepping for Code

Screenshot 2024-12-29 at 2.14.38 PM.png
Screenshot 2024-12-29 at 2.18.36 PM.png
IMG_0179.jpeg

Now that we're done assembling the robot, we can start to code it. Open a new sketch in the Arduino IDE, and download IRremote library by shrriff, and LiquidCrystal I2C by frank.

Then, connect the wire to the Arduino from the USB port, and select "Arduino Uno" on the "Select Board & Port" option.

Getting the IR Remote Codes

Screenshot 2024-12-30 at 4.40.26 PM.png

Now, we're going to get the codes for all our IR remote buttons. This will help us code the robot using case statements.

Copy and paste this code in your Arduino IDE. Open Serial Monitor, and write down the codes for all the buttons (or at least the ones you plan to use). We'll need this for the next step.

This code makes the IR receiver print the codes in the Serial Monitor.



#include <IRremote.hpp>
#define USE_IRREMOTE_HPP_AS_PLAIN_INCLUDE

void setup()
{
Serial.begin(9600);
IrReceiver.begin(2);
}

void loop()
{
while(IrReceiver.decode() == 0)
{ }

if(IrReceiver.decode())
{
unsigned long value = IrReceiver.decodedIRData.decodedRawData;
Serial.print("Key Value in Decimal = ");
Serial.println(value, DEC);
}

IrReceiver.resume();
}

Coding the Robot

Screenshot 2024-12-30 at 3.24.08 PM.png
Screenshot 2024-12-30 at 3.24.18 PM.png
Screenshot 2024-12-30 at 3.24.25 PM.png
Screenshot 2024-12-30 at 3.24.29 PM.png
Screenshot 2024-12-30 at 3.24.35 PM.png

Time for coding! Here's the code you need to upload into your Arduino.

The first part of the code is including libraries, defining variables, entering in pinModes, and all the work we need to make the rest of the code work. In the second part, we're telling the Arduino to:

  1. Not do anything if no buttons are pressed.
  2. Printing the code of each button in the Serial Monitor.
  3. Using switch and creating a case statement for each button so that we can make it do what we want it to do.
  4. Creating functions for the movements of the car.

CODE:

#include <Wire.h>
#include <Servo.h>
#include <IRremote.hpp>
#include <LiquidCrystal_I2C.h>

#define USE_IRREMOTE_HPP_AS_PLAIN_INCLUDE

const int en1 = 5;
const int en2 = 6;
const int in1 = 10;
const int in2 = 12;
const int buzz = 11;
const int echo = A1;
const int trig = A2;


int left = 0;
int right = 0;
int front = 0;

int distance;
int duration;

Servo servo;
LiquidCrystal_I2C lcd(0x27, 16,2);

void setup()
{
lcd.init();
lcd.clear();
lcd.backlight();
servo.attach(9);
Serial.begin(9600);
IrReceiver.begin(2);
pinMode(en1, OUTPUT);
pinMode(en2, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}

void loop()
{
while (IrReceiver.decode() == 0)
{}
if (IrReceiver.decode())
{
unsigned long code =
IrReceiver.decodedIRData.decodedRawData;
Serial.println(code);


switch(code)
{
case 3877175040: //forward
moveForward();
break;

case 2907897600: // back
back();
break;

case 4144561920: // left
moveLeft();
break;

case 2774204160: //right
moveRight();
break;

case 3910598400: //autopilot
servo.write(90);

front = readDist();
Serial.print("frontVal:");
Serial.println(front);

if(front < 20)
{
stop();
delay(1000);
back();
delay(1000);
servo.write(0);
delay(500);
right = readDist();
Serial.print("rightVal:");
Serial.println(right);
servo.write(180);
delay(500);
left = readDist();
Serial.print("leftVal:");
Serial.println(left);

if(left < right)
{
moveRight();
delay(500);
}
else if(right > left)
{
moveLeft();
delay(500);
}
}
else
{
moveForward();
delay(500);
}
delay(10000);
stop();
break;

}

}
IrReceiver.resume();
}

int readDist()
{
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(2);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration * 0.0344/2;
return distance;
}


void moveForward()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
analogWrite(en1, 240);
analogWrite(en2, 255);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("FORWARD");
delay(2000);
stop();
}

void back()
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(en1, 240);
analogWrite(en2, 255);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("BACK");
delay(2000);
stop();
}

void moveRight()
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(en1, 200);
analogWrite(en2, 200);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("RIGHT");
delay(500);
stop();
}

void moveLeft()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(en1, 200);
analogWrite(en2, 200);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("LEFT");
delay(500);
stop();
}

void stop()
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(en1, 0);
analogWrite(en2, 0);
lcd.clear();
lcd.setCursor(4,0);
lcd.println("STOP");
}

Enjoy!

You finished the IR remote controlled car! Have fun with it, and play around with the code to see what else you can incorporate into the robot!

Downloads