Reed switch is an electronic component to connect / disconnect the switch by magnetic field. When it touchs magnetic field, the two iron sheets inside the tube will clip together, so the switch is connected.
The application of Reed Swith:
Door light controller: When you open the door, turn the light on. When you close the door, turn off the light. This is simply install a reed switch on your door handler, and install a magnetite on the doorframe.
Of course, you can also make an alarm and install it on your door.
There are many other scenarios that uses a reed swtich. This article talks about how to turn on/off a light by the reed switch.
1. Physical Connection
Connect Reed's VCC to DC3.3, GND toGROUND, DO to GPIO 05
Connect LED positive pin to DC3.3, nagtive pin to GPIO 06
2. Coding
Define GPIO Controller, Reed switch's GPIO pin, LED's GPIO pin and LED's on/off flag.
public GpioController GpioController { get; set; } public GpioPin ReedPin { get; set; } public GpioPin LedPin { get; set; } public bool IsLightOn { get; set; }
Initiate GPIO controller
GpioController = GpioController.GetDefault(); if (null != GpioController) { }
Rememeber to check "null" first to ensure your program doesn't blow up on non-GPIO devices.
Open the reed switch's GPIO pin:
ReedPin = GpioController.OpenPin(5);
Set it to input mode:
ReedPin.SetDriveMode(GpioPinDriveMode.Input);
Similarly, open LED pin, and set to output.
LedPin = GpioController.OpenPin(6); LedPin.SetDriveMode(GpioPinDriveMode.Output);
By default, the LED is turned off, so it requires a GPIO.HIGH at first.
LedPin.Write(GpioPinValue.High);
Then, listen to the ValueChanged event on the reed's pin.
ReedPin.ValueChanged += (sender, args) => { };
In this event handler, check if the reed signal is GPIO.LOW, if it is LOW, that means it's currently in a magnetic field, so turn on the light. If not, turn off the light.
if (ReedPin.Read() == GpioPinValue.Low) { IsLightOn = !IsLightOn; LedPin.Write(IsLightOn ? GpioPinValue.Low : GpioPinValue.High); }
Final code:
using Windows.Devices.Gpio; using Windows.UI.Xaml.Controls; namespace WindowsIoTReedLight { public sealed partial class MainPage : Page { public GpioController GpioController { get; set; } public GpioPin ReedPin { get; set; } public GpioPin LedPin { get; set; } public bool IsLightOn { get; set; } public MainPage() { this.InitializeComponent(); GpioController = GpioController.GetDefault(); if (null != GpioController) { ReedPin = GpioController.OpenPin(5); ReedPin.SetDriveMode(GpioPinDriveMode.Input); ReedPin.ValueChanged += (sender, args) => { if (ReedPin.Read() == GpioPinValue.Low) { IsLightOn = !IsLightOn; LedPin.Write(IsLightOn ? GpioPinValue.Low : GpioPinValue.High); } }; LedPin = GpioController.OpenPin(6); LedPin.SetDriveMode(GpioPinDriveMode.Output); LedPin.Write(GpioPinValue.High); } } } }
3. Running
Deploy the project to raspberry pi. Take a magnetite and closing in the reed switch, you will see the light will be turned on. Move away the magnetite, the light will be turned off.
ZHWX
求Pi3和外壳购买链接