Skip to content
Guide to the Arduino sketch for the Tronixlabs QDSP Clock Kit

Internals Guide - QDSP Clock Kit 10900

In this guide we'll explain the clock's circuit, review the Arduino sketch that controls your QDSP Clock and how to upload a new sketch. If you're looking for the construction or other guides for this kit, see the links from the product page.

So you have successfully constructed your QDSP Clock Kit, and are interested in how it works. We're going to assume you have some knowledge about the Arduino development platform or other similar systems, and some basic electronics. 

How the circuit works

As your QDSP Clock is a microcontroller-based project, there are two areas to take into consideration when understanding how it works – the hardware side (“the circuit”) and the code/Arduino “sketch” that the microcontroller follows to operate. 

You may find it useful to have the schematic open while reading this - it can be downloaded from the product page.

Let’s start at the top-left, with the power input. Power is received into the clock via a 2.1mm DC socket, which has two smoothing capacitors C2 and C3 across the input. These are fitted in case of variances in input power supply.

QDSP Clock Kit from Tronixlabs Australia

The 5 V is then distributed to the microcontroller, display driver, real-time clock ICs as well as the buttons. We tried a few revisions of the clock with various power sources. USB A sockets, DC sockets with linear regulators to allow for use with plug packs and so on.

For the production revision (three!) we went with the 5V DC socket and included a USB A to DC socket cable with the kit. This allowed us to reduce the part count, a source of heat inside the clock from linear regulators, and we're confident that customers would power their clocks from a USB socked from a device on their desk, or have a spare simple AC to USB adaptor available for use.

Clock control is via the Microchip ATmega328P microcontroller, U2. This is configured to run with a CPU speed of 16 MHz, using the external crystal Y1 and matching load capacitors C5 and C6. The reset pin of the microcontroller is held high (to 5 V) via R2, as we don’t have a reset button.  

QDSP Clock Kit from Tronixlabs Australia

Finally, a bypass capacitor C1 is used across the power and GND to the microcontroller. AVR and Arduino enthusiasts should recognise this circuit, it’s a bare-bones Arduino-compatible. The firmware can be uploaded using the ICSP pins (J3) using one of various methods that are explained in the last section of this page.

QDSP Clock Kit from Tronixlabs Australia

The real-time clock IC is a DS1307-compatible unit which is a common and easily-understood part. It communicates with the microcontroller using the I2C data bus, and has 2k2 pull-up resistors (R7 and R8) to help keep the bus in an active-high state as required.

QDSP Clock Kit from Tronixlabs Australia

The RTC IC also sends a signal back via it’s square-wave output pin (SQW) to the microcontroller every second. R9 pulls the signal to high for a clean square wave. This triggers an interrupt that is used to start the process of refreshing the clock display. The interrupt is triggered on a falling signal, that is when the SQW changes from high to low. 

More about that later. In the same way as a microcontroller, the RTC IC also requires a crystal for accurate timing. In this example we use the little 32.768 kHz unit Y2. The RTC IC keeps time when the main power is disconnected by using power from a CR2032 battery in the holder BT1. Finally, a bypass capacitor C7 is used across the power and GND to the RTC IC.

The three buttons for the user-interface, S1~S3 are wired in a pull-down configuration, that is, the 10k0 resistors (R4~R6) keep the line to the respective input pins on the microcontroller low when the buttons are not pressed. When a button is pressed, 5 V is fed to the matching input pin in the microcontroller.  

QDSP Clock Kit from Tronixlabs Australia
Audio alerts are made with the 5 V buzzer connected to the microcontroller. It requires a current-limiting resistor R3. We have used a buzzer instead of a piezo transducer, as we only need to apply power to make it beep. 

QDSP Clock Kit from Tronixlabs Australia
The LED displays are controlled by the MAX7219-compatible driver IC U1. This is connected to the microcontroller via the SPI data bus interface. R1 is used to set the peak segment current for the displays.

