Skip to content

Instantly share code, notes, and snippets.

@Redsnow237
Created November 23, 2025 17:13
Show Gist options
  • Select an option

  • Save Redsnow237/7d3777952d6119923badd151fe9b528e to your computer and use it in GitHub Desktop.

Select an option

Save Redsnow237/7d3777952d6119923badd151fe9b528e to your computer and use it in GitHub Desktop.
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