This tutorial will guide you through creating an LED bar graph that responds to data collected by a sensor, and produces a visual representation of the data.
The example demonstrated above uses a photo resistor to determine the brightness of the surroundings. A photo resistor is a sensor that responds to changes in light by sensing the amount of light and emitting (/allowing to pass through) a corresponding amount of voltage that is read using the analog pin (A0).
In this tutorial, the resulting data (via A0) corresponds to the number of bars that are ON or OFF in the bar graph (for example, 0 bars = absolute darkness; ~3-5 bars = dim room; ~6-8 = bright room; 10 bars = 100 percent brightness).
YOU WILL NEED:
- Arduino Uno and breadboard
- 10 Segment LED Bar Graph
- Photoresistor
- 330OHM Resistors (10)
- 10kOHM Resistor
- Wires (15)
PREPARING YOUR CIRCUIT:
- Attach the Bar Graph LED to the center of the breadboard
- Connect a red wire from 5V to +
- Connect a black wire from GND to –
- Attach 10 wires to the bread board, in each pin next to the right side of the Bar Graph LED. Connect these wires to Arduino pins 2-11 , maintaining order (e.g. pin 11 connects to the bottom of the LED, while pin 2 is the top)
- On the opposite side of the Bar Graph LED, attach 10 330OHM resistors that connect to – (grounded)
- Place the photo resistor to the left of the breadboard; this should also connect to – on one leg and A0 on the other leg.
- Attach a 10k resistor that connects to the analog leg of the photo resistor. Using the resistor and a wire, connect this path to +
- As you prepare your breadboard, trace the path of the circuit ensuring each LED has a corresponding connection to the arduino and a 330OHM resistor that connects to ground (GND), and ensure that the photoresistor connects to both – and +, and A0
THE CIRCUIT DIAGRAM:
Before uploading your code, ensure that your board looks similar to the photos below.
*Note: for easier identification, wires are color coded based their connections: black wires connect to GND; red wires connect to 5V; the white wire connects to the analog (A0), and orange wires connect to the arduino pins.
THE CODE:
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
/* USING AN LED BAR GRAPH TO INDICATE SENSOR READINGS */ /* Using a photo resistor and a Bar Graph LED, this sketch develops a process by which ten red lights respond to the level of light in the surrounding environment. In particular, the code predetermines 10 categories (0% light to 100% brightness) and as the lighting intensity of the surrounding environment increases in intensity, the LED lights will turn on in proportion (e.g. medium intensity lighting environments will turn on 5-6 bar lights and dim enviornments only turn on 1-2 bar lights). The sketch is comprised of two parts: (1) Configuration of the components and (2) Identifying the desired response based on the level of brightness*/ /*PART ONE: CONFIGURATION OF THE COMPONENTS*/ // define the values used in this project, beginning with the sensor. This tutorial relies on the photoresistor to sense light const int sensorPin = 0; // this is the sensor pin for the photoresistor, which inputs to Analog (A0) int lightLevel; // this is the level of light as read by the photoresistor // define the pins associated with the bar-graph LED using an array (arrays allow the storing of groups of variables, and can be // referred to by their index) int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //For this project, we are assigning integers based on their respective "percentage" of brightness const int pct_10 = 2; // pin 2 is connected to the first LED of the bargraph (10%) const int pct_20 = 3; // pin 3 is connected to the second LED of the bargraph (20%) const int pct_30 = 4; // pin 4 is connected to the third LED of the bargraph (30%) const int pct_40 = 5; // pin 5 is connected to the fourth LED of the bargraph (40%) const int pct_50 = 6; // pin 6 is connected to the fifth LED of the bargraph (50%) const int pct_60 = 7; // pin 7 is connected to the sixth LED of the bargraph (60%) const int pct_70 = 8; // pin 8 is connected to the seventh LED of the bargraph (70%) const int pct_80 = 9; // pin 9 is connected to the eighth LED of the bargraph (80%) const int pct_90 = 10; // pin 10 is connected to the ninth LED of the bargraph (90%) const int pct_100 = 11; // pin 10 is connected to the tenth LED of the bargraph (100%) void setup() { // configure the pins that will connect to the bar graph. Because these are defined above as // constant variables, we can refer to them by their color rather than the pin number. pinMode(pct_10, OUTPUT); // pin 2 is connected to the first LED of the bargraph (10%) pinMode(pct_20, OUTPUT); // pin 3 is connected to the first LED of the bargraph (20%) pinMode(pct_30, OUTPUT); // pin 4 is connected to the first LED of the bargraph (30%) pinMode(pct_40, OUTPUT); // pin 5 is connected to the first LED of the bargraph (40%) pinMode(pct_50, OUTPUT); // pin 6 is connected to the first LED of the bargraph (50%) pinMode(pct_60, OUTPUT); // pin 7 is connected to the first LED of the bargraph (60%) pinMode(pct_70, OUTPUT); // pin 8 is connected to the first LED of the bargraph (70%) pinMode(pct_80, OUTPUT); // pin 9 is connected to the first LED of the bargraph (80%) pinMode(pct_90, OUTPUT); // pin 10 is connected to the first LED of the bargraph (90%) pinMode(pct_100, OUTPUT); // pin 11 is connected to the first LED of the bargraph (100%) } void loop() { /* PART TWO: IDENTIFYING THE DESIRED RESPONSE BASED ON THE LEVEL OF BRIGHTNESS*/ // First, determine that the pin is measuring light level lightLevel = analogRead(sensorPin); // this is used to measure the voltage emitted by the photoresistor //next, dictate a series of arguments that tell the LED how to respond based on the read of the lightLevel // because there are ten desired categories (to represent percentages in groups of 10), an "if/else" statement is used. if (lightLevel >= 1000) // in low brightness, one LED (10%) will light up // This process is repeated for the remaining percentages (10% ranges) up to // the highest brightness (100%). Due to the determined range of analog inputs // (low = 800, high = 300), each new light represents a change up to 100 units in the // analog input (A0) {// digitalWrite(pct_10, HIGH); // display only 10% of the bar graph digitalWrite(pct_20, LOW); // turn LED OFF digitalWrite(pct_30, LOW); // turn LED OFF digitalWrite(pct_40, LOW); // turn LED OFF digitalWrite(pct_50, LOW); // turn LED OFF digitalWrite(pct_60, LOW); // turn LED OFF digitalWrite(pct_70, LOW); // turn LED OFF digitalWrite(pct_80, LOW); // turn LED OFF digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100,LOW); } // turn LED OFF else if (lightLevel >= 900) { digitalWrite(pct_10, HIGH); // display 20% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, LOW); // turn LED OFF digitalWrite(pct_40, LOW); // turn LED OFF digitalWrite(pct_50, LOW); // turn LED OFF digitalWrite(pct_60, LOW); // turn LED OFF digitalWrite(pct_70, LOW); // turn LED OFF digitalWrite(pct_80, LOW); // turn LED OFF digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100, LOW); } // turn LED OFF else if (lightLevel >= 800) { digitalWrite(pct_10, HIGH); // display 30% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, LOW); // turn LED OFF digitalWrite(pct_50, LOW); // turn LED OFF digitalWrite(pct_60, LOW); // turn LED OFF digitalWrite(pct_70, LOW); // turn LED OFF digitalWrite(pct_80, LOW); // turn LED OFF digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100,LOW); } // turn LED OFF else if (lightLevel >= 700) { digitalWrite(pct_10, HIGH); // display 40% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, HIGH); // turn LED ON digitalWrite(pct_50, LOW); // turn LED OFF digitalWrite(pct_60, LOW); // turn LED OFF digitalWrite(pct_70, LOW); // turn LED OFF digitalWrite(pct_80, LOW); // turn LED OFF digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100,LOW); } // turn LED OFF else if (lightLevel >= 600) { digitalWrite(pct_10, HIGH); // display 50% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, HIGH); // turn LED ON digitalWrite(pct_50, HIGH); // turn LED ON digitalWrite(pct_60, LOW); // turn LED OFF digitalWrite(pct_70, LOW); // turn LED OFF digitalWrite(pct_80, LOW); // turn LED OFF digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100,LOW); } // turn LED OFF else if (lightLevel >= 500) { digitalWrite(pct_10, HIGH); // display 60% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, HIGH); // turn LED ON digitalWrite(pct_50, HIGH); // turn LED ON digitalWrite(pct_60, HIGH); // turn LED ON digitalWrite(pct_70, LOW); // turn LED OFF digitalWrite(pct_80, LOW); // turn LED OFF digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100, LOW); } // turn LED OFF else if (lightLevel >= 400) { digitalWrite(pct_10, HIGH); // display 70% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, HIGH); // turn LED ON digitalWrite(pct_50, HIGH); // turn LED ON digitalWrite(pct_60, HIGH); // turn LED ON digitalWrite(pct_70, HIGH); // turn LED ON digitalWrite(pct_80, LOW); // turn LED OFF digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100, LOW); } // turn LED OFF else if (lightLevel >= 300) { digitalWrite(pct_10, HIGH); // display 80% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, HIGH); // turn LED ON digitalWrite(pct_50, HIGH); // turn LED ON digitalWrite(pct_60, HIGH); // turn LED ON digitalWrite(pct_70, HIGH); // turn LED ON digitalWrite(pct_80, HIGH); // turn LED ON digitalWrite(pct_90, LOW); // turn LED OFF digitalWrite(pct_100, LOW); } // turn LED OFF else if (lightLevel >= 200) { digitalWrite(pct_10, HIGH); // display 90% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, HIGH); // turn LED ON digitalWrite(pct_50, HIGH); // turn LED ON digitalWrite(pct_60, HIGH); // turn LED ON digitalWrite(pct_70, HIGH); // turn LED ON digitalWrite(pct_80, HIGH); // turn LED ON digitalWrite(pct_90, HIGH); // turn LED ON digitalWrite(pct_100,LOW); } // turn LED OFF else { digitalWrite(pct_10, HIGH); // display 10% of the bar graph digitalWrite(pct_20, HIGH); // turn LED ON digitalWrite(pct_30, HIGH); // turn LED ON digitalWrite(pct_40, HIGH); // turn LED ON digitalWrite(pct_50, HIGH); // turn LED ON digitalWrite(pct_60, HIGH); // turn LED ON digitalWrite(pct_70, HIGH); // turn LED ON digitalWrite(pct_80, HIGH); // turn LED ON digitalWrite(pct_90, HIGH); // turn LED ON digitalWrite(pct_100,HIGH); } // turn LED OFF } /* That's it! This code is easily adaptable for other sensors, and you can also adjust the pattern of lights turning on/off * based on your desired output of light. For the final outcome of this project, we will be assessing a percentage of space filled, * so 10% ranges will be an appropriate visualization. */ |
Thank you for visiting, using, and sharing this code!
*Note: This tutorial serves to provide an introduction and boost familiarity with the equipment and coding framework that can be adapted for other sensors using the bar graph LED. For the purposes the final project required for CPLN571, this project will ultimately be adapted to respond to an ultrasonic sensor (SEN-13959 ROHS).