If every reading that an Arduino does from an analog pin is between the values of 0 and 1024, how can this value be represented using a seven-segment display? There are always creative ways but the goal is to join different ways to represent the reading of a potentiometer as I presented to you using 8 LEDs and then with a matrix of LEDs.
DiagramBefore making any connection we must recognize the type of 7-segment display that we are using, we verify the corresponding datasheet:
My component has the common cathode, so for this I use the two resistors and the rest of the connections as shown below:
One way to check the connections is by using one of the examples included in the Arduino IDE: barGraph.
/*
LED bar graph
Turns on a series of LEDs based on the value of an analog sensor.
This is a simple way to make a bar graph display. Though this graph uses 10
LEDs, you can use any number by changing the LED count and the pins in the
array.
This method can be used to control any series of digital outputs that depends
on an analog input.
The circuit:
- LEDs from pins 2 through 11 to ground
created 4 Sep 2010
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/BarGraph
*/
// these constants won't change:
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
Inside the bonesJust using a single character there are few ways that representations of a numerical scale can be generated, so I use the Arduino map function again to obtain the interpolated result:
int scale = map(Value, 0, 1020, 0, 8);
In this way it is enough to represent a numerical value between 0 and 8 to indicate the level of rotation in the potentiometer, so it remains simply a graphic representation.
Code detailsAt the beginning we use a variable to store the reading, a vector to register the connection pins and an array to generate the numeric characters.
int Value = 0;
const byte PIN[7] = {2,3,4,5,6,7,8};
int bits[10][7] = {{ 1, 1, 1, 1, 1, 1, 0 }, // 0
{ 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 1, 1, 0, 1, 1, 0, 1 }, // 2
{ 1, 1, 1, 1, 0, 0, 1 }, // 3
{ 0, 1, 1, 0, 0, 1, 1 }, // 4
{ 1, 0, 1, 1, 0, 1, 1 }, // 5
{ 1, 0, 1, 1, 1, 1, 1 }, // 6
{ 1, 1, 1, 0, 0, 0, 0 }, // 7
{ 1, 1, 1, 1, 1, 1, 1 }, // 8
{ 1, 1, 1, 1, 0, 1, 1 } // 9
};
The code in the setup is very simple, you only have to indicate the digital output pins and it seems to me that using the FOR statement saves a lot of work.
void setup(){
for (int i=0; i<7; i++) {
pinMode (PIN[i], OUTPUT);
}
}
Inside the Loop we start the analog reading, then we set the LEDs to off, the scale value is calculated to identify the character that will be displayed on the 7-segment display using the start matrix.
void loop(){
Value = analogRead(A0);
for (int i=0; i<7; i++) {
digitalWrite(PIN[i] , LOW);
}
int scale = map(Value, 0, 1020, 0, 8); // map function to get brihtness
for (int j = 0; j < 7; j++) {
digitalWrite(j+2, bits[scale][j]);
}
delay(100); // Wait for 100 millisecond(s)
}
Even before making any connection, you can run a simulation from ThinkerCAD Sim.
ConclusionThe 7-segment displays are still in use today and are easy to find, sure it takes a while to get used to the common connection but it's worth a try. I hope that this is another interesting information for you and that you take advantage and comment on your attempts and modifications.
Comments