Introduction: RC Car Anti-Crash System Using Arduino

Hi guys! So for my electronics class this year we were tasked with creating some kind of electronic gadget in order to demonstrate what we learned over the semester. Being a car-loving type of guy, I decided to get a cheap RC car and see in what ways I could hack it using an Arduino Leonardo I had leftover from lab. Having an infrared proximity sensor handy, I thought It would be nice to use the sensor to brake the car whenever It got too close to crashing into a wall. I'll try to be as clear as possible in case you guys want to build something similar or in case it'll help with one of your projects. 

Step 1: What You'll Need

Here's a list of what I ended up using for this project.

• Arduino Leonardo (or equivalent) with mini-USB cable
• Computer with proper Arduino programs installed
• Remote Controlled Car
• Sharp Distance Sensor 2D120X  (Infrared Proximity Sensor)
• SN754410 H-Bridge
• Heat Sink for the SN754410 H-Bridge
• Oscilloscope (plus Voltage Probe) or Multimeter
• Breadboard (for prototyping)
• Philips Screwdriver
• Wire Cutters
• Wire Strippers
• Electrical Wire
• Perfboard (for the final circuit)
• Solder and Solder Gun
• Hot glue gun
• 2x 9v Batteries (one for the Arduino and one for the Remote Control)
• 4x AA Batteries (for the car)
• Helping-Hand tool (optional)
• Alligator Clips (optional)
• Zipties (optional)

Step 2: The RC Car

First off, I needed to get an inexpensive RC car I could hack. I didn't  want to get an expensive RC car in case I ended up ruining it over the course of my project so I ended up get this ~$20 car off of Amazon. Overall, there were no big surprises with this little car. The car required 4 AA batteries while the remote control it came with required a 9v battery, sadly the batteries were not included. Anyways, the next step was to open up the car and see what I was dealing with.


Step 3: Opening It Up

The Basics:
The car opened up without too much trouble, it just had a couple of screws on the bottom. Not knowing what to really to expect, I was kind of surprised to see how straightforward a setup the car  actually employed. Essentially there are three main parts; the circuit board, the rear wheel drive DC motor, and the DC motor used for turning. I'll discuss these in more detail in the next step.

The Remote Control:
I also opened up the remote control in order to see what type of input the car would be expecting. It also had a relatively simple set up, each control was basically dependent on two switches. For example, in order to control direction you'd press the switch corresponding to desired direction.

Since the car did not have variable speed or direction, I concluded that the car most likely dealt with digital logic. In other words, I was expecting to see the voltages used to either go HIGH or LOW. 

Step 4: Analyzing the Insides

The Receiver:
Once I got the car opened I tried to work out how the DC motors were being controlled. I decided to look up the chip placed in the middle of the circuit board to see exactly what it did. I discovered that it's a receiver chip with four pins used to control both direction and speed. Flipping the circuit board over, I saw the pair of H-Bridges used to drive the DC Motors.They basically provide enough current to make sure the motors run. I'll talk about H-Bridges a bit more later.

The Motors:
Looking at the circuit board closely, I saw that it had a power and ground  connection going to the battery pack. The other connections went to the motors. Each motor was connected to the circuit board by two wires (one black, one red) and each wire was labeled depending on its function. For example one of the wires was labeled MF for Move-Forward (by my best guess).

The Overall Setup:
My best guess for how the car runs is that it essentially uses the receiver to process signals from the remote control. The receiver then sends a pair of  HIGH or LOW signals to each DC motor.  

Step 5: Measuring Voltages

The next step was to measure the voltages used in the circuit so I'd know how I'd use the Arduino to control the car. For this step, I used an oscilloscope in order to measure the voltages, however, you can easily use a multimeter instead.

Looking at the Receiver:
To begin I measured the voltages coming out of the receiver's pins as I used the remote control. When a pin would go HIGH the voltage on it would be approximately 3.40 volts. When the pin went LOW it would be at about zero volts. These voltages are within the range necessary for the Arduino to correctly read them as HIGH or LOW. 

Looking at the Motors:
I then measured the voltages on the connections going to the DC motors. When input to the motor went HIGH the voltage would range from 4.80 to 5.00 volts. And of course, when the input was LOW the voltage was about zero.

