Fads to Obsessions and Beyond...

Free domain for life, exceptional technical support, website transfer

Interface HC-SR04 Ultrasonic Sensor with ATtiny85

Demonstrate interfacing ATtiny85/Digispark with the HC-SR04 Ultrasonic Sensor, distance readings in cm via RS232-USB to PC.

Ultrasonic ranging has a number of applications and advantages compared to alternatives such as infrared. Two specific applications of current interest include being used as a 'trip wire' for security alarm scenario and for measuring the height of water within a tank.

The output 'beam' from ultrasonic transmitters is generally restricted to a relatively narrow beam (the specification for the HC-SR04 is 15o). In comparison passive infrared sensors (by which I mean the PIR detector with a suitable lens etc) have detection angles of 90o or more. This enables an ultrasonic beam to be used analagous to a 'trip wire' in an alarm system. This would be advantageous in a situation such as a doorway or walking path, with the sensor set ~1m above and parallel to the ground, which would be 'tripped' by a person walking through the beam, but not be set off by pet dogs/cats for example.

Another situation potentially advantageous for ultrasonic ranging is determining the height of water within a tank or other container. The ultrasonic sound waves are reflected from a water surface, so this enables a non-contact method of measuring a water level. This avoids the necessity of having a probe in contact with the water and the resultant corrosion.

Ultrasonic transducers can be purchased relatively cheaply, but then require the necessary circuitry for excitation of the transmission, and a receiver with the necessary amplification and discrimination. Alternatively the HC-SR04 provides within a single breakout board a ultrasonic transmitter, receiver and control circuit, all for the cost of a few dollars (via ebay).

A number of HC-SR04 were purchased to evaluate against the publicised specifications and to test applicability for use as a water level measuring device and 'trip wire' in a potential security alarm. Some bench testing was performed to determine the performance of the HC-SR04 in terms of range, measuring angle, sensitivity and accuracy. This testing and evaluation was done using PIC18F248 microcontroller.

In many situations, a smaller, less expensive microcontroller such as the ATtiny85 is adequate (which if purchased as a 'module' in the form of a Digispark board or similar, as the further advantage of having onboard voltage regulator, a USB style connector on the PCB, etc). This project demonstrates interfacing the ATtiny85/Digispark with the HC-SR04 Ultrasonic Sensor, producing a circuit and firmware that enables distance readings in centimetres to be reported to a PC connected via RS232-USB.

The Digispark board has a surface mount ATtiny85 with headers for all pins. Additionally, the board provides a 5V regulator for external DC power input and PCB copper traces that enable insertion into standard USB connector which can then be used for DC power source. There is an onboard LED to indicate power-on and an additional LED/resistor connected to pin1 for user control.

While the USB supply can be used to power the Digispark board, the HC-SR04 can draw relatively considerable power which may cause problems with the USB PC port. In this case, an external bench top 5V supply is used.

Again for convenience for this simple test, the ATtiny85 onboard oscillator is used rather than an external crystal source.

ISP programming is used as detailed in the ATtiny85 introduction.

The Digispark ATtiny85 is connected for transmission only to the PC via the USB-TTL converter (using the TinyDebugSerial library - see ATtiny85 RS233 for more detail).

The HC-SR04 connections are straight forward (refer the schematic diagram in the Schematic Diagram Section) with the HC-SR04 trigger pin connected to pin 0 of the Attiny85, and the HC-SR04 echo pin connected to pin 4 of the Attiny85.

Note: Image loading can be slow depending on server load.

  • ATtiny85 HC-SC04 SchematicATtiny85 HC-SC04 Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    ATtiny85 HC-SC04 Schematic

    ATtiny85 HC-SC04 Schematic

This project did not require a PCB.

The construction was done using prototyping board. See the photographs and schematic diagram sections.

Qty Schematic Part-Reference Value Notes
1R13301/4W, 10% 
1D1Green LED 
Integrated Circuits
1U1Digispark/ATtiny85microcontroller datasheet
10J1wiresDupont pin jumper wires
1Z1USB-TTLUSB-TTL serial converter module
1Z2HC-SR04Ultrasonic Sensor module
Description Downloads
Bill of Materials Text File Download

Before connecting the Digispark to the USB port, check for any shorts (direct positive voltage to ground connection) as this could damage both the Digispark board and the USB/PC.

Programming the ATtiny85

The Arduino sketch for interfacing the ATtiny85/Digispark with the HC-SR04 Ultrasonic Sensor is as follows:

Code Snippet 1:

#include TinyDebugSerial.h
#include NewPing.h

#define SERIAL_TX_pin 2 // pin 2 is for TX to USB-TTL
#define TRIGGER_PIN   0 // HC-SR04 Trigger 
#define ECHO_PIN      4 // HC-SR04 Echo 

NewPing sonar(TRIGGER_PIN, ECHO_PIN); 
TinyDebugSerial mySerial = TinyDebugSerial();

uint8_t counter = 0;
void setup() {

void loop() {
  uint8_t distance = sonar.ping_cm();
  counter = counter + 1;  //reading number
  mySerial.print(counter, DEC); mySerial.print("\t"); mySerial.print(distance, DEC); mySerial.println();

  // "heart beat" to check system working
  digitalWrite(1, HIGH);   // onboard RED LED on
  digitalWrite(1, LOW);    // onboard RED LED off


Once the circuit has been constructed as per the schematic (photographs of the circuit laid-out on breadboard given in the Photographs Section), the on-board red LED should pulse ~0.5sec (this is just to indicate that the Attiny85 firmware is working) and the HC-SR04 will report the current distance from the sensor to an obstruction (in units of centimetres) to PC via USB port.

The circuit is the basic minimum required to enable the ATtiny85 microcontroller to operate after being programmed with a suitable .hex file. So this circuit was only laid out on a breadboard.

Before connecting the Digispark to the USB port, check for any shorts (direct positive voltage to ground connection) as this could damage both the Digispark board and the USB/PC.

If the red LED fails to flash with a period of ~0.5 second, carefully check your breadboard circuit against the schematic and the photographs in the Photographs Section.

If data is not being received on the connected PC, double-check that the correct port is being used in the terminal program, and that the correct baud rate (9600 is the default in the above code snippet) has been entered.


No comments yet.

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.



Only logged-in users with Silver Membership and above can download.

Silver Membership is only $2, so join now!