Saturday, March 9, 2019

4 Digits 7 Segments Counter 0000-9999 with Arduino UNO, Short Pro-programming function


Today we will program a 4 digits 7 segments as a counter from 0000 to 9999 by increasing every second. I will show you step by step to be clear.

STEP #1: Get to know 4 digits 7-segments

This 7-segments has 4 digits of the number, each could count from 0 to 9. The segments A-G are connected together for all of 4 digits. D1, D2, D3, and D4 are enable-pin for each digit. 

 STEP #2: Setup the circuit with the controller

Place the 7-segments on the board and connect pin one by one to the Arduino with the jump wire (You can use 220 Ohm resistors between Arduino's pins and 7-segment to protect the LEDs).

Now you need to connect correctly of each pin to run with the program:
    2  -  A
    3  -  B
    4  -  C
    5  -  C
    6  -  D
    7  -  E
    8  -  G
    9  -  D1
    10 -  D2
    11 -  D3
    12 -  D4

Recheck your connection and plug the Arduino USB to power up the controller. 

STEP #3: Program the Arduino

I wrote this program base on loop to run the display. It may a bit difficult to understand, but you should try to understand it line by line to improve your program skill. This makes you a shortcode and fasts run on the controller. Here we go:

  1. /* 
  2.   Showing number 0-9 on a Common Anode 7-segment LED display 
  3.   Displays the numbers 0-9 on the display, with one second inbetween. 
  4.     A 
  5.    --- 
  6. F |   | B 
  7.   | G | 
  8.    --- 
  9. E |   | C 
  10.   |   | 
  11.    --- 
  12.     D 
  13.   This example code is in the public domain. 
  14.   Pin 2-8 is connected to the 7 segments of the display. 
  15.   2  -  A 
  16.   3  -  B 
  17.   4  -  C 
  18.   5  -  C 
  19.   6  -  D 
  20.   7  -  E 
  21.   8  -  G 
  22.   09 -  D1 
  23.   10 -  D2 
  24.   11 -  D3 
  25.   12 -  D4 
  26.  */  
  27.   
  28.  #define D1 9  
  29.  #define D2 10  
  30.  #define D3 11  
  31.  #define D4 12  
  32.    
  33. int digit[10] = {0b0111111, 0b0000110, 0b1011011, 0b1001111, 0b1100110, 0b1101101, 0b1111101, 0b0000111, 0b11111111, 0b1101111};  
  34. //int digit[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10};  
  35. int digit1, digit2, digit3, digit4;  
  36. int DelayCount = 100; // Delay Count in (mili-second/4)  
  37. void setup()   
  38. {  
  39.   for (int i = 2; i < 9; i++) {  
  40.     pinMode(i, OUTPUT);  
  41.   }  
  42.   pinMode(D1, OUTPUT);  
  43.   pinMode(D2, OUTPUT);  
  44.   pinMode(D3, OUTPUT);  
  45.   pinMode(D4, OUTPUT);  
  46.     
  47. }  
  48. void loop() {  
  49.   for (int j = 0; j <= 9999; j++) {  
  50.   digit4 = j / 1000;  
  51.   digit3 = (j%1000) / 100;  
  52.   digit2 = (j%100) / 10;  
  53.   digit1 = j % 10;  
  54.   int Digit[4]={digit1, digit2, digit3, digit4};  
  55.   for ( int k = 0; k <= DelayCount; k++){  
  56.      for (int s = 13; s > 9; s--){  
  57.         /* Display number only digit"s" */  
  58.         digitalWrite(D1, HIGH);  
  59.         digitalWrite(D2, HIGH);  
  60.         digitalWrite(D3, HIGH);  
  61.         digitalWrite(D4, HIGH);  
  62.         digitalWrite(s, LOW);  
  63.         dis(Digit[s-10]);  
  64.         delay(1);  
  65.      }  
  66.     }      
  67.   }  
  68. }  
  69.   
  70. /* Declaration: count the number for 0-9 */  
  71. void dis(int num) {  
  72.   for (int i = 2; i < 9; i++) {  
  73.     digitalWrite(i, bitRead(digit[num], i - 2));  
  74.   }  
  75. }  

STEP #4: YEAH Let see

If there no display or you have problems with the number display, please check your connection circuit. 
*Important: Check the type of your 7-segment to see it is Anode or Cathode.
*Try: if the LEDs display with random not a number, you can COMMENT the code line #33 and UNCOMMENT the conde line #34 and upload the code again to see if it is right back.

STEP #5: Please give me feedback or Suggestion

I share this from my experiece with this project. Please comment below to let me know it works or not with you. You can suggest my to do any project or other ways faster.

Thanks.