Intro: Arduino and Micro Switch
Some times it so happens that we do the greater things with ease but struggle to do the little ones. My friend demurred when I revealed to him my thoughts of writing this instructable, but I'm writing it anyways. I believe even the basics sometimes matter the most. So lets begin
Step 1: Components
- Bread board
- Resistors(1kohms and10kohms)
- Push button
With these components in hand rig up the circuit
Step 2: Connections
A led is connected to pin 7 with a series resistor of 1kohm
Push button is connected to pin 10.
The circuit can be rigged up with the aid if images
Step 3: Working
The push button has four pins(legs). A pair of legs are at the same potential.i.e there is continuity in between them . The picture shown above is the below portion of the push button. The pins on the same side one of the line segment (hope it is visible) are continuous while the pins on the same side of the other line are also continuous. Now care must be taken while connecting the switch and other elements to it.
Take a pair and connect one of the pins to pin 10 of arduino while the other one to 5V through a pull up resistor of 10k ohms. Connect one of the pins from the other pair to the ground. So with this connections the state of push button(pin 10) is always high.Whenever the button is pressed the circuit gets completed through the ground and the state of pin 10 becomes low. Now at this condition we have to perform any task. I've made a led glow for 2 sec.You can make a capacitor charge or discharge or any other idea that comes to your mind
Step 4: Code
int led = 7;
int swtch = 10;
int debounce(int pin)
int state, previous_state;
previous_state = digitalRead(pin);
for(int i = 0; i < 25; i++)
state = digitalRead(pin);
if( state != previous_state)
i = 0;
previous_state = state;
Step 5: Understanding the Need of Debouncing
Debouncing is a very important concept that needs to be considered while dealing with the switches. This is to over come the effects of switch bouncing.
What is switch bouncing?
When we press a switch we expect the switch to make a single contact with the circuit below it. But there will be multiple contacts in fraction of seconds(order of micro seconds). These multiple contacts lead to a undesired (unexpected)state of the switch. To over come this we need to give a sufficient time delay say atleast a milli second.
This is the reason to include the debouncing function. Inside this function the state of the pin 10 is read and is passed to 'void loop'. This very logic of debounce is widely followed in many places. I would suggest to bookmark the code if you are beginner with programming.
I would like to mention another common error that is made: in the code I've named pin 10 as 'swtch'. Pls note that 'switch' cannot be used to name the pin as it is pre-defined word
Step 6: Conclusion
I hope all those who were new to this found it useful. Feel free to share the adversities faced if any, while trying it out. Have fun!
Keep thinking,keep doing