Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. hcsr04 sonar module


  • Includes ultrasonic transmitters!
  • Includes receiver and control circuit!
  • Using IO trigger for at least 10us high level signal!
  • The Module automatically sends eight 40 kHz pulses and detects whether there is a pulse signal back!


Quick Start with Teensy 2.0

  1. Wiring: The HC-SR04 Sonar module must be connected to a 5V power supply. This example will detail the wiring for a Teensy 2.0 microcontroller to the HC-SR04 module and a CP210x UART to USB module. For this example the CP210x module will be used to power the Teensy 2.0 at 5V after programming. 
    To wire the system, connect a common ground between the CP210x module and the Teensy 2.0. Common grounds must always be used for communication systems to allow for reliable signal transfer. The Teensy 2.0 has two dedicated UART serial pins located on PORT D, pins 2 and 3. Be aware that when using UART serial, the transmit (TXD) pin of the microcontroller should be connected to the receive (RX) pin of the CP2102. The receive (RXD) of the microcontroller then goes to the transmit (TX) pin of the CP2012. Finally since the CP210x module will power the Teensy, connect the 5V pin of the CP210x to the 5V pin of the Teensy 2.0.
    To connect the HC-SR04 module, connect the power and ground pins as indicated. The 'TRIG' pin will be connected to the Teensy 2.0 on pin D4 that will be configured as a digital output. The 'Echo' pin of the HC-SR04 should be connected to pin D5 of the Teensy 2.0 that will be configured as an input. While operating, a short pulse (10-15 uS) on the 'Trig' input of the sonar will cause an ultrasonic pulse to be emitted. By measure the length of time it takes before the 'Echo' pin rises, a distance can be calculated. 

    wiring diagram   example wiring
  2. After wiring the unit, you can download the C example code below. This code is tested to compile via avr-gcc under Windows and Linux. You may need to install the avr-gcc compiler prior to using these files. The code initializes the hardware UART on the Teensy 2.0 and transmits the value measured from the HC-SR04 module repeatedly over the UART port at a baudrate of 34800. The protocol is set up for 8 data bits, no parity, and one stop bit (8N1). 

    HC-SR04 Module Teensy 2.0 Sample Code
  3. The sample code can be compiled from the command-line via the command 'make all' and downloaded to a Teensy 2.0 with the command 'make program' in windows. The Makefile does not currently support download automatically to Linux or Mac machines. If you have one of these platforms, download the loader from the Teensy 2.0 website and program the hex that way.

    Teensy Loader
  4. To be able to fully use a serial (UART) port on a computer a 'terminal program' should be employed. One suggested program for Windows based systems isTermite (Local Copy). Termite is a versatile tool allowing for display and entry of both ASCII characters and raw hex values. Additionally, most operating system installs will need to download and install a driver for the CP210x family of USB to UART devices. These can be downloaded directly from Silicon Labsor local but older versions for  Windows Mac, or Linux (3.x.x). Driver installation will vary system to system but often only involves a simple installer utility. Consult Silicon Labs support if help is needed.
    Please note in the image below, the configuration of Termite is shown for communicating with this example code. However the 'Port' setting for each computer may vary. For this example the operating system chose COM4. On many machines it may be COM1 or COM2. To find the com port to be used in Windows open the Windows 'Device Manager' and look under the 'Ports' section of the devices list.

Termite Settings Example Termite Output