There are various practical needs to output the value of a potentiometer on a digit 7-segment display without making it super complicated. Things such as an audio player require a display of the audio control knob by using some sort of logic with this kind of project.
In our earlier articles, we have discussed about PWM and control of a line of LED with potentiometer. Both of those articles are important since this guide is dependent on them. It is also possible to combine both if we use TM1637 4 digit 7 segment display module.
By Using a Single 7-Segment LED Display
If you want a one-digit display which shows zero to nine upon rotating the potentiometer knob like this project:
---
You can emulate this on TinkerCAD:
The sketch is super easy if you have understood the previous project with 12 LEDs and a potentiometer:
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 | int Value = 0; const byte PIN[7] = {2,3,4,5,6,7,8}; int salva[3] = {0,0,0}; 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 }; void setup(){ for (int i=0; i<7; i++) { pinMode (PIN[i], OUTPUT); } pinMode(12, OUTPUT); } void loop(){ Value = analogRead(A0); for (int i=0; i<8; i++) { digitalWrite(PIN[i] , LOW); } int scale = map(Value, 0, 1020, 0, 9); // map function to get brihtness for (int j = 0; j < 8; j++) { digitalWrite(j+2, bits[scale][j]); } delay(100); // Wait for 100 millisecond(s) } |
By Using TM1637 4 digit 7 Segment Display Module
This is easier with the wiring part. Connect CLK of TM1637 to pin 2 of Arduino, connect DIO of TM1637
to pin 3 of Arduino, connect Vcc to 5v, GND to GND, connect DIO of TM1637
to pin 3 of Arduino, connect the middle pin of the potentiometer to A0 of Arduino UNO.
This sketch will display zero to 1023 upon rotating the potentiometer knob:
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 | #include <Arduino.h> #include <TM1637Display.h> #define CLK 2 #define DIO 3 TM1637Display display(CLK, DIO); void setup() { } void loop() { display.setBrightness(0x0f); display.clear(); uint8_t data[] = { 0x00, 0x00, 0x00, 0x00 }; int value = analogRead(A0); // read of potentiometer value String texto = String(value); if (value > 999){ data[0] = display.encodeDigit(texto[0]); data[1] = display.encodeDigit(texto[1]); data[2] = display.encodeDigit(texto[2]); data[3] = display.encodeDigit(texto[3]); } else if (value > 99){ data[1] = display.encodeDigit(texto[0]); data[2] = display.encodeDigit(texto[1]); data[3] = display.encodeDigit(texto[2]); } else if (value > 9){ data[2] = display.encodeDigit(texto[0]); data[3] = display.encodeDigit(texto[1]); } else { data[3] = display.encodeDigit(texto[0]); } display.setSegments(data); delay(100); } |