Components and supplies
Arduino UNO
DC Motor, 12 V
Rechargeable Battery, 12 V
Ultrasonic Sensor - HC-SR04 (Generic)
SG90 Micro-servo motor
Tools and machines
Hot glue gun (generic)
Project description
Code
coding.ino
arduino
Downloadable files
Connection diagram
Connection diagram
Comments
Only logged in users can leave comments
ankur2520
a year ago
I have arrange the wire but it is not still working also with code
apandabear_13
a month ago
here is one that might work better: 1 #include <Servo.h> 2 3 #define echoPin A0 // Use A0 if using an analog pin, or change to a valid digital pin 4 #define trigPin 12 5 #define motor 2 // Relay pin to operate motor 6 7 Servo myservo; // Create servo object 8 9 long duration; 10 int distance; 11 12 void setup() { 13 Serial.begin(9600); 14 pinMode(trigPin, OUTPUT); 15 pinMode(echoPin, INPUT); 16 pinMode(motor, OUTPUT); 17 myservo.attach(5, 500, 2500); // Ensure the servo is connected to pin 5 18 } 19 20 void loop() { 21 // Send ultrasonic pulse 22 digitalWrite(trigPin, LOW); 23 delayMicroseconds(2); 24 digitalWrite(trigPin, HIGH); 25 delayMicroseconds(10); 26 digitalWrite(trigPin, LOW); 27 28 // Read echo time and calculate distance 29 duration = pulseIn(echoPin, HIGH); 30 distance = (duration * 0.034 / 2); 31 32 Serial.print("Distance: "); 33 Serial.print(distance); 34 Serial.println(" cm"); 35 delay(100); 36 37 if (distance <= 50) { 38 digitalWrite(motor, LOW); // Turn motor ON 39 delay(1000); 40 myservo.write(180); 41 delay(2000); 42 digitalWrite(motor, HIGH); // Turn motor OFF 43 delay(1500); 44 myservo.write(0); 45 delay(1000); 46 } 47 else { 48 digitalWrite(motor, HIGH); // Ensure motor stays OFF when no motion is detected 49 } 50 }
apandabear_13
a month ago
I can't arrange the wires.