This Arduino project shows how to read analog voltages from analog channel and print the digital values on 7-segment display. A potentiometer is used to get a voltage that varies between 0 and 5V.
A common anode 7-segment display is used in this example.
Last time I made a simple interfacing of Arduino UNO with 7-segment display, project link is below:
Interfacing Arduino with 7-segment display | 4-Digit counter example
Hardware Required:
- Arduino UNO board
- 4-Digit common anode 7-segment display
- 4 x PNP transistor (2SA1015, 2S9015, 2N3906 …)
- 10k ohm potentiometer
- 7 x 100 ohm resistor
- 4 x 4.7k ohm resistor
- Breadboard
- Jumper wires
Print Arduino ADC values on 7-segment display circuit:
The following image shows circuit diagram of the example.
The four transistors used in this example are of the same type (PNP).
The potentiometer has 3 pins VCC , out and GND where:
VCC is connected to Arduino 5V pin
out is connected to Arduino analog channel 0 (A0)
GND is connected to Arduino GND pin
Print Arduino ADC values on 7-segment display code:
The following Arduino code does not use any library for the 7-segment display.
The output pin of the potentiometer is connected to Arduino analog channel 0, it is defined in the code with the following line:
1 | #define POT A0 |
The ATmega328P (Arduino uno microcontroller) has one ADC module with 10-bit resolution which means it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.
So reading from analog channel will give us a digital value between 0 and 1023.
The following line is used in the code for reading analog data (voltage) from POT channel (previously defined as channel 0) and storing it in an integer variable named ‘adc_value’:
1 | adc_value = analogRead(POT); |
Full Arduino 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 196 197 198 199 200 201 202 | /* * Interfacing Arduino with common anode 7-segment display. * Print ADC values on 4-digit 7-segment display. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ */ // potentiometer output pin definition #define POT A0 // segment pin definitions #define SegA 12 #define SegB 11 #define SegC 10 #define SegD 9 #define SegE 8 #define SegF 7 #define SegG 6 // common pins of the four digits definitions #define Dig1 5 #define Dig2 4 #define Dig3 3 #define Dig4 2 // variable declarations byte current_digit; int adc_value; void setup() { pinMode(SegA, OUTPUT); pinMode(SegB, OUTPUT); pinMode(SegC, OUTPUT); pinMode(SegD, OUTPUT); pinMode(SegE, OUTPUT); pinMode(SegF, OUTPUT); pinMode(SegG, OUTPUT); pinMode(Dig1, OUTPUT); pinMode(Dig2, OUTPUT); pinMode(Dig3, OUTPUT); pinMode(Dig4, OUTPUT); disp_off(); // turn off the display // Timer1 module overflow interrupt configuration TCCR1A = 0; TCCR1B = 1; // enable Timer1 with prescaler = 1 ( 16 ticks each 1 µs) TCNT1 = 0; // set Timer1 preload value to 0 (reset) TIMSK1 = 1; // enable Timer1 overflow interrupt } ISR(TIMER1_OVF_vect) // Timer1 interrupt service routine (ISR) { disp_off(); // turn off the display switch (current_digit) { case 1: disp(adc_value / 1000); // prepare to display digit 1 (most left) digitalWrite(Dig1, LOW); // turn on digit 1 break; case 2: disp( (adc_value / 100) % 10); // prepare to display digit 2 digitalWrite(Dig2, LOW); // turn on digit 2 break; case 3: disp( (adc_value / 10) % 10); // prepare to display digit 3 digitalWrite(Dig3, LOW); // turn on digit 3 break; case 4: disp(adc_value % 10); // prepare to display digit 4 (most right) digitalWrite(Dig4, LOW); // turn on digit 4 } current_digit = (current_digit % 4) + 1; } // main loop void loop() { adc_value = analogRead(POT); delay(100); } void disp(byte number) { switch (number) { case 0: // print 0 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, LOW); digitalWrite(SegG, HIGH); break; case 1: // print 1 digitalWrite(SegA, HIGH); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, HIGH); break; case 2: // print 2 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, HIGH); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, HIGH); digitalWrite(SegG, LOW); break; case 3: // print 3 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, LOW); break; case 4: // print 4 digitalWrite(SegA, HIGH); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 5: // print 5 digitalWrite(SegA, LOW); digitalWrite(SegB, HIGH); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, HIGH); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 6: // print 6 digitalWrite(SegA, LOW); digitalWrite(SegB, HIGH); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 7: // print 7 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, HIGH); break; case 8: // print 8 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 9: // print 9 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, HIGH); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); } } void disp_off() { digitalWrite(Dig1, HIGH); digitalWrite(Dig2, HIGH); digitalWrite(Dig3, HIGH); digitalWrite(Dig4, HIGH); } // end of code. |
The following video shows a simple hardware circuit of this project:
and the one below shows Proteus simulation (simulation circuit is not the same as real hardware circuit, example circuit diagram is shown above):
Proteus simulation file download:
Arduino ADC 7-segment display
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Can it be translated to another range/scale of value?
For example, I’d like to have a range/scale between 0.0 up to 2.0 min to max (or 0.000 to 2.000), meanwhile range of this project is 0000 up to 1023. What code to be changed?
Thank you
Arduino UNO board and 6 or 4-Digit common anode 7-segment display using clock .
circuit and program available
No, right now there is no clock project with 7-segment display.