Introduction: INTRODUCTION TO I/O's IN ARDUINO (PUSHBUTTON)

About: B.sc in Electrical Engineering. Microcontrollers,PLC specialist programmer and embedded system developer.

Date: 3/11/2016

Title: Arduino inputs (Digital)

Difficulty: Easy

Hello everyone. I am here to talk about digital inputs in general, kinds of digital push buttons and digital push button programming in arduino. A push button is a digital input device that allows the flow of current through a circuit when closed and eliminates the flow of current when open. Their are two types of switches 1. normally open switch and 2. normally close switch and you can select based on your requirement.

On the other hand switches can be also be Maintained(Latching) or Momentary. The Maintained switch keeps the contact when actuated, these switches don't require software programming and can be easily set up in a circuit. Example is a light switch.

The Momentary switch only keeps contact and allows current flow along the wire as long as it is held in the pressed position, when its released current flow stops. This Topic is based on Momentary switch, and how it can be latched or programmed to work as a Maintained switch.


Step 1: PROJECT RQUIREMENTS

As i said, Momentary switches only stay on as long as they are pressed, but can be programmed to work as a Maintained switch.So for this simple task we will need the followings:-

  • Platform- Arduino
    • Switch - Momentary Push button
      • 2 Resistors
        • Jumper Wires and Breadboard.
          • Output signaling device - LED
            • Power supply - 9v battery / Arduino power outlet(5V - GND)

Step 2: SETUP

Switch:

Connect of the legs/pin of the switch to GND using a Resistor and the other to +5V using a Jumper, then the pin on the opposite side of the leg connected to GND is connected to any digital pin of your choice on the arduino. (Pin 2 here)

LED:

connect the short leg of the LED to GND using a resistor and the longer leg of the LED to any digital pin of your choice on the arduino. (Pin 3 here)

Step 3: SOFTWARE CODE

    1. //Port initlization
    2. int PB = 2;
    3. int LED = 3;
    4. // volatile variables decleration
    5. boolean state=0;
    6. boolean laststate=0;
    7. void setup(){
    8. pinMode(PB,INPUT); //set PB as INPUT
    9. pinMode(LED,OUTPUT); //set LED as OUTPUT
    10. }
    11. void loop(){
    12. state =(digitalRead(PB)); //The value of the state is read from the PB.
    13. if (state == 1 && laststate == 0)
    14. {
    15. //if the value of the state is 1(pressed and the laststate was 0(unpressed)
    16. delay(500); //delay for 500 ms(delay for quenching / debouncing for clean switching)
    17. digitalWrite(LED,HIGH); //pin 3 is set to high
    18. state=1;// the new state is now 1
    19. laststate=1;// the last state is now 1
    20. }
    21. else if (state == 1 && laststate == 1){
    22. //if state is one(ON/PRESSED) and the laststate read is ON/PRESSED
    23. delay(500) ; // delay for 500ms
    24. digitalWrite(LED,LOW); //pin 3 is set to low
    25. laststate=0; // laststate is 0 again.
    26. }
    27. }



    if you have any questions please leave them in the comment box. Thanks.