0

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);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
  Serial.println(distance);
  delay(200);
}

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?

2
  • I think logic requires 5V, but there is a hack to make it work with 3.3 v : instructables.com/…
    – I Like
    Oct 22 '20 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 '20 at 11:24
1

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, privacy policy and cookie policy

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