Skip to content

Instantly share code, notes, and snippets.

@elktros
Created June 4, 2018 10:08
Show Gist options
  • Save elktros/2a174a7a603a11d9fec290e2b25179a8 to your computer and use it in GitHub Desktop.
Save elktros/2a174a7a603a11d9fec290e2b25179a8 to your computer and use it in GitHub Desktop.
Code for Interfacing a Reed Switch with Arduino.
const int reedPin = 2;
const int ledPin = 12;
bool switchState = HIGH;
void setup()
{
Serial.begin(9600);
pinMode(reedPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
switchState = digitalRead(reedPin);
if (switchState == LOW)
{
digitalWrite(ledPin, HIGH);
Serial.println("Reed Switch is Closed. LED is ON");
}
else
{
digitalWrite(ledPin, LOW);
Serial.println("Reed Switch is Open. LED is OFF");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment