0% found this document useful (0 votes)
137 views

Arduino Lesson 16-LED 4 Digit. 8-Segment Displays

This Arduino code controls a 4-digit, 7-segment display. It uses pinMode and digitalWrite functions to activate the correct cathode pins for each digit and number on the display. The code increments a number variable over time and uses modulo operations to extract each digit for display on the appropriate segment. Resistors are needed to limit current to each segment. The code continuously cycles through the digits to display the increasing number on the 4-digit display.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Arduino Lesson 16-LED 4 Digit. 8-Segment Displays

This Arduino code controls a 4-digit, 7-segment display. It uses pinMode and digitalWrite functions to activate the correct cathode pins for each digit and number on the display. The code increments a number variable over time and uses modulo operations to extract each digit for display on the appropriate segment. Resistors are needed to limit current to each segment. The code continuously cycles through the digits to display the increasing number on the 4-digit display.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Arduino Lesson 16

LED 4 Digit. 7-Segment Display

Intro
This experiments we use the arduino to drive a four digital common anode
display. Current limit resistor is a must have, there are two types of current
limiting resistor connection, one is in d1 - d4 anode, a total of four. This type
requires less resistors, but will show different brightness numbers on each
position. #1 is brightest, #8 is the most dark. The other type is connecting with
the other 8 pins. This type gives average brightness, but need more resistors.
Here we use 8pcs 220 resistors.

4-digit display has 12 pins. When the dot down and stay put, left bottom one
is #1, the others are in counterclockwise order,top left corner one is #12.

Below picture is the instruction.

Connect-it-up

ARDUINO CODE
1. //set the cathode interface
2. int a = 1;
3. int b = 2;
4. int c = 3;
5. int d = 4;
6. int e = 5;
7. int f = 6;
8. int g = 7;
9. int p = 8;
10.
// set the anode interface
11.
int d4 = 9;
12.
int d3 = 10;
13.
int d2 = 11;
14.
int d1 = 12;
15.
//set variable
16.
long n = 0;
17.
int x = 100;
18.
int del = 55; //fine-tuning value for clock
19.
void setup()
20.
{
21.
pinMode(d1, OUTPUT);
22.
pinMode(d2, OUTPUT);
23.
pinMode(d3, OUTPUT);
24.
pinMode(d4, OUTPUT);
25.
pinMode(a, OUTPUT);
26.
pinMode(b, OUTPUT);
27.
pinMode(c, OUTPUT);
28.
pinMode(d, OUTPUT);
29.
pinMode(e, OUTPUT);
30.
pinMode(f, OUTPUT);
31.
pinMode(g, OUTPUT);
32.
pinMode(p, OUTPUT);
33.
}
34.
35.
void loop()
36.
{
37.
clearLEDs();
38.
pickDigit(1);
39.
pickNumber((n/x/1000)%10);
40.
delayMicroseconds(del);
41.
42.
clearLEDs();

43.
pickDigit(2);
44.
pickNumber((n/x/100)%10);
45.
delayMicroseconds(del);
46.
47.
clearLEDs();
48.
pickDigit(3);
49.
dispDec(3);
50.
pickNumber((n/x/10)%10);
51.
delayMicroseconds(del);
52.
53.
clearLEDs();
54.
pickDigit(4);
55.
pickNumber(n/x%10);
56.
delayMicroseconds(del);
57.
58.
n++;
59.
60.
if (digitalRead(13) == HIGH)
61.
{
62.
n = 0;
63.
}
64.
}
65.
66.
void pickDigit(int x) //define
pickDigit(x),to open dx port
67.
{
68.
digitalWrite(d1, LOW);
69.
digitalWrite(d2, LOW);
70.
digitalWrite(d3, LOW);
71.
digitalWrite(d4, LOW);
72.
73.
switch(x)
74.
{
75.
case 1:
76.
digitalWrite(d1, HIGH);
77.
break;
78.
case 2:
79.
digitalWrite(d2, HIGH);
80.
break;
81.
case 3:
82.
digitalWrite(d3, HIGH);
83.
break;
84.
default:
85.
digitalWrite(d4, HIGH);

86.
break;
87.
}
88.
}
89.
90.
void pickNumber(int x)
//define
pickNumber(x)to display number x
91.
{
92.
switch(x)
93.
{
94.
default:
95.
zero();
96.
break;
97.
case 1:
98.
one();
99.
break;
100.
case 2:
101.
two();
102.
break;
103.
case 3:
104.
three();
105.
break;
106.
case 4:
107.
four();
108.
break;
109.
case 5:
110.
five();
111.
break;
112.
case 6:
113.
six();
114.
break;
115.
case 7:
116.
seven();
117.
break;
118.
case 8:
119.
eight();
120.
break;
121.
case 9:
122.
nine();
123.
break;
124.
}
125.
}
126.
127.
void dispDec(int x) //set to start the
decimal point

