Excuse me, I am having issues with my program. I am trying to get my button to switch from one relay to the next. The code switches but acts funny. led1pin is not supply 5v but about 4.6 or 4.8 which I thinking why it isn't working. ledPin is supplying 5v. Can you help me please?
const int buttonPin = 2;
const int led1Pin = 12;// the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// 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) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
if (buttonState == LOW) {
digitalWrite(led1Pin, HIGH);
} else {
// turn LED off:
digitalWrite(led1Pin, LOW);
}
}
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
How IS the switch wired? Do you have an external resistor? Connected where?
INPUT_PULLUP makes wiring much easier. Connect one leg to ground and one leg to the pin. LOW means pressed.
Sorry I am new at this. I realized the switch wouldn't work without a resistor after watching videos. I put a 200ohm resistor on one side of the but connecting to the ground. im using a board. I have the button and on the same row, I have one end of the resistor connected there, and a wire to pin 2 for the input. The other end of the resistor is connected to the ground. The other side of the button is connected to the 5v source
****INPUT_PULLUP makes wiring much easier. Connect one leg to ground and one leg to the pin. LOW means pressed.
So on input, low means pressed. But on output low means no voltage? How would the command INPUT_PULLUP make it easier? How you written "Connect one leg to ground and one leg to the pin" I won't have to use a resistor nor voltage?
If you use INPUT_PULLUP as the mode, the internal pullup resistor is enabled. It pulls the pin up to 5V.
If you connect one leg of the switch to ground, and one leg to the digital pin, pressing the switch connects the pin to ground, so that the input is 0V (LOW).
Doing this has NO impact on any output pins.
Thank you, I tried INPUT_PULLUP it helped me not to use a resistor nor power to the button, but I am still having the same issue
dmitc072:
Thank you, I tried INPUT_PULLUP . . . but I am still having the same issue
Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]
FYI
Thanks for your help. I found it. The program didn't catch I was missing "pinMode(led1Pin, OUTPUT);" as I was playing with the name of the pin. Once I added it, it worked
“The program didn't catch I was missing . . .”
That’s your job.