I'm trying to build a cheap device to measure water level in a tank using HC-SR04 sensor and ESP-01 which send the data to MQTT topic. I used the same code with ESP-E12 which works just fine. As I'm trying to drive the cost down with ESP-01, I faced a problem with I/O pins. I searched the web and found some contradicting info about which pins could be used in this case.

So here is the schematic of my setup: enter image description here

Coming to the ESP-01, I used a USB adapter to program it and the code uploaded successfully. Here I'm just showing the Ultrasonic sensor part which I believe I have issues with.

#define echoPin 2 // Echo Pin
#define trigPin 0 // Trigger Pin
long duration, distance; // Duration used to calculate distance
void setup(){
  Serial.begin (115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);   
void loop(){
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;

I read somewhere in the internet that GPIO2 can't be used and must be replaced with Rx = GPIO3. However, I tried that and it didn't work too.

So the question here, am I really using the right pins or is it really possible to do that with esp-01?

  • I think logic requires 5V, but there is a hack to make it work with 3.3 v : instructables.com/…
    – I Like
    Oct 22, 2020 at 6:03
  • you use the right pins. but this pins must be pulled up at boot. if the sensor pulls them LOW, then the esp8266 will not boot. same appiies for ESP-12
    – Juraj
    Oct 22, 2020 at 11:24
  • You need a Logic Level Converter Module Because esp8266 01 support 3v and ultrasonic sensor support 5v Jul 18, 2022 at 2:52
  • And ESP8266 IO pin is 5v tolerant ! facebook.com/groups/1499045113679103/permalink/1731855033731442
    – MatsK
    Aug 6, 2022 at 21:57

2 Answers 2


This question is old, but myself had the same problem, so I will answer it.

The problem here seems to be that you have the GPIO0 and GPIO2 connected to the SR04 module when it is powered (this is how should be expected), but the GPIO0 and GPIO2 (also GPIO15, just for mention it), are used by ESP8266 to change the boot mode, with nothing connected to these pins, the ESP will load the program written in the flash, but if these pins change the levels while the ESP is powering up, it could enter in the Flash mode and your code will not execute. If you turn on the ESP-01 first and connect the wires to the SR04 just after, the code works, I know because I had the same problem and have the circuit in my front right now. But this is just for testing, you should not have to do that, the solution for me was to use the GPIO3.

You can use the pin RX as GPIO3 as you mentioned, but you can't use it at the same time that you are using the Serial, because Serial will use the RX and TX pins. If you write a Webserver to display the distance measured, for example, and remove the lines that use the serial, it will work. I used the GPIO3 and GPIO2 with the same code, just disabling serial and changing the pin numbers and worked.


There are 2 things here. Ultrasonic sensor works on 5V while ESP on 3.3V. So your sensor is sending 5V signal to ESP. It might fry your board but I believe it has a safety cut off of everything above 3.3V and that's the reason your application is not working. Cheap solution would be adding voltage divider to your Echo pin: https://randomnerdtutorials.com/how-to-level-shift-5v-to-3-3v/ but then you will face the issue mentioned by Juraj above. You may then overpass that issue manually or look for more sophisticated (expensive) solution.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.