Saturday, October 19, 2013

Giving RC Car a simple brain using Arduino + Ping (Ultrasonic) Sensor

It has been quite a sometime from my last post. Mainly due to busy schedules and settling down in Malaysia I would say. But I came to back to Sri Lanka on vacation and got time on a lazy Saturday morning and put together a small Arduino + RC Car setup.

In one of my previous posts I teared down a RC Car and explained the internals of that. Working further on that I managed to write a small Arduino sketch to work with a Ping Sensor to give a simple brain to the RC Car. What makes this distinguishable from other RC Car + Arduino hacks is that, Almost all other hacks use the Arduino to control the Remote Control not the Car itself. But I wanted to control the car directly using the Arduino so that I can eventually achieve the goal of using an Android phone connected to Arduino using a USB Host Shield to act as the Smart Brain of the RC Car.

The following items were used in setting up this prototype.
  1. 1 Arduino UNO Rev 3
  2. 1 Breadboard
  3. 1 DFRobot URM37 V3.2 Ultrasonic Sensor
  4. Some Jumper Cables
  5. And of course a RC Car
Following is the Arduino Sketch. The DFRobot Ultrasonic Sensor was used in TTL Mode and Arduino Pin 3 was connected to PWM and 5 was connected to COMP/TRIG of the Sensor. The Arduino Digital Out pin 11 was used for forward actuating and pin 10 was used for backward actuating of the RC Car . I have soldered jumper cables to the RC Car so that they can be easily used and distinguished. 

int URPWM = 3; 
int URTRIG=5; 
int forwardPin = 11;
int backwardPin = 10;
 
unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01};    
boolean forward = false;
boolean backward = false;
int minDistanceCm = 5;

void setup(){                                 
  pinMode(URTRIG,OUTPUT);                     
  digitalWrite(URTRIG,HIGH);                  
  
  pinMode(URPWM, INPUT);                      
  pinMode(forwardPin, OUTPUT);
  pinMode(backwardPin, OUTPUT);
}
 
void loop()
{
   digitalWrite(URTRIG, LOW);
   digitalWrite(URTRIG, HIGH);               
   unsigned long DistanceMeasured=pulseIn(URPWM,LOW);
     
   if(DistanceMeasured==50000){              
         
   } else {
      Distance=DistanceMeasured/50;           
   }
  
   if(Distance > minDistanceCm) {
      forward = true;  
      backward = false;
   } else {
      forward = false;
      backward = true; 
   }
   
   if(forward) {
      digitalWrite(forwardPin, HIGH);
      delay(100);
      digitalWrite(forwardPin, LOW);
   } else {
      digitalWrite(backwardPin, HIGH);
      delay(200);      
      digitalWrite(backwardPin, LOW);     
   }
   delay(400);
}                      
 


The video below shows the prototype in action. The USB cable is only used to supply electricity for the Arduino. Adding a battery will eliminate the need for the USB cable.


This prototype is by no means complete. I did this so that I can achieve the goal of using an Android phone with Camera + GPS as the brain. But this step was a necessary and crucial learning stage for me. Constructive criticism is always welcome.

References
  1. DFRobot Ultrasonic Sensor Manual - http://www.dfrobot.com/wiki/index.php/URM37_V3.2_Ultrasonic_Sensor_(SKU:SEN0001)

No comments:

Post a Comment