Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to work out how to capture a Pin.ValueChanged event in VB.NET.
I have a PIR switch attached to GPIO Pin #21 on my raspberry Pi 3B. When PIR movement is detected it goes high to 3.3v and when no movement is detected for a set time it goes low.

I want the app to turn on a LED tied to GPIO Pin #26 when the PIR detects movement and goes high and off when it goes low.

I have seen some VB.NET examples that use a timer to check every second or so to see if the pin is high or low which I have tried and the circuit does work but its easy to miss events with a timer. I would like to use an event handler in VB.NET.

In my google searches all I can find is C# examples using a lines similar to this one.
buttonPin.ValueChanged += buttonPin_ValueChanged;

Then the procedure to do something if the event happened.
C#
private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
    // toggle the state of the LED every time the button is pressed
    // Code removed
}

But I have no idea on how to convert this to VB.NET. All the conversion websites create incorrect code in VB.for the line -
buttonPin.ValueChanged += buttonPin_ValueChanged;


What I have tried:

I tried the following VB.NET code but got an error.
VB.NET
AddHandler PIR_Pin.ValueChanged, AddressOf test

VB.NET
Private Sub test(ByVal sender As GpioPin, ByVal args As GpioPinValueChangedEventArgs)
    If PIR_Pin.Read = GpioPinValue.High Then
        LED_Pin.Write(GpioPinValue.High)
    Else
        LED_Pin.Write(GpioPinValue.Low)
    End If
End Sub

This is the error I get in VS 2017
System.ArgumentException
  HResult=0x80070057
  Message=Delegate to an instance method cannot have null 'this'.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Delegate.DelegateConstruct(Object target, IntPtr slot)
   at PIR_Test.MainPage..ctor() in C:\Users\Richard\Documents\Visual Studio 2017\Projects\Raspberry Pi Tests\PIR Test\MainPage.xaml.vb:line 22

Can someone shed any light on how to get the event trigger in VB.NET for a GPIO pin on a Raspberry Pi running Windows 10 core IOT.

Thanks in advance.
Posted
Updated 13-May-20 7:17am
v2

1 solution

I managed to fix the problem.
here is the code now. It works the event fires when the PIR sensor detects movement of a warm body.
VB.NET
AddHandler PIR_Pin.ValueChanged, AddressOf PIR_Pin_ValueChanged
VB.NET
Private Sub PIR_Pin_ValueChanged(ByVal sender As GpioPin, ByVal args As GpioPinValueChangedEventArgs)
    Debug.WriteLine("PIR detected heat movement")
    Dim PIR_Status As Boolean = False

    If PIR_Pin.Read = GpioPinValue.High Then
        PIR_Status = True
    Else
        PIR_Status = False
    End If

    Debug.WriteLine(PIR_Pin.Read.ToString & "    " & PIR_Status)
    If PIR_Status = True Then
        LED_Pin.Write(GpioPinValue.High)
    Else
        LED_Pin.Write(GpioPinValue.Low)
    End If
End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900