Project tutorial
LED Bar Graph and Switch Array

LED Bar Graph and Switch Array © GPL3+

Max out the digital I/O on the Arduino Uno Rev3!

  • 147 views
  • 0 comments
  • 2 respects

Components and supplies

Necessary tools and machines

Apps and online services

About this project

Introduction

If you haven’t read our Getting Started Guide for the Arduino Uno Rev3, please read that first. Otherwise, continue reading. In this tutorial, we’ll be maxing out the digital I/O on the Arduino Uno Rev3 (Uno). We interface a 10-segment LED bar graph and an 8-segment switch array. We’ll use 9/10 LEDs on the bar graph and 5/8 switches on the switch array for a total of 14 pins that we’ll need on the Uno. Each LED input will be an output from the Uno and each switch output will be an input to the Uno.

LED Bar Graph

The LED bar graph consists of a vector of independent LEDs. The number and color of the LEDs can vary, but we’ll be using a red, 10-segment bar graph. The internal circuit and pin assignments are shown in the image below. Each LED is oriented the same internally, so the anodes are pins 1-10 and the cathodes are pins 11-20.

To limit the current passing through each LED, we’ll be using a 470 ohm, 9-resistor array. If you do not place a resistor in series with the LED, you are essentially shorting power to ground and could damage the LED or power supply.

There are two important numbers to look for in the LED’s datasheet. The first one is the forward voltage which is the voltage drop across the LED. This is usually given as a maximum rating, so you want to make sure to keep the voltage at or below the forward operating voltage. The second is the forward current which is the maximum current that should be sourced to the LED. For example, the forward voltage and current for each LED in the bar graph is 2.6V and 25mA, respectively. The circuit for one LED is shown below.

The 470 ohm resistor is a little big when connecting to a 5V source. The current through the LED will be around 5mA, but the LED is still bright enough. We did this because the FuelCan does provide +12V, so if you wanted to use that rail for the LEDs, you’d still be under the forward current rating.

Switch Array

The switch array varies in the number of switches and type – we’ll be using an 8-position SPST (single-pull double-throw) slide switch. A schematic of the switch array is shown below. Notice that when the switch is in the OFF position, the switch is open. When the switch is in the ON position, such as switch 5, the switch is closed. Similar to the LED, we also want to limit the current passing through the switch to prevent damage and minimize power consumption.

When you’re consulting the documentation for the switch, you want to look for the max voltage and current. For example, the max voltage and current for this switch array is 24V and 50mA, respectively. We’ll be using a 1.2k ohm resistor in series with each switch array, and the +5V supply on the FuelCan. This means that the current passing through the switch will be ~4.2mA (I = V/R = 5/1.2k). The circuit schematic is shown in the image below.

Let’s say we chose a smaller resistor value, 680 ohms. The current through the switch would be ~7.4mA (I = 5/680), still under the max current rating of the switch. But, we would have a higher power consumption. Power is equivalent to the voltage multiplied by the current (P = V x I), so the power consumption for using the 1.2k ohm and 680 ohm resistor would be 20.8mW and 36.7mW, respectively. This may not seem like a big difference, but when you are designing low-power applications, this difference is huge.

Resistor Array

The resistor array is made up of a number of bussed (one lead of each resistor shares a common node with the other resistors in the array) resistors, in this case, 9 resistors. These are beneficial for adding a series resistor to multiple, identical components such as the LED bar graph or the switch array. They also help with keeping your wiring clean and compact. The internal circuitry of the 1.2k ohm resistor array is shown in the image below (the 470 ohm array has a similar internal circuitry). When you look at the resistor array, pin 1 is identifiable with the dot. This can be connected to power or ground, but make sure to consult the datasheet for the max voltage rating if you are connecting it to power.

Wiring

I’m using a breadboard instead of Modulus since almost everybody has a breadboard. First, let’s place the LED bar graph and switch array onto the breadboard. Insert both so that the body is over the valley of the breadboard. You do not want the pins to short each other by being connected to the same node. Next, place the 470 ohm resistor array on the cathode side of the LED and tie pin 1 of the array to GND. Make sure that pin 1 of the resistor array is not connected to any of the cathodes of the LED bar graph. Since the bar graph has 10 LEDs and the resistor array only has 9 resistors, we can only use 9 out of 10 LEDs.

Next, use a M/M jumper to wire the anode side of the LED bar graph as outputs to the Uno pins 0-8. For example, pin 10 of the bar graph will get wired to Uno pin 0, pin 9 of the LED will get wired to Uno pin 1, and so on.

Place the 1.2k ohm resistor array as shown in the image below. Tie pin 1 of the array to GND, and make sure it does not connect to any of the switch pins on the switch array. The breadboard holes outlined in yellow will be tied to Uno pins 9-14. For example, switch 1 will be wired to pin 9, switch 2 will get wired to pin 10, and so on. Tie the other switch pins, 1-5, to +5V.

If you haven’t mounted the Uno onto the prototyping area of the FuelCan, go ahead and do that. I placed the breadboard in the bottom storage compartment to limit the length of the jumper wires. We need to supply +5V and GND to the power and ground rails on the breadboard. Use the provided banana jack to test-lead clip cables to do so. You will need two male header pins to mount the test-lead clips to on the breadboard side. Plug the Type A side of the USB cable into USB1 receptacle and the Type B side into the Uno’s receptacle. Power up the FuelCan with the AC-DC power adapter.

Software

Once the wiring is complete and the FuelCan is powered up, we can now load the sketch onto the Uno. To change the LED function, simply change switches 1-5 to the ON position. If there are multiple switches in the ON position, the LED function will take priority based on the lowest switch number. For example, if switches 1 and 3 are ON, the LED function will be the one mapped to switch 1.

Complete Setup

Code

