Unlike the ubiquitous button switch which has a switching cycle of OFF-ON-OFF a simple toggle switch has a switching cycle of OFF-ON or ON-OFF - it will stay in the switch position until switched again.
Pretty obvious stuff? Yes, but if we are to provide a software solution for reliably reading a toggle switch, or any switch for that matter, we must understand the particular design elements at play.
Okay, point taken, so why not simply read the switch's status using digitalRead()? This will tell us if the switch is LOW or HIGH.
Yes, we can do this and for 99% of the time we will get a valid result. However, and just to further complicate matters, we have our old nemesis and baggage in that the act of physically switching will invariably mean that we will experience 'noise' and 'bouncing' during switch transition. Therefore, if we wish for a software solution that will read a toggle switch reliably 100% of the time then we must factor this into our design.
Not a problem - there are many such solutions on the internet that will do this. So why have I put together this article? It is because I wished to build on my previous and similar articles concerning the fundamentals of switches (see the end of this article) - to offer further understanding and an alternative approach, one that is flexible to the switch's circuit design.
The associated sketch offers an approach to reading a toggle switch that it:
- is flexible in being configured for the two common styles of switch circuit design, these are:
circuit_C1 - circuit with a 10k ohm pull down resistor, initialised as INPUT, and
circuit_C2 - circuit without any external resistors, initialised as INPUT_PULLUP
- allows either circuit design to be specified with one change in a configuration parameter (#define circuit_type)
- incorporates debounce code
- allows the debounce period to be easily changed (#define debounce), as required
- code for reading toggle switch encapsulated in standalone Boolean function, allowing it to be called and integrated throughout a sketch, as required
- provides toggle switch status both as a switch read function call return value and as a global variable (current_toggle_status)
- correctly sets the toggle switch status after setup().
Have a look at the sketch, load it up and play around with it. The sketch uses the in built LED to indicate the switch's status - illuminated if on, otherwise not illuminated. Don't forget to specify which switch circuit you have connected to your board (#define circuit_type circuit_C1, or #define circuit_type circuit_C2).
For an understanding of the two circuit designs reference by this article/sketch see Understanding and Using Button Switches, the basics.
Further ReadingYou might also find these contributions interesting and useful, by the same author:
- UnderstandIngandUsIngButtonSwItches,thebasIcs - button switches, a simple but often tricky piece of kit. The tutorial provides in ins and outs of implementing a simple button switch, with flexibility to explore differences in circuit design, different reading methods and debouncing.
- Interrupts Driven Button Switches - an approach and example of tying a button switch to an external interrupt.
- External Interrupts, a generic framework supporting concurrent asynchronous multiple interrupts. Configure multiple external interrupts with different characteristics and add code to provide post-interrupt asynchronous processing.
- Programmatic Timed Reminder Alerting, a programmatic framework for both elapsed and real-time asynchronous alerting. Define any number of reminder alerts (sub second to hours) and process asynchronously.
Other Online Resources
enjoy!
Comments
Please log in or sign up to comment.