Reading The HC-SR04 With Timer1 And No Interrupts - Arduino Forum
Maybe your like
Attached is my "device checkout code" for the HC-SR04 Ultrasonic sensor. This code only triggers and reads the sensor echo. It uses Timer1 in the capture mode (capture Rising edge and capture falling edge).
The code (sketch) is kind of a bare bones approach used only as a starting point for other programs utilizing the HC-SR04. Writing this code I learned (from the forum community):
- Interrupts are possible in the setup portion of a sketch.
- To clear the Timer1 capture flag (ICF1) one must set it to 1 !
You may be interested in this deep dive into the HC-SR04. His goal was an accuracy of 1 mm. One of the first steps was to use the capture register, but that wasn't enough....
https://www.davidpilling.com/wiki/index.php/HCSR04
He reports achieving his goal.
1 Like johnwasser March 12, 2022, 5:38pm 3If you don't mind using the Input Capture interrupt:
// Measure the HC-SR04 echo pulse on Arduino UNO Pin 8 (ICP1 pin) and calculate distance const byte TriggerPin = 9; const byte EchoPin = 8; // MUST be 8 (ICP1) // Note: Since this uses Timer1, Pin 9 and Pin 10 can't be used for // analogWrite(). // Speed of sound: // 343 meters per second // == 0.002915452 seconds per meter // == 2915.452 microseconds per meter const float MicrosecondsPerCM = 29.15452; const float MicrosecondsPerRoundTripCM = MicrosecondsPerCM * 2; // ~58.3 const float HalfMicrosecondsPerRoundTripCM = MicrosecondsPerRoundTripCM * 2; // ~116.6 // TCCR1B values to start Timer 1, prescale = 8 // Input Capture Edge Select (1=Rising, 0=Falling) const uint8_t TCCR1BCaptureRisingEdge = _BV(CS11) | _BV(ICES1); const uint8_t TCCR1BCaptureFallingEdge = _BV(CS11); uint16_t RisingEdgeTime = 0; unsigned long TimeOfLastPing = 0; void setup() { Serial.begin(115200); while (!Serial); Serial.println("Ready"); pinMode(TriggerPin, OUTPUT); noInterrupts (); // protected code // reset Timer 1 TCCR1A = 0; TCCR1B = 0; TCNT1 = 0; TIMSK1 = 0; TIFR1 = _BV(ICF1); // clear Input Capture Flag TCCR1B = TCCR1BCaptureRisingEdge; interrupts (); } void loop() { // Is the Input Capture Register set? if (TIFR1 & _BV(ICF1)) { uint16_t edgeTime = ICR1; TIFR1 = _BV(ICF1); // Clear the flag if (RisingEdgeTime) { // This must be the falling edge uint16_t elapsed = edgeTime - RisingEdgeTime; // Look for the next rising edge RisingEdgeTime = 0; TCCR1B = TCCR1BCaptureRisingEdge; // Now calculate distance: float cm = elapsed / HalfMicrosecondsPerRoundTripCM; Serial.println(cm, 3); tone(4, cm*10); } else { // This is the rising edge RisingEdgeTime = edgeTime; TCCR1B = TCCR1BCaptureFallingEdge; } } // Be sure to leave 20 to 30 milliseconds between pings so // the sensor doesn't catch old echoes from the prevous ping. if (millis() - TimeOfLastPing >= 30) { TimeOfLastPing = millis(); digitalWrite(TriggerPin, HIGH); digitalWrite(TriggerPin, LOW); } } 1 Like JohnRob March 12, 2022, 7:42pm 4@johnwasser Thank you. BTW it was your first post of similar code that drove into my skull the concept of clearing the ICF1 flag by writing a 1 . Although it stated that in the datasheet, I was hung up on the flag must to to zero when triggered.
In my current case I plan on using the sensor to measure the top down distance in my heating oil tank. Time is NOT of the essence here ![]()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
Related topics
| Topic | Replies | Views | Activity |
|---|---|---|---|
| getting hc sr04 to work with a interupt Sensors | 6 | 686 | May 6, 2021 |
| Ultrasonic sensor HCSR04 with registers Programming | 7 | 580 | July 15, 2023 |
| HC-SR04 with low delay, causes to all go slow Sensors | 27 | 1153 | April 30, 2024 |
| Ultrasonic sensor HC-SRO4 giving 0 distance whenever I tried to test the sensor with arduino Sensors | 3 | 69 | December 4, 2025 |
| Issue with large range values from HC-SR04 Ultrasonic sensor on Interrupt Sensors | 11 | 2155 | February 13, 2023 |
Tag » Arduino Hc-sr04 Interrupt
-
Simple Arduino HC-SR04 (HCSR04) Distance Detection Using ...
-
Generating An Interrupt From HC-SR04 Ultrasonic Sensor
-
Arduino Programming The HC-SR04 With Interrupts
-
Arduino Interrupt For Ultrasonic Sensor - YouTube
-
Arduino Programming The HC-SR04 With Interrupts - YouTube
-
Non-blocking Ultrasonic Sensor For Arduino : 3 Steps - Instructables
-
Interrupt Arduino When A Particular Value Is Read By A Ultrasonic ...
-
How To Measure Distance Using Ultrasonic Sensor
-
Arduino Multi-tasking - The Online Shed
-
HC-SR04 Sensor Timer Interrupt Issues - AVR Freaks
-
Arduino Interrupt Tutorial
-
Trying To Use Ultrasonic Sensor With Interrupts - ESP32 Forum
-
Arduino - En Utilisant Les Interruptions De Gels De Traitement Et De ...
-
Interrupt | Cộng đồng Arduino Việt Nam