I made a very simple circuit and program to turn on an LED while a button is pressed (just to test it out for another project), but the LED isn't turning off completely even when the pin should be on LOW, it just glows and gets a bit brighter when the button is pressed. It also sometimes seems, strangely, to be affected by how close I am to the circuit.
This problem has only occurred with this specific kind of program, and doesn't happen, for example, when I load the "Blink" example program.
Here's my code:
int outpin = 7;
int inpin = 12;
int val = 0;
void setup() {
pinMode(7, OUTPUT);
pinMode(12, INPUT);
}
void loop() {
val = digitalRead(inpin);
digitalWrite(outpin, val);
}
An LED and resistor are connected in series between digital pin 7 and ground, and a momentary push button is connected between +5v and digital pin 12.
I don't think the problem is with the arduino, since it happened on both of the boards I tried it with.
So, if anyone has any suggestions or can point out what I'm doing wrong, it would be greatly appreciated.
Thanks
FellowBob:
I made a very simple circuit and program to turn on an LED while a button is pressed (just to test it out for another project), but the LED isn't turning off completely even when the pin should be on LOW, it just glows and gets a bit brighter when the button is pressed. It also sometimes seems, strangely, to be affected by how close I am to the circuit.
This problem has only occurred with this specific kind of program, and doesn't happen, for example, when I load the "Blink" example program.
void loop() {
val = digitalRead(inpin);
digitalWrite(outpin, val);
}
An LED and resistor are connected in series between digital pin 7 and ground, and a momentary push button is connected between +5v and digital pin 12.
I don't think the problem is with the arduino, since it happened on both of the boards I tried it with.
So, if anyone has any suggestions or can point out what I'm doing wrong, it would be greatly appreciated.
Thanks
When do you give your visual persistence time to recognize the LED is off??????
FellowBob:
a momentary push button is connected between +5v and digital pin 12.
Do you have a (10k) pull down resistor between that pin and ground.
Without that, the inpin will be 'floating' when the button is not pushed, causing the problems you see.
Better/easier to connect the button between pin and ground, and use internal pull up on the pin with code.
pinMode(12, INPUT_PULLUP); // button between pin and ground
Wawa:
Do you have a (10k) pull down resistor between that pin and ground.
Without that, the inpin will be 'floating' when the button is not pushed, causing the problems you see.
Better/easier to connect the button between pin and ground, and use internal pull up on the pin with code.
pinMode(12, INPUT_PULLUP); // button between pin and ground