Reading a Push Button Toggle

9.0K242

Intro: Reading a Push Button Toggle

Push buttons are very simple to use in analog circuits as switches. Pushing the button simply means a close circuit and letting it go back to rest is an open circuit, under the assumption of a normally open push button switch. However, the use of a push button goes beyond as a simple switch when an Arduino or any microcontroller is involved.

An Arduino can read the input signals of a push button switch and act accordingly. In addition, it can store this value and act as a 'programmed toggle flip-flop.' However, this particular use case for reading a push button switch may not be quite as intuitive to make as expected. As such, the focus of this project is the proper use and wiring for a digital reading of a push button to be used to toggle multiple colours of an RGB LED!

STEP 1: Tools and Materials

  • Arduino 101 or Arduino Uno
  • Breadboard
  • Push Button Switch
  • RGB LED
  • 3 pieces of 100Ω resistors
  • Jumper Wires

STEP 2: Circuitry

Connecting the Arduino Power to the Breadboard

  • Connect the 3.3V pin of the Arduino to the red power rail of the breadboard.
  • Connect the GND pin of the Arduino to the black power rail of the breadboard.

Wiring the Push Button

  • Connect a 10K Ω resistor from one of the pins of the Push Button Switch to the red power rail of the breadboard.
  • Connect the same pin above to the digital pin 3 of the Arduino.
  • Connect the other pin on the same side to the ground rail of the breadboard.

Lastly, connect the RGB LED to the Arduino.

  • Connect the longest pin of the RGB to the common ground rail of the breadboard.
  • Connect the remaining three pins to a 100Ω resistor in series to pins 9, 10, 11 respectively.

STEP 3: Code

const int buttonPin = 3;
 //RGB LED pins  const int redPin = 11;      const int greenPin = 10;  const int bluePin = 9;
//create a variable to store a counter and set it to 0 int counter = 0; void setup() {   // Set up the pushbutton pins to be an input:   pinMode(buttonPin, INPUT);
  // Set up the RGB pins to be an outputs:   pinMode(redPin, OUTPUT);     pinMode(greenPin,OUTPUT);   pinMode(bluePin,OUTPUT); }
void loop() {  // local variable to hold the pushbutton states   int buttonState;  
  //read the digital state of buttonPin with digitalRead() function and store the           //value in buttonState variable   buttonState = digitalRead(buttonPin);
  //if the button is pressed increment counter and wait a tiny bit to give us some          //time to release the button   if (buttonState == LOW) // light the LED   {     counter++;     delay(150);   }
  //use the if satement to check the value of counter. If counter is equal to 0 all         //pins are off   if(counter == 0)   {    digitalWrite(redPin,LOW);    digitalWrite(greenPin,LOW);    digitalWrite(bluePin,LOW);   }
  //else if counter is equal to 1, redPin is HIGH   else if(counter == 1)   {    digitalWrite(redPin,HIGH);    digitalWrite(greenPin,LOW);    digitalWrite(bluePin,LOW);   }
  //else if counter is equal to 2 greenPin is HIGH   else if(counter ==2)   {    digitalWrite(redPin,LOW);    digitalWrite(greenPin,HIGH);    digitalWrite(bluePin,LOW);   }
  //else if counter is equal to 3 bluePin is HIGH   else if(counter ==3)   {    digitalWrite(redPin,LOW);    digitalWrite(greenPin,LOW);    digitalWrite(bluePin,HIGH);   }
  //else reset the counter to 0 (which turns all pins off)   else   {    counter =0;   } }

STEP 4: Demo


As I push the push-button, it first toggles to red, then green, then blue, and finally off. This cycles through all the colours as only 1 push button is pressed!

2 Comments

First of all, the code... This way it is very difficult to read it. Please, fix it so that it is readable..

Then this is a good start, but if you want to make a real reading of the button you will have to consider some more things:

* you use an external pullup; you can activate the internal one (so no resistor required on the button)

* the debounce you use is not very good. A better approach is to have a pin state variable, and change its value only if the button has been in the different state for, let's say, 50ms. This way you will not be blocking the program when pressing

* what happens if you don't remove the finger from the button? Yes, it cycles. You can remove this by triggering the change of "counter" only on a falling edge of the button

You can fix the last two points by using a library I started to use a long time ago: Bounce2. You can use it to monitor buttons; you will not have bounces, and it gives you two functions (rose() and fell()) that let you trigger something on the variation of the pin status

First of all, I'd like to sincerely thank you for taking the time to comment on my instructable. I very much appreciate it.

I will fix the formatting when I have a bit of free time this weekend. I'll definitely take your advices for my next projects using a push button. I learned so much today from you. Thanks again!