773
The KY-003 is a magnetic switch based on a 3144EUA-S sensitive Hall-effect switch. If no magnetic field is present, the signal line of the sensor is HIGH . If a magnetic field is presented to the sensor, the signal line goes LOW, at the same time the LED on the sensor lights up.
Connection
Connect the Power line (middle) and ground (-) to +5 and GND. Connect signal (s) to pin 3 on the Arduino for this example
KY-003 | Arduino |
S | Pin 3 |
middle | +5V |
– | GND |
Code
[codesyntax lang=”cpp”]
int led = 13;//LED pin
int sensor = 3; //sensor pin
int val; //numeric variable
void setup()
{
pinMode(led, OUTPUT); //set LED pin as output
pinMode(sensor, INPUT); //set sensor pin as input
}
void loop()
{
val = digitalRead(sensor); //Read the sensor
if(val == HIGH) //when magnetic field is detected, turn led on
{
digitalWrite(Led, HIGH);
}
else
{
digitalWrite(Led, LOW);
}
}
[/codesyntax]
Links
37 in 1 box Sensor Kit For Arduino Starters brand in stock