Depending on the selected speed or direction, the connections going to the motors would alternatively go HIGH and LOW. For example, when the car would turn left the wire labeled ML (Move-Left) would go HIGH and the wire labeled MR (Move-Right) goes LOW. The opposite is true when going right, and when there is no input both wires are LOW.

Step 6: Setting Up a Prototype: the H-Bridge

Okay,if we're going to use the Arduino to drive the DC motors in the car we are going to need an H-Bridge in our circuit. I could have just used the H-Bridges which are already integrated into the car's circuit, but I found it easier to just use my own chip instead of deciphering the manufacturers circuit. The H-Bridge I used was the SN754410 which is used to drive small DC motors.

Some Background Info:
Basically, an H-Bridge lets you switch the polarity on a DC motor so can run it both clockwise and counterclockwise. Its called an H-Bridge because of the shape of the circuit. The circuit itself has four switches and depending on how you close each switch, you can run current through the motor in different directions giving you the ability to run a motor in two directions.

The Half-Bridges and Full-Bridges
A half-bridge accepts one digital input and has a corresponding digital output. However, the output only works if the corresponding Enable pin on the chip is set HIGH, the chip is powered (VCC is set to 5 volts), and the Ground pins are actually grounded. The half-bridge lets you drive a DC motor in one direction. Two half-bridges create a full-bridge which lets you drive a DC motor in two directions.

The SN754410 H-Bridge is a Quadruple Half-Bridge driver meaning that it has two full-bridges. Each side is a full-bridge and is capable of driving a DC motor in two directions. 

Step 7: Setting Up a Prototype: Testing the Circuit

Using a breadboard and some electrical wire used for prototyping, I set up the H-Bridge and the Arduino in order to drive the DC motors on the car.

