Home / Books / Electronics Engineering / Arduino / 4-Digit 7 Segment Display Arduino Code

4-Digit 7 Segment Display Arduino Code


Warning: Use of undefined constant EASYCODE_DIR - assumed 'EASYCODE_DIR' (this will throw an Error in a future version of PHP) in /home/eebooksh/public_html/wp-content/plugins/easycode/class.php on line 349

Warning: Use of undefined constant border - assumed 'border' (this will throw an Error in a future version of PHP) in /home/eebooksh/public_html/wp-content/plugins/easycode/include-extra-file.php on line 211

Warning: Use of undefined constant width - assumed 'width' (this will throw an Error in a future version of PHP) in /home/eebooksh/public_html/wp-content/plugins/easycode/include-extra-file.php on line 211

Warning: A non-numeric value encountered in /home/eebooksh/public_html/wp-content/plugins/easycode/include-extra-file.php on line 211

Warning: A non-numeric value encountered in /home/eebooksh/public_html/wp-content/plugins/easycode/include-extra-file.php on line 211

4-Digit 7 Segment Display Arduino Code

4 digit 7 segment display arduino

Today We’ll learn how to create a countdown timer using a 4 Digit 7-Segment Display that can be reset with a push button. In this tutorial we listed the required materials, the wiring diagram, and source code that was used to program the 4 Digit 7-Segment Display.

To follow this tutorial you will require the following materials:

 

  • 4 Digit 7-Segment Display (In our case 5641AS)
  • Connecting Wires
  • Connecting wires are used to connect the 4 Digit 7-Segment Display to the Arduino and to connect the Push Button to the Arduino
  • 1 wire for the option to include decimal points on your 4 Digit 7-Segment Display
  • Arduino Uno and its USB Cable
  • Push Button

 

In order to wire 4 Digit 7-Segment Display we first need to identify pins of the 4 Digit 7-Segment Display and their function.

4 digit 7 segment display
4 digit 7 segment display

After identifying the pins of the 4 Digit 7-Segment Display, we can summarize the function of each pin and their function in the form of a table. First column shows the pin number on the 4 Digit 7-Segment Display the second column shows the light variable and the third column shows the pin number on the Arduino to which respective pin is going to connect. place the 4 Digit 7-Segement and the Push Button on breadboard and wire the 4 different digit pins to the first four pins to the Arduino as shown by the chart.

7 segment Pin Number 7 segment Light Variable Arduino Pin Number
Pin 11 A 12
Pin 7 B 10
Pin 4 C 8
Pin 2 D 7
Pin 1 E 6
Pin 10 F 11
Pin 5 G 9
Pin 9 Digit 1 1
Pin 8 Digit 2 2
Pin 6 Digit 3 3
Pin 12 Digit 4 4

 

Next we need to wire other pins of the 4 Digit 7-Segment Display to Arduino according to the chart.  Next, the remaining pins of the Arduino (13 and 5) will be connected to the Push Button. These wires will be on the same side as the push button.

Here is the source code for the 4 Digit 7-Segment Display.

