Building Your First Arduino Robot Car

Building Your First Arduino Robot Car

This robot car is very simple. It uses ultrasonic sensors to measure the distance in front and the Arduino controls the car to move forward, backward, or turn left and right based on the received distance. In other words, it has its own way of “thinking” and can wander around a room without bumping into anything as long as the power is on and no one interferes.

First, let’s look at the effect:

Building Your First Arduino Robot Car

Building Your First Arduino Robot Car

Building Your First Arduino Robot Car

The camera aperture has been opened wider~~ You can only see the effect, the detailed wiring may not be clear, haha..

If you are also interested in this, you can join me in making such a ROBOT.

Materials needed:

1. An Arduino board, I am using the Arduino Duemilanove 2009 – ATMega328P, because I think it has the best cost performance. Arduino UNO can also be used.

Building Your First Arduino Robot Car

2. An ultrasonic distance measuring module

Building Your First Arduino Robot Car

Building Your First Arduino Robot Car

3. DC motor + tires

Building Your First Arduino Robot Car

4. An L298N motor driver module. It is best if the motor driver has an optocoupler, otherwise it may interfere with the ultrasonic signal.

Building Your First Arduino Robot Car

5. Universal wheels + car chassis + Dupont wires + screws, nuts + soldering iron + screwdriver + scissors, etc.

Universal wheels can be purchased on Taobao.

The car chassis is used to fix the motor and circuit board, you can choose to make it yourself with PVC board or buy it ready-made.

Once you have all the materials, you can connect them together. The wiring method can be seen from the comments in the program. Initially, I planned to use a servo to control the ultrasonic module’s direction, but later found it too cumbersome to fix the servo, so I didn’t use it. The Android program is as follows:

Building Your First Arduino Robot Car

#define DEBUG 0    // set to 1 to print to serial monitor, 0 to disable#include <Servo.h>
Servo headservo;  // Head servo object
// Constants
const int EchoPin = 2; // Ultrasonic signal input
const int TrigPin = 3; // Ultrasonic control signal output
const int leftmotorpin1 = 4; // DC motor signal output
const int leftmotorpin2 = 5;
const int rightmotorpin1 = 6; const int rightmotorpin2 = 7;
const int HeadServopin = 9; // Servo signal output, only pins 9 or 10 can be used
const int Sharppin = 11; // Infrared input
const int maxStart = 800;  // run dec time
// Variables
int isStart = maxStart;    // Start
int currDist = 0;    // Distance
boolean running = false; 
void setup() {
  Serial.begin(9600); // Start serial monitoring
  // Signal input interface  pinMode(EchoPin, INPUT);  pinMode(Sharppin, INPUT);
  // Signal output interface  for (int pinindex = 3; pinindex < 11; pinindex++) {    pinMode(pinindex, OUTPUT); // set pins 3 to 10 as outputs  }
  // Servo interface  headservo.attach(HeadServopin);
  // Start the buffer activity head  headservo.write(70);  delay(2000);  headservo.write(20);  delay(2000);}
void loop() {
  if(DEBUG){    Serial.print("running:");    if(running){      Serial.println("true");     }    else{      Serial.println("false");     }}
  if (isStart <= 0) {    if(running){      totalhalt();    // stop!    }    int findsomebody = digitalRead(Sharppin);    if(DEBUG){      Serial.print("findsomebody:");         Serial.println(findsomebody);       }        if(findsomebody > 0) {      isStart = maxStart;     }    delay(4000);    return;  }  isStart--;  delay(100);
  if(DEBUG){    Serial.print("isStart: ");    Serial.println(isStart);    }
  currDist = MeasuringDistance(); // Read front distance  if(DEBUG){    Serial.print("Current Distance: ");    Serial.println(currDist);    }  
  if(currDist > 30) {    nodanger();  }  else if(currDist < 15){    backup();    randTrun();  }  else {    // whichway();    randTrun();  }}
// Measure distance in centimeters
long MeasuringDistance() {  long duration;  //pinMode(TrigPin, OUTPUT);  digitalWrite(TrigPin, LOW);  delayMicroseconds(2);  digitalWrite(TrigPin, HIGH);  delayMicroseconds(5);  digitalWrite(TrigPin, LOW);
  //pinMode(EchoPin, INPUT);  duration = pulseIn(EchoPin, HIGH);
  return duration / 29 / 2;}
// Move forward
void nodanger() {  running = true;  digitalWrite(leftmotorpin1, LOW);  digitalWrite(leftmotorpin2, HIGH);  digitalWrite(rightmotorpin1, LOW);  digitalWrite(rightmotorpin2, HIGH);  return;}
// Move backward
void backup() {  running = true;  digitalWrite(leftmotorpin1, HIGH);  digitalWrite(leftmotorpin2, LOW);  digitalWrite(rightmotorpin1, HIGH);  digitalWrite(rightmotorpin2, LOW);  delay(1000);}
// Choose route
void whichway() {  running = true;  totalhalt();    // first stop!  headservo.write(20);  delay(1000);  int lDist = MeasuringDistance();   // check left distance  totalhalt();      // Restore probe  headservo.write(120);  // turn the servo right  delay(1000);  int rDist = MeasuringDistance();   // check right distance  totalhalt();      // Restore probe  if(lDist < rDist) {    body_lturn();  }  else{    body_rturn();  }  return;}
// Reset to initial state
void totalhalt() {  digitalWrite(leftmotorpin1, HIGH);  digitalWrite(leftmotorpin2, HIGH);  digitalWrite(rightmotorpin1, HIGH);  digitalWrite(rightmotorpin2, HIGH);  headservo.write(70);  //  set servo to face forward  running = false;   return;  delay(1000);}
// Turn left
void body_lturn() {  running = true;  digitalWrite(leftmotorpin1, LOW);  digitalWrite(leftmotorpin2, HIGH);  digitalWrite(rightmotorpin1, HIGH);  digitalWrite(rightmotorpin2, LOW);  delay(1500);  totalhalt();}
// Turn right
void body_rturn() {  running = true;  digitalWrite(leftmotorpin1, HIGH);  digitalWrite(leftmotorpin2, LOW);  digitalWrite(rightmotorpin1, LOW);  digitalWrite(rightmotorpin2, HIGH);  delay(1500);  totalhalt();}
void randTrun(){  long randNumber;  randomSeed(analogRead(0));  randNumber = random(0, 10);  if(randNumber > 5) {    body_rturn();  }  else  {    body_lturn();  }}

Leave a Comment

×