A Simple Counter
This project was developed with an Arduino Micro and a Common Anode (CA) 4x7-Segment Display only, and so it can count from 0000 to 9999.
List of Materials
1 Arduino Micro, 5V
1 Common Anode, 4x7-Segment Display
1 USB-A to USB-B Micro Cable
Identifying the Display’s Pins
Check the display’s pins before beginning to connect them to your Arduino Micro.
Connecting Display and Arduino Pins Respectively
CA 4x7-Segment Display & Corresponding Arduino Micro Pins
Pin 1(E)-----------------A2
Pin 2(D)-----------------A1
Pin 3(DP)NC-----------NC
Pin 4(C)------------------15
Pin 5(G)------------------14
Pin 6(dig. 4)---------------4
Pin 7(B)------------------10
Pin 8(dig. 3)---------------7
Pin 9(dig. 2)---------------8
Pin 10(F)-------------------6
Pin 11(A)-------------------5
Pin 12(dig. 1)-------------16
Uploading the Code
Once done the connections between the 4x7-segment display and the Arduino, you can already upload the following code:
//A Simple Counter
#define A 5
#define B 10
#define C 15
#define D A1
#define E A2
#define F 6
#define G 14
// Pins driving common anodes
#define CA1 16
#define CA2 8
#define CA3 7
#define CA4 4
// Pins for A B C D E F G, in sequence
const int segs[7] = {5, 10, 15, A1, A2, 6, 14};
// Segments that make each number
const byte numbers[10] = {192, 249, 164, 176, 153, 146, 130, 248, 128, 144};
void setup() {
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
pinMode(15, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(6, OUTPUT);
pinMode(14, OUTPUT);
pinMode(16, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
for (int digit1=0; digit1 < 10; digit1++) {
for (int digit2=0; digit2 < 10; digit2++) {
for (int digit3=0; digit3 < 10; digit3++) {
for (int digit4=0; digit4 < 10; digit4++) {
unsigned long startTime = millis();
for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() - startTime) {
lightDigit1(numbers[digit1]);
delay(2);
lightDigit2(numbers[digit2]);
delay(2);
lightDigit3(numbers[digit3]);
delay(2);
lightDigit4(numbers[digit4]);
delay(2);
}
}
}
}
}
}
void lightDigit1(byte number) {
digitalWrite(CA1, LOW);
digitalWrite(CA2, LOW);
digitalWrite(CA3, LOW);
digitalWrite(CA4, HIGH);
lightSegments(number);
}
void lightDigit2(byte number) {
digitalWrite(CA1, LOW);
digitalWrite(CA2, LOW);
digitalWrite(CA3, HIGH);
digitalWrite(CA4, LOW);
lightSegments(number);
}
void lightDigit3(byte number) {
digitalWrite(CA1, LOW);
digitalWrite(CA2, HIGH);
digitalWrite(CA3, LOW);
digitalWrite(CA4, LOW);
lightSegments(number);
}
void lightDigit4(byte number){
digitalWrite(CA1, HIGH);
digitalWrite(CA2, LOW);
digitalWrite(CA3, LOW);
digitalWrite(CA4, LOW);
lightSegments(number);
}
void lightSegments(byte number){
for (int i = 0;i < 7;i++) {
int bit = bitRead(number, i);
digitalWrite(segs[i], bit);
}
}