Serial Monitor with incorrect output

I am doing tutorial 3 on my arduino basics book, when I press the button on my tact switch, the led connected to pin 13 turns off, which means that the circuit and the arduino board are fine, but my serial monitor is only showing 0 (default button state is 1, when the button is press, the button state is 0), I don't know what is happening, but here is my code

const int buttonPin=13; int buttonState=0; void setup(){ pinMode(buttonPin,INPUT); Serial.begin(9600); } void loop(){ buttonState=digitalRead(buttonPin); Serial.print("The button state is "); Serial.println(buttonState); delay(1000);

schematic diagram }

Your tact switch isn't the reset button is it? Only you're not doing anything in your sketch to affect the LED on pin 13.

No, reset button is on my arduino board, and the tact switch is on a bread board

We need to see the schematic: we don’t all have that book.

According to your sketch you have the button on pin 13. Yet you also mention the LED being on pin 13. Further to this, there is also an LED onboard the arduino that is attached to pin 13. Furthermore, when the arduino boots up, the bootloader writes to pin 13 to signal that it is running.

I suggest you move everything off of pin 13, put your button and your LED on two separate pins (maybe 11 and 12) and also try dong some writing to the LED pin (but do NOT write to your button, this could damage your arduino).

I have the schematic now, you can take a look. The led is actually on the arduino board itself, and not in my project. Btw I'm using a UNO instead of a UNO R3 PLUS, because of driver issues.


NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.

If the default button state is 1, then you must be using an external pullup resistor to 5v, since I don't see you using INPUT_PULLUP.

Change your your wiring to use a pull down resistor from pin 13 to ground and use the switch between 5v and pin13. See the schematic in this tutorial

That circuit is just providing 5v to the onboard LED through the resistor. When you close the switch you block that signal. The arduino is NOT doing anything, It's just the onboard LED, your resistor to 5v and your switch blocking it.

Maybe that's the sole point of this tutorial. It doesn't matter what sketch you have on the arduino. It's just a button turning off an LED.

It was unfortunate that this tutorial chose pin 13. As the LED will be holding the voltage down to below the threshold that registers as a high. Try changing to pin 12 (and alter your sketch to use pin 12). Although the LED will no longer be affected (it's hardwired to pin 13), you should start seeing a 1 come up on your serial monitor when you haven't got the button down.

I changed it to pin 12, and it worked perfectly, you are absolutely amazing my friend!

Good spot, Ken