ItsyBitsy: Simple Time, Date, and Temp
I am stewing over a clock project. It has been on my bucket list of projects to complete. I have made a couple in the past, but simple and using 7-segment displays. I have some perfect parts for new, big, analog’ish-looking, one; but before I get moving on telling time with LEDs a quick sidebar. I haven’t used a Real Time Clock with an ItsyBitsy. or CircuitPython. I have with an Arduino, so I figure it can’t be too far off. I am using the wiring and code from my last ItsyBitsy project, JoyDraw.



In the end I want to be able to set the time on the RTC, and then display the date and time onto the OLED screen. While the OLED screen will not be used in the final project, is provides a quick and easy way to check on the results. And is useful as a clock on the desk while I work on the larger piece.


IBM4 Clock Schematic


Putting it together
The wiring as shown above is pretty simple. Both devices, the RTC and SSD1306 use I2C to communicate with the IB4M. Each device is provided an address. It is important that no two devices have the same address or the data will be wrong or not there at all. As seen in the schematic below, the address of each device are written down to be sure. The address for the OLED is 0x30 and the RTC is 0x68.
Then both get power and ground. A note, when I had OLED alone with the IB, I had to add 2 x 10K pull-up resistors. When RTC was added they were no longer needed. The SCL and SDA pins are labeled on the IB board. They are in the general location as in the schematic above.

I was a bit worried about the power, but as I wasn’t using anything that sucked up a lot, all of the devices were run from the USB (power) pin on the ItsyBitsy. I still want to measure and see how much is used overall.
In the code section there is the simple, simple clock. It reads the time from the RTC and prints it out to the screen. In order to accomplish this specific libraries are required. Make sure the following are copied into the lib library on the CIRCUITPY. Time, board, adafruit_ssd1306, busio, adafruit_ds3231. Three of them are pretty self-explanatory; time, _ssd1306 and _ds3231. Of the other two, board is used for various board / pin configurations and busio deals with the I2C communication.
Once the time is read, it is printsed to the serial console. This provides a method of debugging when first trying out some new code. The statement will be removed when it comes to production time. 

Quick Addition (’cause I can)
Having some extra time to kill and since I did have a spare temp sensor with me, I wire up a BME280. It also uses I2C for communication was it was a quick addition to this little project. As one adds more I2C devices to the chain addressing must be looked at. The address of the BME is 0x76, so all of the devices in this chain are unique. Usually you can change them on the hardware by adding a resistor or in software as well.
The adafruit_bme280 library is added to the lib directory. If the local pressure at sea level is added, elevation can also be calculated.


IBM4 schemIBM4 temp







Now that I have some basics down, I am off in search of 80 WS2818 LEDs, some copper magnet wire, and an afternoon of coding. I will be back with the finish creation soon.


# ItsyBitsy RTC, OLED Clock, with Temp
# This is my first adventure into the world of both Circuitpython
# and the M4 platform. I am using the Adafuit ItsyBitsy M4.

import time
import board

import busio
import adafruit_ds3231
import adafruit_ssd1306
import adafruit_bme280

# Setup I2C
i2c = busio.I2C(board.SCL, board.SDA)

# Create instances for i2c devices:
rtc = adafruit_ds3231.DS3231(i2c)
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x76)

# change this to match your local pressure (hPa) at sea level
# Check your local airport or weather forcast
bme280.sea_level_pressure = 1016.25

# Lookup table for the days of the week.
days = ("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat")

# Below, change False to True to set the time. Once set
# change it back to False and save again.
if False:
    #                     year, mon, date, hour, min, sec, wday, yday, isdst
    t = time.struct_time((2019,  7,   4,   14,  00,  100,    4,   -1,    -1))
    # print("Setting the time to:", t)
    rtc.datetime = t

while True:
    t = rtc.datetime
    # get values for:
    Tmp = bme280.temperature
    Prs = bme280.pressure
    Hmd = bme280.humidity
    Alt = bme280.altitude   # calculated by adafruit library from pressure

    # Print statements below are for debugging to the serial console
    # remove or comment out when done.
    # print(t)
    print("The date is {} {}/{}/{}".format(days[int(t.tm_wday)],
          t.tm_mday, t.tm_mon, t.tm_year))
    print("At the tone the time will be {}:{:02}:{:02}"
          .format(t.tm_hour, t.tm_min, t.tm_sec))
    print("\nTemperature: %0.1f C" % Tmp)
    #  print("\nTemp degrees %0.1f F" % bme280.temperature * 1.8 + 32)
    print("Humidity: %0.1f %%" % Hmd)
    print("Pressure: %0.1f hPa" % Prs)
    print("Altitude = %0.2f meters" % Alt)

    display.text("wrightMac time is:", 20, 10, 1)
    display.text("{} {}/{}/{}".format(days[int(t.tm_wday)],
                 t.tm_mday, t.tm_mon, t.tm_year), 20, 45, 1)
    display.text("{}:{:02}".format(t.tm_hour, t.tm_min), 50, 28, 1)
    #  display.text("{}:{:02}:{:02}".format(t.tm_hour, t.tm_min,
    #            t.tm_sec), 30, 40, 1)
    display.text("the temp", 20, 10, 1)
    display.text("Temperature: %0.1f C" % Tmp, 30, 30,1)

    time.sleep(15)  # wait a second
    #  need to work on this as screen is refreshed ever¥ second
    #  and the blinking is not cool
    #  if it isn't cleared the pixels pile up

© 2019, wrightmac. All rights reserved.

No tags for this post.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.