Created
November 23, 2025 17:13
-
-
Save Redsnow237/7d3777952d6119923badd151fe9b528e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| long d, t; | |
| // NBTECH | |
| void setup() { | |
| pinMode(5, INPUT); // Echo pin of ultrasonic sensor | |
| pinMode(6, OUTPUT); // Trigger pin of ultrasonic sensor | |
| pinMode(7, OUTPUT); // Buzzer pin | |
| Serial.begin(9600); | |
| } | |
| void loop() { | |
| // Triggering the ultrasonic sensor | |
| digitalWrite(6, LOW); | |
| delayMicroseconds(2); | |
| digitalWrite(6, HIGH); | |
| delayMicroseconds(10); | |
| digitalWrite(6, LOW); | |
| // Read the echo time | |
| t = pulseIn(5, HIGH); | |
| if (t > 0) { | |
| // Calculate distance in centimeters | |
| d = t * 0.0343 / 2; | |
| Serial.print(d); | |
| Serial.println(" cm"); | |
| // Activate buzzer if distance <= 20 cm | |
| if (d <= 20) { | |
| digitalWrite(7, HIGH); | |
| delay(50); | |
| digitalWrite(7, LOW); | |
| delay(50); | |
| } | |
| delay(50); | |
| } else { | |
| Serial.println("No object detected"); | |
| } | |
| delay(50); //NBTECH | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment