Light Seeking R.C Car Hack (with Arduino)

by Zaphod Beetlebrox in Circuits > Robots

26662 Views, 50 Favorites, 0 Comments

Light Seeking R.C Car Hack (with Arduino)

fiished1.jpg
Creating a robotics platform from scratch takes allot of work and a few dollars. Buying a ready built one is easy but costs allot of money (at least for me). So instead I decided to piggy back off of the companies who make remote control cars.
This is a great because it comes ready made with two h-bridges and a steering mechanism already built. The cost of the whole project is probably $90 if you don’t have any of the materials but for me I was about $3(for the batteries).
This test shows how the robot works. I didn't have a large enough space to test it while it moved.  And I also had it running the dark seeking sketch. 
Because i'm entering this in the robot contest I'd like to say i'm 13-18 years old and belong to the 401 venture company (scouts).  


Materials

parts1.jpg
computer and usb1.jpg
third hand1.jpg
parts21.jpg
Materials:
1x Arduino (I used an Uno but any will do)
1 x Breadboard
1 x 9v Battery
1 x 9v to Arduino cable (link to make your own)
1 x Arduino to USB cable
1 x R.C Car
2 x LEDs (hopefully the same)
3 x AA batteries (for the car)
About 50cm of four strand wire
15 – 20 jumper wires
A few elastics
Some electrical tape
Some solder (lead free)

Tools:
Soldering iron
Wire strippers
Small screwdrivers
Third Hand (for soldering)
Computer (with Arduino software installed)

Taking Apart the Car

body off1.jpg
The first thing that we need to do is take the body of the car. Depending on which car you have you may require some strange screwdrivers. Luckily mine came with standard screws. Also depending on the car you may have to cut off the antenna don’t worry we don’t need it for this project (remember to keep it as it comes in handy latter).
Next we need to take the screw out of the PCB (Printed Circuit Board) so that we can more easily access the brain of the car.

Figuring Out the Chip

pcb with chip1.jpg
chip out1.jpg
In order to replace the car’s on board chip with the Arduino we must first find out what each pin on the on board chip controls. To do this Google the name and number of your chip (most cars use this one).If you can’t find a diagram online you can manually find out what each pin does using this guide.

Once the use of each pin is known we can remove the chip (if yours doesn’t have a diagram draw your own or label the chip). I found that because I don’t have a solder braid trying to remove the chip was tricky. The best way I found was to thread the antenna of the car (or similar piece of thin wire) under one pin, then melt the solder and pull up on both ends of the antenna. However the important thing is that you get the chip out.

Basic Movement

soldering wires to pcb1.jpg
all wires soldered to pcb1.jpg
basic movement all1.jpg
basic movement breadboard to arduino1.jpg
Basic movment all done1.jpg
The first thing that we need to do is solder a piece of wire (10 – 15 cm) to the holes in the PCB that control left, right, forward, backward, and ground (or negative). Once that is done connect the negative wire to ground on your breadboard and then from ground on the breadboard to the gnd pin on the Arduino. Next connect the forwards wire (directly or through the breadboard) to pin 7 on the Arduino. The others follow the same method
Left to pin 2      right to pin 4    and backward to pin 8

THE CODE
This sketch will test out each of the car’s basic movements if your car fails to perform any of the actions double check all connections and ensure that it is connected to the right pin on the PCB and on the Arduino.
Basic Movements Sketch: copy and paste or download the file at the bottom of the page and open it with Arduino.exe
int left = 2; // left connected to pin 2
int right = 4; // right connected to pin 4
int forward = 7; //forward to pin 7
int backward = 8; // backward to pin 8

void setup() // happens only once when the Arduino is turned on
{
    // setting all the pins to outputs
   pinMode (left,OUTPUT);
  pinMode (right,OUTPUT);
  pinMode (forward,OUTPUT);
  pinMode (backward,OUTPUT);
}
// creating functions so we don’t have to type as much
void go_forward() {
  digitalWrite (forward,HIGH);
  digitalWrite (backward,LOW);
}
void go_backward() {
  digitalWrite (backward,HIGH);
  digitalWrite (forward,LOW);
}
void go_left() {
  digitalWrite ( left,HIGH);
  digitalWrite (right,LOW);
}
void go_right () {
  digitalWrite (right,HIGH);
  digitalWrite (left,LOW);
}
void go_stop() {  // could't use just stop because it's taken
  digitalWrite (right,LOW);
  digitalWrite (left,LOW);
  digitalWrite (forward,LOW);
  digitalWrite (backward,LOW);
}
void loop() { // runs over and over again until Arduino is turned off
go_forward();
delay (1000); // car goes forward for one second

go_backward();
delay (1000); // then goes backwards for a second

go_right();
go_forward();
delay (1000); // car will make a right turn for a second

go_left();
go_forward();
delay (1000); //car will make a left turn for a second

go_stop();
delay(1000); // car will stop for a second
}
STOP COPYING HERE

Incorporating the Light Sensing LEDs

soldering led1.jpg
LEDs with long wires1.jpg
LEDs wired up1.jpg
LED sensing test.jpg
LED diffrence.jpg
All LEDs produce a small voltage when in contact with light; we will use this feature to sense light with two normal LEDs. If you have photocells you can use them to but you will need to change the code.
First solder a piece of wire (40 – 50 cm) to each leg of the LEDs. Connect the negative ends to ground on the breadboard and the positive end of the left led to analog pin 5 on the Arduino. Also the positive end of the right led to analog pin 4 on the Arduino.

