The code and circuit is exactly as it appears on the website but when I upload it to the Arduino, the LED is always on but gets slightly brighter when I push the button. It took me a while to even notice that it was actually getting brighter when I pushed the button.
I tried it with an external LED attached to pin 13 as well as using different pins as an output and it still does the same thing. The other examples up to now have worked fine and in the blink one the LED goes completely off and on so I'm not sure what's going on here.
Does anybody have any ideas? Sorry, I'm fairly new to electronics and still probably considered a beginner in programming.
Edit: Ok guys thanks for the replies. I ended up getting it to work by shifting the whole circuit over on the breadboard so I'm not sure what exactly was going on. Maybe something wonky with the breadboard or some kind of interference I don't understand. The button was oriented correctly before as well so it wasn't that.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
Serial.println("Program started");
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println("button pressed");
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
Serial.println("button released");
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
After uploading that sketch open Tools > Serial Monitor and make sure 9600 baud is selected in the menu at the bottom right of the window. This should print the state of the button so you can check to be sure it's working correctly(should change from "button released" to "button pressed" when you press the button. The tactile switches used in the tutorial have to be turned the right way to work so that could be the issue.
Hi,
If your button switches the input to 5V and you have no pull down resistor then the digital input is open when the button is OFF,
The LED is actually flashing at 50 or 60Hz, 50% duty because of noise pick up on the input.
Put a 10K or 4k7 resistor between the input and gnd to act as a pull down.
switch orientation. If the (small, square, momentary & 'tactile') switch is put in 90 degrees off, then it will always be closed, no matter if you are pushing the button or not. Hook up the switch diagonally, then it will be correct no matter which way it's in.
that pesky pull down resistor. If it's too small, or too big, it'll be a problem. 10K to 50K should work, but why use a resistor at all when your Arduino has them built in already?
In Larry's diagram above, the left-most switch is the easiest, the least error prone, and the one I always use any more (I used to be a fan of the "pull up" resistor, until I realized that they really, really did put them in there for just that purpose.
Here is some code (be sure to use the pinMode INPUT_PULLUP for the button):
#define button 8 // pin --> switch --> gnd
#define led 13 // the built-in LED
void setup()
{
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led, !digitalRead(button));
}
@larryd that schematic is the most helpful thing I've seen for buttons. I wish the "button" tutorial would have walked through all of those options and the explanation of why.
All of us using illuminated SPST toggle switches which connect power to ACC signal need to always remember to add the 10K pull-down resistor between the input and ground to fix the "always high" pin state.