I was trying to get the ultrasonic sensor to work with the ESP. I wrote this script to measure the time interval between Echo pin going high and low, but the pulsein() returns only 0. Is the code correct?
#### code
from machine import Pin
import time
class IHandler(): #provides interrupt handlers
@staticmethod
def rise_handler(p): #rising edge handler
start=time.time()
print("started")
@staticmethod
def fall_handler(p):
stop=time.time()
print("stopped")
class UltraSonic():
def __init__(self,trig,echo):
self.tp=Pin(trig,Pin.OUT) #trigger pin set to output
self.ep=Pin(echo,Pin.IN) #echo pin set to input
self.start=0
self.stop=0
def pulsein(self):
self.tp.value(0)
time.sleep(.002)
self.tp.value(1)
time.sleep(.01)
self.tp.value(0)
self.ep.irq(trigger=Pin.IRQ_RISING,handler=IHandler.rise_handler)
self.ep.irq(trigger=Pin.IRQ_FALLING,handler=IHandler.fall_handler)
return self.start-self.stop
### code ends..
I know that the module needs 5V to work and the ESP gave only 3.3 volt to it. But I m not sure if its that or the code. Thank you for reading
PS: this is my first forum post, is this the right place to post queries?
Git version of code: https://github.com/pro-metheus/esp8266/ ... raSonic.py
Getting the HC-SR04 module to work with ESP
-
- Posts: 1
- Joined: Mon Jun 12, 2017 5:23 am
Re: Getting the HC-SR04 module to work with ESP
First off, I am a total novice at python, so I will not venture an opinion on the code. However, I have looked at the HC-SR04 module and I can tell you ( from internet posts) that you need to use 5 volts or it will not perform correctly. Google "hc-sr04 3.3 v" and you will see alot of info supporting this.
Specifically, look at this link: https://raspberrypi.stackexchange.com/q ... the-5v-pin
Specifically, look at this link: https://raspberrypi.stackexchange.com/q ... the-5v-pin