Mini Arduino with Push Switch

Hi everyone :) Unfortunately, I'm in need of a bit of help!

I've attached my Mini Arduino 04 to my USB Adapter and have managed to get a simple blinking LED work, with no problems at all.

I've tried many times to get a simple push switch to turn on the LED when pressed, and turn the LED off when not pressed, but the LED is just doing weird things. Currently the LED is permanently on unless I touch the push switch, but then there is a massive delay between releasing the push switch and the LED coming back on. It doesn't always act like this either!

I've used pin 9 (digital pin 7) on my Mini Arduino 04 for the push switch and pin 17 (digital pin 13) for the LED.

The code I used is:

// Example 02: Turn on LED while the button is pressed

define LED 13 // LED pin

define BUTTON 7 // pushbutton pin

int val = 0; // val stores state of input pin

void setup() { pinMode(LED, OUTPUT); // LED = output pinMode(BUTTON, INPUT); // BUTTON = input }

void loop() { val = digitalRead(BUTTON); // read input, store in 'val'

// is input high? (button pressed?) if (val == HIGH) { digitalWrite(LED, HIGH); // turn LED on } else { digitalWrite(LED, LOW); } }

If anyone can help me that would be fantastic!

Thanks ever so much, Sarah.

Here's a picture of the breadboard:

Be sure to check out the orentation of the switch. All four terminals of the switch do not conduct when pushed, rather two pins on each side conduct when pushed. You might have to rotate the switch 90 degrees. It's easier to check with a ohm or volt meter but play with the switch layout, because it sounds like the wire going to the input pin is not being switched properly and 'floating' when not pressed.

Second point, you don't seem to have a current limiting resistor between your digital output pin and the LED. This will cause the LED to draw much too much current from the pin and could damage the output pin and drastically shorten the life of the LED.

Lefty

Your code is almost identical to Tom Igoe's example for making a switch light an LED. http://arduino.cc/en/Tutorial/Button

...and I just used your code... and it works for me...

pw

I bet a know what it is...

The power/ground bus along the sides of the solderless breadboard are not connected. There is a SPLIT in the bus halfway down that requires you to ad vertical jumpers that connect the top half to the bottom half.

Look Here: http://www.robocommunity.com/blog/entry/14700/Introduction-to-breadboards/

There is a SPLIT in the bus halfway down that requires you to ad vertical jumpers that connect the top half to the bottom half.

Possible on some, however all the ones I've bought in the last couple of years are continous for their whole length. Quick check with a meter should determine if that's the problem or not.

Lefty

I agree. My OLD ones have that feature. All the new ones I've picked up in the last few years do not have a split bus. (which is why I didn't catch that possibility right away)

There is a SPLIT in the bus halfway down that requires you to ad vertical jumpers that connect the top half to the bottom half.

It turns out that this was the problem! I never even thought of it as I'd used similar breadboards before which don't have a split. Thanks ever so much for all your replies! :)