Devices & Components
Arduino Uno Rev3
Buzzer, Piezo
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
Hardware & Tools
Tape, Electrical
Software & Tools
Arduino IDE
Arduino Web Editor
Project description
Code
code
c_cpp
copy this code onto your arduino
1int distanceThreshold = 0; 2 3int cm = 0; 4 5int inches = 0; 6 7long readUltrasonicDistance(int triggerPin, int echoPin) 8{ 9 pinMode(triggerPin, OUTPUT); // Clear the trigger 10 digitalWrite(triggerPin, LOW); 11 delayMicroseconds(2); 12 // Sets the trigger pin to HIGH state for 10 microseconds 13 digitalWrite(triggerPin, HIGH); 14 delayMicroseconds(10); 15 digitalWrite(triggerPin, LOW); 16 pinMode(echoPin, INPUT); 17 // Reads the echo pin, and returns the sound wave travel time in microseconds 18 return pulseIn(echoPin, HIGH); 19} 20 21void setup() 22{ 23 Serial.begin(9600); 24 25 pinMode(5, OUTPUT); 26} 27 28void loop() 29{ 30 31 // measure the ping time in cm 32 cm = 0.01723 * readUltrasonicDistance(7, 6); 33 34 if (cm > 50) { 35 digitalWrite(5, LOW); 36 } 37 if (cm < 50) { 38 digitalWrite(5, HIGH); 39 } 40 delay(10); // Delay a little bit to improve simulation performance 41}
code
c_cpp
copy this code onto your arduino
1int distanceThreshold = 0; 2 3int cm = 0; 4 5int inches = 0; 6 7long readUltrasonicDistance(int triggerPin, int echoPin) 8{ 9 pinMode(triggerPin, OUTPUT); // Clear the trigger 10 digitalWrite(triggerPin, LOW); 11 delayMicroseconds(2); 12 // Sets the trigger pin to HIGH state for 10 microseconds 13 digitalWrite(triggerPin, HIGH); 14 delayMicroseconds(10); 15 digitalWrite(triggerPin, LOW); 16 pinMode(echoPin, INPUT); 17 // Reads the echo pin, and returns the sound wave travel time in microseconds 18 return pulseIn(echoPin, HIGH); 19} 20 21void setup() 22{ 23 Serial.begin(9600); 24 25 pinMode(5, OUTPUT); 26} 27 28void loop() 29{ 30 31 // measure the ping time in cm 32 cm = 0.01723 * readUltrasonicDistance(7, 6); 33 34 if (cm > 50) { 35 digitalWrite(5, LOW); 36 } 37 if (cm < 50) { 38 digitalWrite(5, HIGH); 39 } 40 delay(10); // Delay a little bit to improve simulation performance 41}
code
c_cpp
copy this code onto your arduino
1int distanceThreshold = 0; 2 3int cm = 0; 4 5int inches = 0; 6 7long 8 readUltrasonicDistance(int triggerPin, int echoPin) 9{ 10 pinMode(triggerPin, 11 OUTPUT); // Clear the trigger 12 digitalWrite(triggerPin, LOW); 13 delayMicroseconds(2); 14 15 // Sets the trigger pin to HIGH state for 10 microseconds 16 digitalWrite(triggerPin, 17 HIGH); 18 delayMicroseconds(10); 19 digitalWrite(triggerPin, LOW); 20 pinMode(echoPin, 21 INPUT); 22 // Reads the echo pin, and returns the sound wave travel time in microseconds 23 24 return pulseIn(echoPin, HIGH); 25} 26 27void setup() 28{ 29 Serial.begin(9600); 30 31 32 pinMode(5, OUTPUT); 33} 34 35void loop() 36{ 37 38 // measure the ping 39 time in cm 40 cm = 0.01723 * readUltrasonicDistance(7, 6); 41 42 if (cm > 50) 43 { 44 digitalWrite(5, LOW); 45 } 46 if (cm < 50) { 47 digitalWrite(5, 48 HIGH); 49 } 50 delay(10); // Delay a little bit to improve simulation performance 51}
Downloadable files
schematics
connect the wires to the corresponding pin
schematics

schematics
connect the wires to the corresponding pin
schematics

Comments
Only logged in users can leave comments