Arduino Code
 
  1. /*
  2. 7 segement pin number - 7 segement light - arduino pin number
  3. pin 11 A = 12
  4. pin 7 B = 10
  5. pin 4 C = 8
  6. pin 2 D = 7
  7. pin 1 E = 6
  8. pin 10 F = 11
  9. pin 5 G = 9
  10. pin 12 first digit = 1
  11. pin 9 second digit = 2
  12. pin 8 third digit = 3
  13. pin 6 fourth digit = 4
  14. pin 3 is decimal point
  15. 12 11 10 9 8 7
  16. | | | | | |
  17. A
  18. ---
  19. F | | B
  20. | G |
  21. ---
  22. E | | C
  23. | |
  24. --- . dec
  25. D
  26. | | | | | |
  27. 1 2 3 4 5 6
  28. */
  29. const int a = 12;
  30. const int b = 10;
  31. const int c = 8;
  32. const int d = 7;
  33. const int e = 6;
  34. const int f = 11;
  35. const int g = 9;
  36. int p = 0;
  37. int startStopReset = 13;
  38. const int d1 = 1;
  39. const int d2 = 2;
  40. const int d3 = 3;
  41. const int d4 = 4;
  42. long n = 30000; //start time -> CAN CHANGE TO WHATEVER TIME YOU WANT
  43. int x = 100;
  44. int del = 55; //delay value
  45. void setup()
  46. {
  47. pinMode(d1, OUTPUT);
  48. pinMode(d2, OUTPUT);
  49. pinMode(d3, OUTPUT);
  50. pinMode(d4, OUTPUT);
  51. pinMode(a, OUTPUT);
  52. pinMode(b, OUTPUT);
  53. pinMode(c, OUTPUT);
  54. pinMode(d, OUTPUT);
  55. pinMode(e, OUTPUT);
  56. pinMode(f, OUTPUT);
  57. pinMode(g, OUTPUT);
  58. pinMode(p, OUTPUT);
  59. pinMode(startStopReset, INPUT);
  60. digitalWrite(startStopReset, HIGH);
  61. }
  62. void loop()
  63. {
  64. digitalWrite (p, HIGH);
  65. clearLEDs();
  66. pickDigit(1);
  67. pickNumber((n / x / 1000) % 10);
  68. delayMicroseconds(del);
  69. clearLEDs();
  70. pickDigit(2);
  71. pickNumber((n / x / 100) % 10);
  72. delayMicroseconds(del);
  73. clearLEDs();
  74. pickDigit(3);
  75. dispDec(3);
  76. pickNumber((n / x / 10) % 10);
  77. delayMicroseconds(del);
  78. clearLEDs();
  79. pickDigit(4);
  80. pickNumber(n / x % 10);
  81. delayMicroseconds(del);
  82. n--; //'n++' for stopwatch
  83. if (digitalRead(13) == LOW)
  84. {
  85. n = 30000; //re-start time -> CHANGE TO WHATEVER TIME YOU ORIGONALLY SET (start time)
  86. }
  87. }
  88. void pickDigit(int x) //changes digit
  89. {
  90. digitalWrite(d1, HIGH);
  91. digitalWrite(d2, HIGH);
  92. digitalWrite(d3, HIGH);
  93. digitalWrite(d4, HIGH);
  94. switch (x)
  95. {
  96. case 1:
  97. digitalWrite(d1, LOW);
  98. break;
  99. case 2:
  100. digitalWrite(d2, LOW);
  101. break;
  102. case 3:
  103. digitalWrite(d3, LOW);
  104. digitalWrite(p, HIGH); //new
  105. break;
  106. default:
  107. digitalWrite(d4, LOW);
  108. break;
  109. }
  110. }
  111. void pickNumber(int x) //changes value of number
  112. {
  113. switch (x)
  114. {
  115. default:
  116. zero();
  117. break;
  118. case 1:
  119. one();
  120. break;
  121. case 2:
  122. two();
  123. break;
  124. case 3:
  125. three();
  126. break;
  127. case 4:
  128. four();
  129. break;
  130. case 5:
  131. five();
  132. break;
  133. case 6:
  134. six();
  135. break;
  136. case 7:
  137. seven();
  138. break;
  139. case 8:
  140. eight();
  141. break;
  142. case 9:
  143. nine();
  144. break;
  145. }
  146. }
  147. void dispDec(int x)
  148. {
  149. digitalWrite(p, LOW);
  150. }
  151. void clearLEDs()
  152. {
  153. digitalWrite(a, LOW);
  154. digitalWrite(b, LOW);
  155. digitalWrite(c, LOW);
  156. digitalWrite(d, LOW);
  157. digitalWrite(e, LOW);
  158. digitalWrite(f, LOW);
  159. digitalWrite(g, LOW);
  160. digitalWrite(p, LOW);
  161. }
  162. void zero()
  163. {
  164. digitalWrite(a, HIGH);
  165. digitalWrite(b, HIGH);
  166. digitalWrite(c, HIGH);
  167. digitalWrite(d, HIGH);
  168. digitalWrite(e, HIGH);
  169. digitalWrite(f, HIGH);
  170. digitalWrite(g, LOW);
  171. }
  172. void one()
  173. {
  174. digitalWrite(a, LOW);
  175. digitalWrite(b, HIGH);
  176. digitalWrite(c, HIGH);
  177. digitalWrite(d, LOW);
  178. digitalWrite(e, LOW);
  179. digitalWrite(f, LOW);
  180. digitalWrite(g, LOW);
  181. }
  182. void two()
  183. {
  184. digitalWrite(a, HIGH);
  185. digitalWrite(b, HIGH);
  186. digitalWrite(c, LOW);
  187. digitalWrite(d, HIGH);
  188. digitalWrite(e, HIGH);
  189. digitalWrite(f, LOW);
  190. digitalWrite(g, HIGH);
  191. }
  192. void three()
  193. {
  194. digitalWrite(a, HIGH);
  195. digitalWrite(b, HIGH);
  196. digitalWrite(c, HIGH);
  197. digitalWrite(d, HIGH);
  198. digitalWrite(e, LOW);
  199. digitalWrite(f, LOW);
  200. digitalWrite(g, HIGH);
  201. }
  202. void four()
  203. {
  204. digitalWrite(a, LOW);
  205. digitalWrite(b, HIGH);
  206. digitalWrite(c, HIGH);
  207. digitalWrite(d, LOW);
  208. digitalWrite(e, LOW);
  209. digitalWrite(f, HIGH);
  210. digitalWrite(g, HIGH);
  211. }
  212. void five()
  213. {
  214. digitalWrite(a, HIGH);
  215. digitalWrite(b, LOW);
  216. digitalWrite(c, HIGH);
  217. digitalWrite(d, HIGH);
  218. digitalWrite(e, LOW);
  219. digitalWrite(f, HIGH);
  220. digitalWrite(g, HIGH);
  221. }
  222. void six()
  223. {
  224. digitalWrite(a, HIGH);
  225. digitalWrite(b, LOW);
  226. digitalWrite(c, HIGH);
  227. digitalWrite(d, HIGH);
  228. digitalWrite(e, HIGH);
  229. digitalWrite(f, HIGH);
  230. digitalWrite(g, HIGH);
  231. }
  232. void seven()
  233. {
  234. digitalWrite(a, HIGH);
  235. digitalWrite(b, HIGH);
  236. digitalWrite(c, HIGH);
  237. digitalWrite(d, LOW);
  238. digitalWrite(e, LOW);
  239. digitalWrite(f, LOW);
  240. digitalWrite(g, LOW);
  241. }
  242. void eight()
  243. {
  244. digitalWrite(a, HIGH);
  245. digitalWrite(b, HIGH);
  246. digitalWrite(c, HIGH);
  247. digitalWrite(d, HIGH);
  248. digitalWrite(e, HIGH);
  249. digitalWrite(f, HIGH);
  250. digitalWrite(g, HIGH);
  251. }
  252. void nine()
  253. {
  254. digitalWrite(a, HIGH);
  255. digitalWrite(b, HIGH);
  256. digitalWrite(c, HIGH);
  257. digitalWrite(d, HIGH);
  258. digitalWrite(e, LOW);
  259. digitalWrite(f, HIGH);
  260. digitalWrite(g, HIGH);
  261. }

This code shows the different pins their function and how they should be wired to the Arduino. It also shows the lines of the code which you can change and customize for example the starting time of the count down and the time at which the timer is reset after the reset button is pressed.

Note: time is mentioned in milliseconds. In order to achieve 1 second, you need to mention 1000 in the code.

1 second = 1000 ms

If you followed the guidelines the timer should work as charm. When the reset button is pressed, all four digits will display digit 0.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

"