QDSP Clock Kit from Tronixlabs Australia
The display driver IC is connected to the four cathodes and eight anodes of each LED display via the vertically-mounted display PCB, whose schematic is shown below:

QDSP Clock Kit from Tronixlabs Australia

Finally, a bypass capacitor C4 is used across the power and GND to the display driver IC.

How the sketch (code) works

Your clock is based on an Arduino Uno-compatible circuit, and we’ve used the Arduino IDE to work with the “sketch” (code). You can download the sketch and required Arduino libraries from the product page., and open it in the Arduino IDE or any text editor (Notepad++ is a good one, visit https://notepad-plus-plus.org).

Please have the sketch open while reading this explanation. This guide was written for sketch version 3.5. You can learn more about the libraries from:

If you are new to the world of Arduino, consider the book “Arduino Workshop”, 2nd Edition by John Boxall, published by No Starch Press. Available from all good booksellers. Anyhow, enough plugging books. 

We’ll first go through the functions in the sketch, then review how they work together for clock operation. To help located particular parts of the sketch we will refer to the line number, however these may vary a little if you are modifying the code or spacing within the code. 

If you are interested in modifying the code, you will need to add each library in turn to the Arduino IDE, using the Sketch>Include Library>Add ZIP Library … function: 

QDSP Clock Kit from Tronixlabs Australia
You will need to edit one file in the "LEDcontrol" Arduino library. Open LedControl.h in your text editor and change the class from private to public on line 62. Then, save and close the file, and open the Arduino IDE.

QDSP Clock Kit from Tronixlabs Australia

So now back to the sketch. From lines one to 25, we include the required libraries, and define some constants and variables from lines 27 to 54. These are used to store various statuses of operation.

We have also created an array of byte variables (byte characters[]) that are used to represent letters and numbers, which are sent to the display driver IC. Each bit of a byte represents a segment of the digit – A through F and the decimal point, and they’re arranged as shown below:

QDSP Clock Kit from Tronixlabs Australia
The structure of each byte is PABCDEFG, where P is the decimal point. So if you wanted to display, for example, the letter H, you would send B00110111. Or B10110111 for H and the decimal point. This is why we have the character array to save time. More on this shortly.

Between lines 98 and 1217 are the custom functions required for operation, which we’ll run through in order of apperance. 

void sayHello() displays the friendly “Hello”. This uses the function spiTransfer from the LedControl library used to control the MAX7219-compatible display driver IC via the SPI bus. The first parameter, 0 is for the display number. As we only have one display driver IC this is always 0. The next parameter is the digit position on the display, from 1 to 8. The final parameter is the data to display in the particular digit.

You can either pass an element from the array characters[], or a byte itself. We’ve used the array, and sent three decimal points after HELLO for visual effect, each represented by B10000000 (only turn on the decimal point. 
From lines 113  to 290 we have various functions that simply display a word or group of characters on the display, such as days of the week or words for the menu options. 

At line 292 we have void retrieveEEPROMdata() which retrieves some information the clock stores in the microcontroller’s internal EEPROM. This is called after every power reset or power-up. This is used to store the 12/24 hour status, alarm information, screen brightness and the display type. 

At line 308 we have void notDelay() which is a delay function that doesn’t use the Arduino’s delay() function. This is necessary as we can’t use delay() inside functions that are called by an interrupt.

At line 316 we have the function void beep() that simply sounds the buzzer for 50 milliseconds.

Line 325 is the function void flagUpdateTime(). This is used to change a flag variable displayNow to 1. This function is called by an interrupt when the 1 Hz signal from the real-time clock IC goes from high to low, which means the time display needs to be updated. More on this when we examine void loop().

At line 331 we have void displayTime(). This function displays the current time on the display using the selected format set by the user in the main menu. It also sounds the alarm if the alarm is on and the current hours and minutes match the alarm settings. It first clears the display at 335, then gets the current time from the real-time clock IC at 338, and the alarm settings from the EEPROM from 341. If the alarm is set to on and it’s time, the alarm sounds due to the code at 345. 

The alarm sounds is three beeps repeating. As the clock display is updated every second, the alarm will “beep beep beep” every second until the user turns it off by pressing button 1. The user will then need to turn the alarm back on if required the next day, etc. 

Before displaying the time, we check if the user wants 12 or 24 hour time at 353 then sets the AMPM flag if required. PM is then reflected by displaying the decimal point in the second digit, for example at 380.
The alarm status is also shown by showing the decimal point in the last digit if the alarm is on, for example at 386.
The user can set the format for the time display in the main menu, which is set in EEPROM and retrieved into the variable displayType. This is tested from 368 and the display type used as required with the switch…case functions down to line 432. 

To make things easier when controlling digits on the display, we use display.setDigit() again. The first parameter is the display number (0), the second is the digit position (1~8), the third is the digit to display (0~9) and the fourth is the decimal point control (1 on, 0 off). Once the time has been displayed, the flag variable displayNow is set back to zero. Recall this is set to 1 in void flagUpdateTime(). 

Our next function at line 437, version(), simple displays the sketch version number. If we release updated versions, this will be updated accordingly and published on the clock’s store page.

Next we have the function called from the clock menu to set the time, void setHHMM() at line 452. It first displays the current hours and minutes on the display, and loops around monitoring the three buttons:
  • If button 1 is pressed (at line 472) the hours are incremented. 
  • If button 2 is pressed (at line 478) the minutes are incremented.
  • If button 3 ("select") is pressed, the loop ends and the function moves on to line 491 where the hours and minues are set in the real-time clock IC using the RTC library functions setHours and setMinutes.
At line 499 we have the function void zeroSeconds(), which simply sets the value for seconds back to zero when the user presses button 3. This function is available from the main menu and used for more accurate setting of the time. 

The function void set1224() from line 527 is called from the main menu, and allows the user to change the clock between 12 and 24 hour time. It shows “12  24” on the display, with decimal points with the 12. By pressing buttons 1 or 2, the display shifts the decimal points between the 12 and 24 respectively. When the user presses button 3, the choice is locked in and written to the EEPROM at line 568.

The next function, void setDate() from line 573 allows the user to set the year, month and date. First we set the year, from line 585, where the do loop waits for the user to press button 1 to decrease the year, 2 to increase the year, and 3 to set the year. Note  the checks at 601 and 608 to ensure the year can’t fall lower than 25 or higher than 99. 

If you’re reading this in the year 2024 or 2100, we apologise for the inconvenience. 

At line 627 the process repeats in the same way to set the month. From 668 the process repeats for the date. As different months contain different numbers of days, we have to set the maximum number of possible days in a month with the switch… case function at 637. Then, we need to check if the month set is a February, and a leap year at 688. If so, the maximum number of days is set to 29.

Next, we need to check that the current date requested isn’t greater than the maximum at 698.

Finally, the user can loop around the date with buttons 1 and 2, and set it with 3 from line 713 to 745. After this, the new date is set into the real-time clock IC and current date variables.

The function setDisplayType() from line 751 is called from the main menu and gives the user three types of time display formats to choose from. Via the do loop from line 770 the user can press button 1 to scroll through the three options, then press button 3 to make their choice, which is then written to the EEPROM. 

From line 844 there’s void setAlarm(), which is called from the main menu and allows the user to set the alarm time. Alarms are daily and set in 24 hour time. The process is the same as setting the time, after writing the alarm time to EEPROM at 882, the alarm time is shown in the display for a moment followed by a beep. 

The alarm is turned on and off with the function void alarmOnOff() from line 905. This is called by an interrupt triggered by pressing button one. First we check the status of the retrieved alarm on/off variable from the EEPROM at 910, as the alarm status doesn’t ship with a valid setting. Then, based on the alarm status, it is set to on or off with the switch…case function from 912 to 940. After this, the status is set and written to the EEPROM at line 941.

The user can set the brightness of the display with the function void setBrightness() from line 946. As with void setAlarm(), we first create a value for brightness if one doesn’t exist at tline 952. After that, the user can change the brightness up or down with buttons 1 or 2 between sixteen levels, and lock it in with button 3. 

The brightness value is sent to the display driver IC with the function display.setIntensity(). The first parameter is the driver IC number (0), the second is the brightness value which falls between zero and 15). Once the brightness it set, the value is also saved to EEPROM for retrieval after a reset at line 995. 

Moving on. At line 1000 we have the function void displayDateInfo(), which is called when the user presses button 2 during time display. It simply displays the day of the week, then the complete date.

Instead of saving and retrieving the day of week from the real-time clock IC, we instead calculate the day using “Zeller’s Formula” from line 1018. You can learn more about this from https://en.wikipedia.org/wiki/Zeller's_congruence. 

Finally we arrive at void mainMenu() at line 1051, which is called when the user presses button 3 from the clock display mode. This function monitors all the buttons while in a large do loop. The loop stays active until the flag variable quitMenu is set to true. 

The user can press 1 or 2 to scroll through the menu options, and 3 to select the menu option shown on the display. A variable int menuPointer is used to track which menu option is currently selected (0 to 8) and incremented or decremented when the user presses button 1 or 2 respectively. 

Each time that button 1 or 2 is pressed, the resulting menu option is displayed via a switch…case function, as shown at lines 1078 and 1121. Once the user presses 3 to select their option, another switch…case function at 1161 calls the required function. Once that function has completed, the user is returned back to this main menu function. 

If the user selects the menu option “return”, the flag variable quitMenu is set to true and they’re dropped out of the main menu function and returned to the main void loop at line 1269.

Our last function is void imAlive(). This simply toggles the digital output for Arduino D13 three times. We use this here at Tronixlabs to confirm programming the sketch to the microcontroller has been successful during kitting. 

The programmer we use to upload the sketch to the microcontroller has an LED connected to D13, so after upload the sketch to the new microcontroller, we see the LED blink three times which confirms the process worked. 

As with every Arduino sketch, we have void setup() at line 1219. In order, this configures the required GPIO pins as inputs or outputs as necessary; blinks the LED on our programmer with void imAlive(); starts the real-time clock IC library and then forces it to start operation. 

At line 1241 we turn on the 1 Hz output from the real-time clock IC. This is connected to an interrupt which, when triggered (once per second), runs the void flagUpdateTime() function – which sets the variable displayNow to 1. This will be used in void loop(). 

The interrupt mentioned previously is configured at line 1247. Following this are three commands to setup the display driver IC and blank the display.  Following this, we retreive user data (alarm time, screen brightness, etc) from the microcontroller’s EEPROM at 1259. 

Finally, we say “hello” and clear the display for operation.

QDSP Clock Kit from Tronixlabs Australia

Now on to void loop().

Once the clock has processed the instructions in void setup(), it will continually follow the instructions in void loop() until the power is removed. The main job is to, unsurprisingly, show the time. We don’t want the display to flicker unnecessarily – and some display options involve seconds, so we need the time display to be updated once per second. 

This is achieved by the interrupt which is triggered once per second by the 1 Hz output from the real-time clock IC. When this happens, the variable displayNow is set to 1.

So, in line 1275, we check if displayNow has a value of 1, and if, the time is displayed using the function void displayTime(). When that function is finished, it sets the value of displayNow back to zero – ready for the next updated caused by the 1 Hz output. 

Apart from organising the time to be displayed, void loop() also manages monitoring the three buttons. 
At 1284 it checks if button 3 ("select") has been pressed (for the menu). If so, interrupts are deactivated (as we don’t want the time to be displayed while the user is in the menu system), and then the main menu function is called.

After the user has finished in the main menu, the code returns to line 1291 where interrupts are re-activated so the time can be displayed once more. This same process is repeated for buttons 2 and 1, at lines 1294 and 1303 respectively. 

Please note that any function called from void loop() must first disable interrupts, then enable interrupts when finished before returning to void loop(), otherwise the time display will take over whatever you're doing every second.

Therefore, you should use this structure:

    // deactivate interrupts to stop time being displayed
    detachInterrupt(digitalPinToInterrupt(SQW));
    // call function ...........
    functionToCall(); // insert your own function to run in this line
    // enable interrupt for time display - DS1307 SQW signal to Arduino D3
    attachInterrupt(1, flagUpdateTime, FALLING);

At this point we’d like to note that different people write code or Arduino sketches in various ways. We have written this one to be quite verbose, to make it easier for more people to understand. Those of you with more experience or knowledge in embedded software development are certainly free to write their own version, and upload it to the microcontroller in the clock circuit.

How to upload a new sketch (code) 

Although the clock is based on an Arduino-compatible circuit - you can't just plug in a USB cable and click "upload" in the Arduino IDE. Instead you can choose from a few different methods of getting your own sketch into the clock.

Please note that we supply the microcontroller with the kit pre-programmed, so you don’t need to do this for the first time before using the clock. However if you’re wanting to modify the clock’s operation in some way, then you will need a method of uploading the compiled sketch to the microcontroller.

Method One – IC/Uno swap
If you have an Arduino Uno or compatible board with an ATmega328P-series microcontroller, you could remove the MCU from the Uno and replace it with the one from your clock. Then upload the clock sketch to the Uno board as normal, then put the MCU back into your clock and the Uno’s MCU back into the Uno board. 

We have flashed the clock’s MCU with the Arduino bootloader along with the firmware to allow this process.
We don’t recommend this as a solution, however if you don’t have any other choice it is an option if you’re very careful with the removal and insertion of the microcontrollers. 

Method Two – Uno as a programmer
You can use an Arduino Uno or compatible board as an AVR programmer. Along with the Arduino board and a USB cable, you'll also need six male to female jumper wires. This is explained on the Arduino website at https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoISP/.

Method Three – Using a USBasp In-circuit Programmer
This is the preferred method of uploading firmware to your clock. It’s inexpensive, simple and doesn’t require pulling or inserting microcontrollers. 

You will need a USBasp programmer, such as Tronixlabs part number 10289. We’re also assuming you have installed the Arduino IDE on your host computer. So, open the IDE, and open the sketch you wish to upload. 
Connect your USBasp programmer to your clock PCB with the six to ten pin adaptor board as shown below:

USBasp from Tronixlabs Australia
The board with the six pin socket must be connected a certain way. Line it up so the side labelled “MISO SCK RST” is facing out, as shown below:

USBasp from Tronixlabs Australia
Next, connect the USBasp to your computer. Windows PC users may need to check that the USBasp appears in the Device Manager, e.g.:

QDSP clock from Tronixlabs Australia
Our (limited) experience with MacOS computers is that the USBasp installs without any user intervention. Next, open the Arduino ID software and the clock’s sketch.

(Windows users only) Open the Device Manager and determine which USB port the USBasp programmer is using. Next, open the Tools menu in the Arduino IDE and set the board to “Arduino Uno”, the programmer to “USBasp” and Windows users also need to set the “Port” to the required COM:, for example:

QDSP Clock Kit from Tronixlabs Australia
The same is required for MacOS, except the port, for example:

QDSP Clock Kit from Tronixlabs Australia
Now you can upload your sketch. Select “Upload Using Programmer” from the “Sketch” menu. The progress of the sketch compilation and processing for upload will display as normal in the IDE’s Output window, and after a few moments the flashing and checking process will be displayed.

Once completed your new code will (hopefully!) run on the clock as you expect. If you have any questions, let us know via email - support@tronixlabs.com.

And there you have it. Once again, we’d like to thank you for choosing a kit from Tronixlabs, and wish you many years of interesting and useful timekeeping. 

Where to from here? 

If you're interested in the world of microcontrollers - consider reading one of John's books published by No Starch Press.

And there’s plenty more to learn by building, so check out our other kits, all from the tronixlabs.com website.

Previous article Assembly Instructions - Numitron Clock One Kit 10100
Next article User Instructions - QDSP Clock Kit 10090