how to make an hardware switch

Hello, I'm still a beginner in Arduino and electronics. :slightly_smiling_face:

I will power my Arduino with 12V, 15W, 1.25A.

I would like to know how to make an hardware switch (3 positions) that I could manually push in order to have three functioning mode in my Arduino program. Is this possible? With which electronic component would you suggest it? And how would the wiring look like between the switch and the Arduino?

Thanks for your help!

HI, what experience do you have with electronics, programming and hardware. I am asking so we know at what level to give you information.

Tom.... :)

I thought that from my previous post you could have guessed that I'm not very at ease with electronics. I know a bit though, and I don't want to take you a lot of time so you can answer quickly and I'll try to think a bit! :)

You could have a 3-position rotary switch. Connect the common to ground, and the 3 other pins to 3 digital inputs say for argument's sake pins 4, 5 and 6.

Then declare those pins as inputs with the pullup enabled, with eg pinMode(4, INPUT_PULLUP);

So each pin will read high unless it is the one connected to ground through the switch.

Read each pin with digitalRead() and if the pin is low, do something in the sketch which is that pin's activities.

Something like:

if (digitalRead(4) == LOW)
{
    // do the stuff associated with that switch position
}

Thank you very much!!

Actually, a three position, centre off toggle switch makes more sense, the common grounded and the two side contacts connected to inputs using INPUT_PULLUP as above.

You need only two inputs. If one is low, you perform the corresponding function, if the other is low, the other function and a third function if neither is low.

if (digitalRead(4) == LOW)
{
    // do the stuff associated with that switch position
}
else if (digitalRead(5) == LOW)
{
    // do the stuff associated with that other switch position
}
else 
{   // do the stuff associated with the middle switch position
}

Paul__B:
Actually, a three position, centre off toggle switch makes more sense

It does, indeed.

Actually, a three position, centre off toggle switch makes more sense, the common grounded and the two side contacts connected to inputs using INPUT_PULLUP as above.

Yes. Note: these come in in a center off spring loaded version if you need that.

Male headers + one jumper?

Actually, a three position, centre off toggle switch makes more sense

momentary toggle switch

Male headers + one jumper?

rather crude wouldn’t say ?

For different modes of operation? No, I'd say that's what the jumpers were invented for.

Shpaget: For different modes of operation? No, I'd say that's what the jumpers were invented for.

Depends how "long term" a mode is. If for example you had a "calibration mode" for your sensors, then yep jumpers would make sense. Calibrate once, move the jumpers, and not accidentally enter that mode again.

But your sweeping statement is a bit strong, since in the OP's mind a "mode" might just be a fleeting operational thing: it could conceivably be "forward" vs "backward" in which case a switch is ideal.

Reading the three push buttons at the start of loop() every time would make sketch responsive as well.

void loop(){

programMode = digitalRead(pin4)*4 + digitalRead(pin3)*2 + digitalRead(pin2); // result is 0 to 7
switch (programMode){
case 0:
//code for 0
break;
case 1:
//code for 1
break;
:
: 
case 7:
// code for 7
break;
} // end switch
} // end loop

I think if the OP has some little push buttons like these , it would be easy to implement. If he needs something for panel mount then he would need a standard push button or toggle.

CrossRoads: programMode = digitalRead(pin4)*4 + digitalRead(pin3)*2 + digitalRead(pin2); // result is 0 to 7 switch (programMode){

Cunning.....

Or you could even make them binary weighted DCBA (8421), if you were so inclined.

billbourrin: Hello, I'm still a beginner in Arduino and electronics. :slightly_smiling_face:

I will power my Arduino with 12V, 15W, 1.25A.

I would like to know how to make an hardware switch (3 positions) that I could manually push in order to have three functioning mode in my Arduino program. Is this possible? With which electronic component would you suggest it? And how would the wiring look like between the switch and the Arduino?

Thanks for your help!

You want to MAKE a switch? Don't know why, but I can tell you what I did to my dad when I was around 5 or 6...

I took two pieces of aluminum foil, separated them with a thin piece of wax and connected the "switch" to a cassette tape recorder with a long, hideous scream recorded.

I placed the "switch" between the toilet bowl and the seat (under one of the "bumper balls" or whatever they're called) so that when my dad sat down, the small contact area of the ball squeezed out the wax and made contact, turning on the tape recorder.

My dad sat down, a hideous scream blasted from the tape recorder and, needless to say, my dad had no problem "filling the bowl" that day! LOL!!!

(I also got rapped upside the head for it, but it was worth it!) LOL!!!!!

I’m sure that solved the OP’s problem…