// Created by Rahul Ashokayan on 2026.10.22. // Copyright © 2026 Riant Studio. All rights reserved. // Sample: Uno R3 + HC-SR04: VCC -> 5V, GND -> GND, TRIG -> D7, ECHO -> D6. // The 5V echo signal must not be connected directly to a 3.3V-only board. const byte triggerPin = 7; const byte echoPin = 6; void setup() { digitalWrite(triggerPin, LOW); pinMode(triggerPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { digitalWrite(triggerPin, LOW); delayMicroseconds(2); digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); const unsigned long duration = pulseIn(echoPin, HIGH, 30000UL); if (duration == 0) { Serial.println("No echo within timeout"); } else { const float distanceCm = duration * 0.0343f / 2.0f; Serial.print(distanceCm, 1); Serial.println(" cm"); } delay(100); }