Interfacing with Ultrasonic Ranging Module HC-SR04 via One Signal Line

Tuesday , 4, April 2017 Leave a comment

To connect the HC-SR04 ultrasonic ranging module to microcontroller (for example, Arduino UNO), four wires are usually required. But while the Vcc and GND wires are necessary, the Trig and Echo wires can be “combined” into one with a simple circuit. The software to control the HC-SR04 ultrasonic ranging module via one signal line see here too.

Usually, the following four wires are needed to connect the HC-SR04 ultrasonic ranging module to the microcontroller:

Vcc+5V supply;
Trig – trigger pulse input;
Echo – echo pulse output;
GND0V ground.

The timing diagram of ordinary using of the HC-SR04 ultrasonic ranging module is shown below:

The microcontroller initiates the emission of a short ultrasonic signal by applying a short pulse with a duration of about 10 µs at the Trig input of the HC-SR04 ultrasonic ranging module. Then the response pulse at the Echo output follows, the duration of which is equal to the delay time of the echo signal. Thus, since the start pulse at the Trig input and the response pulse at the Echo output are separated in time, the use of two signal lines to communicate with the HC-SR04 ultrasonic ranging module is redundant.

The schematic diagram of communication with the HC-SR04 ultrasonic ranging module via a single line is shown below. Thus, to interface the microcontroller (in this case, it is the ATmega328P-PU microcontroller installed on the Arduino Uno board) with the module, only one pin of any I/O port is needed:

This works as described below.

Before the microcontroller issues a short start pulse, its port PB0 (pin 14) is in the output mode and its state is high level (see yellow waveform below), so the VT1 switch is closed and the state of Trig input of the HC-SR04 module is low. The state of the Echo output of the HC-SR04 module and the gates of transistors VT2 and VT3 is still low and, accordingly, the VT2 and VT3 switches are kept opened, so that they do not have any effect on the state of the control signals.

The microcontroller puts the PB0 port in low state to apply a short pulse with a duration of about 10 µs at the gate of the VT1 MOSFET-switch (see yellow waveform below). So, the VT1 switch opens and switches the Trig input of the HC-SR04 ultrasonic ranging module to high level state for about 10 µs (see turquoise waveform below). This is the short start pusle. Immediately after this, the microcontroller must change the mode of operation of its PB0 control port from output mode to input mode. At the same time, the high level at the gate of the transistor VT1 will be maintained by the resistor R1, respectively, the Trig input of the HC-SR04 ultrasonic ranging module will be in low level state.

The response high level pulse generated by the HC-SR04 ultrasonic ranging module at its Echo output (see violet waveform below) closes the VT2 and VT3 switches for the duration of this pulse. So, the inverted copy of this echo-pulse is applied to the P0 control port (see yellow waveform below), that works as input (in input mode), while the VT2 switch blocks the Trig input from a false start pulse at the same time.

In order to use the same input/output port both for issuing a short start pulse and for measuring the duration of the response echo-pulse, the microcontroller must control this I/O port in the following order, as described below:

  1. Switch the control I/O port P0 of the microcontroller to output mode;
  2. Set the P0 port to low level state;
  3. Keep the P0 port in low level state during 10 µs;
  4. Set the P0 port to high level state;
  5. Change the mode of I/O port P0 to input mode;
  6. Measure the duration of the response echo-pulse;
  7. Calculate the measured distance.

The software for the Arduino Uno board that implements this algorithm, see below:

#define SOUND_SPEED 340.0

void setup()
{
 digitalWrite( 8, HIGH );
 pinMode( 8, OUTPUT );
 Serial.begin(9600);
 delay(10);
}

unsigned long firstSampleTimer, secondSampleTimer;
float distance;

void loop()
{
 pinMode( 8, OUTPUT );
 digitalWrite( 8, LOW );
 delayMicroseconds(10);
 digitalWrite( 8, HIGH );
 pinMode( 8, INPUT );
 while( digitalRead(8) );
 firstSampleTimer = micros();
 while( !digitalRead(8) );
 secondSampleTimer = micros();
 distance = ( ( ((float)(secondSampleTimer — firstSampleTimer)) * SOUND_SPEED ) / 20000.0 );
 Serial.print( “Distance is” );
 Serial.print(distance);
 Serial.println( ” cm” );
 delay( 500 );
}

Copyright © Sergii Zadorozhnyi, 2017

Please give us your valuable comment