In this post, I will explain how to display numbers received through the serial port on 7-segment displays.
MATERIALS:
- 1 Arduino UNO
- 2 resistors of 1 kohm
- 2 resistors of 220 ohms
- 2 7-segment displays
- 2 transistors 2N3904
WIRING DIAGRAM:
FUNCTIONING:
To display the numbers on the 7-segment displays, we will use the serial monitor; in this case, we can only send values of 2 digits at most. After entering the number and pressing enter, the number is automatically displayed on the displays.
EXPLAINED CODE:
We declare an array for the connection pins of the displays as follows:
byte pins [] = {2, 3, 4, 5, 6, 7, 8};
// G, F, E, D, C, B, A
We define the connection pins of the transistors
#define decenaTr 11 // tens display #define unitTr 12 // display unit
We declare some variables
int number; // stores the number received through the serial port byte unit; // stores the value of the unit byte ten; // stores the value of the ten byte pause = 5; // multiplexing pause byte digits[2]; // array with the number of digits bool fullNumber = false; // flag to check if a number has been received
We define an array in which we will store the values of the numbers in binary code in ascending order from 0 to 9.
byte anodeNumbers[10] {
B0000001,//0
B1001111,//1
B0010010,//2
B0000110,//3
B1001100,//4
B0100100,//5
B0100000,//6
B0001111,//7
B0000000,//8
B0000100,//9
};
Void Setup:
We initialize the serial port and define the connection pins of the transistors and displays as outputs.
void setup() {
Serial.begin(9600);
delay(1000);
pinMode(decenaTr, OUTPUT);
pinMode(unidadTr, OUTPUT);
for (byte led = 0; led < 8; led++) {
pinMode(pins[led], OUTPUT);
}
}
Void loop:
We create a variable
byte n; // it will help us select the positions of the array digitos[]
We create a function to read the data received from the serial port: readSerialPort(), we store that data in the variable: serialPortNumber.
int serialPortNumber = readSerialPort();
Inside the function readSerialPort(), we have the following:
int readSerialPort() {
while (Serial.available()) { //Every time we receive a data point:
// we read the string until we press the enter key
// we store the data as a string in the variable number
String number = Serial.readStringUntil('\n');
// since the data is complete, we change the flag state to true
fullNumber = true;
// with the following code we prevent entering numbers with more than two digits
if (number.length() > 2) {
} else {
return number.toInt(); // we return the value of the variable number, transformed into an integer
}
}
}
We return to the void loop where we check with the flag if a new data has been entered.
if (fullNumber) { // if a new data was entered:
// we assign the value of the data to the variable number
//entered through the serial port.
number = serialPortNumber;
fullNumber = false; // we change the flag state to false.
}
At this point, we already have the entered number, what we need to do now is separate that number into units and tens, so we can display the units on one display and the tens on another. To separate the number, we create the function:separateNumber()to this function we send as a parameter the number to separate. In the following way.
separateNumber(number);
Inside the function separateNumber() we have the following
void separateNumber(int number) {// we enter the number to separate as a parameter
// we divide the number by 10 to get the ten
ten = number / 10;
// (we take the remainder of the division of the number by 10)
// we divide it by 1 and thus we obtain the unit
unit = (number % 10) / 1;
}
We return to the void loop in this part of the program:
We select from the array anodeNumbers[] the value corresponding to the position determined by the value of the variable unit and we store it in position 0 of the array digits[].
digits[0] = anodeNumbers[unit];
We select from the array anodeNumbers[] the value corresponding to the position determined by the value of the variable tens and we store it in position 1 of the array digits[].
digits[1] = anodeNumbers[tens];
Example:
if the entered number is 57, the value of the unit variable is 7 and the value of the tens is 5, therefore:
digits[0] = anodeNumbers[7]; digits[1] = anodeNumbers[5]; digits[0] = B0001111,//7 digits[1] = B0100100,//5
Now having the number separated and stored in the different positions of the array digits[]what we are going to do is perform the multiplication to first show the tens place and then the units place. To do this we do the following:
digitalWrite(tensTr, 1); // we activate the transistor for the tens digitalWrite(unitsTr, 0);// we deactivate the transistor for the units // we call the function showDigit() // we send the value of the variable n as a parameter showDigit(n); // we show the number of the tens n = 1; delay(pause); // pause to visualize the number digitalWrite(tensTr, 0); // we deactivate the tens transistor digitalWrite(unitTr, 1); // we activate the units transistor // we call the function showDigit() // we send the value of the variable n as a parameter showDigit(n); n = 0; delay(pause); // pause to visualize the number
Inside the function showDigit(): the lighting of the display's LEDs will take place, thus forming the previously defined number.
void showDigit(byte n) { // n is the index for the array digits[]
// we create a for loop which will give us the index for the array pins[]
for (int i = 6; i >= 0; i--) {
//We turn on or off the LEDs depending on the binary code of the entered number
digitalWrite(pins[i], (digits[n] & (1 << i) ? 1 : 0));
}
}
FULL CODE:
byte pins [] = {2, 3, 4, 5, 6, 7, 8};
#define tensTr 11
#define unitsTr 12
int number;
byte unit;
byte ten;
byte pause = 5;
byte digits[2];
bool integer = false;
byte anodeNumbers[10] {
B0000001,//0
B1001111,//1
B0010010,//2
B0000110,//3
B1001100,//4
B0100100,//5
B0100000,//6
B0001111,//7
B0000000,//8
B0000100,//9
};
void setup() {
Serial.begin(9600);
pinMode(decenaTr, OUTPUT);
pinMode(unidadTr, OUTPUT);
for (byte led = 0; led < 8; led++) {
pinMode(pins[led], OUTPUT);
}
}
void loop() {
byte n;
int serialPortNumber = serialPortReading();
if (completeNumber) {
number = serialPortNumber;
completeNumber = false;
}
separateNumber(number);
digits[0] = anodeNumbers[unit];
digits[1] = anodeNumbers[tens];
digitalWrite(tensTr, 1);
digitalWrite(unitsTr, 0);
showDigit(n);
n = 1;
delay(pause);
digitalWrite(tensTr, 0);
digitalWrite(unitsTr, 1);
showDigit(n);
n = 0;
delay(pause);
}
int serialPortReading() {
mientras (Serie.disponible()) {
Cadena number = Serial.readStringUntil('\n');
fullNumber = true;
if (number.length() > 2) {
} else {
return number.toInt();
}
}
}
void separateNumber(int number) {
tens = number / 10;
units = (number % 10) / 1;
}
void showDigit(byte n) {
for (int i = 6; i >= 0; i--) {
digitalWrite(pins[i], (digits[n] & (1 << i) ? 1 : 0));
}
}

Post a Comment
Hello! We're so glad you've made it this far and are reading this article on Edeptec.
This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.
» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?
Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.
Thank you for being part of this learning community! Your participation is what makes this project grow.