Controlling DC Motor With an Ultrasonic Sensor With Conformation LEDs

by bigfoot4263 in Circuits > Arduino

752 Views, 1 Favorites, 0 Comments

Controlling DC Motor With an Ultrasonic Sensor With Conformation LEDs

39E5967A-DD58-4C2A-B80F-DA4141DF19A5.jpeg

This instructable will explain the steps and materials needed to Controlling DC Motor with an Ultrasonic sensor with conformation LEDs.

Supplies

  1. Arduino Uno 3 Board
  2. Arduino Wi-Fi Cable
  3. DC Motor
  4. Ultrasonic Sensor HR-SR04
  5. Power Source (if you want a mobile system)
  6. Breadboard
  7. 2x LEDs
  8. 2x 220ohm Resistors
  9. Jumpers

Assembly

084ED711-AC9E-4A91-B589-7D63DEE65EB5.jpeg

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);}

 }

Casing and Staging (Optional)

903CB5F7-C7CA-4E74-8CC7-F79E39E1F3B5.jpeg
4DD72771-F155-43D6-8484-EB59346E3F3A.jpeg
15711549-907A-41C9-835B-8AA2A9CBCAC1.jpeg

My Final

Downloads