Controlling DC Motor With an Ultrasonic Sensor With Conformation LEDs
by bigfoot4263 in Circuits > Arduino
938 Views, 1 Favorites, 0 Comments
Controlling DC Motor With an Ultrasonic Sensor With Conformation LEDs
This instructable will explain the steps and materials needed to Controlling DC Motor with an Ultrasonic sensor with conformation LEDs.
Supplies
- Arduino Uno 3 Board
- Arduino Wi-Fi Cable
- DC Motor
- Ultrasonic Sensor HR-SR04
- Power Source (if you want a mobile system)
- Breadboard
- 2x LEDs
- 2x 220ohm Resistors
- Jumpers
Assembly
Using the visual above, assemble the circuit system accordingly. For specific ports, it depends on your main board you are using. You can choose for yourself or match it to the coding - if not, make sure you change the coding to work for the pins you choose.
Coding
int trig = 9;
int echo = 8;
int motorPin1 = 5;
int led1 = 12;
int led2 =3;
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);
pinMode(led1, OUTPUT);
pinMode(led2 , OUTPUT);
pinMode(motorPin1 , OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);
duration = pulseIn(echo , HIGH);
distance = (duration / 2) / 28.5 ;
if ( distance < 40 ) {
Serial.print(distance);
digitalWrite(motorPin1, HIGH);
digitalWrite(led1 , HIGH);
digitalWrite(led2 , LOW);
Serial.println("cm MOTOR START");
delay(1000);
}
else {
Serial.print(distance);
digitalWrite(motorPin1, LOW);
digitalWrite(led1 , LOW);
digitalWrite(led2 , HIGH);
Serial.println("cm MOTOR STOP");
delay(2);}
}