So admittedly i'm new to a bit of this. i've been searching quite a bit, but if i've missed a good link, please let me know. I setup my Nano to read a reed switch (in the form of magnetic door sensor), and when it's closed it should do "something". Right now that something is printing something to serial until I get past this point. i know the sensor works, i've used it without the Nano. the problem is that the nano doesn't seem to be reading pin 4 correctly, it's either always reading "HIGH" or randomly switching from high to low without any actual change the the switch state. i know the switch works, and there's proper power getting all the way to the pin, so i'm confused. i checked to make sure the nano is fully seated in the breadboard (it's tough to keep it firmly seated sometimes), and i've wired it exactly according to the attached diagram, with 10K res and the switch coming in at D4. Also, the battery power runs to the bus, then use connectors to get the power where its need to go (instead of wiring straight to the pins from the battery). Here's my code:
int reed = 4;
void setup() {
// put your setup code here, to run once:
pinMode(reed, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(digitalRead(reed));
delay(1000);
}
Change this line
pinMode(reed, INPUT);
to
pinMode(reed, INPUT_PULLUP);
Also:
int reed = 4;
to
byte reed = 4; // byte holds values from 0 to 255, int is -32786 to 32767, unsigned int 0 to 65535
Wire the switch to connect pin reed to Gnd when the switch closes. No other connections.
Pin reed will read 1 (HIGH) when open, an 0 (LOW) when closed.
CrossRoads:
Change this line
pinMode(reed, INPUT);
to
pinMode(reed, INPUT_PULLUP);
Also:
int reed = 4;
to
byte reed = 4; // byte holds values from 0 to 255, int is -32786 to 32767, unsigned int 0 to 65535
Wire the switch to connect pin reed to Gnd when the switch closes. No other connections.
Pin reed will read 1 (HIGH) when open, an 0 (LOW) when closed.
Try the code again.
So I made the code changes, but wiring the switch (red positive to D4 and black to GND, instead of red positive coming from 5V to switch and black to D4) doesn't change the output; the serial still reads "1" all the time and doesn't change when I activate the switch. I double checked that I was connected to NO and COM on the switch, which I am, so everything appears to be connected correctly.
CrossRoads:
Wire the switch to connect pin reed to Gnd when the switch closes. No other connections.
Pin reed will read 1 (HIGH) when open, an 0 (LOW) when closed.
Try the code again.
After my last reply, I tried really pushing down hard on the nano .... worked. stupid thing really has to be held down good to get the contact for the pin. But your suggestion absolutely did the trick, just had to muscle it. Thanks!
After your previous reply, I checked the pin and it is in D4. Also, the nano is finally staying firmly seated so I don’t need to hold it in now and is working properly.
But your diagram shows it on pin 2. It's labelled D2 meaning digital pin 2. Your code, reading pin 4, was showing classic symptoms of a "floating" pin, connected to neither 5V or ground, which can read high or low at random, and can be influenced by touching the pins with a finger.

Ah I see. I do need to be more mindful of my diagrams then, the site I used either had it wrong or the image was misleading. My actual wiring was set to D4. Thanks!