Arduino Digital Inputs & Outputs Revisited – RGB LED example

Playing with Digital Arduino Inputs and Outputs

The absolute fundamentals you need to master when it comes to Arduino, are the Digital Inputs and Outputs. They will allow you to accomplish 90% of all tasks. These include applications such as blinking LEDs, turning motors on and off, detecting a presence of an object and much more. In fact, you should be able to create fairly complex robots with a mastery of digital inputs and outputs.

The focus of this tutorial and example is on toggling the states of an RGB LED. By mastering this technique you should be able to store states based on button presses and react to them accordingly.

The required Hardware

For this tutorial, you will need the following:
Arduino Digital Inputs

  • Arduino
  • Breadboard
  • Push Button
  • RGB LED or three separate LEDs
  • 4 Resistors – 3 Current Limiting for the LED and 1 Pull-up for the button (Internal can also be used)
  • Wires

Make sure to pay attention to the LED you’ve selected. The common anode LED will be connected and programmed differently from the common cathode one. See the diagram below to correctly connect the LED and understand which states will toggle it.
rgb LED diagram arduino

Software for the Arduino Digital Inputs & Outputs

The following software has been developed for the application. I will guide you through the lines and clarify what is being accomplished. Feel free to copy/paste the complete code at the end to try and run it yourself.


int RED = 13;
int GREEN = 12;
int BLUE = 11;
int BUTTON = 10;
int BUTTON_STATE = 0;
int BUTTON_HOLD = 0;
int LED_STATE = 0;

The above code defines each variable. The first three lines specify the LED pins. They are followed by button registers and an LED register. It will be clear what they are used for later in the program.


void setup(){
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(BUTTON, INPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
}

The setup function holds all the pin definitions used in the program. We specify three outputs for the LED and one input for the button. The digitalWrite command is used to turn the LED off on startup. See Arduino Digital Pins for more information on these functions.


void loop() {
LED_UPDATE(LED_STATE);
BUTTON_STATE = digitalRead(BUTTON);
if (BUTTON_STATE == LOW && BUTTON_HOLD == LOW) {
BUTTON_HOLD = HIGH;
if (LED_STATE == 3) {
LED_STATE = 0;
}else{
LED_STATE++;
}
}
if (BUTTON_STATE == HIGH) {
BUTTON_HOLD = LOW;
}
}

The main loop accomplishes multiple steps. The LED state is updated through a function called LED_UPDATE. The button is read with the digitalRead command which returns LOW or HIGH based on the current state of the button. The state of the LED is updated if the button is pushed down. Notice that the BUTTON_HOLD will prevent the button to keep toggling states until it is released.


void LED_UPDATE(int state) {
if (state == 0){
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
delay(50);
}else if (state == 1){
digitalWrite(RED, LOW);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
delay(50);
}else if (state == 2){
digitalWrite(RED, HIGH);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, HIGH);
delay(50);
}else if (state == 3){
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, LOW);
delay(50);
}
}

The above is a simple switch statement. Based on the passed variable, it will execute the given lines of code. They will in turn turn ON the appropriate LEDS on/off.

Complete Code


int RED = 13;
int GREEN = 12;
int BLUE = 11;
int BUTTON = 10;
int BUTTON_STATE = 0;
int BUTTON_HOLD = 0;
int LED_STATE = 0;
void setup(){
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(BUTTON, INPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
}
void loop() {
LED_UPDATE(LED_STATE);
BUTTON_STATE = digitalRead(BUTTON);
if (BUTTON_STATE == LOW && BUTTON_HOLD == LOW) {
BUTTON_HOLD = HIGH;
if (LED_STATE == 3) {
LED_STATE = 0;
}else{
LED_STATE++;
}
}
if (BUTTON_STATE == HIGH) {
BUTTON_HOLD = LOW;
}
}
void LED_UPDATE(int state) {
if (state == 0){
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
delay(50);
}else if (state == 1){
digitalWrite(RED, LOW);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
delay(50);
}else if (state == 2){
digitalWrite(RED, HIGH);
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, HIGH);
delay(50);
}else if (state == 3){
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, LOW);
delay(50);
}
}

Thank you for reading and hopefully this has cleared some questions about the Arduino Digital Inputs and Outputs as well as state transitions.

Comments 1

Leave a Reply

Your email address will not be published.

CommentLuv badge