Introduction: How to Make an Obstacle Detecting Car
this is a car
Step 1: Getting Materials
you will need
- abra electronics geeekus gk kit 003 ($74)
- 2 non polarized capacitors ($8)
- h bridge($7)
- 2 electric motors ($8)
- arduino uno ($23)
- distance sensor ($8)
- wires($15)
- breadboard ($10)
Step 2: Assemble the Car
attach the arduino to the top of the car with screws and attach a small breadboard to the other side of the car
put the motors into the mounts and solder wires to the end of the motor terminals
Step 3: Wiring
follow this circuit except wire trig on the distance sensor to pin 8 and wire echo to pin 7
Step 4: Coding Part 1
------------------------
// for distance sensor
const int trig = 8;
const int echo = 7;
int duration = 0;
int inches = 0;
// for right motor const int controlPin1 = 5;
const int controlPin2 = 2;
const int enablePin1 = 9;
// for left motor const int controlPin3 = 3;
const int controlPin4 = 4;
const int enablePin2 = 11;
---------------------------------------
this goes before the void setup and sets up the pins
Step 5: Coding Part 2
--------------------------------
void setup() {
Serial.begin(9600);
// for distance sensor
pinMode(trig,OUTPUT);
pinMode(echo, INPUT);
// for right motor
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin1, OUTPUT); //turn the motor off, initially
digitalWrite(enablePin1, LOW);
// for left motor
pinMode(controlPin3, OUTPUT);
pinMode(controlPin4, OUTPUT);
pinMode(enablePin2, OUTPUT); //turn the motor off, initially
digitalWrite(enablePin2, LOW); }
----------------------------------------
this sets up the h bridge and the distance sensor
Step 6: Coding Part 3
------------------------------
void loop() {
// for distance sensor
digitalWrite(trig,LOW);
delay(0.001);
digitalWrite(trig,HIGH);
delay (0.01);
digitalWrite(trig, LOW);
duration = pulseIn(echo,HIGH);
inches = duration /74/2;
Serial.print("inches: ");
Serial.print(inches);
Serial.print("");
Serial.println();
delay (5);
if (inches <5){
analogWrite (enablePin1, 200);
digitalWrite (controlPin1, LOW);
digitalWrite (controlPin2, HIGH);
analogWrite (enablePin2, 200);
digitalWrite (controlPin3, LOW);
digitalWrite (controlPin4, HIGH); }
else if (inches >=5 and inches <9 ){
digitalWrite (controlPin1, LOW);
digitalWrite (controlPin2, LOW);
analogWrite (enablePin1, 0);
digitalWrite (controlPin3, LOW);
digitalWrite (controlPin4, LOW);
analogWrite (enablePin2, 0); }
else if (inches >=9){
analogWrite (enablePin1, 255);
digitalWrite (controlPin1, HIGH);
digitalWrite (controlPin2, LOW);
analogWrite (enablePin2, 255);
digitalWrite (controlPin3, HIGH);
digitalWrite (controlPin4, LOW); }
}
--------------------------
this makes it so that the car stops between 5 and 9 inches, move backwards between 0 and 5 inches, and moves forwards from 9 inches to infinity