LED and Switch ArrayArduino
//Control LED bar graph function based on input from switch array
//Last modified April 2019

int LED0 = 0;
int LED1 = 1;
int LED2 = 2;
int LED3 = 3;
int LED4 = 4;
int LED5 = 5;
int LED6 = 6;
int LED7 = 7;
int LED8 = 8;

int switch1 = 9;
int switch2 = 10;
int switch3 = 11;
int switch4 = 12;
int switch5 = 13;

int s1status = 0;
int s2status = 0;
int s3status = 0;
int s4status = 0;
int s5status = 0;

void setup() {
  //set pins 0-8 as output
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);

  //set pins 9-14 as input
  pinMode(switch1, INPUT);
  pinMode(switch2, INPUT);
  pinMode(switch3, INPUT);
  pinMode(switch4, INPUT);
  pinMode(switch5, INPUT);

}

void loop() {
  //check inputs from switches
  s1status = digitalRead(switch1);
  s2status = digitalRead(switch2);
  s3status = digitalRead(switch3);
  s4status = digitalRead(switch4);
  s5status = digitalRead(switch5);
  
  if(s1status == 1){
    //perform LED function 1 -- forward light
    
    digitalWrite(LED0, HIGH);
    delay(200);
    digitalWrite(LED0, LOW);
    digitalWrite(LED1, HIGH);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(200);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    delay(200);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
    delay(200);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, HIGH);
    delay(200);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, HIGH);
    delay(200);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, HIGH);
    delay(200);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, HIGH);
    delay(200);
    digitalWrite(LED8, LOW);
  }

  else if (s2status == 1){
    //perform LED function 2 -- group toggle

    //Turn ON LEDs 0-3, turn OFF 4-7
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED0, HIGH);
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    delay(200);

    //Turn OFF LEDs 0-3, turn ON 4-7
    digitalWrite(LED0, LOW);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, HIGH);
    delay(200);
  }

  else if (s3status == 1){
    //perform LED function 3 -- backward light

    digitalWrite(LED8, HIGH);
    delay(200);
    digitalWrite(LED8, LOW);
    digitalWrite(LED7, HIGH);
    delay(200);
    digitalWrite(LED7, LOW);
    digitalWrite(LED6, HIGH);
    delay(200);
    digitalWrite(LED6, LOW);
    digitalWrite(LED5, HIGH);
    delay(200);
    digitalWrite(LED5, LOW);
    digitalWrite(LED4, HIGH);
    delay(200);
    digitalWrite(LED4, LOW);
    digitalWrite(LED3, HIGH);
    delay(200);
    digitalWrite(LED3, LOW);
    digitalWrite(LED2, HIGH);
    delay(200);
    digitalWrite(LED2, LOW);
    digitalWrite(LED1, HIGH);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED0, HIGH);
    delay(200);
    digitalWrite(LED0, LOW);
  }

  else if (s4status == 1){
    //perform LED function 4 - forward and backward light

    //foward
    digitalWrite(LED0, HIGH);
    delay(200);
    digitalWrite(LED0, LOW);
    digitalWrite(LED1, HIGH);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(200);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    delay(200);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
    delay(200);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, HIGH);
    delay(200);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, HIGH);
    delay(200);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, HIGH);
    delay(200);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, HIGH);
    delay(200);
    digitalWrite(LED8, LOW);

    //backward
    digitalWrite(LED8, HIGH);
    delay(200);
    digitalWrite(LED8, LOW);
    digitalWrite(LED7, HIGH);
    delay(200);
    digitalWrite(LED7, LOW);
    digitalWrite(LED6, HIGH);
    delay(200);
    digitalWrite(LED6, LOW);
    digitalWrite(LED5, HIGH);
    delay(200);
    digitalWrite(LED5, LOW);
    digitalWrite(LED4, HIGH);
    delay(200);
    digitalWrite(LED4, LOW);
    digitalWrite(LED3, HIGH);
    delay(200);
    digitalWrite(LED3, LOW);
    digitalWrite(LED2, HIGH);
    delay(200);
    digitalWrite(LED2, LOW);
    digitalWrite(LED1, HIGH);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED0, HIGH);
    delay(200);
    digitalWrite(LED0, LOW);
  }

  else if (s5status == 1){
    //perform LED function 5 -- Inward

    digitalWrite(LED0, HIGH);
    digitalWrite(LED7, HIGH);
    delay(200);
    digitalWrite(LED0, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED1, HIGH);
    digitalWrite(LED6, HIGH);
    delay(200);
    digitalWrite(LED1, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED5, HIGH);
    delay(200);
    digitalWrite(LED2, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    delay(200);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    delay(200);
    
  }

  else{
    //perform default LED function if no switch is ON
    digitalWrite(LED0, LOW);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
  }

}

Schematics

Schematic
Schematic v3 k8igwz3gkd

Comments

Similar projects you might like

Bluetooth control led with lcd led status display real time.

Project tutorial by Youssef Sabaa

  • 19,972 views
  • 4 comments
  • 40 respects

Bar Graph

Project tutorial by Tech with Matthew

  • 9,018 views
  • 16 comments
  • 41 respects

Johnny Five and Socket.IO LED web switch

Project tutorial by Gerardo Ramírez

  • 2,090 views
  • 0 comments
  • 2 respects

LED Emergency Lights using WS2812 RGB LED Module

Project tutorial by bigboystoys13

  • 11,644 views
  • 7 comments
  • 29 respects

DMX RGB LED Outdoor

Project tutorial by Sebastian Wiessner

  • 10,752 views
  • 7 comments
  • 15 respects

Control your LED with Cortana

Project tutorial by Mohamed Emam

  • 9,343 views
  • 7 comments
  • 21 respects
Add projectSign up / Login