Introduction: Obstacle Avoidance Car
This is a simple, yet easy and fun to make robotics project. it features an Arduino connected to 2 little n20 motors, (they only draw between 15 t0 20ma, so it is safe) an ultrasonic sensor (HC-SR04) housed in a 3d printed body (u can use anything it doesn't matter :) . It is powered by 2 1200mah cells and the motors are connected to n20 wheels. you can also put on a castor wheel, as in this case i used an n20 castor wheel. it doesn't require a motor driver which might be a big relief to you since every other obstacle avoidance car uses a motor driver and 80% percent of the people dont have that.
Supplies
- n20 castor wheel
- ultrasonic sensor
- structure for robot
- 2x 3.7v li-ion cells (16046)
- li-ion cell holder with barrel jack
- Arduino uno
- n20 wheel
- n20 motor 6v 150 rpm (geared)
Step 1:
- connect the the vcc of the ultrasonic sensor to the 5v of arduino
- connect trig of the sensor to pin 2
- connect echo of the sensor to pin 3
- connect gnd of the sensor to gnd of the arduino
Step 2:
- connect the positive terminal of the left motor to pin 12
- connect the negative terminal of the left motor to pin 8
- connect the positive terminal of the right motor to A1
- connect the negative terminal of the right motor to A0
Step 3:
the code-
// C++ code
//
int distance = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
pinMode(8, OUTPUT);
pinMode(12, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
}
void loop()
{
if (0.01723 * readUltrasonicDistance(2, 3) < 25) {
digitalWrite(8, LOW);
digitalWrite(12, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(8, LOW);
digitalWrite(12, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
delay(2000); // Wait for 2000 millisecond(s)
} else {
digitalWrite(8, LOW);
digitalWrite(12, HIGH);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
delay(2000); // Wait for 2000 millisecond(s)
}
}
the file attachment is below:)