MORE CODE

This sketch will print the value of each led to the Serial monitor via a USB connection.
Place the car (or just the two LEDs) in direct light so each LED receives an equal amount. Look at the values in the serial monitor, this will show by what amount the LEDs differ in their light conductive properties. My LEDs were off by about 2-3 when in direct light. Remember (or write down) how much you’re LEDs differ when in direct light as we will use this value later. Also check how much the LED’s values change when one is in complete darkness and the other in light. If one (or both) of your LEDs are reading in the thousands while under a light bulb you probably have them wired wrong.
LED Values sketch:

//LED values sketch

int ledleft = A5; // positive wire of the left led connected to analog pin 5
int ledright = A4; //  positive lead of the right led connected to analog pin 4
int lf = 0; // used to store the value of the left led
int lr = 0; // used to store the value of right led

void setup() {
pinMode (ledleft,INPUT);// this is not necessary because analog pins
pinMode (ledright,INPUT);  // have a default setting to be inputs but i did it anyway
Serial.begin(9600); // gets Arduino ready to send info to the computer
}
void loop() {
  lf = analogRead (ledleft); // sets lf to the vale of the leftled
  lr = analogRead (ledright); // same thing but for the right led
 
  Serial.println ("left led ="); // prints what is in brackets to the computer
  Serial.println (lf); // prints the value of the left led
  Serial.println ("/t"); //prints a tab
  delay(500); // waits half a second
 
  Serial.println ("right led ="); //prints what’s in quotes
  Serial.println (lr); // prints value of right led
  Serial.println ("/t:"); // prints a tab
  delay(500); // waits half a second
}// goes to top of the loop
STOP COPYING HERE

Bringing It All Together

fiished1.jpg
Now we need to tell the motors what to do based on what the values of the LEDs are. I used a do while loop with if statements inside it.  There are many other ways to write this code but I found this way to work well. I’m not a programmer so it probably isn’t the most efficient code but it works.

THE FINAL CODE:

Light seeking robot sketch:

int left = 2; // left motor control attached to pin 2
int right = 4; // right motor control attached to pin 4
int forward = 7; // forward motor control attached to pin 7
int backward = 8;// backward motor control attached to pin 8
int ledleft = A5; // left light sensing led attached to analog pin 5
int ledright = A4; // right sensing led attached to analog pin 4
int lf = 0; // stores value of left led
int lr = 0; // stores value of right led
int x = 0; // used to store absolute value of the two leds
int i = 1; // only used to keep the do while loop open

void setup() //does only once when the Arduino is turned on
{
  pinMode (left,OUTPUT); // setting types of pins for each motor and led
  pinMode (right,OUTPUT);
  pinMode (forward,OUTPUT);
  pinMode (backward,OUTPUT);
  pinMode (ledleft, INPUT);
  pinMode (ledright, INPUT);
 
}
void go_forward() {    // creating functions to reduce the amount of typing later
  digitalWrite (forward,HIGH);
  digitalWrite (backward,LOW);
}
void go_backward() {
  digitalWrite (backward,HIGH);
  digitalWrite (forward,LOW);
}
void go_left() {
  digitalWrite ( left,HIGH);
  digitalWrite (right,LOW);
}
void go_right () {
  digitalWrite (right,HIGH);
  digitalWrite (left,LOW);
}
void go_stop() {         
  digitalWrite (right,LOW);
  digitalWrite (left,LOW);
  digitalWrite (forward,LOW);
  digitalWrite (backward,LOW);
}
void loop() {  // goes over and over until turned off
lf = analogRead (ledleft); // sets lf to the value of the left led
lr = analogRead (ledright); // sets lr to the value of the right led
x = abs ( lf - lr); // sets x to the difference between the two leds Google it if you want more info on how it works

do {
   if (x < 10) { //change the ten to the amount that your leds were of by when they were both in direct                                                                                                               //light. This is the sensitivity of your robot and will take some trial and error to get right            
     go_forward();
     delay(500);
     go_stop();
     break; // exits the do while loop and goes back to the top
   }
   if ( lr > lf) { // turns left when there is more light on the left side of the car
     go_right();
     go_forward();
     delay (500);
     go_stop();
     break; // goes back to the top of the loop and reevaluates the leds values
   }
   if (lf > lr) { // turns right if there is more light on the right of the car
  go_left();
  go_forward();
  delay(500);
  go_stop();
  break;  //goes back to the top of the loop and reevaluated the leds values
   }
  
  
} while (i == 1); // keeps the do while control loop open
}
STOP COPYING HERE

Final Things

First elastic band the breadboard and the Arduino onto the car, next attach the 9v battery to the car and plug it into the Arduino using the 9v to Arduino cable. Also remember to put batteries into the car and turn it on.  
Finished your R.C car should now seek out the light. If not then double check all the connections and make sure nothing is shorting out. If your car just goes straight then your sensitivity is to low and you need to decrease the x < __ number. If your car constantly switches directions your sensitivity is too high and the x < __ number needs to be larger. And make sure the batteries aren’t dead : )
If you have any problems comment them and please rate. Thanks for reading (and building?)