Components and supplies
Arduino UNO
Toggle Switch, Toggle
Resistor 10k ohm
Jumper wires (generic)
Apps and platforms
Arduino IDE
Project description
Code
Reading a Toggle Switch
c_cpp
Reliably read a toggle switch, irrespective of circuit configuration.
1// Ron D Bentley, Stafford, UK 2// Jan 2021 3// 4// This example and code is in the public domain and may be used without restriction and 5// without warranty. 6/* 7 Reading a simple toggle switch, with debounce 8 ''''''''''''''''''''''''''''''''''''''''''''' 9 This sketch presents a method for reading a simple toggle switch 'cleanly' such 10 that any electrical noise created during switch transition is removed. Without 11 this, then simply relying on digitalRead() could provide flaky results. 12 13 The sketch has been designed to support two commonly wired switch circuits, as below, but note: 14 15 1. The digital pin chosen as the input is initialised according to the 'circuit_type' macro 16 parameter, either INPUT_PULLUP or INPUT using a call to pinMode(button_switch,circuit_type) 17 in the setup() function. 18 19 As the conditions for detecting switch on/off are different for each 'circuit_type' (they are 20 reversed) this is automatically accommodated for in the reading of the toggle switch. 21 The only requirement is to define the 'circuit_type', everything else is 22 automatically taken care of. 23 24 2. The wiring designs for each type of switch circuit are: 25 26 circuit_C1 - With switch pull down resister configured and 'circuit_type' of INPUT 27 28 10K ohm button (onboard) 29 resister switch LED 30 ____|<><><>|_______ \\___ O 31 | | | | 32 0v pin = 2 +5v pin = 33 LED_BUILTIN 34 ___________________________________ 35 | ARDUINO | 36 37 circuit_C2 - With NO switch pull down resister configured and 'circuit_type' of INPUT_PULLUP 38 39 button (onboard) 40 switch LED 41 ___ \\___ O 42 | | | 43 pin 2 0v pin = 44 LED_BUILTIN 45 _______________________ 46 | ARDUINO | 47 */ 48 49#define circuit_C1 INPUT // switch configured with a 10k ohm pull down resistor 50#define circuit_C2 INPUT_PULLUP // switch simply connected to ground, no resistor 51#define circuit_type circuit_C2 // defines which of the two common circuits types is configured 52 53bool current_toggle_status = LOW; // start assuming switch switch is off 54 55int toggle_switch_pin = 7; 56#define debounce 10 // debounce period in milli seconds 57 58#define LED LED_BUILTIN // normally pin 13, but this ensures it is set correctly by the compiler 59 60// 61// test the toggle switch to see if its status has changed since last look. 62// note that, although, switch status is a returned value from the function, 63// the current status of the switch 'current_toggle_status' is always maintained 64// and can be tested outside of the function at any point/time. 65// 66bool read_toggle_switch() { 67 static long int elapse_time = 0; 68 static bool transition_started = false; 69 int pin_value; 70 pin_value = digitalRead(toggle_switch_pin); // test current state of toggle pin 71 if (circuit_type == circuit_C2) { 72 // need to invert HIGH/LOW if circuit design sets pin HIGH representing switch in off state 73 // ie inititialised as INPUT_PULLUP 74 pin_value = !pin_value; 75 } 76 if (pin_value != current_toggle_status && !transition_started) { 77 // switch change detected so start debounce cycle 78 transition_started = true; 79 elapse_time = millis(); // set start of debounce timer 80 } else { 81 if (transition_started) { 82 // we are in the switch transition cycle so check if debounce period has elapsed 83 if (millis() - elapse_time >= debounce) { 84 // debounce period elapse so assume switch has settled down after transition 85 current_toggle_status = !current_toggle_status; // flip status 86 transition_started = false; // cease transition cycle 87 } 88 } 89 } 90 return current_toggle_status; 91} // end of read_toggle_switch 92 93void setup() { 94 pinMode(toggle_switch_pin, circuit_type); // setup switch read pin in accordance with circuit design 95 pinMode(LED, OUTPUT); // setup LED pin 96 digitalWrite(LED, LOW); // ensure LED off 97} // end of setup 98 99void loop() { 100 digitalWrite(LED_BUILTIN, read_toggle_switch()); 101} 102
Comments
Only logged in users can leave comments
Anonymous user
3 years ago
This is very well put together; just at a time I needed clarity in regards to toggle switches. Thanks ever so much!
ronbentley1
5 Followers
•28 Projects
4
2
nouman_babar
2 years ago
code not working, when button pressed led will be on but when you leave the button then led will be off. toggle means first press led on and second press led off. and vice versa. This code waste my lot time