128.
{
129.
digitalWrite(p, LOW);
130.
}
131.
132.
void clearLEDs() //clear contents
133.
{
134.
digitalWrite(a, HIGH);
135.
digitalWrite(b, HIGH);
136.
digitalWrite(c, HIGH);
137.
digitalWrite(d, HIGH);
138.
digitalWrite(e, HIGH);
139.
digitalWrite(f, HIGH);
140.
digitalWrite(g, HIGH);
141.
digitalWrite(p, HIGH);
142.
}
143.
144.
void zero() //define 0 as cathode pin
switch
145.
{
146.
digitalWrite(a, LOW);
147.
digitalWrite(b, LOW);
148.
digitalWrite(c, LOW);
149.
digitalWrite(d, LOW);
150.
digitalWrite(e, LOW);
151.
digitalWrite(f, LOW);
152.
digitalWrite(g, HIGH);
153.
}
154.
155.
void one() // define 1 as cathode pin
switch
156.
{
157.
digitalWrite(a, HIGH);
158.
digitalWrite(b, LOW);
159.
digitalWrite(c, LOW);
160.
digitalWrite(d, HIGH);
161.
digitalWrite(e, HIGH);
162.
digitalWrite(f, HIGH);
163.
digitalWrite(g, HIGH);
164.
}
165.
166.
void two() // define 2 as cathode pin
switch
167.
{
168.
digitalWrite(a, LOW);

169.
digitalWrite(b, LOW);
170.
digitalWrite(c, HIGH);
171.
digitalWrite(d, LOW);
172.
digitalWrite(e, LOW);
173.
digitalWrite(f, HIGH);
174.
digitalWrite(g, LOW);
175.
}
176.
177.
void three() // define 3 as cathode pin
switch
178.
{
179.
digitalWrite(a, LOW);
180.
digitalWrite(b, LOW);
181.
digitalWrite(c, LOW);
182.
digitalWrite(d, LOW);
183.
digitalWrite(e, HIGH);
184.
digitalWrite(f, HIGH);
185.
digitalWrite(g, LOW);
186.
}
187.
188.
void four() // define 4 as cathode pin
switch
189.
{
190.
digitalWrite(a, HIGH);
191.
digitalWrite(b, LOW);
192.
digitalWrite(c, LOW);
193.
digitalWrite(d, HIGH);
194.
digitalWrite(e, HIGH);
195.
digitalWrite(f, LOW);
196.
digitalWrite(g, LOW);
197.
}
198.
199.
void five() // define 5 as cathode pin
switch
200.
{
201.
digitalWrite(a, LOW);
202.
digitalWrite(b, HIGH);
203.
digitalWrite(c, LOW);
204.
digitalWrite(d, LOW);
205.
digitalWrite(e, HIGH);
206.
digitalWrite(f, LOW);
207.
digitalWrite(g, LOW);
208.
}
209.

210.
void six() // define 6 as cathode pin
switch
211.
{
212.
digitalWrite(a, LOW);
213.
digitalWrite(b, HIGH);
214.
digitalWrite(c, LOW);
215.
digitalWrite(d, LOW);
216.
digitalWrite(e, LOW);
217.
digitalWrite(f, LOW);
218.
digitalWrite(g, LOW);
219.
}
220.
221.
void seven() // define 7 as cathode pin
switch
222.
{
223.
digitalWrite(a, LOW);
224.
digitalWrite(b, LOW);
225.
digitalWrite(c, LOW);
226.
digitalWrite(d, HIGH);
227.
digitalWrite(e, HIGH);
228.
digitalWrite(f, HIGH);
229.
digitalWrite(g, HIGH);
230.
}
231.
232.
void eight() // define 8 as cathode pin
switch
233.
{
234.
digitalWrite(a, LOW);
235.
digitalWrite(b, LOW);
236.
digitalWrite(c, LOW);
237.
digitalWrite(d, LOW);
238.
digitalWrite(e, LOW);
239.
digitalWrite(f, LOW);
240.
digitalWrite(g, LOW);
241.
}
242.
243.
void nine() // define 9 as cathode pin
switch
244.
{
245.
digitalWrite(a, LOW);
246.
digitalWrite(b, LOW);
247.
digitalWrite(c, LOW);
248.
digitalWrite(d, LOW);
249.
digitalWrite(e, HIGH);

250.
251.
252.

digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

Copy the code and download to Arduino and see the effects.

You might also like