HC-SR04 with ESP8266 and TM1637 Display

In this tutorial, we’ll explore how to integrate an HC-SR04 ultrasonic sensor with an ESP8266 microcontroller and display the measured distance on a TM1637 7-segment display. The HC-SR04 sensor uses ultrasonic waves to calculate distance, and the TM1637 display makes it easy to showcase the results.


Understanding the Logic Level Conversion

The Wemos D1 Mini operates at 3.3V, while the TM1637 and the Ultrasonic Sensor typically works at 5V. To bridge this voltage gap, we’ll use a logic level converter to ensure proper communication between the devices.


  1. Connect the HC-SR04 sensor:
    • Trigger Pin to D1 (GPIO5)
    • Echo Pin to D2 (GPIO4)
  2. Connect the TM1637 display:
    • CLK Pin to D5 (GPIO14)
    • DIO Pin to D6 (GPIO12)


Installing Libraries:

  1. Open Arduino IDE.
  2. Go to “Sketch” -> “Include Library” -> “Manage Libraries…”
  3. Search for and install “TM1637Display” and “NewPing” libraries.



Code Implementation:


#include <TM1637Display.h>
#include <NewPing.h>

const int CLK_PIN = D5;  // CLK pin connected to D5 (GPIO14)
const int DIO_PIN = D6;  // DIO pin connected to D6 (GPIO12)
const int trigPin = D1;  // Trigger pin connected to D1 (GPIO5)
const int echoPin = D2;  // Echo pin connected to D2 (GPIO4)

TM1637Display display(CLK_PIN, DIO_PIN);
NewPing sonar(trigPin, echoPin, 200);

void setup() {

void loop() {
  int distance_cm = sonar.ping_cm();
  int distance_mm = distance_cm * 10;
  // No delays or display clearing




  • We use the TM1637Display library to control the 7-segment display and the NewPing library to interface with the HC-SR04 sensor.
  • The sonar.ping() function retrieves the distance in centimeters, which is then multiplied by 10 to convert it to millimeters.
  • The result is displayed on the TM1637 display.


By following this tutorial, you can easily set up a system to measure distances using an HC-SR04 sensor, display the results on a TM1637 display, and integrate it into various projects such as distance measurement devices, obstacle detection systems, or smart home applications.