Arduino car projects: Build an obstacle avoiding robot with less than $30

A lot of people want to start building robots but they don’t now where to start. It may seem too sophisticated for beginners when in reality it is not. Hopefully, there is an endless source of knowledge called internet, where you can find literally every information you want.

This is a good project that can help you build and program an obstacle avoiding car using the Arduino board.

Arduino car project

Here is what we want the car to do: navigate and avoid obstacles.

We could decide to add an option to control this car remotely using Bluetooth, but we will stick with the autonomous behavior for the sake of letting you grasp how simple working with Arduino is.

Our robot will sense obstacles with an ultrasonic sensor mounted on its front, the brain of it will be an Arduino UNO clone, we will use a cheap chassis that you can easily buy online with very popular cheap motors. To control the motors we will need 2 channel motor controllers also called H bridge (it is made out of transistors whose connections look like the letter H and that’s why it is named H bridge, this way of connection between transistors allows you to change the direction of rotation of a motor).

We will also need some cables to connect everything together. And just to let you know, soldering is not needed in this tutorial.

Here is a list of parts needed for this project:

That’s all you need and it shouldn’t cost more than $30. You can also find a lot of kits having all these components.

Let’s start by fixing all the components to the chassis. You can use M3 screws.

While assembling, make sure that you will be able to connect the cables to those components. Sometimes it is impossible to fix for example an Arduino with 4 screws so you can use 3 or at least 2 to make it rigidly tied to the chassis.

To fix the ultrasonic sensor, I used a 3D printed part that I designed in AutoDesk Fusion 360, you can download it here. If you do not have a 3D printer, don’t worry, you can find a cheap sensor holder on Amazon or simply mount it on the chassis the best way you can.

On the image above, you can see how I placed the mounting screw to receive the Arduino board. On the next image, the Arduino board is fixed to the chassis.

There were only 2 holes to mount the motor driver so I used them.

Here are all the connections we have to make. Different colors represent different signals and voltages. Before plugging in the battery, make sure that everything is connected correctly, otherwise you can burn something.

If a motor is rotating in the opposite direction than it should, you have to change its polarity (just swap the black cable with the red one).

The code


/*
* Code written by Nikodem Bartnk
* visit:
* instructables.com/member/Nikus/
* arduinopolska.cba.pl
* nikodembartnik.pl
*
* C by Nikodem Bartnik
*/

//speed of motors betwen 0 and 255, if you like you can change it
int pwm_speedA = 103;
int pwm_speedB = 80;
//trig of ultrasonic sensor
int trig = 12;
//echo of ultrasonic sensor
int echo = 13;

void setup() {
Serial.begin(9600);
//pins for motor controller
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
//set trig as output and echo as input for ultrasonic sensor
pinMode(trig, OUTPUT);
pinMode(echo,INPUT);

delay(3000);
}

void loop() {

 

 

//thats the sequence for ultrasonic sensor to start reading
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

//here we got travel time of sonic wave
int duration = pulseIn(echo, HIGH);
//and gere we calculate distance from it
int distance = (duration/2) / 29.1;
Serial.println(distance);
if(distance > 20){
//move forward by 100 ms
forward(200);
//when distance is smaller than 20cm we have to go back and turn
}else if(distance < 20){
motors_stop(1000);

//backward by 500ms = 0.5 second
backward(500);
//we got the random number and decide to go left or right
if(random(2) == 1){
left(600);
}else{
right(600);
}

}
}

// function for driving straight
void forward(int delay_time){
digitalWrite(10, HIGH);
digitalWrite(11, LOW);

digitalWrite(9, HIGH);
digitalWrite(6, LOW);

analogWrite(5, pwm_speedA);
analogWrite(3, pwm_speedB);
delay(delay_time);
}

//function for reversing
void backward(int delay_time){

digitalWrite(10, LOW);
digitalWrite(11, HIGH);

digitalWrite(9, LOW);
digitalWrite(6, HIGH);

analogWrite(5, pwm_speedA);
analogWrite(3, pwm_speedB);
delay(delay_time);
}

//function for turning left
void left(int delay_time){
digitalWrite(10, LOW);
digitalWrite(11, HIGH);

digitalWrite(9, LOW);
digitalWrite(6, LOW);

analogWrite(3, pwm_speedB);
analogWrite(5, 0);
delay(delay_time);
}

//function for turning right
void right(int delay_time){
digitalWrite(11, LOW);
digitalWrite(10, LOW);

digitalWrite(9, HIGH);
digitalWrite(6, LOW);

analogWrite(3, 0);
analogWrite(5, pwm_speedA);
delay(delay_time);
}

//function for stopping motors
void motors_stop(int delay_time){

digitalWrite(11, LOW);
digitalWrite(10, LOW);

digitalWrite(9,LOW);
digitalWrite(6, LOW);

analogWrite(5, 0);
analogWrite(3, 0);
delay(delay_time);
}

And now it’s time to upload the code into the Arduino board. Copy the code above, Open you Arduino IDE, paste the code and upload it to the board.

The code is a very simple one. It just lets the robot go forward as long as there is no obstacles in front of it, when it senses an obstacle, it randomly rotates left or right and goes forward again until the next obstacle has been found.

You don’t need to add any extra library to get this working, the code is all you need. It is so simple I think you will have no problem understanding it. It is a kind of perfect for beginners. As an exercise you can try writing your own program similar to this one, this way you will better understand how things work and hopefully be able to create a more advanced programs based on this project. You can also play with variables at the beginning of the programs. They determine the rotation time, frequency of sensor reads and so on.

Now we can test our robot. Put it on the floor and plug it to power. Your robot should start moving 3 seconds after connecting the battery.

tchouken2

Related Posts