Go Down

Topic: Simple switch - delay in pin going to low state (Read 1 time) previous topic - next topic


Hi all,

Just started with Arduino and electronics, got a starter kit and worked through the basic projects so this is a noob question. I'm working with a piezo transducer and wanted to add a simple switch to the project so that I don't have to unplug the USB each time I want to stop it sounding. I've followed the simple switch demo but I'm using a 6 pin toggle switch instead of a tactile switch.

I have no idea of the spec of the switch but I've tested it with my Ohmmeter and worked out I need to use 3 of the pins on one side: Power to the center pole, "off" state (left pin) to 10k resistor and then back to ground and "on" state (right pin) to pin 2.

I've tested this without the Arduino and a 6v buzzer to make sure that I've got the pins set up correctly and it works fine. 

When I wire up the Arduino and upload the demo code though I find that when I put the switch in the "on" position the LED on the board lights up but when I switch to the "off" position there is a long delay before the light goes off, almost as if there's a capacitor discharging somewhere. I've tried adding a delay to the loop (from 10ms to 100ms) in case it was some kind of latency issue. When I output to the serial monitor and switch from on to off I can see that after a few moments the "on" fluctuates between 1 and 0 before it goes low.

Is it possible I've got some kind of capacitive switch?

I've taken a snapshot of the setup so you can see what I've got. (The L, C and R dots indicate what I'm referring to as center, left and right pins in the description above).

Thanking you in advance for any help you can offer.



Jan 22, 2013, 08:32 pm Last Edit: Jan 22, 2013, 08:34 pm by Brandeaux Reason: 1
The way I understand it is that you have Vcc going into the middle pin. The middle pin when flipped connects with either the left pin or the right pin, correct? If that is true, then when you flip the switch to ground, you aren't grounding the Arduino, you are running Vcc to ground through the resistor. The Arduino pin is left floating. If you were to connect the Arduino to the middle pin, ground to the left pin, and Vcc to the right pin, it should work.

Another option would be to attach the 10K resistor (leading to ground) to the same pin your Arduino is currently on. In this situation, the Arduino would register high when Vcc is toggled to the Arduino as the resistor to ground provides more resistance (path of least resistance and whatnot), but low otherwise as the pin would be able to register the only path it has to read: ground. This would leave Vcc with nowhere to go in the "off" state, but that is alright as that is what a tactile switch would do anyway: break the Arduino's connection to Vcc and then pull it low.

Hope that helps!


Jan 23, 2013, 01:20 am Last Edit: Jan 23, 2013, 01:39 am by DVDdoug Reason: 1
I have no idea of the spec of the switch but I've tested it with my Ohmmeter and worked out I need to use 3 of the pins on one side: Power to the center pole, "off" state (left pin) to 10k resistor and then back to ground and "on" state (right pin) to pin 2.
OK.  Just make sure the "center" terminal is the electrical center.  (It usually is.)   

Then +5V can be connected to one of the other terminals, and ground to the remaining terminal.   That way, the center is either connected to +5V or ground, depending on the switch position.  In that configuration, you shouldn't need the 10K resistor at all. 

But, the 10k resistor (between 5V and the switch) is slightly "safer" because there are some very-rare switches (called "make before break") where the two "other" terminals are connected together for a moment in-between positions.    If that happens, you'll momentarily short-out the 5V.   Everything will probably survive a momentary short, but the Arduino would reset/restart and that would be a problem.

You can test the switch (with or without the resistor) with your multi-meter before connecting it to the Arduino.   Just check for 5V (at the center) in one position and zero volts in the other position.   And, it would be a good idea to turn-off the power and use the ohmmeter function to confirm that the "zero volts" connection is a good-ground, and not simply an open.   

The REAL solution is to use a pull-up resistor:   The Arduino has optional internal pull-up resistors which can be enabled (see this page).   The pull-up resistor pulls the input high (+5V) when there is no connection.   Then, you only need two connections to your switch to force the input low (to ground) when the switch is on.

Brandeaux suggests using a pull-down resistor.   That will work fine too.  But, pull-ups are the more standard way of doing it, and the built-in resistors are pull-ups.

With a pull-up resistor, you are "shorting-out" the input and shorting one side of the resistor to ground.  That's fine, since only a teeny-tiny current flows through the resistor.   If you were to make a "hard connection" to 5V (without the resistor), turning-on the switch would short-out the 5V supply, shutting everything down.

I've tried adding a delay to the loop (from 10ms to 100ms)...
That's only going to make things worse! ;)  As the program is "stepping" through the loop, depending on where the program execution is in the loop, the input-read might get delayed or your "action" might get delayed 'till the delay is done.

FYI - As a general programming rule, it's best to avoid using delay().   Because while delay() is running, your program can't do anything else.   (Of course, you can use it when appropriate.)   So, if you you want to delay something without holding-up your entire program, you can read the "time" and compare like the Blink Without delay()Example..    With this techinque, you can have multiple delays/timers running at the same time, and they won't interfere with each other or with your main loop.


Brandeaux, spot on. Now you've pointed it out I feel a bit stupid for not spotting it! I'd read about floating pins but I couldn't quite see how I'd left it that way. Note to self: pay attention to the schematic rather than getting distracted by the pretty pictures.

I'll be trying it with the resistor to the Arduino pin as well shortly.

DVDdoug, I am just about to google "electrical" centre to make sure I understand exactly what that means.  I'll be googling "make before break" switches shortly too. 

Have to admit that I'm still at the scaredy cat stage of the electronics learning curve for fear of breaking components, of which there seem to be so many and by different manufacturers, each one seems to need a different kind of voodoo to identify without the datasheet.

Once I've made sure I understand this switch properly, I'll be trying it the way you suggested (no resistor) as I'm a bit of a purist (programming background) and the fewer components the better (simple is always best as they say).

I went with the pull-down resistor for now to remind myself that I'm relying on it and that I must remember to make sure to put one in there.  I wasn't 100% sure whether the pull-up resistors are internal to the chip or whether I'd have to put them on my PCB later when I'm soldering together the finished project?

If they're on the chip then I'll opt for the pull-up resistor when I assemble my final project as then I have less components but still have the protection of that resistor in there.

Roger that on using the delay() function, I wanted to check if the Arduino was somehow "stacking" the pin values (eg. a longer delay would still output the same number of 0's even with a longer time delay because they were being pulled off of a stack) but I realise now that there is no stack and when you read the value off of the pin then that is the current value.

Many thanks to both of you for taking the time to answer my noob question, it is very much appreciated.



If you're trying to switch the pin between a HIGH and LOW state then it's the pin that needs to go to the Common connection, whether that is centre pin of the switch or not. Switches can be different layouts. For example a normal domestic 2 way switch, that you find on your wall for lighting, will have Common at the bottom and then L1 and L2 being your 2 Way contacts at the top.

Common will be the one pin that connects to each of the others alternately when you flick the switch.

IN your case I'd be inclined to use a pullup resistor (internal is fine, but I usually add my own), so that the default state of the input is HIGH. When you switch to the contacts that are connected to Ground then your switch goes LOW. With this, you don't have even a fraction of a second where the contacts might be open, plus you actually only need a 1 Way switch; Open or Closed. In the Open state your input is HIGH via the pullup to 5V. When your switch is Closed, the input is grounded to a LOW state, just the same as when you press a momentary tactile switch, but obviously it holds the LOW state until you switch back.


Hi Tack,

Ah, so if I go with the pull-up resistor then I can get away with a 1 way switch.  That's a better solution.  The examples for Arduino all seem to use a double throw so I was wary of using a straight switch. Is the pull-up resistor internal to the ATMega chip or is it on the Arduino board and Is there any disadvantage to having the default state of the switch set to HIGH, I mean am I draining batteries whilst the Arduino is in the off state or wasting power via heat lost in the pull-up resistor?

My project will eventually be Ni-cad powered once I assemble it so I want to make sure I'm not drawing current when it's off. Although having said that, I suppose the Arduino is always essentially on standby rather than off when I'm running the switch through it rather than placing the switch between my power supply and the ATmega chip itself so that is probably a silly question!

That 6-pin switch I'm using came in a Maplins lucky bag and is the only one which would fit nicely on my breadboard which is why I chose to use it. Picked up a large piezo transducer (for a small chladni plate experiment) and a sound bomb from there today which will apparently give me 104db at 1 metre. Can't wait to wire that up! Funny how the older you get the more the simple things pleaseā€¦


In reality a SPST switch works with a pull-up OR pull-down resistor. It is at your discretion to use whichever you want, it just changes which state, 0 or 1, you will be using to trigger something. With a pull-down resistor, you would code for your program to react to a HIGH input. If you use a pull-up resistor, you will be coding your program to react to a LOW input.

I really like to use tactile or other momentary pushbutton (SPST) switches when I can. I really love being able to make a toggle switch with a little programming and a MOM switch. I am pretty new to Arduino and electronics in general and I suggested a pull-down instead of a pull-up only because I was exposed to a pull-down resistor first, and it's all I've experimented with in my few months of prototyping silly little projects. Right now I am working on a robot arm for Science Olympiad and am certainly going to play with the internal pull-ups of the Arduino. That just seems too convenient to pass up!

Good luck with your project!

Go Up