Well, I've had my UNO r3 for only 2 days and even though I can envision all kinds of ambitious projects with it, I'm already struggling with simple operations.
I've got a bunch of click switches lying around and figured I'd use one to turn on an LED before I go connecting motors and such, but I'm not seeing the results I expect. Visual attached.
The click switch has three prongs: common, normally open and normally closed. I wired the C to the ground and the NO to Pin 5. Then I wired the LED to the ground rail and Pin 9. Set pin 9 to output, Pin 5 to input and run.
The issues I'm seeing are twofold: WHEN the switch works, it's opposite of what I want. Clicking turns the LED off, while leaving it untouched keeps it on. Oh, and if I take out the leads to the switch altogether, the LED seems to turn on and off by moving my hand near it.
I use a basic (modified) sketch to test it. I'm sure I'm doing something wrong and I'm hoping someone will see it immediately. Or is it possible, what with the light going on an off when my hand gets close or jiggles the board), that I've got a bad UNO or breadboard?
There are a few issues that jump out from this. Firstly the switch, when activated, pulls the line connected to pin 5 to ground however there's nothing to force it to the alternate state when it's off. One way to do this is to use a pull-up resistor. That would look like a 10,000 Ohm (10kR) resistor between the leg of the switch that connects to D5, and the +5V rail which would make the "default" forced to 5V or HIGH when you use digitalRead() on that pin. A very convenient way to do that is by activating the internal pull-up resistor that's a feature of the Arduino's processor. To do that, after setting the pin to OUTPUT as you have in setup(), follow it up with the line digitalWrite(pushButton, HIGH);. For a full description of that feature, check this page.
So that will give you a solid result to your digitalReads every time. But be aware, buttonState will only be 1 when your switch is off (ie it's pulled high). So swap the logic around or it will be backwards as you're seeing.
Next, be sure to use a current limiting resistor on your LED or bad things can happen to it. A good explanation of that is available from this sparkfun page and many other places elsewhere when you google the topic.
Thanks so much for taking a look at my setup. Guess I was missing more than I thought.
I see just about every tutorial suggesting a resistor for any LED I put on the board, but I've put a half dozen or more in during the basic blink and fade sketches and had no problems. The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink (choosing to use the breadboard, not pin 13) and the LED wouldn't light until I removed the resistor. Does that make any sense?
Anyway, thanks again and thanks for the links too. I'll be futzing this weekend, hopefully with better results.
jedimasta:
The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink (choosing to use the breadboard, not pin 13) and the LED wouldn't light until I removed the resistor.
What pin exactly? What sketch? Normally you would do this:
jedimasta:
The very first thing I did was put a 220 Ohm resistor between the 5v and an LED for the blink (choosing to use the breadboard, not pin 13) and the LED wouldn't light until I removed the resistor. Does that make any sense?
It's possible to get LEDs with integral current-limiting resistors, but it seems unlikely that you'd have got one of those without knowing it.