B4R Code Snippet HB100 RF sensor

hb100.jpg


This is a Doppler RF transmitter and receiver which can be used as a movement alert.
You can read about it here http://www.theorycircuit.com/hb100-microwave-motion-sensor-interfacing-arduino/
I have made the electrical circuit described there to amplify the signal, added a voltage level converter (2 resistors of 5 kohm and 10kohm) to connect it to WEMOS (which connects to a server).
I connected the output to A0 pin.

I tried first to use the sketch but it didn't work for me... so I wrote a little sub to measure the output.
It is all experimental and probably needs calibration due to many variations in the complete device.
The sensors output is not a steady voltage but some kind of waveform so I made the following:
1. Measure the mean value of the output.
2. Measure some sample of the frequency by counting every time that the value is above mean and the next value is below. I got a count of about 50-60 for a loop of 10000.
3. The counted sample drops meaningfully when a movement is detected (both directions), so I have defined a number as a lower limit to define as detection.

Notes:
1. The antenna's face is the flat side of the sensor.
2. The measured values are affected by the power supply's voltage and the position of the sensor so you need to define the values in the intended location and with the final power supply.
3. The attached code is partial for the sensor's part only.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public sensepin, ledpin As Pin
    Private D1 As D1Pins
    Private mean As UInt = 490
...
end sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    sensepin.Initialize(sensepin.A0 , sensepin.MODE_INPUT)
    ledpin.Initialize(D1.D7, ledpin.MODE_OUTPUT)
    tmr.Initialize("tmr_Tick",1000)
   ...
end sub

Sub tmr_tick
    Dim x As Int
    Dim flag As Boolean = False
    Dim count As UInt = 0
    For i = 0 To 10000
        x = sensepin.AnalogRead
        If x > mean Then
            flag = True
        End If
        If x < mean And flag = True Then
            count = count + 1
            flag = False
        End If
    Next
    Log(count)
    If count < 40 Then
        ledpin.DigitalWrite(True)
        If RFClient.Connected Then
            astream.Write(Array As Byte(1))
            Delay(1000)
        End If
    Else
        ledpin.DigitalWrite(False)
    End If


'                    Measure mean value
'Dim x As Double = 0
'For i = 0 To 15000
'    x = x + sensepin.AnalogRead
'Next
'mean = x/15000
'Log(mean)

End Sub
 
Last edited:

derez

Expert
Licensed User
Longtime User
Using B4j Serial Connector and drawing the output signal on canvas I got this graph, showing area of detection and no detection.
The results are drawn every 80 msec so it is just a sample.
Accordingly I changed the criteria for alert to getting a reading under value of 40:

upload_2016-12-11_22-48-49.png


B4X:
Sub tmr_tick
    Dim x, minx As Int
    Dim flag As Boolean = False
    Dim count As UInt = 0
    minx = 1023
    For i = 0 To 1000
        x = sensepin.AnalogRead
        minx = Min(x,minx)
        Log(minx)
        If minx < 40 Then
            ledpin.DigitalWrite(True)
            If RFClient.Connected Then
                astream.Write(Array As Byte(1))
                Delay(1000)
            End If
            Exit
        End If
        ledpin.DigitalWrite(False)
    Next
end sub
 

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
... for simply detecting approaches, this easy example might work:

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.
    Public Serial1 As Serial
    Public ti As Timer
    Public PHB100 As Pin
    Public change As Double
    Public inval As Double
    Public lastINval As Double
    Public forelastINval As Double
    Public count As Int
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
   
    ti.Initialize("myTickSub",20)
    ti.Enabled = True
   
    PHB100.Initialize(PHB100.A0,PHB100.MODE_INPUT)
   
End Sub


private Sub myTickSub
   
    count = count + 1
    inval = PHB100.AnalogRead
    If inval <> lastINval And inval <> forelastINval Then ' wobbel filter
            change = change + Abs(inval- lastINval)   
    End If
    forelastINval = lastINval
    lastINval = inval
   
    If count = 100 Then
        Log(change)
        count = 0
        change = 0
    End If
   
   
   
   
    'Log(PHB100.AnalogRead)
   
End Sub
 
Last edited:

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
for testing just use a voice recorder... you got the perfect cucumber and can difference noise, expecially from Handy net.
loudness depends on distance, frequency (10 to 30 hz) depends on speed - together approaching and disapproaching
 

Attachments

  • hb100rf.jpg
    hb100rf.jpg
    269.3 KB · Views: 589
Last edited:

Hans- Joachim Krahe

Active Member
Licensed User
Longtime User
, within an amp, for instance: https://www.elv.de/mikrofonverstaerker.html

you can split the signal with a schottky an fill an 10microFarad capacitor with 1MOhm leak to ground. Its better then, to take maxValues at A0, to find patterns, that could alarm you over VPN and let you check your cam....

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.
    Public Serial1 As Serial
    Public ti As Timer
    Public PHB100 As Pin
    Public change As Double
    Public inval As Double
    Public lastINval As Double
    Public forelastINval As Double
    Public count As Int
    Public invalMax As Double
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    ti.Initialize("myTickSub",20)
    ti.Enabled = True

    PHB100.Initialize(PHB100.A0,PHB100.MODE_INPUT)

End Sub

private Sub myTickSub

    count = count + 1
    inval = PHB100.AnalogRead
    If invalMax<inval Then invalMax = inval
    If count = 50 Then
        Log(invalMax)
        invalMax=0
        count = 0
     End If


End Sub

... for more range try an low pass, followed by a smith trigger,

Very fine: the ELV SMV5 (10€) has a trimmable noise reduction. With this trimmer, you can can directly trim the sensivity of the sensor - up to 5m and more im my case and 3m behind a wooden brick of 6 cm:))))))

... about 15% less distance at 3.3V (sensor and amp) minimum voltage 2.5V. Sensor and amp take 50 mA at 5V, 30 mA at 3V
 
Last edited:
Top