Introduction: Talking to Ultrasonic Distance Sensor HC-SR04 Using an ATtiny84

About: Open Source Hardware Projects - Microcontrollers, Programming, and new prototyping technologies.


This instructable is about interfacing an ATtiny84 with the HC-SR04 ultrasonic distance sensor using just C code – no Arduino hardware or libraries.

The HC-SR04 works as follows:

- Send a 10us HIGH pulse on the Trigger pin.
- The sensor sends out a “sonic burst” of 8 cycles.
- Listen to the Echo pin, and the duration of the next HIGH signal will give you the time taken by the sound to go back and forth from sensor to target.

Here, the PB0 pin is used to send out the 10 us pulse. To measure the width of the echo pulse, we can use a pin-change interrupt and a timer. Here is the idea:

- Setup pin change interrupt PCINT0 so that any logical change on pin will cause an interrupt.
- Send a 10 us pulse to the trigger pin.
- Loop till the PCINT0 interrupt sets a flag to indicate that measurement is done.
- In the PCINT0 interrupt, start an 8-bit timer when you see a rising edge – ie., the echo pulse has gone from low to high. The 8-bit timer is setup to use the overflow interrupt.
- The 8-bit counter overflows every time it reaches 255, and so when that interrupt fires, we add 255 to a running 32-bit counter value.
- In the PCINT0 interrupt, stop 8-bit timer when you see a falling edge – ie., the echo pulse has gone from high to low. Update 32 bit count, and set flag to indicate that the measurement is done.
- The measured pulse width is in terms of a counter value, and we can convert that into seconds, since we know the clock speed. This time value is then used to calculate the distance.

The distance is then sent using serial communications on pin PB1 – I’ve covered this part in a separate article. This is also the reason we cannot use the 16-bit timer to measure the pulse width – it’s already being used for serial communications. Plus it’s fun to learn how to use the 8-bit timer to count large values, right? ;-)

The images for schematic and the breadboard layout are attached.

This is the Makefile that goes along with the above code:

https://gist.github.com/electronut/5763929

And here is the Python code used to plot the data:

https://gist.github.com/electronut/5730160

The Python code is a minor modification to what I posted before on the subject.