728x90
작은 프로젝트로 MNIST 숫자 데이터셋을 PyTorch로 학습시키고, OpenCV와 카메라 모듈로 전처리한 이미지를 인식하여 해당 숫자를 7-Segment display로 출력하는 실습을 해본다. 프로젝트명은 DeepSeg(Deep learning + Segment display).
사용 부품: 아두이노 우노 R3, SH5461AS FND, 330옴 저항, 점퍼 케이블
우선 SH5461AS FND는 common cathode 형식의 4-digit 7-Segment display로 다음과 같은 핀 정보를 갖는다.
D1, D2, D3, D4는 각각 자리수를 선택한다. 나머지 핀은 7-segment 와 소수점(3번)을 표시한다.
Number | g f e d c b a | Hex Code |
0 | 0111111 | 3F |
1 | 0000110 | 06 |
2 | 1011011 | 5B |
3 | 1001111 | 4F |
4 | 1100110 | 66 |
5 | 1101101 | 6D |
6 | 1111101 | 7D |
7 | 0000111 | 07 |
8 | 1111111 | 7F |
9 | 1001111 | 4F |
위 코드는 Common Cathode 형식의 7-segment display 코드이다. 이 코드를 바탕으로 아두이노에서 핀을 설정한다.
다음과 같이 핀을 연결한다. Decimal 핀은 선택이며, 디스플레이 핀과 FND를 연결할 때는 330 옴 저항을 추가해주어야 한다.
디스플레이 핀 | 기능 | 아두이노 핀 | 저항 |
A | 세그먼트 A | D2 | Yes |
B | 세그먼트 B | D3 | Yes |
C | 세그먼트 C | D4 | Yes |
D | 세그먼트 D | D5 | Yes |
E | 세그먼트 E | D6 | Yes |
F | 세그먼트 F | D7 | Yes |
G | 세그먼트 G | D8 | Yes |
Decimal | 소수점 제어 | D13 (Optional) | Yes |
D1 | 디스플레이 1 | D9 | No |
D2 | 디스플레이 2 | D10 | No |
D3 | 디스플레이 3 | D11 | No |
D4 | 디스플레이 4 | D12 | No |
연결이 완료된 모습
그리고 아두이노 우노 보드에 다음 코드를 업로드한다.
#include <Arduino.h>
// 세그먼트 핀 배열 (A ~ G)
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
// 디스플레이 선택 핀 (D1 ~ D4)
const int digitPins[] = {9, 10, 11, 12};
// 숫자 세그먼트 패턴 (Common Cathode)
const uint8_t numbers[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void setup() {
// 세그먼트 핀을 출력으로 설정
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// 디스플레이 핀을 출력으로 설정
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
digitalWrite(digitPins[i], HIGH); // 초기화 (Common Cathode에서는 HIGH가 비활성화)
}
}
void loop() {
static int counter = 0; // 카운터 초기화
static unsigned long lastUpdate = 0; // 타이머 변수
const int updateInterval = 1000; // 1초 간격
// 1초마다 카운터 증가
if (millis() - lastUpdate >= updateInterval) {
lastUpdate = millis();
counter++;
if (counter > 9999) {
counter = 0; // 9999 이후 다시 0으로 초기화
}
}
// 4개의 디스플레이에 숫자를 표시 (멀티플렉싱)
for (int digit = 0; digit < 4; digit++) {
int numberToDisplay = (counter / (int)pow(10, 3 - digit)) % 10; // 각 자리 숫자 추출
displayDigit(digit, numbers[numberToDisplay]); // 자리별 숫자 출력
delay(2); // 멀티플렉싱 딜레이 (2ms)
}
}
// 특정 디스플레이에 숫자 출력
void displayDigit(int digit, uint8_t number) {
// 이전 디스플레이 비활성화
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], HIGH);
}
// 선택된 디스플레이 활성화 (Common Cathode에서는 LOW로 활성화)
digitalWrite(digitPins[digit], LOW);
// 세그먼트 값 출력
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], bitRead(number, i));
}
}
위 코드를 업로드하면 7-segment 디스플레이가 1초마다 1 증가하여 점등된다.
728x90
'Side Project > DeepSeg' 카테고리의 다른 글
[DeepSeg] YOLOv5 설치 및 MNIST 데이터셋으로 학습 (0) | 2025.02.14 |
---|---|
[DeepSeg] MNIST 데이터셋 YOLO 형식으로 변환 (0) | 2025.02.13 |