Advanced AVR 3.0 time management - HC-SR04 Ultrasonic distance measurer test
Able to measure from 20mm - 4000mm in 3mm-increments means a lot of things;
First the minimal amount of time in resolution to measure it at 3mm is about:
0.003/331=0.000,009,063s === 9us * 2 =18us
Second the maximum amount of time for the maximize 4m distance timing is about:
4/331=0.01208s === 12ms * 2 = 24ms
so the MCU needs to be able to measure time from 18us-24000us; 2x here because sound travels both way.
A MCU clocked in 16MHz has 288 1-clock-cycle CPU instructions in time to do one increment at 9us;
0.000018/(1/16000000)=288;
In the path to develop Advanced AVR 3.0 time management, the HC-SR04 is tested and played with. Control at the lowest level is the key to integrate high time sensitive thing such as the HC-SR04 accurately, effectively and responsively. For starter, the ECHO pin should be connected to the ICP of the AVR timer 1 module. Once configured correctly, timing is done so automatically and this means an AVR MCU running at 16MHz can measure time in increment as low as (1/16000000)=0.0000000625s or 62.5ns.
PS. Most HC-SR04 are controlled by a STC11 MCU, and the firmware can be easily modified to improve its true capability, but it's good enough as-is.