Go Down

Topic: Replacing a pushbutton with a on/off rocker switch (Read 1 time) previous topic - next topic

DerekErb

On my project I currently have a tiny pushbutton which my code waits for someone to press on. I then toggle the project on/off, in the loop() function, each time someone presses the button.

Currently my tiny pushbutton is connected using 2 of the 4 posts coming out of it: 1 post is connected to the Arduino Uno's 5V out and the other post is connected to pin 7 with a 1K resistor going to the GND pin.

My tiny pushbutton is this one:
http://snootlab.com/lang-en/parts/102-push-button-12mm.html

I realised that what I really wanted was a simple On/Off switch. So I went out and got this one:
https://www.sparkfun.com/products/8837

My question therefore is how I plug this thing in to my project. It's the electronics (hardware) side where I am still flying blind or with really dark and foggy sunglasses.

I can see that this rocker switch is rated up to 16A at 125V from the sales sheet. I see numbers like that on the side of the switch as well as things like 1/3HP and 125/250VAC. I believe I know what the A (amps), V (volts) and VAC (volts alternating current?) stand for but I have no idea what the HP stands for. I assume horsepower doesn't fit in to this equation {vbg}.

I looked at the Mechanical Drawing (http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Components/Switches/Rocker%20Switch%20Right%20Angle.pdf) naively thinking this might help me figure out how to wire this up. I know just about every word on that sheet of paper is in English. But it's like showing the Kama Sutra to a virgin. I just don't know what fits where...

Am I so lucky that I can just replace the 2 posts on my button with the 2 posts on this rocker switch (one going to 5V and the other going to pin 7 and GND with a 1K resistor)? Does it matter which post is which?

Or should I be looking at this switch totally differently than I would a pushbutton?

Thank you in advance both for any practical help you could provide and, if you have the time, any explanation you could add to let me know how I could have, and perhaps should have, figured this out for myself.

CrossRoads

Make it simpler. Connect 1 post to Gnd, the other to your input pin, and enable its pullup resistor.
When the switch is open it will read high, when its closed it will read low.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Wire up pushbuttons the same way. Don't connect switches to +5V.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

DerekErb

Thank you for your reply and explanation.

Do I use the same resistor? Is the 1K enough or too much?

Where do the 2 ends of the resistor go? One end goes to the wire going to Gnd right? Does the other end of the resistor go to the wire going to the input pin?

I thought the push button, and therefore switch, had to be connected to power (5v) and the input pin as in the tutorial I learned from : http://www.arduino.cc/en/Tutorial/Pushbutton

DerekErb

I wired up the rocker switch exactly as I had wired up the pushbutton and it worked!

One post goes to 5V. One post goes to one end of 1K resistor and pin 7 the other end of the 1K resistor goes to Gnd.

My code required very little modification, as I still get a 1 or a 0 from the switch, and it works even better than the push button did - for this project.

Thanks!

DerekErb

Even though it works I still have a question...

I just copied the resistor which had been used in the pushbutton tutorial. This 1K resistor seems to work fine at the moment.

But, normally, how would I have calculated what value resistor would have been right for this rocker switch?

Thanks!

CrossRoads

My suggestion uses No resistor, just the internal pullup resistor


in void setup:

pinMode (pinX, INPUT_PULLUP); // use with button connected to pinX and to Gnd

in void loop:

if ( digitalRead(pinX) == 0) {
// button was pressed
//do whatever the action is
}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up