Build an Arduino-Based Binary Stopwatch with Tinkercad
2022-03-08 | By Maker.io Staff
License: See Original Project
Binary clocks are a fun way of displaying time, and a binary clock will surely spark a conversation when friends and family come over for a surprise visit. But instead of paying lots of money for a designer clock, this article shows you how you can build a custom binary clock at home using only a handful of common components. In addition, the article discusses how binary clocks function and how to read them.
This image shows the finished product. The stopwatch uses a simple LED matrix to display the time.
About This Project
A previous article discussed free online tools for simulating digital and analog circuits. Therefore, I decided to build this project using Tinkercad Circuits, one of the free online Arduino simulation programs discussed earlier. I recommend reading this article if you have never used Tinkercad Circuits before. Of course, you can also build the project at home using the following physical components:
Part/Where to Buy/Amount
- Arduino UNO - 1
- Red 5mm LED - 21
- NPN Transistor - 6
- 680 Ohm Resistor - 6
- 150 Ohm Resistor - 4
How to Read a Binary Clock
Note that there are various ways you can design the watch face of a binary clock. However, in this project, I used a six by four LED matrix, as it’s common in larger binary clocks, for example, wall-mounted ones. Smaller wristwatch-style devices may use a slightly different arrangement to save space. Either way, in this project, the LEDs are arranged as follows:
This picture shows an example of a binary clock. The red circles represent lit LEDs.
As you can see, you can divide the 6-by-4 matrix into three equal parts, each containing two columns of LEDs. The leftmost part shows you the hours, the middle two columns display the minutes, and the last two represent the current seconds. The left column in each section represents the tens place, and the second one represents the ones place of the final number. Equally, each row represents one power of two. To read the current time, look at each of the three segments, starting with the hours. Add the values of each lit LED in the left row of the hour section, and then multiply the result by ten. Then, add the values of the LEDs in the second column to the result. Repeat this process for minutes and seconds:
Use the provided formulas to determine the time shown on the LED matrix.
So, in this example, the displayed time is 16:24:39, or four PM, 24 minutes, and 39 seconds. Most binary clocks use the 24-hour format, sometimes also referred to as military time. However, you can simplify your design and omit two LEDs if you decide to build one that only uses the 12-hour format.
The Schematic Diagram
As mentioned, this project uses a custom six by four LED matrix to display the time. Note that the first column of each block doesn't have an LED for displaying an eight, as there's no need for it. The following schematic explains how to connect the LEDs in the matrix:
This image shows the schematic diagram of this project. Scheme-It link.
Connect the anodes of all LEDs in one row. Then, connect the cathodes of all the LEDs in a column. Next, attach each column to the collector pin of an NPN transistor. Don’t forget to add a resistor to the base of each transistor. Also, note how there is only one current-limiting resistor per row. In this configuration, each transistor controls one column of the matrix. This means that the Arduino scans the columns of the matrix, and only a single column will be active at any time.
However, the Arduino might simultaneously activate multiple lines. Therefore, adding the resistors to the lines ensures that each active LED is connected to a resistor. If this weren’t the case, multiple LEDs would use a single current-limiting resistor, and the brightness of the LEDs in a row would vary depending on the number of lit LEDs.
Assembling the Project in Tinkercad Circuits
As mentioned in the introduction of this article, I used Tinkercad Circuits to assemble and simulate this project. There are various reasons why I decided to do so. Utilizing the simulator leads to a more visually appealing result, and the final product is easier to understand. Further, I wanted to try how the simulator reacts to a more complex circuit.
This image shows the finished build in Tinkercad Circuits. Follow this link to open the design in Tinkercad Circuits.
Arduino Binary Clock Project Code
Next, copy and paste the following code into the code editor of Tinkercad Circuits:
#define SR_COL 2
#define SL_COL 3
#define MR_COL 4
#define ML_COL 5
#define HR_COL 6
#define HL_COL 7
#define ROW_1 13
#define ROW_2 12
#define ROW_3 11
#define ROW_4 10
unsigned h = 0;
unsigned m = 0;
unsigned s = 0;
unsigned long lastMillis = 0UL;
void setup()
{
pinMode(SR_COL, OUTPUT);
pinMode(SL_COL, OUTPUT);
pinMode(MR_COL, OUTPUT);
pinMode(ML_COL, OUTPUT);
pinMode(HR_COL, OUTPUT);
pinMode(HL_COL, OUTPUT);
pinMode(ROW_1, OUTPUT);
pinMode(ROW_2, OUTPUT);
pinMode(ROW_3, OUTPUT);
pinMode(ROW_4, OUTPUT);
}
void decimalToBinary(unsigned dec, unsigned *target_array)
{
unsigned index = 0;
if(dec > 9)
dec = 9;
while (dec > 0)
{
target_array[index++] = dec % 2;
dec = dec / 2;
}
}
void displayColumn(unsigned column, unsigned *bin_array)
{
digitalWrite(column, HIGH);
for(int i = 0; i < 4; i++)
digitalWrite(ROW_4 + i, bin_array[i]);
delay(2);
digitalWrite(column, LOW);
}
void displayValues(unsigned leftColumn, unsigned rightColumn, unsigned value)
{
unsigned tens = value / 10;
unsigned ones = value - (tens * 10);
unsigned tens_binary[4] = {0,0,0,0};
unsigned ones_binary[4] = {0,0,0,0};
decimalToBinary(tens, tens_binary);
decimalToBinary(ones, ones_binary);
displayColumn(leftColumn, tens_binary);
displayColumn(rightColumn, ones_binary);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - lastMillis > 1000)
{
s++;
if(s >= 60)
{
m++;
s = 0;
if(m >= 60)
{
h++;
m = 0;
if(h >= 24)
h = 0;
}
}
lastMillis = currentMillis;
}
displayValues(SL_COL, SR_COL, s);
displayValues(ML_COL, MR_COL, m);
displayValues(HL_COL, HR_COL, h);
}
The setup method initializes all the pins of the Arduino used in this project. The decimalToBinary function takes in a decimal number and converts it to its binary representation. It then stores the result in the supplied array. A straightforward method to convert a decimal number to a binary number is to divide it by two and save the remainder in an array.
The displayValues function displays the binary coded decimals on the clock’s LED matrix. First, the method splits a given number into its digits, and then the program calls the binaryToDecimal method to convert each digit into its binary representation. Then, the displayValues function calls a helper method that displays each binary digit in one column of the matrix. The displayColumn helper-function iterates over each bit in the binary array. It then turns on the LEDs in a given row according to the bits in the array.
The loop method first checks whether it needs to update the hour, minute, and second variables. It does that once every second. Then, the loop function calls the displayValues method three times, once for the seconds, then for the minutes, and lastly for displaying the hours.
You can run the simulation once you paste the source code into the code editor of Tinkercad Circuits. You should see that the stopwatch starts counting the seconds and eventually the minutes. After a while, the clock should look similar to this:
Once you run the simulation, the stopwatch should start counting up.
Summary
Reading the time on a binary clock is simple once you know how to do it. Split the matrix into three equal regions of two columns each. Each row represents a power of two. Add the values represented by the lit LEDs in the first column of the section. Then, multiply the result by ten. Next, add all values of the second column to the multiplication result. Repeat these steps for the other two regions.
This project utilizes a simple LED matrix with six columns and four rows. Note that only one column should be active at any time, and the Arduino can turn on multiple rows to light up more than one LED. The Arduino quickly switches through all the columns to display the time. The software of this project first converts the seconds, minutes, and hours from their decimal representation into the binary system. Then, it displays each number one column at a time.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum