LED Bar Graph

Start 

The LED Bar Graph has 10 individually controllable LEDs. 1 red, 1 yellow, and 8 green.

Modules 

Gather the following parts to complete this project.

Parts

All Parts
All Parts
LED Bar Graph
Rotary Potentiometer
BME280
Cable
PartQuantity
LED Bar Graph 1
Rotary Potentiometer 1
BME280 1
Cable 2

LED Bar Graph Levels Demo 

Take a cable and unwrap it. Plug one side into the LED Bar Graph socket and the other into the D8 socket.

All the parts you'll need
All the parts you'll need
Take a cable...
... and unwrap it
Plug one side into the LED Bar Graph socket
... and the other into the D8 socket

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
Grove LED Bar - Level Example
This example will show you how to use setLevel() function of this library.
The setLevel() function illuminates the given number of LEDs from either side.
Syntax setLevel(level)
0  = all LEDs off
5  = 5 LEDs on
10 = all LEDs on
*/

#include <Grove_LED_Bar.h>

Grove_LED_Bar bar(9, 8, 0);  // Clock pin, Data pin, Orientation

void setup()
{
  // nothing to initialize
  bar.begin();
}

void loop()
{
  // Walk through the levels
  for (int i = 0; i <= 10; i++)
  {
    bar.setLevel(i);
    delay(100);
  }
}

Observe

Look at the LED Bar Graph as it cycles through all 10 levels.

Bar Graph Levels
Bar Graph Levels
Bar Graph Levels
Bar Graph Levels

LED Bar Graph Basic Control 

Each individual LED on the bar graph is controllable. The following example code utilizes binary to turn off the individual LEDs.

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Grove LED Bar - Basic Control Example
This example will show you how to use the setBits() function of this library.
Set any combination of LEDs using 10 bits.
Least significant bit controls the first LED.
The setBits() function sets the current state, one bit for each LED.
First 10 bits from the right control the 10 LEDs.
eg. 0b00000jihgfedcba
a = LED 1, b = LED 2, c = LED 3, etc.
dec    hex     binary
0    = 0x0   = 0b000000000000000 = all LEDs off
5    = 0x05  = 0b000000000000101 = LEDs 1 and 3 on, all others off
341  = 0x155 = 0b000000101010101 = LEDs 1,3,5,7,9 on, 2,4,6,8,10 off
1023 = 0x3ff = 0b000001111111111 = all LEDs on
                      |        |
                      10       1
The bits >10 are ignored, shown here as x: 0bxxxxx0000000000
*/

#include <Grove_LED_Bar.h>

Grove_LED_Bar bar(9, 8, 0);  // Clock pin, Data pin, Orientation

void setup()
{
  // nothing to initialize
  bar.begin();
}

void loop()
{
  // Turn on all LEDs
  bar.setBits(0x3ff);
  delay(1000);

  // Turn off all LEDs
  bar.setBits(0x0);
  delay(1000);

  // Turn on LED 1
  // 0b000000000000001 can also be written as 0x1:
  bar.setBits(0b000000000000001);
  delay(1000);

  // Turn on LEDs 1 and 3
  // 0b000000000000101 can also be written as 0x5:
  bar.setBits(0b000000000000101);
  delay(1000);

  // Turn on LEDs 1, 3, 5, 7, 9
  bar.setBits(0x155);
  delay(1000);

  // Turn on LEDs 2, 4, 6, 8, 10
  bar.setBits(0x2AA);
  delay(1000);

  // Turn on LEDs 1, 2, 3, 4, 5
  // 0b000000000011111 == 0x1F
  bar.setBits(0b000000000011111);
  delay(1000);

  // Turn on LEDs 6, 7, 8, 9, 10
  // 0b000001111100000 == 0x3E0
  bar.setBits(0b000001111100000);
  delay(1000);
}

Observe

Look at the LED Bar Graph as it cycles through controlling each of the LEDs.

Experiment

Modify the code to create your own patterns.

Alternative

You can also control the individual LEDs using the setLed() function. It takes two numbers as an argument. The first is the number of the led that you want to control and the second is whether you want it on or off (1 or 0). For example, the line bar.setLed(5, 1); turns the fifth led on and the line bar.setLed(8, 0); turns the eighth led off. Here is the previous example but using this function.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <Grove_LED_Bar.h>

Grove_LED_Bar bar(9, 8, 0);  // Clock pin, Data pin, Orientation

void setup()
{
  // nothing to initialize
  bar.begin();
}

void loop()
{
  // Turn on all LEDs
  for (int i = 0; i <= 10; i++)
  {
    bar.setLed(i,1);
  }
  delay(1000);

  // Turn off all LEDs
  for (int i = 10; i >= 0; i--)
  {
    bar.setLed(i,0);
  }
  delay(1000);

  // Turn on LED 1
  bar.setLed(1,1);
  delay(1000);

  ledsOff();

  // Turn on LEDs 1 and 3
  bar.setLed(1,1);
  bar.setLed(3,1);
  delay(1000);

  ledsOff();

  // Turn on LEDs 1, 3, 5, 7, 9
  bar.setLed(1,1);
  bar.setLed(3,1);
  bar.setLed(5,1);
  bar.setLed(7,1);
  bar.setLed(9,1);
  delay(1000);

  ledsOff();

  // Turn on LEDs 2, 4, 6, 8, 10
  bar.setLed(2,1);
  bar.setLed(4,1);
  bar.setLed(6,1);
  bar.setLed(8,1);
  bar.setLed(10,1);
  delay(1000);

  ledsOff();

  // Turn on LEDs 1, 2, 3, 4, 5
  bar.setLed(1,1);
  bar.setLed(2,1);
  bar.setLed(3,1);
  bar.setLed(4,1);
  bar.setLed(5,1);
  delay(1000);

  ledsOff();

  // Turn on LEDs 6, 7, 8, 9, 10
  bar.setLed(6,1);
  bar.setLed(7,1);
  bar.setLed(8,1);
  bar.setLed(9,1);
  bar.setLed(10,1);
  delay(1000);
}

void ledsOff(){
    bar.setBits(0x0);
}

Displaying the Potentiometer Value 

Take a cable and unwrap it. Plug one side into the rotary potentiometer socket and the other into the A0 socket.

Take a cable...
Take a cable...
... and unwrap it
Plug one side into the rotary potentiometer socket
... and the other into the A0 socket

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <Grove_LED_Bar.h>
#define dialSocket A0

Grove_LED_Bar bar(9, 8, 0);  // Clock pin, Data pin, Orientation

void setup()
{
  pinMode(dialSocket, INPUT);

  // nothing to initialize
  bar.begin();
}

void loop()
{
  int dialValue = analogRead(dialSocket);
  int barGraphLevel = map(dialValue, 0, 1023, 0, 11);
  bar.setLevel(barGraphLevel);
}

Observe

Rotate the potentiometer and see how the level of the LED Bar Graph changes.

Dial one way
Dial one way
Dial other way
A connection

Additional Information

You might be wondering why we've set the range on the map function from 0 to 11 and not 0 to 10. That's because if you use the value 10 then you'll only be able to completely fill the bar if the potentiometer is turned to the max. If it's even a little bit short you won't reach it. Try this yourself: just change the 11 to a 10 and you'll see that you can barely fill the bar. You might not even be able to completely fill the bar at all (don't try to force it).

Temperature Indicator 

Let's modify the code to display temperature instead. This way we can know whether to dress for warm weather or cold weather by quickly glancing at the LED Bar Graph. Swap out the Rotary Potentiometer for the BME280.

Unplug the rotary potentiometer from A0 socket
Unplug the rotary potentiometer from A0 socket
Plug it in to any I2C socket
Unplug the cable from the rotary potentiometer
Plug in the BME280

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <Grove_LED_Bar.h>
#include "Seeed_BME280.h"
#include <Wire.h>
#include <math.h>

BME280 bme280;

float temperature;

//These are in Celsius
float highTemp = 30;
float lowTemp = 15;


Grove_LED_Bar bar(9, 8, 0);  // Clock pin, Data pin, Orientation

void setup()
{
  bar.begin();
  bme280.init();
}

void loop()
{
  temperature = bme280.getTemperature();
  int barGraphLevel = map(temperature, lowTemp, highTemp, 1, 11);
  bar.setLevel(barGraphLevel);
}

Modify 

Change the highTemp and lowTemp variables to temperatures that you find comfortable. lowTemp is the lowest you feel comfortable with. Any lower than your lowTemp and it will appear red on the LED Bar Graph.