Arduino clock with LED indicator. DIY LED clock
The photo shows a prototype that I assembled to debug the program that will manage this entire facility. The second arduino nano in the upper right corner of the breadboard does not belong to the project and sticks out there just like that, you don’t have to pay attention to it.
A little about the principle of operation: Arduino takes data from the DS323 timer, processes it, determines the light level using a photoresistor, then sends everything to the MAX7219, and it, in turn, lights up the required segments with the required brightness. Also, using three buttons, you can set the year, month, day, and time as desired. In the photo, the indicators display time and temperature, which is taken from a digital temperature sensor
The main difficulty in my case is that the 2.7-inch indicators have a common anode, and they had to, firstly, somehow make friends with the max7219, which is designed for indicators with a common cathode, and secondly, solve the problem with their power supply, since they need 7.2 volts for glow, which max7219 alone cannot provide. Having asked for help on one forum, I received an answer.
Solution in the screenshot:
A microcircuit that inverts the signal is attached to the outputs of the segments from max7219, and a circuit of three transistors is attached to each pin that should be connected to the common cathode of the display, which also invert its signal and increase the voltage. Thus, we get the opportunity to connect displays with a common anode and a supply voltage of more than 5 volts to the max7219
I connected one indicator for the test, everything works, nothing smokes
Let's start collecting.
I decided to divide the circuit into 2 parts due to the huge number of jumpers in the version that was separated by my crooked paws, where everything was on one board. The clock will consist of a display unit and a power and control unit. It was decided to collect the latter first. I ask aesthetes and experienced radio amateurs not to faint because of the cruel treatment of parts. I have no desire to buy a printer for the sake of LUT, so I do it the old fashioned way - I practice on a piece of paper, drill holes according to the template, draw paths with a marker, then etch.The principle of attaching indicators remained the same as on.
We mark the position of the indicators and components using a plexiglass template made for convenience.
Markup process
Then, using a template, we drill holes in the right places and try on all the components. Everything fit perfectly.
We draw paths and etch.
bathing in ferric chloride
Ready!
control board:
indication board:
The control board turned out great, the track on the display board was not critically eaten up, it can be fixed, it’s time to solder. This time I lost my SMD virginity and included 0805 components in the circuit. At the very least, the first resistors and capacitors were soldered into place. I think I'll get better at it, it will be easier.
For soldering I used flux that I bought. Soldering with it is a pleasure; now I use alcohol rosin only for tinning.
Here are the finished boards. The control board has a seat for an Arduino nano, a clock, as well as outputs for connecting to the display board and sensors (a photoresistor for auto-brightness and a digital thermometer ds18s20) and a power supply with adjustable output voltage (for large seven-segment devices) and for powering the clock and Arduino, on the display board there are mounting sockets for displays, sockets for max2719 and uln2003a, a solution for powering four large seven-segment devices and a bunch of jumpers.
rear control board
Rear display board:
Terrible smd installation:
Launch
After soldering all the cables, buttons and sensors, it's time to turn it all on. The first launch revealed several problems. The last large indicator did not light up, and the rest glowed dimly. I dealt with the first problem by soldering the leg of the SMD transistor, and with the second - by adjusting the voltage produced by lm317.IT'S ALIVE!
After making a lot of Arduino prototypes on a breadboard, I decided to make something useful that I could use at home. And what could be more useful than a luminous watch, which for some reason has been discontinued since 2010? I started assembling a digital watch by searching for the necessary parts. One of the criteria that helped to quickly collect the necessary components was the availability of parts in local stores and from manufacturers from China and Malaysia.
Arduino real time clock (RTC) with 7-segment displays
When assembling the watch, several options appeared for setting the exact time on it. First: set time on Arduino , keeping it powered all the time. But this method is not very practical, since every time you need to set the time, you will need to turn on power to the Arduino.
The second option was the idea connection of 7-segment LED - indicators for the GPS module . Since the GPS signal gives a very accurate time, this option should solve the problem, and would not have to adjust the clock every time you turn it on. I took my Garmin GPS 60 C pocket navigator, serialized it into the Arduino and loaded several GPS libraries, thus obtaining a very accurate time signal.
The problem with the GPS method turned out to be that since I live in the center of the city, a concrete jungle with impenetrable high-rise buildings surrounds my house, and it would be necessary to place an external GPS antenna outside the window in order to receive a GPS signal from a clear sky. Without satellite coverage, no GPS device is able to receive a time-locked signal. Either the clock should be on the window, or it was necessary to take out the GPS antenna and lay a 7-meter cable to it.
The third method of setting the clock turned out to be the best. It lies in work Arduino together with DS1307 real time clock (RTC). They are powered by a 3-volt button cell battery, which retains settings when the device is turned off and when the microcontroller is disconnected.
I went to the local "electronic heaven" located in the city center to try my luck in finding the necessary components. To my surprise, there I found all the necessary parts to assemble a digital watch.
The required parts are:
- Arduino board for prototyping and loading the sketch into the microcontroller;
- ATmega328P microcontroller for clock operation;
- four red 7-segment LED indicators (or other cooler colors you can find at your local market);
- real time clock DS1307;
- quartz resonator at 32.768 kHz;
- holder for coin cell battery CR2025 or CR2032;
- four 74HC595 shift register chips for driving 7-segment LED indicators;
- resistors 220 Ohm 0.25 W each;
- single row pin connectors;
- integrated circuit (IC) sockets;
- connecting wires.
If you don’t have the skills to make printed circuit boards, I recommend using soldered breadboard (textolite plate with many holes for fixing components on it by soldering, which is mistakenly called circuit board) and solder all IC sockets of microcircuits and pin connectors to it. Thanks to these quick-release contacts, all 7-segment LEDs and ICs can be easily replaced if necessary.
Since the size of the prototyping board is very limited, we were able to place only 35mm LED indicators on it, because there still had to be room for a button cell battery holder. I would like to install much larger 7-segment indicators, but the larger ones require increased voltage, over 5 V, and it was already necessary to complicate the circuit with double power circuits. I don’t want to deal with a stabilizer for two output voltages; it’s better to focus on a simpler version of a digital clock.
100 nF ceramic blocking capacitors on the Vcc power pin of each 74HC595 register are added to prevent any low frequency interference problems.
The assembled digital clock uses only 5 Arduino pins:
- 3 digital outputs for 74HC595 shift registers;
- 2 analog outputs for real time clock connected using I2C connection.
The advantage of an assembled digital clock on Arduino compared to a factory one is that you can easily add any functions that may be useful to them.
Here are some ideas for modifying your watch:
- Alternating display of hours/minutes and minutes/seconds;
- Playing a melody every hour;
- Installing the LM35 sensor and using the watch as a thermometer;
- Morning alarm function;
- Even control of other electrical devices using an electromechanical relay that turns on in accordance with time-defined events or readings from connected sensors.
Since the four indicators are quite large and bright, they can also be used to display letter information.
After I soldered the first digit of the 7 segment common cathode LED to the 74HC595 shift register, the first problem appeared. I used only one 220 ohm resistor connected to the common cathode of the LED indicator to save resistors, and found that when the number 8 turned on, all the segments lit up very dimly. This is fine for a prototype, but not suitable for a live digital watch. It would be very annoying to have a watch with numbers that glow differently. So I had to cut individual wires and get more 220 ohm resistors to connect them to each of the seven LED segments.
The second problem was that I forgot to allocate space for two 5mm LEDs to act as the flashing colon seconds indicator. And the indicator of the third digit was already soldered.
Since it takes too much work to solder one indicator, along with attaching all the resistors to the wires, I decided to make a remote board with two LEDs as seconds indicators. I'll find a way to set two dots between the hour and minute digits! In the photo below, I simply shoot the LED on pin 13 blinking at 500 ms intervals.
- There are codes .
- Final sketch
Here are some photos of the assembled, working device. Now I just need something like acrylic to secure the breadboard and hide the Arduino clock in the overall enclosure.
This clock is powered by an Arduino remote board in the version with FTDI cable and DC socket.
The Arduino clock assembly is completed after installing the DHT11 humidity and temperature sensor.
Homemade temperature and humidity sensor DHT11 and DHT22 - connection to Arduino GPS clock on Arduino
There are many ways to assemble an electronic watch with your own hands: diagrams are widely presented in the literature and on the Internet. Most modern implementations are based on microcontrollers. The implementation of such projects often requires extensive practical skills and theoretical knowledge in the field of electronics: the ability to use specialized software, create printed circuit boards at home using ferric chloride etching, and good soldering. You also need to have a variety of tools and supplies.
However, there is a simple and affordable way to assemble an electronic watch with your own hands at home: use the Arduino platform. It is a software and hardware complex specifically designed for teaching the basics of programming and electronics. With the help of Arduino, anyone, even without special prior training, can build an electronic clock with their own hands: circuit diagrams, engineering programs and even a soldering iron are not needed!
The connection of all electronic components is carried out on a special contact (“solderless”) breadboard, which eliminates the risk of burns, cuts and other injuries - therefore, you can work with the Arduino designer together with children. A visual way of presenting a circuit diagram will help you avoid making mistakes when assembling the device.
Step 1. List of components
To assemble a simple watch on LED matrices you will need only a few cheap components:
- Arduino platform. The simplest models will do - or Micro;
- contact breadboard;
- connecting wires for breadboard;
- Adafruit DS3231 real-time clock module;
- LED matrix module 32x8 MAX7219;
- two buttons.
You will also need a personal computer and a USB-mini-USB cable to load the control program into memory. That's all - a soldering iron, insulation strippers, assembly knives and other professional tools are not needed: all operations are performed by hand. Perhaps in some cases it is more convenient to use tweezers, but you can do without it.
Step 2. Assembling the electronic circuit
The circuit of an electronic clock with LED display using Arduino will seem quite simple even for inexperienced radio amateurs. Only a few wires are required for assembly. Connection table:
Arduino module → 32x8 MAX7219 LED matrix
Arduino module → Adafruit DS3231 real time clock
Arduino module → buttons
D2 - button 1
D3 - button 2
The second pin of the buttons is connected to GND.
You just need to pay attention and remember how the contact holes on the breadboard are connected to each other. The following diagram illustrates the method of internal connection of contact holes:
Two rows (1 and 4) on both sides are shorted horizontally - they are usually used as the +5V power line and GND ground. All internal contacts (2 and 3) are closed vertically. In this case, the circuit board is divided both vertically and horizontally into two symmetrical parts independent of each other. This allows, for example, to assemble two different devices on one board.
The diagram of an electronic clock with LED indication, as well as the arrangement of elements on the circuit board, is shown in the illustration:
Carefully check that all connections comply with the diagram shown. Also make sure that the conductors are well secured in the contact holes of the circuit board.
Step 3. Arduino firmware
Once the assembly and testing of the circuit is completed, you can begin loading the control program (or “firmware”) into the Arduino memory.
To do this, you need to install the free official development environment - . You will also need the source code of the project, which you can download below in the archive with all the libraries and a sketch, and if you just need a sketch, you can copy it separately:
//include libraries: #include "LedControl.h" #includeNow, to complete work on the device, you only need to perform a number of simple operations:
Compiling the program code and further loading it into the microcontroller’s memory will take some time, usually no more than one minute. The successful completion of the operation will be reported in the Arduino IDE console. After which all that remains is to reboot the Arduino using the Reset button on the device - a simple clock on LED matrices is ready!
Ready clock on Arduino
The clock is set using two buttons. The device supports 12- and 24-hour time formats, displays the date and day of the week, and displays the time with or without seconds. It is also possible to change the brightness of the LEDs.
You will probably want to add more features in the future (for example, a thermometer), or install the device in a body of your own design - good results can be achieved by manufacturing on laser cutting machines. But now you can safely say that you have assembled a full-fledged electronic watch with your own hands!
So, after a short technical break, we continue our acquaintance with the ARDUINO MK family. In this lesson we will try to make a clock that works from the internal generator of the MK (with an external generator there will be one of the following lessons) and displays information on an LCD indicator of type 1602 (which means 16 characters in 2 lines, there is also type 1604 - 16 characters in 4 lines, you already understand that the first 2 digits indicate the number of indicator characters and the second ones indicate the number of lines). Let's not delay the introduction, let's get to work.
For the project we will need:
- Arduino Uno
- LCD indicator 1602
- Development board
- Wires
- Trimmer resistor 10 kOhm
For those who are especially lazy, I advise you to go to the bottom of the page and download the finished sketch; for those who want to learn how to make sketches on their own, I will describe in more detail all the steps of the project. To work on a project correctly and quickly, a work algorithm is required. It’s better to sketch out almost any project on paper and then follow the algorithm step by step. We will do exactly the same. So let's create an algorithm. We have several conditions, let's write them in ascending order:
- Seconds work in the range from 0 to 59 in a cycle with a second interval (this is understandable).
- Minutes operate in the range from 0 to 59 in a cycle, switching occurs when the seconds value reaches 0.
- The clock operates in the range from 0 to 24 (here you can choose to display it in a foreign style from 0 to 12 with AM and PM values, whichever you prefer) in a cycle, switching occurs when the minute value reaches 0.
- Display all the necessary information on the display (for example, you may decide not to display seconds but just make a flashing dot between hours and minutes)
We assemble our watches according to this scheme:
Connecting LCD indicator 1602 to ARDUINO
Assembly tips. The 1602 indicator usually comes from China in a “naked” form, i.e. no pins are soldered, I advise you to use double-row computer sockets from motherboards for these purposes, one pin of the socket is inserted into 1602, the second pin is behind the edge of the board, solder both pins to one pin - this increases the mechanical and electrical strength. This diagram does not indicate the backlight connection diagram; these are the next 2 pins to the right of D7. You can connect them to the 3.3V power supply on ARDUINO, you can make a smooth switching on/fading if you connect the positive pin (it is labeled as A-anode) to the ARDUINO output and control the power through this pin, this is a secondary task, for now just connect the pin And on 1602 to 3.3V on ARDUINO, and pin K 1602 to GND ARDUINO.
Now let's start actually designing the watch. We launch the ARDUINO shell on the computer. Let's try to play with 1602 to check the correct connections of the circuit. Let's go File-Examples-LiqidCrystal and select any of the files. We upload the sketch to ARDUINO and watch what happens. If instead of symbols you see black squares, tighten the trimming resistor, this is a contrast regulator (do the same if nothing is displayed at all). Information must be displayed correctly and there simply should not be any “crazy stuff”. If they appear, check the connection diagram, where it was assembled incorrectly. You can immediately see in the sketches how to access the LCD indicator and be amazed at the ease of working with it! If everything works correctly for you, let’s move directly to programming.
Let's decide that our timer will work without the delay operator as written. Therefore, we enter the following code:
#include
// Variables will change:
void setup
() {
lcd.begin(16, 2);
void loop
()
{
if (currentMillis - previousMillis >= interval) (
This code will already work but will not display anything. 1 will be added to the variable s every second. I.e. we already got the exact interval of 1 second! Now, following the algorithm, we need a limit of the variable between 0 and 59. Let's do it.
if(s>
{
}
Add this code to the program. According to the description, everything is clear - if the value of s is greater than 59, then we assign it 0 and add 1 minute to the variable m. At the moment we have a fully working second timer and an infinite (up to 32768 - the maximum value of the integer type) minute counter. Now you need to calculate the minutes in the same way. We write the following:
if (m>59) // if m values are greater than 59
{
}
Add lines to the program. It should already look like this:
int h,m,s; // variables for hours, minutes, seconds
boolean z; // variable to display the point
// connect the indicator library
#include
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup
() {
lcd.begin(16, 2);
void loop
()
{
Unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) (
// save the last time you blinked the LED
previousMillis = currentMillis;
s++; // add one, equivalent to writing s=s+1;
// seconds counting section
if (s>59) // if the value of s is greater than 59
{
s=0; // assign value 0 to variable s
m++; // add 1 to the variable m responsible for minutes
}
// minute counting section
if(m>
{
m=0; // assign value 0 to variable m
h++; // add 1 to the variable h responsible for the clock
}
In principle, everything is clear. All that remains is to process the clock. Let's do it. We add after the minute counting section:
if(h>
{
}
That's it, the watch is ready! Fill in the sketch and the clock will run as it should! I would like to draw your attention to the fact that they will count in 24-hour format. Try making a 12-hour format yourself. All that remains is to display the information on the LCD indicator. There are 2 ways to write code to display information.
- Calculate some data and immediately output it
- Calculate all the data and display it all at once.
At this point you will decide for yourself which path you will take. If you follow the first path, then you need to write the display of information immediately in the calculation sections, if you follow the second path, you write a block after all the calculations. Let's take the second path because... it is more preferable and more logical (although, to be honest, my test sketch was written along the first path). So, to transfer data to the 1602 indicator, only 2 commands are used:
lcd.setCursor(3, 0); // sets the cursor to the 3rd character of the 0th line (counting lines and characters starts from 0)
lcd.print(0); // print (print- print, learn English) 0
There is also the lcd.clear command; meaning clear screen but we don't have to use it here.
We begin to display information. Let's start with seconds (you can start with any value, do whatever is convenient for you). Set the cursor to line 0 in position 6 and display the seconds value. Why in 6th position you ask? Let's imagine the following: the clock display format is 2 characters (hours), a separator (let's say:), 2 characters (minutes), a separator (:) and, finally, seconds. We count from the zero position: 0+2+1+2+1=6. Since the counting starts from 0, we subtract one from the data (zero is also a number), we get 6-1=5. This is how long it takes to display hours and minutes with delimiters, the next position is the second position and it is equal to 5+1=6. A little confusing but I'll write the following hh:mm:ss and calculate the coordinates first starting from 0. This is how coordinates are calculated on indicators of the 16xx family. Under these conditions, the clock will be displayed in the upper left corner, you can change the location as you wish, you can even enter a variable and, by selecting it, select the display position you need. Okay, let's write these lines:
lcd.setCursor(6, 0); // sets the cursor to the 6th character of the 0th line (line counting starts from 0)
The program will look like this:
int h,m,s; // variables for hours, minutes, seconds
boolean z; // variable to display the point
// connect the indicator library
#include
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup
() {
lcd.begin(16, 2);
void loop
()
{
Unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) (
// save the last time you blinked the LED
previousMillis = currentMillis;
s++; // add one, equivalent to writing s=s+1;
// seconds counting section
if (s>59) // if the value of s is greater than 59
{
s=0; // assign value 0 to variable s
m++; // add 1 to the variable m responsible for minutes
}
// minute counting section
if (m>59) // if the value of m is greater than 59
{
m=0; // assign value 0 to variable m
h++; // add 1 to the variable h responsible for the clock
}
// hour counting section
if (h>23) // if the value of h is greater than 23
{
h=0; // assign value 0 to variable h
}
// information output section
lcd.setCursor(6, 0);
lcd.print(s); // print data from variable s
Fill in the sketch and... seconds started to appear!!! Just pay attention, when counting from 0 to 59, everything is fine, but as soon as the next minute begins, tens of seconds begin to change instead of units of seconds, i.e. The time is displayed incorrectly. Let's figure this out. We have given the program a strict position 6,0 , and it displays the data exactly at this position without overwriting what is after this position. Exit 2. Apply lcd.clear or describe the display correctly, especially since with the first option it will be quite difficult to get used to the jumping digits of seconds (later minutes and hours). Let's write a correct display handler. What conditions will there be in this processing? Let's think about it. If the seconds are less than 10, then we write their value in the 7th position (6+1=7) and in the 6th position we write 0, if greater than or equal to 10, we write in the 6th position. It's quite simple. The condition will look like this:
if (s<10) //если секунд меньше 10
{
lcd.print(0); //print 0
}
else
{
}
Paste this code instead
lcd.setCursor(6, 0); // sets the cursor to the 7th character of the 0th line (line counting starts from 0)
lcd.print(s); // print data from variable s
and we are happy with the result already obtained! Everything is displayed correctly! In addition, a separator “:” appeared before the seconds! In the same way we write a handler for minutes and hours with the corresponding cursor coordinates. It might look like this for minutes:
If (m<10)
{
lcd.setCursor(3, 0);
lcd.print(0);
lcd.setCursor(4, 0);
lcd.print(m);
}
else
{
lcd.setCursor(3, 0);
lcd.print(m);
}
and so on for hours:
If (h<10)
{
lcd.setCursor(0, 0);
lcd.print(0);
lcd.setCursor(1, 0);
lcd.print(h);
}
else
{
lcd.setCursor(0, 0);
lcd.print(h);
}
Our program will take the following form:
int h,m,s; // variables for hours, minutes, seconds
boolean z; // variable to display the point
// connect the indicator library
#include
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup
() {
lcd.begin(16, 2);
void loop
()
{
Unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) (
// save the last time you blinked the LED
previousMillis = currentMillis;
s++; // add one, equivalent to writing s=s+1;
// seconds counting section
if (s>59) // if the value of s is greater than 59
{
s=0; // assign value 0 to variable s
m++; // add 1 to the variable m responsible for minutes
}
// minute counting section
if (m>59) // if the value of m is greater than 59
{
m=0; // assign value 0 to variable m
h++; // add 1 to the variable h responsible for the clock
}
// hour counting section
if (h>23) // if the value of h is greater than 23
{
h=0; // assign value 0 to variable h
}
// information output section
// output seconds
if (s<10) //если секунд меньше 10
{
lcd.setCursor(6, 0); // cursor at position 6, line 0
lcd.print(0); //print 0
lcd.setCursor(7, 0); //cursor at position 7 of line 0
lcd.print(s); //print the value of the variable s
}
else
{
lcd.setCursor(6, 0); //otherwise the cursor is in the 6th position of the 0th line
lcd.print(s); // print the value of the variable s
}
lcd.setCursor(5, 0); // cursor at position 5, line 0
lcd.print(':'); // print separator between seconds and minutes
// output minutes
if (m<10)
{
lcd.setCursor(3, 0);
lcd.print(0);
lcd.setCursor(4, 0);
lcd.print(m);
}
else
{
lcd.setCursor(3, 0);
lcd.print(m);
}
lcd.setCursor(2, 0); // cursor at position 2 of line 0
lcd.print(':'); // print separator between minutes and hours
// output clock
if (h<10)
{
lcd.setCursor(0, 0);
lcd.print(0);
lcd.setCursor(1, 0);
lcd.print(h);
}
else
{
lcd.setCursor(0, 0);
lcd.print(h);
}
The entire code fit into just over 3 kilobytes! Most of them were consumed by the library for the LCD indicator. I would like to note right away that this code is only the body of the program; you also need to add a function for setting the time. In addition, you can add a photoresistor and brighten the display backlight. You can add an alarm input function and work with sound. You can also display the room temperature, date, etc... In general, this indicator with the appropriate sensors can turn this watch into a unique device! The only disadvantage of this device is that if there is a power outage, you will have to set the clock again. Therefore, in the near future I will describe how to work with the RTC module. When working with it, even if the power goes out, when voltage is applied, the clock will work as if nothing had happened. For a cheaper version of the clock, you can use arduino pro mini, this is the same controller only does not have a USB connector but costs several times less and is very small in size. You can also use arduino nano, the same pro but with a USB connector. Until the next lesson. Thank you all for your attention.
PS*. By the way, the procedure for displaying values can be written as a separate function and passing the necessary values to it. Try this and compare the amount of memory occupied. Always aim for the smallest volume possible.