First, I am a beginner for this. I just ordered my first book on coding Arduino and I'm working through some simple applications using a DS3231 (that has a temperature reading), LCD, MicroSD card module, etc. My board is a Uno R3. Currently, I need a sketch to run part of a program which contains a loop to collect data and have this run when a toggle switch is turned on. So, toggle goes on and part of program runs, toggle goes off and this part exits. The reason I need a toggle with constant on/off is the problem with G forces and vibration. I momentary push button input will not be reliable. Ultimately, all the components will be in a amateur high-power rocket to run a roll control program. If you can help, I greatly appreciate your assistance.

  • push button switch and a toggle button switch are the same electrically ..... a toggle switch has a mechanical device that keeps the switch closed when it is activated .......... pressing a pushbutton, or flipping a toggle switch, or twisting two wires together are all the same thing as far as the Arduino is concerned – jsotola Dec 26 '18 at 19:24
  • I did find an example where a push button was held during a calibration. As you probably know, there are many examples where a momentary switch is used, but few where a toggle is used. I appreciate the suggestion to use the if-else statements to get this accomplished. I'm waiting to receive some resistors and the screw-type switches I use for rocketry to test the circuit out. I'm interested in how to prevent the potential bounce when I'm closing the screw switch. This could happen because these switches do have some play until the switch is tightened. – RED Dec 26 '18 at 21:20
  • i am uncertain that you understand my comment ...... electrically, there is no difference between a pushbutton and a toggle switch ...... there is no reason why you should be distinguishing between the examples on the web ....... just use a debounce library in your sketch – jsotola Dec 26 '18 at 21:26

There are different ways to accomplish the same thing.

The condition is a flag, or a read port.

     // Your first code here.
     // second code

Switch contacts will bounce when opening or closing. You may want to consider that. Usually it is stable within 10 milliseconds. Depending on what you are doing it may or may not be relevant.


You need a few things. At the top of your sketch, before setup()

byte togglePin = x; // from 2 to 19 with an Uno. Wire to connect pin to Gnd when closed.

In setup()

 pinMode (togglePin, INPUT_PULLUP);

In loop()

if (digitalRead(togglePin) == LOW){
// switch closed, do something - or stop doing something
// maybe set a flag too, like
toggleState = 0;
else {
// switch, stop doing something - or start doing something
toggleState = 1;

The flag can be used elsewhere in your code

if (someOtherCondition && toggleState == 0){
// run a timer or motor or something
if (someOtherCondition && toggleState == 1){
// turn off timer or motor or something
  • Thank you! I'm going to try this. RE previous comments, sorry if there was any misunderstanding. Just wanted to assure that people understood that I wasn't using a momentary switch like turning an LED on and off. I need to turn on the system, start the program and then wait for me to turn on the screw switch to start the sensor and control program. This will only be done once per flight. – RED Dec 28 '18 at 18:03
  • You could use a while (digitalRead()==Low) in setup() instead. It will sit there reading the pin until the condition becomes false. – CrossRoads Dec 29 '18 at 19:39
  • The if-else statement works great; thanks again for the help. To gain some experience, I also used the toggleState variable too. I just had to add to define as an int in the beginning of the program. Do you think the (digitalRead()==Low) would be faster or behave any differently? Also, I'm curious, do comments (i.e. //) slow program execution, or does compiling disregard these comments? – RED Jan 1 at 20:57
  • Comments do nothing, the compiler strips them out. "Do you think the (digitalRead()==Low) would be faster or behave any differently?" Than what? – CrossRoads Jan 1 at 23:47
  • I was thinking that although there might be several ways to accomplish the same behavior, some routines may be faster. And, I mean perceived faster, not just machine time. Although I'm sure that slower routines would add time if they are repeated many times. Anyway, I just received my first book on Arduino sketching and started reading it. Hopefully, I'll know more in about a week. But, I'll still have questions no doubt. This is an excellent resource! – RED Jan 3 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.