반응형

아두이노 우노로 7 Segment로 4자리의 표시(7 Segment 4 Digit Module)를 할 수 있는 LED모듈을 컨트롤 해 보자. 모듈은 아래와 같이 생겼다. 


4 자리의 표시를 할 수 있으며 각각 7Segment로 구성 되어 있으며 점(.)까지 포함해서 8 Segment이다. 핀은 12개를 가지고 있다.




위의 그림과 같이 1 Digit 모듈과 표시하는 구성은 같다. 다만 4 Digit 이라는 점이 다르다.

7 Segment 1 Digit 포스팅 보기 ==> http://deneb21.tistory.com/221


아래 6개 위 6개의 핀이 있는데 배열은 위와 같다.


아두이노 우노와의 연결은 다음과 같다.

12, 9, 8, 6핀은 아두이노 디지털핀 2,3,4,5에 연결되며 각각 Digit1~4를 컨트롤 한다.

나머지 핀은 위의 그림과 같이 각 Segment의 LOW/HIGH를 통해 숫자/문자를 나타내는 역할을 한다. 


세그먼트(Segment)

 LED Display Pin

 Arduino Pin 

 A

 11

 Digital 6

 B

 7

 Digital 7

 C

 4

 Digital 8

 D

 2

 Digital 9

 E

 1

 Digital 10

 F

 10

 Digital 11

 G

 5

 Digital 12

 DP

 3

 Digital 13


7 Segment 4 Digit LED 데이터시트: 

A-5461AS.pdf


※ LED 모듈의 보호를 위해서 아두이노 핀 2,3,4,5 번의 연결에는 220Ω의 저항을 연결해 주었다.


fritzing 파일: 

7segment4digit.fzz

##소스##

시리얼모니터 입력을 통하여 숫자를 입력하면 디스플레이에서 표시해 주는 소스이다.

숫자 및 알파벳을 정의해주고 입력값을 switch 문을 통하여 표시해준다.


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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
boolean DigitOn = LOW;
boolean DigitOff = HIGH;
boolean SegOn=HIGH;
boolean SegOff=LOW;
int DigitPins[] = {2345};
int SegmentPins[] = {678910111213};
 
