KY-021 Mini Dry Reed Switch N-O troubleshooting

Hi everyone! I am creating a home security system for my capstone project. I am using KY-021 reed switches as door and window sensors. As I was trying to make it work, the two-reed switch sends the same data even though I only triggered 1 switch.

here is the code:



#define ReedSwitch01 2
#define ReedSwitch02 7

void setup() {
  // put your setup code here, to run once:
  pinMode(ReedSwitch01, INPUT);
  pinMode(ReedSwitch02, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(millis() % 2000 == 0){  
    Serial.println("Switch 01:" + String(digitalRead(ReedSwitch01)));
    Serial.println("Switch 02:" + String(digitalRead(ReedSwitch02)));
  }
}
  
  

Welcome to the forum

What is keeping the inputs in a known state (HIGH or LOW) when the switches are not closed ?

Read up on pullup/pulldown resistors if you do not understand the question

1 Like

Hi, @bigz
Welcome to the forum.

Can you please post a circuit diagram so we can see how you have connected the hardware?
In particular your reed switches.

Thanks... Tom.. :smiley: :+1: :coffee: :australia: :santa:

i used the INPUT_PULLUP and came out right. thanks, @UKHeliBob

  pinMode(ReedSwitch01, INPUT_PULLUP);
  pinMode(ReedSwitch02, INPUT_PULLUP);

Well done for figuring out how to fix it from the clue that I gave you

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.