Overview
The HC-SR04 is an ultra-sonic range finder. It use sound wave at the speed of 340m/s that travel in air to measure any detected obstacle. That sound wave signal will travels back whenever there is a barrier created by any object. It uses a speaker that propagates an ultra-sonic signal that human ears could not hear. The detector is a microphone circuit that has an amplifier. The distance of the detected object is the time the sound wave travel divides by 2. The measurement formula is,
Test distance = (high level time * velocity of sound ) / 2.
The velocity of sound is 340m/s.
![]() |
| Distance Measurement |
The table below summarizes the features of the HC-SR04 Distance Sensor.
![]() |
| HC-SR04 Parameters |
This Arduino module is very popular because of its low cost, easy-to-use, etc.
![]() |
| The Arduino HC-SR04 Module |
![]() |
| The Arduino HC-SR04 Module |
The high level time is the input high time creates by the echo pin of the HC-SR04 Ultra-Sonic Range Finder Module. That time duration is measure in micro-seconds, and the Test distance is measure in cm. To measure that time duration, the micro-controller need to process it precisely. In Arduino, it has the pulseIn function that can measure any logic level ( HIGH or LOW) time at any specific input pins.
ESP8266 Interfaces with the PCF8574AP and HC-SR04
In this example, I use the HC-SR04 to measure the distance. The result will show on a 16x2 character LCD controlled by the PCF8574AP I/O expansion chip. Because the ESP-12E has a limited of digital I/O pins. On the other hands, Some pins of this IoT module are connected to other peripherals.
The 7-bit Arduino slave address of the PCF8574AP here is 0x38. Commonly they use the PCF8574T Arduino Two-Wire LCD module that has different slave address.
Circuit Connection
It needs a small breadboard to build the circuit. Connect the all components as follow.
- Connection between ESP-12E and HC-SR04
| HC-SR04 | ESP12-E |
|---|---|
| VIN | VCC |
| Trig | D6 (GPIO 12) |
| Echo | D5 (GPIO 14) |
| GND | GND |
- Connection between ESP-12E and PCF8574AP LCD
| ESP-12E | PCF8574AP LCD |
|---|---|
| GND | GND |
| D1 (GPIO 12) | SCL |
| D2 (GPIO 14) | SDA |
| VIN | VCC |
ESP8266 Arduino Source Code
I use the Arduino LiquidCrystal_I2C library. It's originally developed to work with the Arduino AVR. However for the ESP8266, it is compatible. It works fine.
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x38,16,2);
- #define trigPin D6
- #define echoPin D5
- /*
- PWM OUTPUT 0-25000uS in which 1cm=50uS
- */
- long duration;
- int distance;
- void setup() {
- pinMode(trigPin,OUTPUT);
- pinMode(echoPin,INPUT);
- lcd.init();
- // Print a message to the LCD.
- lcd.backlight();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("ESP-12E PCF8574AP");
- lcd.setCursor(0,1);
- lcd.print("and HC-SR04");
- delay(10000);
- lcd.clear();
- }
- void loop() {
- digitalWrite(trigPin,LOW);
- delayMicroseconds(7);
- // Set the trigPin high for 10 uS
- digitalWrite(trigPin,HIGH);
- digitalWrite(trigPin,HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin,LOW);
- duration=pulseIn(echoPin,HIGH);
- // Calculation for distance
- if(duration<=25000)
- distance = duration * 0.034 / 2;
- else
- distance=0;
- lcd.setCursor(0,0);
- lcd.print("Time: " + String(duration) + "uS ");
- lcd.setCursor(0,1);
- lcd.print("Distance: " + String(distance) + "cm ");
- delay(500);
- }
Pictures
I can not find these module at any online circuit simulator now. Currently WOKWI only support some high-end board.
![]() |
| Distance Measurement |




