/************************************************** Arduino Basics Unit 1 Lesson 14 4-Digit 7-Segment LED by J. Cantlin July 17, 2020 ***************************************************/ /************************************************** Description of Program 4-digit 7 segment LED display demo. 4 place value display shows digits 0000 - 99999 and decimal point. The program is written for a Common Anode 4-digit display. The pinout matches that shown in the osoyoo tutorial and was determined using a 3,3 vdc breadboard power supply and 220 ohm resistor: Because each Common Anode or Common Catode must use an Arduino digital pin, but the 7-segment display plus decimal point shares the 8 digital pins needed to light them, only an additional 4 digital pins are needed to drive the 4-digit display for a total of 12 pins used for the display. ref.: https://osoyoo.com/2017/08/08/arduino-lesson-4-digit-7-segment-led-display/ 220 ohm current limiting resistors were used for each LED segment. ref.: https://www.jameco.com/Jameco/workshop/techtip/working-with-seven-segment-displays.html The operation and wiring is identical to that for a 1-digit display but takes advantage of the fact the Arduino can operate in the microsecond range which is an order of magnitude faster than the 10's or 100's of milliseconds the human eye needs to register a response. So instead of the Common Anode or Common Cathode of each display being drived by a static 5 vdc or Ground connection, each display's Common Anode or Common Cathode is tied to an Arduino digital pin and the desired segments of each display are turned on so fast that the human eye doesn't have a chance to notice that each digit is actually off when the next one is lit before the cycle repeats. TV uses a similar concept. So does subliminal advertising which was made illegal in the early days of TV and probably is used on the internet without a viewer's knowledge. I wonder if the Arduino could be used to make a "subliminal ad detector"? That might make an interesting project. ***************************************************/ /************************************************** Convention Used For the 8 LED Segment Names and 4-Digits Capital letters are for Top and Middle LED segments. Always attempt to use meaningful variable names. Always! Four LED Digits Left to Right D3, D2, D1, D0 Place Value: D3 = 10^3 = 1000, D2 = 10^2 = 100 D1 = 10^1 = 10, D0 = 10^0 = 1 T = Top segment R = Right top segment r = right bottom segment b = bottom segment f = left bottom segment F = leFt top segment M = Middle segment d = decimal point ***************************************************/ /************************************************** Summary of Arduino Uno Analog Pins Used: A0 = A1 = A2 = A3 = A4 = A5 = ***************************************************/ /************************************************** Summary of Arduino Uno Digital Pins Used: 00 = 01 = 02 = d = decimal point 03 = M = Middle segment 04 = F = leFt top segment 05 = f = left bottom segment 06 = b = bottom segment 07 = r = right bottom segment 08 = R = Right top segment 09 = T = Top segment 10 = D3 = 1000's place Common Anode = High to Light (LED Pin 12) 11 = D2 = 100's place Common Anode = High to Light (LED Pin 9) 12 = D1 = 10's place Common Anode = High to Light (LED Pin 8) 13 = D0 = 1's place Common Anode = High to Light (LED Pin 6) ***************************************************/ /************************************************** Summary of 7-Segment LED Pins Used (as shown on osoyoo.com): Left to Right Facing LED display, Pins 1 to 6 at bottom, 12-7 on top --------------------------bottom LED pins--------------------- 1 = Arduino digital pin 05 = f = left bottom segment 2 = Arduino digital pin 06 = b = bottom segment 3 = Arduino digital pin 02 = d = decimal point 4 = Arduino digital pin 07 = r = right bottom segment 5 = Arduino digital pin 03 = M = Middle segment 6 = Arduino digital pin 13 = D0 = 1's place Common Anode = High to Light --------------------------top LED pins--------------------- 12 = Arduino digital pin 10 = D3 = 1000's place Common Anode = High to Light 11 = Arduino digital pin 09 = T = Top segment 10 = Arduino digital pin 04 = F = leFt top segment 9 = Arduino digital pin 11 = D2 = 100's place Common Anode = High to Light 8 = Arduino digital pin 12 = D1 = 10's place Common Anode = High to Light 7 = Arduino digital pin 08 = R = Right top segment ***************************************************/ // Define Arduino Pin to LED Segment Connection int segPins[] = {9, 8, 7, 6, 5, 4, 3, 2 }; // { T R r b f F M d ) int digitPwrPins[] = {10, 11, 12, 13}; // {D3, D2, D1, D0} // Create truth table for lighting the segments to make 0-9 and decimal point // this truth table is for a Common Anode display - the Arduino pins are driven Low = 0 // to light an LED segment. They are normally Low so at the beginning they all should // be driven HIGH to turn all the LED segments off. This is not needed for Common Cathode displays. byte segCode[11][8] = { // T R r b f F M d LED segments // 9 8 7 6 5 4 3 2 Arduino pins // drive pins Low = 0 to light Common Anode display LED segments { 0, 0, 0, 0, 0, 0, 1, 1}, // 0 = all segments except Middle and decimal point { 1, 0, 0, 1, 1, 1, 1, 1}, // 1 = both right segments { 0, 0, 1, 0, 0, 1, 0, 1}, // 2 = Top + Right top + Middle + left bottom + bottom { 0, 0, 0, 0, 1, 1, 0, 1}, // 3 = T + R + r + M + b { 1, 0, 0, 1, 1, 0, 0, 1}, // 4 = R + F + M + r { 0, 1, 0, 0, 1, 0, 0, 1}, // 5 = T + F + M + r + b { 0, 1, 0, 0, 0, 0, 0, 1}, // 6 = all segments except Top right and decimal point { 0, 0, 0, 1, 1, 1, 1, 1}, // 7 = T + R + r { 0, 0, 0, 0, 0, 0, 0, 1}, // 8 = all sements except decimal point { 0, 0, 0, 0, 1, 0, 0, 1}, // 9 = all segments except bottom left and decimal point { 1, 1, 1, 1, 1, 1, 1, 0} // . = d }; //****************************************************************************** void setup() { for (int i=0; i < 8; i++) { pinMode(segPins[i], OUTPUT); digitalWrite(segPins[i], HIGH); // for Common Anode, drive all segPins High = 1 to turn off all LED segments } } //****************************************************************************** void loop() {//start infinite loop // display 0-9 and decimal for each digit sequentially for (int p=0; p < 4; p++) { digitalWrite(digitPwrPins[p], HIGH); // set power pin (anode) for digit to High to enable all segment LEDs for that digit pinMode(digitPwrPins[p], OUTPUT); for (int n = 0; n < 11; n++) // display digits 0 - 9 and decimal point { displayDigit(n); delay(500); } digitalWrite(digitPwrPins[p], LOW); // set power pin (anode) for digit to LOW to disable all segment LEDs for that digit delay(1000); } // display 4321 for (int r = 0; r < 5000; r++) {//start 4321 loop digitalWrite(digitPwrPins[0], HIGH); displayDigit(4); delay(5); digitalWrite(digitPwrPins[0], LOW); digitalWrite(digitPwrPins[1], HIGH); displayDigit(3); delay(5); digitalWrite(digitPwrPins[1], LOW); digitalWrite(digitPwrPins[2], HIGH); displayDigit(2); delay(5); digitalWrite(digitPwrPins[2], LOW); digitalWrite(digitPwrPins[3], HIGH); displayDigit(1); delay(5); digitalWrite(digitPwrPins[3], LOW); }//endof 4321 loop }//endof infinite loop //**********display function********** void displayDigit(int digit) { for (int i=0; i < 8; i++) { digitalWrite(segPins[i], segCode[digit][i]); } }