Raspberry Pi as a clock

Task: Display clock on the Raspberry Pi display

Implementation with 'epd' library:

1. Create python application (main.py) to display date and time:

import epd2in7
import datetime
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

def main():
   epd = epd2in7.EPD()

   font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 42)
   font42 = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 80)

   image = Image.new('1', (epd2in7.EPD_WIDTH, epd2in7.EPD_HEIGHT), 255)

   # Time HH:MI
   text = datetime.datetime.today().strftime("%H:%M")
   img_txt = Image.new('L', font42.getsize(text), 255)
   draw_txt = ImageDraw.Draw(img_txt)
   draw_txt.text((0, 0), text, font = font42, fill = 0)
   t = img_txt.rotate(90, expand=1)
   image.paste(t, (10, 10))

   # Date YYYY/MM/DD
   text = datetime.datetime.today().strftime("%Y/%m/%d")
   img_txt = Image.new('L', font.getsize(text), 255)
   draw_txt = ImageDraw.Draw(img_txt)
   draw_txt.text((0, 0), text, font = font, fill = 0)
   t = img_txt.rotate(90, expand=1)
   image.paste(t, (110, 5))

   # Draw

if __name__ == '__main__':

2. Create run.sh script to run python application:

cd /home/ubuntu/bin/clock
sudo python3 main.py

3. Add library files:


4. Add run it every minute by crontab job:

* * * * *       root    /home/ubuntu/bin/clock/run.sh


Have a fun.

1 thought on “Raspberry Pi as a clock

  1. Thank you for another informative website. Where else could I get that type of information written in such a perfect way? I have a project that I’m just now working on, and I’ve been on the look out for such information.

Leave a Reply

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

Enter Captcha Here :