DISANCE SENSOR 2WD CAR

by 994425 in Circuits > Arduino

57 Views, 1 Favorites, 0 Comments

DISANCE SENSOR 2WD CAR

unnamed.jpg
F1ZA0VNKYMZLT7Y.png

This is a Distance Sensing car with 2 motors, a pushbutton and LEDS. It's fun and you can build it in your own ways!

Supplies

FF98EBAKYOF0UC6.png

The supplies needed are:

- Breadboard

- Arduino

- Potentiometer

- 2 LED's (Red and Green)

- 2 DC motors

- 9V battery

- H-Bridge

- Ultrasonic Distance Sensor

- 3 Resistors (1Kohm, 1 Kohm, 10 Kohm)

- PushButton

- Jumper Wires

Building the Circuit

unnamed (2).jpg

The circuit is built by connecting all the components to the Arduino and breadboard. The 2 dc motors are connected to the h-bridge. The input pins are connected to the Arduino using the digital pins. The distance sensor, potentiometer, and pushbutton are also connected to the digital pins and power and ground. The digital pins you choose do not matter as long as they align with your code.


Attach the BreadBoard.

Building Continued

unnamed (3).jpg

Attach the Arduino!

Distance Sensor.

unnamed (4).jpg

Attach the Distance Sensor and connect the pins correctly.

Finishing Connecting Power, Ground, and Pins.

unnamed (1).jpg

Connect the jumper wires to the correct pins, connect the ground and power, attach the distance sensor to male/female wires and connect them to the pins and power plus ground. Make sure everything is correct and add a 5V battery to your breadboard circuit.

The CODE!

const int enableM1 = 9;
const int enableM2 = 11; //the 2 motor enables through black and red wires
const int logic1M1 = 3;
const int logic2M1 = 2;
const int logic1M2 = 12;
const int logic2M2 = 13; //the four logic control pins for the 2 motors
const int onOffPin = 5;
const int dirPin = 4; //Puhbutton pins for On/Off and direction
const int speedPin = A0; //the potentiometer pin for speed control


int onOffState = 0;
int prevOnOffState =0;
int dir = 0;
int speed = 0;
int enabled = 0;
int direction = 0;


int trig = 6;
int echo = 4;


long duration, inches;


void setup() {
  Serial.begin(9600);
  pinMode(enableM1, OUTPUT);
  pinMode(enableM1, OUTPUT);
  pinMode(logic1M1, OUTPUT);
  pinMode(logic2M1, OUTPUT);
  pinMode(logic1M2, OUTPUT);
  pinMode(logic2M2, OUTPUT);
  pinMode(onOffPin, INPUT);
  pinMode(dirPin, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);


  analogWrite(enableM1, 0);
  analogWrite(enableM2, 0); //This makes sure that both motors are off the start
}


void forward(){
    digitalWrite(logic1M1,LOW);
    digitalWrite(logic2M1,HIGH);
    digitalWrite(logic1M2,HIGH);
    digitalWrite(logic2M2,LOW);
}
void backward(){
    digitalWrite(logic1M1,HIGH);
    digitalWrite(logic2M1,LOW);
    digitalWrite(logic1M2,LOW);
    digitalWrite(logic2M2,HIGH);
}
void right(){
    digitalWrite(logic1M1,LOW);
    digitalWrite(logic2M1,HIGH);
    digitalWrite(logic1M2,LOW);
    digitalWrite(logic2M2,LOW);
}
void left(){
    digitalWrite(logic1M1,LOW);
    digitalWrite(logic2M1,LOW);
    digitalWrite(logic1M2,HIGH);
    digitalWrite(logic2M2,LOW);
}
void stopMotors(){
    digitalWrite(logic1M1,LOW);
    digitalWrite(logic2M1,LOW);
    digitalWrite(logic1M2,LOW);
    digitalWrite(logic2M2,LOW);
}
void checkDistance(){
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  inches = duration / 74 / 2;
  Serial.println(inches);
}


void objectAvoid() {
  
  if (inches > 10) 
  {
  //motorEnable();
    forward();
    delay(1000);
    
    stopMotors(); 
    delay(1000);
    
    right();
    delay(1000);
    
    stopMotors();
    delay(1000);
  } 
  
  else { // this else conditional is basically the case where inches is less than 10
   
    stopMotors();
    delay(1000);
    while (inches <= 10) 
      backward();
      delay(100); 
      checkDistance(); 
    }
    
 
    stopMotors();
    delay(1000);
  }


void loop() {


  long duration, distance;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  
  checkDistance();
  objectAvoid();


  forward();
  delay(1000);
  backward();
  delay(1000);
  right();
  delay(1000);
  left();
  delay(1000);
  stopMotors();
  delay(1000);
  duration = pulseIn(echo, HIGH);
}


And Your Done :)

Congrats you have a fully functioning distance sensing car.