//looks terrible, but I didn't find a way to copy Arrays or merge them from parts
//N is for numbers and NxP is a number with a decimal point behind
int BLANK[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int N0[]    = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
int N0P[]   = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH};
int N1[]    = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
int N1P[]   = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH};
int N2[]    = {HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH, LOW};
int N2P[]   = {HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH};
int N3[]    = {HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, LOW};
int N3P[]   = {HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, HIGH};
int N4[]    = {LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW};
int N4P[]   = {LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, HIGH};
int N5[]    = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
int N5P[]   = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, HIGH};
int N6[]    = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
int N6P[]   = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int N7[]    = {HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
int N7P[]   = {HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH};
int N8[]    = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
int N8P[]   = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int N9[]    = {HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
int N9P[]   = {HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH};
int MIN[]   = {LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW};
//The letters K, M, N, T, V, W, Z are off limits with a 7 segment display
//Some letters like D, G, Q are hard to recognize, as D is like O and G like 6
int A[] = {HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, LOW};
int B[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
int C[] = {HIGH, LOW, LOW, HIGH, HIGH, HIGH, LOW, LOW};
int D[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
int E[] = {HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH, LOW};
int F[] = {HIGH, LOW, LOW, LOW, HIGH, HIGH, HIGH, LOW};
int G[] = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
int H[] = {LOW, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, LOW};
int I[] = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
int J[] = {LOW, HIGH, HIGH, HIGH, HIGH, LOW, LOW, LOW};
int L[] = {LOW, LOW, LOW, HIGH, HIGH, HIGH, LOW, LOW};
int O[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
int P[] = {HIGH, HIGH, LOW, LOW, HIGH, HIGH, HIGH, LOW};
int Q[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH};
int R[] = {HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, LOW};
int S[] = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
int U[] = {LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
int Y[] = {LOW, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
 
//Array of pointers for the 4 digits
int* lights[4];
 
//char array coming from the serial interface
//4 numbers or chars, 4 optional decimal points, 1 end-of-line char
char incoming[9= {}; 
 
void setup() {
  Serial.begin(9600);
 
  for (byte digit=0;digit<4;digit++) {
    pinMode(DigitPins[digit], OUTPUT);
  }
  for (byte seg=0;seg<8;seg++) {
    pinMode(SegmentPins[seg], OUTPUT);
  }
  //initialize display with 1.234
  lights[0= N1P;
  lights[1= N2;
  lights[2= N3;
  lights[3= N4;
}
 
void loop() {
  //read the numbers and / or chars from the serial interface
  if (Serial.available() > 0) {
    int i = 0;
    //clear the array of char 
    memset(incoming, 0sizeof(incoming));
    while (Serial.available() > 0 && i < sizeof(incoming) - 1) {
      incoming[i] = Serial.read();
      i++;
      delay(3);
    }
    Serial.println(incoming); 
     
    //tmp is for the incoming string and counter for the 4 digits
    int counter = -1;
    for (int tmp = 0; tmp < 9; tmp++) {
      counter++;
      switch(incoming[tmp]){
        case '0'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N0P;
            tmp++;
          } else {
            lights[counter] = N0; 
          }
          break;
        case '1'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N1P;
            tmp++;
          } else {
            lights[counter] = N1; 
          }
          break;
        case '2'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N2P;
            tmp++;
          } else {
            lights[counter] = N2; 
          }
          break;
        case '3'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N3P;
            tmp++;
          } else {
            lights[counter] = N3; 
          }
          break;
        case '4'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N4P;
            tmp++;
          } else {
            lights[counter] = N4; 
          }
          break;
        case '5'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N5P;
            tmp++;
          } else {
            lights[counter] = N5; 
          }
          break;
        case '6'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N6P;
            tmp++;
          } else {
            lights[counter] = N6; 
          }
          break;
        case '7'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N7P;
            tmp++;
          } else {
            lights[counter] = N7; 
          }
          break;
        case '8'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N8P;
            tmp++;
          } else {
            lights[counter] = N8; 
          }
          break;
        case '9'
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            lights[counter] = N9P;
            tmp++;
          } else {
            lights[counter] = N9; 
          }
          break;
        case '-':
          lights[counter] = MIN; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        //with letters the decimal point is ignored!
        //if you need it, just write AP, BP etc with HIGH in the last position
        case 'a'//falls through to the next case
        case 'A'
          lights[counter] = A; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'b'//falls through to the next case
        case 'B'
          lights[counter] = B; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'c'//falls through to the next case
        case 'C'
          lights[counter] = C; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'd'//falls through to the next case
        case 'D'
          lights[counter] = D; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'e'//falls through to the next case
        case 'E'
          lights[counter] = E; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'f'//falls through to the next case
        case 'F'
          lights[counter] = F; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'g'//falls through to the next case
        case 'G'
          lights[counter] = G; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'h'//falls through to the next case
        case 'H'
          lights[counter] = H; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'i'//falls through to the next case
        case 'I'
          lights[counter] = I; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'j'//falls through to the next case
        case 'J'
          lights[counter] = J; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'l'//falls through to the next case
        case 'L'
          lights[counter] = L; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'o'//falls through to the next case
        case 'O'
          lights[counter] = O; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'p'//falls through to the next case
        case 'P'
          lights[counter] = P; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'q'//falls through to the next case
        case 'Q'
          lights[counter] = Q; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'r'//falls through to the next case
        case 'R'
          lights[counter] = R; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 's'//falls through to the next case
        case 'S'
          lights[counter] = S; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'u'//falls through to the next case
        case 'U'
          lights[counter] = U; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;        
        case 'y'//falls through to the next case
        case 'Y'
          lights[counter] = Y; 
          if (tmp  < 8 && (incoming[tmp + 1== '.' || incoming[tmp + 1== ',')) {
            tmp++;
          } 
          break;       
        case 1 ... 43: counter--break;//special chars are ignored 
        //44 to 46 are , - .
        case 47: counter--break;//special chars are ignored 
        //chars between Z and a
        case 91 ... 96: counter--break;//special chars are ignored 
        case 123 ... 127: counter--; Serial.println("above 122"); break;//special chars are ignored 
         
        default: lights[counter] = BLANK;
      }
    } //end for
     
    //show the input values
    for (int y = 0; y < 4; y++) {
      Serial.print(y);
      Serial.print(": ");
      for (int z = 0; z < 8; z++) {
        Serial.print(lights[y][z]);
      }
      Serial.println("");
    }
  } //end if, i.e. reading from serial interface
 
  //This part of the code is from the library SevSeg by Dean Reading
  for (byte seg=0;seg<8;seg++) {
    //Turn the relevant segment on
    digitalWrite(SegmentPins[seg],SegOn);
 
    //For each digit, turn relevant digits on
    for (byte digit=0;digit<4;digit++){
      if (lights[digit][seg]==1) {
        digitalWrite(DigitPins[digit],DigitOn);
      }
      //delay(200); //Uncomment this to see it in slow motion
    }
    //Turn all digits off
    for (byte digit=0;digit<4;digit++){
      digitalWrite(DigitPins[digit],DigitOff);
    }
 
    //Turn the relevant segment off
    digitalWrite(SegmentPins[seg],SegOff);
  } //end of for
}
cs



##결과##


시리얼 모니터 입력



7 Segment 4 Digit LED 표시내용



추후에 RTC 모듈 등과 연결하여 시계를 만들어보거나 온습도 모듈과 붙여서 온도, 습도를 표시하는 용도로 사용하면 더욱 좋을 것 같은 모듈이다.






■ 추가사항 (2016.05.11)

위의 글은 기초적인 부분부터 쓰여있으므로 소스가 조금 길고 복잡하게 되어 있습니다. 인터넷을 뒤져보니 조금 편하게 사용할 수 있도록 누군가가 만들어 놓은 라이브러리가 있어서 추가로 올립니다.


SevSeg library 2.3.zip


위의 파일을 아두이노 라이브러리에 추가하시고 안에 예제도 같이 있으니 참고하시면 원하는 기능을 더욱 간단하게 구현할 수 있을 것입니다. 추가로 이 라이브러리 이용해서 1234 찍어보는 소스와 회로도를 올려 봅니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "SevSeg.h"
 
//Create an instance of the object.
SevSeg sevseg;
 
void setup() {
  // Set up pins
  // Arguments : 
  // First 0 is for common cathode
  // Following 0 to 11 numbers are for Arduino pins
  // connected to display in the following order
  // 1,2,3,4,A,B,C,D,E,F,G,DP
  sevseg.Begin(0,0,1,2,3,4,5,6,7,8,9,10,11);
}
 
void loop() {   
  // Prepare number to display, and dot position 
  // (0 to 3, other number means no dot)  
  sevseg.NewNum(1234,0);
  // Display number
  sevseg.PrintOutput();  
  // To maintain display with this wiring, 
  // we are forced to loop over those commands rapidly
}
cs


위의 라이브러리를 이용해서 1234 를 표시해주는 소스.

반응형

+ Recent posts