Driving One Motor:
I started simple by driving just one motor to begin with.  I connected the Enable pin and the two Logic inputs from the H-Bridge to three of the Digital pins on the Arduino. Setting the Enable pin HIGH (with the Arduino's digitalWrite() command), I could then make the motor turn clockwise or counterclockwise depending on which of the two Logic inputs I set HIGH and which I set LOW. I included the H-Bridge logic table in the pictures for reference. Connecting the H-Bridge's output pins to the DC motor I could make the tires turn Left/Right (or Forward/Backward depending on the motor).

Testing the Circuit:
In order to test the circuit I wrote simple Arduino code that would make the tires turn left and right every other second (or forward and backward for the other motor). This consisted of alternating which Arduino pins connect to the Logic inputs were set HIGH and LOW.Just remember that the oscilloscope and the Serial.println() command on the Arduino are your friends when troubleshooting your circuit.

To test the IR Proximity Sensor, I used the same test code as before except I read in the sensor value through an Analog pin. I then used an if statement and the sensor value to control when my test code would run. 

Driving Two Motors:
Driving two motors is no different than driving just one. Its the same deal since the H-Bridge chip is symmetrical. For the anti-crash system you really only need to control the Forward/Backward motor but by connecting the second motor you can program the car run by itself (e.g. do figure eights, have an autonomous mode sort of like a Roomba,etc. ). The soldering and wires in my car were not the best so I chose to control both motors with the Arduino.

Sensor Datasheet:
http://www.phidgets.com/documentation/Phidgets/3520_0_Datasheet.pdf

I included the H-Bridge datasheet as a PDF below.





Step 8: Soldering Together the Final Circuit

Soldering:
Once I got a working prototype the next step was to recreate the circuit on a perfboard and solder it together.This part can get a bit tricky because of the soldering but no worries, I'm a beginner at soldering and I managed to get it working. You'll need wire cutters, wire strippers, solder and a solder gun, and of course some electrical wire.

I went about creating the final circuit by recreating the circuit I made on the breadboard one wire at a time. I first soldering my chip in place and then from there I went about soldering pieces of wire according to my breadboard circuit making sure It looked more or less identical. The Helping-Hand came in handy in this part since it involved flipping the perfboard over and over again to check whether I was making the right connections.

Testing It Again:
Once I was finished, I tested my final circuit with the same code I tested my breadboard circuit with. If you got the breadboard circuit working with this code you should be able to get the final circuit working with the same code. Use the oscilloscope to make sure each pin on the H-Bridge has the correct voltage you'd expect.

Step 9: Powering It Up

Okay all that's really left is powering the Arduino when it's not connected to a computer. I planned on using a 9 volt battery so i needed to solder together a battery connector. You can get these connectors already soldered but, in case you have to piece one together yourself here is how you do it.

Building the Battery Holder:
To begin, twist together the red and black wires and place the plastic covering over the wires. Make sure that you can screw the metal connector back in correctly so that it looks like the final product.

Once you have the wires going through the plastic covering, you'll want to solder together the red wire to the middle piece on the metal connector. You then have to solder the black wire to the outer piece on the metal connector. All you have to do then is screw the metal connector back in and you're done. 

Step 10: Finishing It Up

The Final Touches:
Once I finished my final circuit and obtained power for my Arduino, I set up the components inside the car. I used a hot glue gun to keep the perfboard and battery holder in place. In order to place the sensor in front I unscrewed  the car's windshield a bit and fed the sensor through a little gap. I then hot glued it in place at the front. Once I had all the components in place, I uploaded some code into the Arduino, connected the power supply, and screwed in the plastic covering back into place.

Where We're At Now:
Okay, the original plan was to connect the receiver output to the Arduino so the car would run normally until it got too close to a wall.However...when I tried soldering connections to the receiver's pins the receiver gave up on me. I actually got it to the point where I could read the correct voltages from the connections I soldered to the receiver but, the pins broke off after a while. 

Plan B:
So now that I can't control the car using the remote control I decided to program the car and make it autonomous. I'm still in the process of making it completely autonomous (sort of like a Roomba) but, I have at least gotten it to the point were it senses a wall and  it stops. I can also make the front tires turn based on commands from the Arduino. I'll include the snippet of code that I know works for sure below.

Possible Things for the Future:
I hope to get the car to the point where it is completely autonomous. Also, I believe there's a way to return remote control capabilities to the car using a Bluetooth module like the BlueSMiRF Silver. Such a module would allow me to control the car from a Bluetooth enabled computer or phone which would be cool. Something else that I noticed is that the batteries I've used have been drained quickly. This means that I might have to change the way I'm powering the circuit. In other words, I'd have to use something other than a 9v battery. 

If I can, I'll post any further developments I make. But for now, I'm just glad It can sense walls and stop accordingly. 
Thanks for reading and I hoped you've found this informative.

Code:
//Label the pins we will be using
int enable12=7;
int input1=6;
int input2=5;

int enable34=4;
int input3=3;
int input4=2;

void setup(){
  Serial.begin(9600); //Set baud rate

  //Set up the necessary pins as outputs
  pinMode(enable12,OUTPUT);
  pinMode(input1,OUTPUT);
  pinMode(input2,OUTPUT);

  pinMode(enable34,OUTPUT);
  pinMode(input3,OUTPUT);
  pinMode(input4,OUTPUT);

  //Set the Enable pins to HIGH
  digitalWrite(enable12,HIGH);
  digitalWrite(enable34,HIGH);
}

void loop(){
  int sensorValue=analogRead(A2); //read in the sensor value
  Serial.println(analogRead(A2)); //allow us to see the
  //sensor value for troubleshooting

  delay(1); //small delay for stability

  if (sensorValue < 150){
    goForward();
  }
  else {
    stopMovement();
  }

}

//Functions used to control the car's movement
void goForward(){
  digitalWrite(input1,HIGH);
  digitalWrite(input2,LOW);
}

void goBack(){
  digitalWrite(input1,LOW);
  digitalWrite(input2,HIGH);
}

void stopMovement(){
  digitalWrite(input1,LOW);
  digitalWrite(input2,LOW);
}

void goLeft(){
  digitalWrite(input3,HIGH);
  digitalWrite(input4,LOW);
}

void goRight(){
  digitalWrite(input3,LOW);
  digitalWrite(input4,HIGH);
}

void stopTurning(){
  digitalWrite(input3,LOW);
  digitalWrite(input4,LOW);
}