Wemos Mini and HC-SR04 ultrasonic sensor

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. Ultrasonic ranging module HC-SR04 provides 2cm – 400cm measurement, the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit.

How it works:
(1) IO trigger for at least 10us high level signal
(2) The Module automatically sends eight 40 kHz and detects whether there is a pulse signal back.
(3) If there is a signal back, through high level , time of high output IO duration is the time from sending ultrasonic pulse to return.

Test distance = (high level time × velocity of sound (340M/S) / 2)

Here is  a picture of one of these modules

Parts List

Here are the parts I used


Name Links
Wemos Mini
Connecting cables



The HC-SR04 Ultrasonic Module has 4 pins these are Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Wemos Mini respectively and the trig and echo pins to any Digital I/O pin on the Wemos Mini, in our example we use D6 for Trig and D7 for Echo

Here is a breadboard layout showing how to connect a wemos mini and HC-SR04

wemos and HC-SR04
wemos and HC-SR04



#define echoPin D7 // Echo Pin
#define trigPin D6 // Trigger Pin

long duration, distance; // Duration used to calculate distance

void setup()
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

void loop()
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
//Delay 50ms before next reading.



As you can see in the output below no object was detected inside about 73 centimetres on average

wemos and hcsr04 output



  1. Doesn’t 5V coming out of the sensor too much for the mini board?
    I am using the mini pro and I know it can only tolerate 3.3V.

  2. Hi
    5 v supply to the HS04 and 1 k ohm from Ecco to Ecco pin on esp8266
    then its working fine

    KR Henrik Schack

  3. @Henrik. It is not working for me as well. However you say 1k ohm from echo to echo pin. Could you clarify this? Thanks in advance.

  4. @Nick, I had similar problem as yours but on WeMos D1 R2. Later I found that the problem was due to pin number mapping. I was using pin 12 for echo and 13 for trigger and connected sensor to these pins, i.e, D12 and D13 on the WeMos board. But pins referred to in sketch do not directly corresponds to the the pins on the board as WeMos D1 R2 pin numbers do not match with those of Arduino board. Refer to https://github.com/esp8266/Arduino/blob/master/variants/d1/pins_arduino.h for more info. These pins 12 and 13 correspond to D6 and D7 on the WeMos board. Once I connected the sensor to these pins D6 and D7 but referred to them in the sketch as 12 and 13, the distance started working correctly! Good luck.


Please enter your comment!
Please enter your name here