Example Code for Arduino-10 Segment LED Bar Graph Control

The sketch for using the 10 Segment LED Bar Graph with prototype shield. Users can learn how to control multiple LEDs sequentially using Arduino digital pins and understand bitwise operations for LED state control.

Hardware Preparation

Software Preparation

Wiring Diagram

FIT0188 10 Segment LED Bar Graph - Red Connection Diagram

Other Preparation Work

Please note that the connection diagram shows the pins connected in reverse order for the sake of clarity. When you run the sample code included here you might notice a reverse order sequence. If you wish to wire the LEDs in the correct order you should wire:
Arduino D2 to PIN1
Arduino D3 to PIN2
Arduino D4 to PIN3
Arduino D5 to PIN4
and so on...

Alternatively you could modify the sample code to operate in the expected order.

Sample Code

// #
// # Editor     : Lauren from DFRobot
// # Date       : 19.03.2012

// # Product name: 10 Segment LED Bar Graph
// # Product SKU : FIT0188
// # Version     : 1.0

// # Description:
// # The sketch for using the 10 Segment LED Bar Graph with prototype shield

// # Connection:
// #        LED Bar positive pins -- Arduino digital pins through a 1K resistor
// #        LED Bar negative pins -- GND

const int ledPin[10] =  {
  2,3,4,5,6,9,10,11,12,13};      // the number of the LED pins
int ledState = 0;

void setup() {
  Serial.begin(9600);
  // set the digital pin as output:
  for(int i = 0; i < 10 ;i   ){
    pinMode(ledPin[i], OUTPUT);
    digitalWrite(ledPin[i], LOW);
  }
  ledState = 1;
  display(ledState);
  delay(100);
}

void loop()
{
  for(int i = 0; i < 9; i  ){
    ledState = ledState << 1;
    display(ledState);
    delay(50);
    Serial.println(ledState,BIN);
  }
  for(int i = 9; i > 0; i--){
    ledState = ledState >> 1;
    display(ledState);
    delay(50);
    Serial.println(ledState,BIN);
  }
}

void display(int sat){
  for(int i = 0; i < 10 ;i   ){
    digitalWrite(ledPin[i],bitRead(ledState,i));
  }
}

Was this article helpful?

TOP