Numitron clock & thermometer using Microcontroller atmega48

I really like nixie and numitron clocks, but I never worked with them before. So I decided to give it a go. I choose numitrons because of 2 reasons: first of all nixies need a higher voltage than numitrons to work. Nixies need around 170V DC and numitrons only 4,5V so they are safer to work with and don’t need a special powersupply.

Numitron clockThe timekeeping is done by a ds1307 I2C realtime clock and the temperature is measured with a DS18B20 1wire temperaturesensor.

An Atmega48 is used to proces the data and to drive the segments in the numitrons. I used Bascom to write the code and a MyAvr MK2 Programmer to stuff it into the microcontroller. You can find a free Bascom demo here. The only limitation of the demo version is that you can only compile 4Kbytes of code (but the atmega48 has only 4Kbytes so that will work fine.)
I added this instructable to the microcontroller contest, so give it a vote if you like it.

The DS1307 realtime clock

The timekeeping will be done by a DS1307 IC. This is a handy little IC, because it not only keeps track of time but also of the date and the day of the week.
For¬†this little project we‚Äôll only use it to keep track of the time. Therefore it needs a 32.768kHz quartz crystal connected between pins 1 and 2. We can¬†also add a battery with + to pin 3 and ‚Äď to¬†pin 4. This enables the IC to keep working when the mainpower is switched off. If you don‚Äôt want to use this feature, you can just connect pin 3 to pin 4 and everything will work fine.
Pin 5 and pin 6 will be used to transfer the data to our microprocessor. They should be connected to the SCL and SDA pins on your microprocessor. These lines need to be pulled high by a 4K7 pullup resistor.

Bascom makes working with I2C devices easy. You only need to know 4 commands:

  1. I2cstart: This commant will startup I2c communications
  2. I2cstop: This command will stop I2c communications
  3. I2crbyte var: This command¬†reads a byte from the¬†device and stores it in ‚Äėvar‚Äô
  4. I2cwbyte¬†var: This command writes the variable ‚Äėvar‚Äô to the device

Using the write or read command is not enough, we will also have to tell the device whether we want to write to it or read from it. We do this by using the right address. These addresses can be found in the datasheet. The write-address for the DS1307 is D0H and the read-address D1H (the H behind it tell us that these are hexadecimal figures).
The DS1307 sends and wants to receive data in BCD format. This is a variation on binary for diplays where every digit is represented by four bits. More about that here. Luckily converting from BCD to decimal and visa versa is very easy in Bascom.

  • var = Makebcd(var) will convert decimal, hex and¬†binary into BCD
  • var = Makedec(var) will convert hex, binary and BCD into decimal

The data is stored on the IC in register. You can imagine them as those oldfashioned filingcabinets. Each drawer has its number and contains some info:
00H Seconds
01H Minutes
02H Hours
03H Day
04H Date                                               The H tell us that these are hexadecimal figures.
05H Month
06H Year
07H Control
08H to 3FH Ram
If we want to read or store some data we’ll first have to tell the device in which drawer we want to be. We can do this by writing the hex code for that drawer to the device. The device then will grant us acces to that drawer. After you write or read something from or to this register the device will automatically jump to the next one. So there is no need to send the location every time
Now lets put this in code:
For this code you will need to dim hours as byte, minutes as byte and seconds as byte.
First, we will set the clock:

Seconds = Makebcd(Seconds)

We convert our variables into BCD format

Minutes = Makebcd(Minutes)
Hours = Makebcd(Hours)

 We reset bit 6 of the hoursbyte to make sure that our Clock runs in 24h modus. If bit 6 is 1 then the clock runs in 12h modus and bit 5 will then contain the AM/PM data.

I2cwbyte &HD0

We tell the device that we want to write a byte

2cwbyte &H00

We start at the register for seconds hex 00  


Seconds                                                    Adding seconds

I2cwbyte Minutes

Adding minutes

I2cwbyte Hours

Adding hours


Now our clock is set! Lets read from it now.

I2cwbyte &HD0

                                                       We tell the device that we want to write a byte.

I2cwbyte &H00

                                                      We ask the device to go to the seconds register.



I2cwbyte &HD1

                                                       We tell the device that we want to read bytes.

I2crbyte Seconds , Ack

                                       We read the data and acknowledge that we want to read the next byte too.                  

I2crbyte Minutes , Ack
I2crbyte Hours , Nack

                                            We don’t ackowledge here so the device knows that we are done reading.

Hours = Hours And &B00111111

                        We remove bits 6 and 7 as they contain other data. If you are in 12h modus, then you need to remove bit 5 too

Hours = Makedec(Hours)

                                    We convert back to decimal format.

Minutes = Makedec(minutes)
Seconds = Makedec(seconds)

Now we know what time it is.
In the next step we will take a closer look at the DS18B20.
The DS18B20 is a so-called 1wire device. Sadly, as with many things in life, this description was made up by some clever salesmen as we need at least two wires for it and in our case even three.
There are two ways to set up a DS18B20:

  1. With parasite power
  2. With external supply

To know more about these options, just check the datasheet.
The data line DQ will need a 4K7 pullup resistor and can be connected to the vast majority of pins on your microcontroller.
Communication with a 1wire device is, again, not very difficult in Bascom. There are some commands but we only need three of them for this project.

  1. 1wreset: This commands resets the communication
  2. 1wwrite var: This command writes ‚Äėvar‚Äô to the device
  3. 1wread var: This command reads from the device into ‚Äėvar‚Äô

We also need to setup the 1wire bus with the following code: config 1wire = pinX.y Where X is the name of the port and y the number of the pin.
Lets try to put this all into code now:
We will use the DS18B20 in 12bit mode (default setting) so every bit corresponds with 0.0625degr C or to put it easier: we will have to divide the result by 16 to get the temperature.
For this code you will need to dim tempdata(9) as byte and temperature as integer.

config 1wire = portd.0

                  This tells the microcontroller whereto look for the device


resets and starts the communication

1wwrite &HCC

This skips transmitting the unique ROM code for the device. This code is needed when there are more devices on the same wire but we have only one so we can skip it.

1wwrite &H44

 Starts the A/D convertion in the sensor and stores the data into the scratchpad

waitms 750

 The convertion in 10bit mode can take upto 750ms so we wait 750ms before we start to read the scratchpad.

1wwrite &HCC
1wwrite &HBE

Tells the device that we want to read the scratchpad.

Tempdata(1) = 1wread(9)

We read 9 bytes into tempdata(), starting from tempdata(1).

If tempdata(9) = Crc8(tempdata(1) , 8) Then

This checks the validity of the data and

Temperature = Makeint(tempdata(1) , tempdata(2))

combines the 2 first bytes into an

Temperature = temperature / 16

By dividing this integer by 16 we have our temperature in degr C.

End If

We have our sensor working now.
In the next step we will talk about the buttons.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top