Other Arduino Codes and Videos by Robojax

Using reed switch to control relay and AC/DC load with Arduino Robojax.com

Reed Magnetic Switch Arduino with Relay Code

this video shows you how to use reed switch with relay to detect door or window opening to turn on ligh or Alarm using a relay. You will learn how to use it without Arduino and with Arduino.

 //*
 * This is the Arduino code for reed switch to
 * turn a relay ON which the relay
 * can turn a light or alarm ON
* reed switch is connected to pin 2
 * Relay is connected to pin 10
 * 
 * Written by Ahmad Nejrabi for Roboja Video
 * Date: Dec 05, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 
 */


 int LD = 200; // time in melliseond to wait before make another reading.

void setup() {
    Serial.begin(9600);
    // out pins
    pinMode(10, OUTPUT);// connected to relay
         

    // input pins
    pinMode(2, INPUT);// reed switch  input pin 2  


    Serial.println("Robojax Test: Reed switch");

}

void loop() {

    // read the REED switch
    if(digitalRead(2)){
      Serial.println("Switch ON "); 
      digitalWrite(10, LOW); // Turn the relay ON    
      delay(LD); 
    }else{
      delay(5000);// wait 5 seconds before turning the alarm
				// remove this line if you don't want any delay
      digitalWrite(10, HIGH);// Turn the relay OFF
      
    }
  




}// loop