Hi everyone,
I tried to connect an HC-SR04 sensor to a WeMos D1 R3 but I cannot get it to work. Here is my materialization and the code follows. Do you see any errors?
The values of the resistors in the Fritzing picture are 4,7kΩ, 10kΩ and 100Ω from left to right (there are only fixed values). Mine are respectively (measured with the resistor setting in the voltometer) : 5,5kΩ, 9,86kΩ and 110Ω from left to right. No echo signal is returned from the sensor. The 5V pin gives 4.79V which I do not know if it is enough. The code follows:
I tried to connect an HC-SR04 sensor to a WeMos D1 R3 but I cannot get it to work. Here is my materialization and the code follows. Do you see any errors?
The values of the resistors in the Fritzing picture are 4,7kΩ, 10kΩ and 100Ω from left to right (there are only fixed values). Mine are respectively (measured with the resistor setting in the voltometer) : 5,5kΩ, 9,86kΩ and 110Ω from left to right. No echo signal is returned from the sensor. The 5V pin gives 4.79V which I do not know if it is enough. The code follows:
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Private SendTriggerTimer As Timer
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private d1 As D1Pins
Private pin2 As Pin
Private pinSend As Pin
Private pinReceive As Pin
Private distance As Double
Private pulseduration As Long
End Sub
Private Sub AppStart
Serial1.Initialize(9600)
Log("AppStart")
pin2.Initialize(d1.D0, pin2.MODE_OUTPUT)
pinSend.Initialize(d1.D7, pinSend.MODE_OUTPUT)
pinReceive.Initialize(d1.D8, pinReceive.MODE_INPUT)
SendTriggerTimer.Initialize("SendTriggerTimer_Tick", 500)
SendTriggerTimer.Enabled = False
FlashGreenLight3Times
If wifi.Connect2("SSID", "Password") Then
Log("Connected to router.")
pin2.DigitalWrite(True)
SendTriggerTimer.Enabled = True
Else
Log("Failed to connect to router.")
Return
End If
End Sub
Private Sub SendTriggerTimer_Tick
If CheckDistance Then
Log("Ok, measured")
End If
End Sub
Private Sub CheckDistance As Boolean
'Begin trigger
pinSend.DigitalWrite(True)
Log("Begin trigger")
'Trigger Off
pinSend.DigitalWrite(False)
Log("End trigger")
'Distance proportional to pulse duration received on Echo Pin
RunNative("pulseins", pinReceive.PinNumber)
'distance=(0.5*pulsduration)/29.1
Log("Pulse duration: ", pulseduration)
distance=(0.5*pulseduration/29.1)
'Discard inaccurate distance values (here between 3 and 100 cm)
If (distance> 100) Or (distance<3) Then
Log("Inacurate Distance - place the sensor correctly ")
Return False
Else
Log("Distance (cm) =",distance)
Return True
End If
End Sub
#if C
void pulseins (B4R::Object* o) {
b4r_main::_pulseduration = pulseIn(o->toULong(),HIGH);
}
#End if
Sub FlashGreenLight3Times
For ii = 1 To 3
pin2.DigitalWrite(True)
Delay(500)
pin2.DigitalWrite(False)
Delay(500)
Next
End Sub
Attachments
Last edited: