Tactile Button Help

Hello,

I'm very new to arduino and I'm trying to build a simple tactile button and LED circuit using a NANO.
I can get the circuit to light the LED without a switch, however, when I put the switch into the circuit and use the arduino button code, I can't seem to get the button to work.

Any help would massively be appreciated.

Can u pass the code? i mean the progamation code u made

Hi,

Sure, I will attach the code below.

/*
Button

Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.

The circuit:

  • LED attached from pin 13 to ground

  • pushbutton attached to pin 2 from +5V

  • 10K resistor attached to pin 2 from ground

  • Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

created 2005
by DojoDave http://www.0j0.org
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// 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 = 13; // 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 == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Hi,
The code and circuit seem OK. Is the LED in backwards? Maybe try moving everything to a new breadboard, some breadboards have faulty pins.

Hi,
This may help with your tactile button wiring.
buttonlegsdiag

Tom... :smiley: :+1: :coffee: :australia:

Hi,

Thanks for the quick reply. I think the LED is in the right way as it turns on without the switch in the circuit. I will try another breadboard now!

Thanks
Lew

Hi Tom,
Thanks for your help! I've just checked, i've got the LED legs in the right way around, however still no luck...
I think my switch is connected correctly?

Thanks

Hi,
Instead of using the tactile swtich.
Just connect the wire to Pin2 from 5V to Gnd and see if the LED turns ON and OFF.

Also try changing your code and use another output pin.

Tom.... :smiley: :+1: :coffee: :australia:

Hi Tom,

Sorry, could you please just clarify the wires I need to change. I'm a little confused!

Thanks. :grinning:

Hi,
Instead of attaching pin #2 of the Nano to the switch, try attaching it to +5V. That should light up the LED just like the switch.

And attaching it to gnd should turn it OFF.

Tom... :smiley: :+1: :coffee: :australia:

Hi Tom and Kgray9,

That's great! I attached the pin 2 from the nano to the 5v, and now the switch works when I press it! I don't understand why though?

Cheers :star_struck:

Hi,
Nice! It was probably some finicky connection on the breadboard. Glad it works!

Hi,

Will this allow me to use it as a toggle switch with other code? I'd like to have it so when the button is pressed the LED stays on. I have the code for this but will this work with pin 2 connected to 5v instead?

Thank you so much for all your help!!

Hi,
Post a picture of your protoboard with the change you have made, please.

Tom.. :smiley: :+1: :coffee: :australia:

Hi Tom,

Sure, please find attached below. It seems to be working great!!!

Lew
:grinning: :grinning:

How do you mean that config works with your button, the button isn't even connected to the NANO?

You may need to apply a bit more heat to those solder joints on the header pins on the NANO.

Tom... :smiley: :+1: :coffee: :australia:

Hi,

I'm not sure. But when the wire is connected from Pin 2 to the 5v rail. the switch is working?

Lew :slight_smile:

Hi,
The switch ISN'T working? I thought you said it was. Did you try the switch again?
Try this code for the toggle:

// 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 = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

boolean flag = false;

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) {
flag = !flag;
while (buttonState == HIGH);
delay(100);
}
if (flag == true){
digitalWrite(ledPin, HIGH);
} 
else if (flag == false){
digitalWrite(ledPin, LOW);
}
}

Hi,

The switch is working, however, I was just unsure why as pin 2 is now connected to the 5V instead.

Cheers