I am completely new to arduino boards and electronics in general. I am trying to learn as much as I can so I can eventually use arduinos to build a flight sim.
My question for today is: can I use a 120v 20amp toggle switch to turn an led on and off? I am using an arduino uno and the leds have their own resistors wired in. If I cant use those switches, what specs can I use safely? Thanks for your help.
Yes, a switch is a switch, unless you still have it wired to 120V as well, in which case, POOF!
I assume you just don't have a more appropriate switch available.
Just make sure it isn't a more unusual 120V switch, like one with a built-in illumination.
Most switches have specifications just like most quality LEDs have specifications. Here is a snippet from your LED link.
Details: **ITEM Size (mm): 5mm; ** Wire Length: 21MM; **Forward Voltage (V): 12VDC; **Forward Current (mA): 20mA; **White Color MCD: 16000-20000 MCD
FWD Voltage 12 VDC
FWD Current 20 mADC
Not much so about any switch will work just fine. However out of the box your LEDs you linked to are designed around 12 VDC operation based on their series resistors. So what's the plan for driving your 12 volt LEDs?
arduino uno; needs 22 ga wires poked into the female pin locations. Wire that wire to the screw terminal. Other screw terminal gets a common wire for all switches. That common comes back to Arduino ground. Use "INPUT_PULLUP" when configuring the inputs. Current is negligible. The three-terminal switches, you'll want a wire from top and bottom to separate inputs, and run the common across the center terminals, again to GND.
In your code, the inputs will all show as LOW, or 0, when the switch is closed/pressed.
HTH
I did it! kinda. For some reason when my switch is in the ON position, the LED will turn off, and with the switch in the OFF position, the LED will turn on. I tried reversing my button state in my code, but all that does is dim the light instead of turning it all the way off when the switch is in the OFF position. Any ideas?
code:
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 7; // the number of the LED pin
// variables will change:
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 == LOW
) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW
);
}
}
Nope. Please follow posting guidelines for code. And, I presume then you've wired your 12V LEDs to the Arduino outputs, but are only powering the LEDs with 5V? Or connecting them from output to ground? A schematic would be really useful at this point.
Ahh, so simple LED and resistor now, no 12 V devices.
Your LED is on when the output is high, but your input is wired so that when the switch is closed, your signal is LOW. So, of course, when your switch is closed the LED is off. Simple. Invert the button reading.
See, I tried that. I changed by buttonstate to HIGH and then changed it to LOW. When its in LOW, it works, but the LED doesnt turn all the way off, it just dims. Or am I understanding you wrong?
With this code, LED comes on with switch in OFF position.
If I swap line 47 with LOW and line 48 with HIGH, the led comes on with switch in ON position, but in the OFF position, it dims rather than going all of the way off. Same if i just try to change the button state.
OK, maybe you've got something going on that is subtle.
Please learn how to post code. I have squinted at your screen shot as much as I can tolerate, and I am not going to type in that sketch to test it myself.
Use the IDE Autoformat tool to fix up you formatting.
Next use the IDE Copy for Forum tool on the sketch.
Then come here and post it in your next contribution to this thread.
At which point maybe I'll look harder.
Am I too lazy to figure how you've wired this, or have you posted a schematic showing?