How to connect two magnetic reed switches to ON / OFF of LED ARRAY


I am trying to connect an LED array through a driver IC (ULN2003), Two magnetic reed switches with arduino uno. I want ON and OFF these LED array(Parallel connected) when a magnet passes over reed switch.

Condition 1: if sensor 1 triggers First, then LED ON and sensor 2 triggers second, LED OFF.

Condition 2: if sensor 2 triggers First, then LED ON and sensor 1 triggers second, LED OFF.

Plz help me in programming and circuit wise to sort out this problem

circuit diagarm and code is attached

ILS3.ino (757 Bytes)

circuit diagram.doc (177 KB)

s2Pin & s1Pin can be HIGH or LOW, not a number. Also you have to read them with dlgitalRead() like you do in lines 32 & 33.

You don’t show it but I hope pin8 of the ULN2003 is connected to GND?

Also, just connecting the reedswitched between a digital pin and 5V will not work. You have to use a pull down or pull up resistor. Easiest way, connect the reedswitched to GND instead of 5V and enable the internal pullups. Keep in mind digitalRead will now read LOW when a reed is active and HIGH when inactive :wink:


LiquidCrystal lcd (2,3,4,5,6,7);

const byte S1Pin = 8; //Byte will do, good job making them const ;)
const byte S2Pin = 9; //a whitespace is free and makes things look better...
const byte LedPin = 13; //made it start with a Capital to indicate a const

//int s1State = 0; //don't need to be global
//int s2State = 0;
byte turnedOnBy = 0;

void setup(){

  pinMode(LedPin, OUTPUT);//You just made a variable name for this, use them!
  pinMode(S1Pin, INPUT_PULLUP);
  pinMode(S2Pin, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print("  INTELLIGENT ");
  lcd.setCursor(0, 1);
  lcd.print("LIGHTING SYSTEM");  

void loop(){  
  //It's turned on
    //So it was turned on by 1 and now 2 is switched
    if(turnedOnBy == 1 && !digitalRead(S2Pin){
      turnedOnBy = 0; //Reset it
      digitalWrite(LedPin, LOW);
    //So it was turned on by 1 and now 2 is switched
    else if(turnedOnBy == 2 && !digitalRead(S1Pin){
      turnedOnBy = 0; //Reset it
      digitalWrite(LedPin, LOW);
  //Light is off
    //If 1 is used, turn on and save
      turnedOnBy = 1;
      digitalWrite(LedPin, HIGH);
    //If 2 is used, turn on and save
    else if(!digitalRead(S2Pin)){
      turnedOnBy = 2;
      digitalWrite(LedPin, HIGH);