Codebug & Raspberry Pi Clock

Introduction

You can create some amazing projects, by controlling your CodeBug with Python using the Raspberry Pi’s I2C port. This guide will show you how to create a scrolling digital CodeBug clock.

Make sure you have followed the ‘RASPBERRY PI CONTROLLED CODEBUG WITH I2C’ guide and have your CodeBug loaded with the I2C tethered mode program and connected your CodeBug in the correct position on the Raspberry Pi’s GPIO before trying this activity

You will need
CodeBug CodeBug
Micro USB cable
Computer
Raspberry Pi

1 Question
  • Can I use an app on raspberry pi to code CodeBug without being connected to the Internet? Werner @ Oct. 30, 2015, 9:50 a.m.


    This is not currently possible, however you can control CodeBug using Python or Scratch without being connected to the Internet. Thomas Macpherson-Pope @ Oct. 30, 2015, 10:05 a.m.

    Answer this question:

  • Ask a new question:

Writing the Python

Your Python program needs to get the time and date from the Raspberry Pi and scroll it on CodeBug’s display.

Open a Terminal and start a new Python program by typing:

nano ~/codebug_i2c_clock.py

Enter the following code into the file and save it:

import codebug_i2c_tether

import datetime

import time

# make a connection with CodeBug

cb = codebug_i2c_tether.CodeBug()

cb.open()

# function for scrolling messages on CodeBug’s display

def scroll_message(message):

    length = len(message)

    for i in range(0,length*-5,-1):

        cb.write_text(i, 0, message, direction="right")

        time.sleep(.15)

while True:

    # get the time and scroll it

    time_str = datetime.datetime.now().strftime("%H:%M:%S")

    scroll_message(" Time " + time_str)

    # get the date and scroll it

    date_str = datetime.datetime.now().strftime("%d/%m/%Y")

    scroll_message(" Date " + date_str)

Test your clock by entering the following command into a Terminal:

python3 ~/codebug_i2c_clock.py

Exit the program by pressing Ctrl C.


3 Questions
  • does the program save itself or do I need to copy paste it in every time thomas heitzman @ Feb. 3, 2016, 2:01 p.m.


    You can use nano or leafpad to save the file. Thomas Macpherson-Pope @ Feb. 4, 2016, 9:36 a.m.

    Answer this question:

  • On windows and on usb port how feasible? Frankó Zsolt @ June 28, 2016, 1:25 p.m.


    You will have to use the USB tethered module instead of the I2C one, instructions here: http://www.codebug.org.uk/learn/activity/66/tethering-codebug-with-python/ and http://codebug-tether.readthedocs.io/en/latest/installation.html#install-codebug-tether-on-windows otherwise, shouldn't be any problem :) Thomas Macpherson-Pope @ July 1, 2016, 1:38 p.m.
    • please, write a working code this program codebug_tether transcripts, i can't do it (sorry for my english) — Frankó Zsolt @ July 1, 2016, 7:59 p.m.


    Answer this question:

  • I get an error at cb.write_text(i, 0, message, direction="right") That's because write_text was removed in the latest versions. You need to use draw_sprite with a StringSprite instead. — veryalien @ Nov. 6, 2016, 6:49 p.m.


    Answer this question:

  • Ask a new question:

Changing timezones

You may find that your CodeBug is displaying the wrong time, this means you need to change the timezone on your Raspberry Pi or update the time by being connected to the Internet. To change timezones open a terminal and type:

sudo raspi-config

Use your arrow keys to navigate through the list and select Internationalisation Options then select Change Timezone and select your region and closest large city.


  • Ask a new question:

Running automatically

Run your clock automatically whenever you turn on your Raspberry Pi (even if you don’t log in)!

To do this you need to edit a system file with this command:

sudo nano /etc/rc.local

Enter the following line directly above exit 0 line:

python3 /home/pi/codebug_i2c_clock.py &

Now restart your Raspberry Pi and you will see that your clock runs before you log in.

If you need to stop the clock running, type:

ps aux

And find the process that has python3 /home/pi/codebug_i2c_clock.py in the end column, and take a note of its process id (from the second column). Now run:

sudo kill -9 your-process-id

e.g:

sudo kill -9 2887

To remove your clock simply edit the rc.local file and remove the line you inserted:

sudo nano /etc/rc.local

  • Ask a new question:

What next?

See if you can expand the functionality of your clock, such as an alarm system. See what other information you can display on your CodeBug, for example the CPU temperature.


  • Ask a new question:

Back to top