HC-SR04 Ultrasonic Sensor On An Atmel ATtiny13 at PDF
HC-SR04 Ultrasonic Sensor On An Atmel ATtiny13 at PDF
com
EZ TOP
H-B L T
AD9850 S -G
U S
D D D
EZ SHOP
C M
There is quite a lot of information on the web regarding this sensor coupled with AVR
microcontrollers, but basically they are all done in C with hefty AVRs, like those found on Arduinos. I
really wanted to see if I could get this sensor to work on my favorite AVR, the Tiny13, and do it in
AVR assembly language. Happily, I've discovered that this sensor can be used quite easily on a Tiny13
in assembly language. And obviously, if it works on a Tiny13, it'll work on other AVR chips just as
well.
https://www.ezdenki.com/ultrasonic.php 1/8
5/26/2020 HC-SR04 Ultrasonic Sensor on an Atmel ATtiny13 at EZdenki.com
A good starting point is the sensor datasheet from iteadstudio.com. Download it, print it out, and get
familiar with page 2 of the document. There you will find a diagram of the pulse timing. What follows
is pretty much that same information in somewhat more detail.
1. Make "Trig" (pin 2) on the sensor high for 10µs. This initiates a sensor cycle.
2. 8x40kHz pulses will be sent from the "T" transmitting piezzo transducer of the sensor, after
which time the "Echo" pin on the sensor will go from low to high.
3. The 40kHz sound wave will bounce off the nearest object and return to the sensor.
4. When the sensor detects the reflected sound wave, the the Echo pin will go low again.
5. The distance between the sensor and the detected object can be calculated based on the length of
time the Echo pin is high.
6. If no object is detected, the Echo pin will stay high for 38ms and then go low.
What follows is the mathmatics behind the code of the project. If you are only interested in the nuts
and bolts of getting your sensor working then please feel free to jump directly to the hardware and
software sections.
https://www.ezdenki.com/ultrasonic.php 2/8
5/26/2020 HC-SR04 Ultrasonic Sensor on an Atmel ATtiny13 at EZdenki.com
At first glance there may seem to be a lot of math involved. In fact it's just a few very simple
calculations to convert between speed, time, and distance.
We will be measuring distance to an object by the time it takes a sound wave to make a round trip to
the object and back again, so the the useful number is actually:
Since our sensor can only detect items relatively nearby, let's change to more useful units by
converting from m/s to µs/cm:
s m 1x106µs 58.772µs
------- X ----- X ------- = --------
170.15m 100cm s cm
Time for pulse to travel 1cm to an object and then return to the sensor: 58.772µs.
Time/Distance Quiz
Question 1: A ping takes 150µs to hit an object and return. How far away is the object?
Answer:
cm
150µs X -------- = approx. 2.55cm
58.772µs
Question 2: How long for a ping to hit and return from an object 30cm away?
Answer:
58.772µs
30cm X -------- = approx. 1,763µs or 1.763ms
cm
https://www.ezdenki.com/ultrasonic.php 3/8
5/26/2020 HC-SR04 Ultrasonic Sensor on an Atmel ATtiny13 at EZdenki.com
AVR Timings
The main piece of hardware used in this project (besides the sensor itself) is an Atmel ATtiny13(A)
operating at the default fuse setting, which means it's running at 9.6MHz with a /8 prescaler, which
comes out to a 1.2MHz clock on the chip. This is the speed at which instructions on the chip are run.
Note that some instructions may require 2 or more clock cycles to complete. See the ATtiny13
Datasheet for details.
NOTE: The Tiny13 does not directly accept an external crystal ceramic resonator like other AVRs.
Therefore the timings/measurements in this particular project are only accurate to ±10%. An external
clock source could be used to drive an input pin, but it would hardly be worth the effort. If you really
need more accuracy, I would suggest using an AVR that accepts an external crystal and, if you decide
to go that route, the calculations that follow will change depending on the frequency of the crystal
used.
What we really need is a quick conversion between the AVR clock cycles and distance. We get that by
combining the ultrasound timings with the AVR timings like this:
cm µs
-------- X -------- = 0.014179 cm/clks
58.772µs 1.2 clks
So the time for a pulse to shoot out, hit something a centimeter away, and return to the sensor would
be about 71 clock cycles. Likewise, in a single clock cycle, a distance of 0.14mm can be reckoned.
(This is a theoretical value and cannot be attained by this project because the resolution of the sensor
itself is only 3mm.)
Clock-Cycle/Distance Quiz
Question 1: A ping takes 2,110 clock cycles (clks) to hit an object and return. How far away is the
object?
Answer:
cm
2,110 clks X ----------- = approx. 30cm
70.526 clks
Question 2: How many clock cycles will it take to sense an object 7cm away?
Answer:
70.526 clks
7cm X ----------- = approx. 496 clks
cm
You can see that the clock-cycle to distance conversions won't end up in exact numbers of clock
cycles. This isn't a huge issue since the sensor itself is only rated for a resolution of 0.3cm, so the
1.2MHz clock speed is fast enough to achieve that resolution, albeit with a 10% error factor based on
the accuracy of the AVR's internal clock. In practice, without a crystal, the best we can do is probably
closer to about 5mm~1cm of resolution -- YMMV.
The Hardware
https://www.ezdenki.com/ultrasonic.php 5/8
5/26/2020 HC-SR04 Ultrasonic Sensor on an Atmel ATtiny13 at EZdenki.com
The sensor itself needs 5V to operate, so a simple 5V regulated supply running off of a 9V battery is
https://www.ezdenki.com/ultrasonic.php 6/8
5/26/2020 HC-SR04 Ultrasonic Sensor on an Atmel ATtiny13 at EZdenki.com
used. Other than that, you have the sensor itself, the Tiny13, and 2 LEDs and their current limiting
resistors. The "Sense LED" (LED1 on the schematic) indicates if an object is detected, and the "Range
LED" (LED2) tells us if the object is within the range specified in the program.
The Code
At this point, it's just a matter of counting clock cycles in the code. The code is heavily documented
and includes most of the information on this page. It lists the number of clock cycles per instruction
where it matters. There are 2 timing loops, a single-byte loop (BDELAY) and a word-length loop
(WDELAY), to burn through clock cycles.
;---------------------------;
; Ultrasonic Sensor Trial ;
;---------------------------;
; Mike Shegedin
; 20.November.2011
;
; Ver1.2
; Cleanup of code and documentation
;
; AVR: ATTiny13A
; AVR PIN FUNCTION
; ======= =======================
; Pin2 PB3 SENSELED (sense LED) (optional)
; Pin3 PB4 BUTTON (optional)
; Pin5 PB0 TRIG pin on ultrasonic sensor
; Pin6 PB1 ECHO pin on ultrasonic sensor
; Pin7 PB2 RANGELED (range LED) (optional)
;
; Ultrasonic Sensor:
; HC-SR04 Ultrasonic ranging module
;
; Operation:
; Apply a 10µs signal on TRIG to start the sensing cycle. The HC-SR04 sensor will
; send out a 40kHz pulse for 200µs. If something is in the path of the ultrasonic
; pulse, this pulse will bounch off that and return to the sensor. The ECHO line on
; the sensor will go high for 150µs~25ms depending on distance to detected object.
Here are links to the above code and the AVR hex file.
https://www.ezdenki.com/ultrasonic.php 7/8
5/26/2020 HC-SR04 Ultrasonic Sensor on an Atmel ATtiny13 at EZdenki.com
And, just in case, the avrdude command to reset the Tiny13 fuses to their default values is:
sudo avrdude -p t13 -c usbasp -e -B 3 -U lfuse:w:0x6a:m -U hfuse:w:0xff:m
Sample Application
I was able to throw together the following fairly high-level application in about 5 minutes using the
above design as a base and the GNU Image Manipulation Program. :-)
https://www.ezdenki.com/ultrasonic.php 8/8