ATtiny Clock

by 陳亮 in Circuits > Arduino

1468 Views, 18 Favorites, 0 Comments

ATtiny Clock

DSCN1115.JPG
DSCN1115副本.JPG

This instructables show how to use 3 components to build a simple digital clock.

This is a simplified design from my previous project, ATtiny Watch Core. This clock design is much simpler than previous watch design. The clock is put on the desk and direct powered by USB, so it doesn't need battery power management. It keeps the time by RTC, so no UI required to adjust time manually. And this time I use Digispark micro USB dev board, so no extract MCU programmer is required.

This project only requires a tiny dev board, a RTC module and an OLED display module, it is very suitable for a beginner to make it.

Supplies

DSCN1110.JPG
  1. Digispark micro USB dev board
  2. DS3231 RTC module (with backup battery)
  3. 0.91 inch 128x32 SS1306 OLED display module
  4. breadboard and breadboard wires

What Is Digispark?

Digispark is a 2012 kickstarter project. The original design is USB port A interface, then micro USB version appear. Now you may found clone version with USB Type C in the market. The original project is over 10 years and their web site already gone. But time proved it is a good tiny design, it still a good dev board for many simple projects.

Ref.:

https://www.kickstarter.com/projects/digistump/digispark-the-tiny-arduino-enabled-usb-dev-board

https://www.instructables.com/Digispark-DIY-The-smallest-USB-Arduino/

What Is RTC?

real-time clock (RTC) is an electronic device (most often in the form of an integrated circuit) that measures the passage of time.
RTCs are present in almost any electronic device which needs to keep accurate time of day.

This project use a DS3231 RTC module, thanks to Raspberry Pi, it is a common RTC module in electronics hobbyist market.

Note:

RTC requires a backup battery to retain the time, but I found some module not included a battery. If no backup battery, RTC cannot do the job in most case. So please make sure your RTC module has a backup battery before using it.

Digispark Pins

DSCN1111.JPG

This project use breadboard for simple wire connection. You need soldering some Digispark pin headers before that.

The power pins have 3 pins, there are 5V, GND and VIN. We need 5V and GND pins to power the other 2 modules and should leave VIN alone not soldered.

The GPIO pins are from PB0 to PB5, you can all soldering it. However, since PB3 and PB4 is actually used for USB communication, so I suggest not soldering it. It may introduce upload program problems, if it connect to something that will pull-up or pulldown the pins.

In this project, only 5V, GND, PB0(I2C SDA), PB2(I2C SCL) four pins are used.

Simple Wire Connection

DSCN1112.JPG

Both RTC and OLED module are communicate with I2C interface, so the connection is relatively simple.

Here are the connection summary:

Digispark DS3231 RTC SSD1306 OLED
========= ========== ============
5V -> +ve -> VCC
GND -> -ve -> GND
PB0(SDA) -> D -> SDA
PB2(SCL) -> C -> SCL


Simple Program Design

螢幕截圖 2024-10-10 下午6.05.55.png

The program design is also very simple, it read the time data from RTC and then display to OLED and loop forever.

Software Preparation

螢幕截圖 2024-10-10 下午8.24.15.png

Arduino IDE

Download and install Arduino IDE latest 1.x version if not yet:

https://www.arduino.cc/en/software

ATTinyCore

Follow installation step to add ATTinyCore support if not yet:

https://github.com/SpenceKonde/ATTinyCore/blob/v2.0.0-devThis-is-the-head-submit-PRs-against-this/Installation.md

DS3231

Open Arduino IDE Library Manager by selecting "Tools" menu -> "Manager Libraries...". Search "DS3231" and press "install" button.


Download Source Code

Please download the source code at Github:

https://github.com/moononournation/ProjectMINTIA

Note:

This project is part of "Project MINTIA", further update will coming soon...

Initial RTC Time

螢幕截圖 2024-10-10 下午10.02.31.png
螢幕截圖 2024-10-10 下午9.00.40.png

This simple project does not have UI to adjust the time. It should not have problems since the RTC can keep the time.

But we still need to initial the RTC time at the first time. We can use a simple program and simply use the compile time to set the RTC. The compiler can pass "__date__" and "__time__" as macro to the code, but if you want the day of week value correct, we need the UNIX timestamp to direct set epoch value. I used the jerabaul29's sample marco code to do the job.

Here are the initial steps:

  1. Open DS3231SetTime.ino in Arduino IDE
  2. Select Tools menu -> Board: "ATtiny85 (Micronucleus / DigiSpark)"
  3. Press Upload button
  4. Wait compile finish and ask for plugin the Digispark
  5. Plugin the Digispark
  6. Wait upload finish
  7. The OLED do not have any display yet, please do not unplug until upload the main program

Note:

There will have few seconds time gap between compile time and the RTC actual set, it should be ok. But if you unplug and replug the Digispark the RTC will set again and the time gap will be much larger. So please do not unplug until upload the main program.

Ref.:

https://github.com/jerabaul29/Compile_time_Cpp_UNIX_timestamp

Upload Main Program

  1. Open ATtinyClock.ino in Arduino IDE
  2. Select Tools menu -> Board: "ATtiny85 (Micronucleus / DigiSpark)"
  3. Press Upload button
  4. Wait compile finish and ask for plugin the Digispark
  5. Plugin the Digispark
  6. Wait upload finish
  7. The OLED will show the current date and time

Enjoy!

DSCN1114.JPG

Congratulations on just completing this Arduino beginner project! Keep going!

Advanced Level: Custom Font

ASCII_Table_viewable.png
font_3x.png

ATtiny85 only have 8 KB flash for the program, so it is limited room for the font.

For the small size font, 8 pixels height, it covered all viewable ASCII characters from SPACE(32) to "~"(126).

For the 3x size font, 24 pixels height, only characters 0-9 for display the time.

You can generate your custom 3x font by the ImageMagick command:

magick -depth 1 -font SpotMono-Bold -pointsize 30 label:"_0123456789_" -crop 180x24+18+13 -flip -rotate 90 font_3x.xbm

Then overwrite the char array details to the font_3x.h header file.

Ref.:

https://en.wikipedia.org/wiki/ASCII

Advanced Level: Parameter

SSD1306 OLED display have many different size, you can change the parameter in ssd1306.h header file change to fit your model:

#define SCREEN_128X32 // SCREEN128X64, SCREEN128X32, SCREEN64X48 or SCREED64X32 (default)