[How To] Digital Alarm Clock with Matrix Led 4x8x8

Guides and tutorials for various stuff. Posted by DietPi users.
Post Reply
User avatar
pilovis
Posts: 19
Joined: Sat Sep 29, 2018 4:32 pm
Location: Italy
Contact:

[How To] Digital Alarm Clock with Matrix Led 4x8x8

Post by pilovis » Fri Oct 05, 2018 6:57 am

I've done a lot of searching but haven't found any tutorial about how to create a simple alarm digital clock with MAX7219 Matrix Led 4x8x8 (nice piece of hardware), all the tutorials I found are just for Arduino :cry:

So I decided to create my own one ;) .

This is the complete and simple tutorial.

Total cost: about 60 Euro
Difficulty = easy
Time = 1 hour
Skills required = dummy :lol:

Hardware requisites:

- one 5V 3A power supply, or better two: one 5V 2A for "Raspberry + amplifier" and one 5V 1A for "Led matrix".
- Raspberry Pi1 512 or above
- Raspbian Stretch Lite on 4 GB sd card (or bigger)
- USB Wifi dongle - optional (for Pi1 and Pi2, not needed for Pi3)
- RTC Module (DS3231 suggested)
- MAX7219-LED-4x8x8-Matrix
- 5V 2W mini audio amplifier (mono is OK)
- 2W Mini speaker(s)
- a plastic box
- some wires (arduino jumper wires)

---------------------------------------------------------
Max Mem usage: less than 100 Mbytes
Max CPU usage: less than 30% @ RP1 no overclock
---------------------------------------------------------

Important Notes:
connect the Vcc pin of the Led Matrix directly to the 5V of the power supply, do not connect it to the Raspberry +5V pins.
All GND pins ('ground' or '-') of Raspberry, Amplifier and MAX7219 Led matrix, need to be connected together (just one pin per board)
The audio amplifier gets +5V directly to the +5V (pin 2 or 4) of the raspberry

Raspberry
Image

RTC
Image

RTC installation
Image

MAX7219 Led Matrix module
Image

Pin-out description on the bottom side
Image

BOX
Image

Image

Image

Image

Image

Stereo mini audio amplifier
Image

Image

I decided to solder the audio wires from raspberry to the amp, but you can use the 3.5 audio connector as well.

Image

Finished project :)
note: in this picture the TIME is shifted to the right, I modified the code to better center it, also the leds bright is settable (see code comments)
Image

You can also add scrolling text to the clock (there is a little startup demo in my code):

Image


You might use Raspberry Zero, but you need a mini adapter like this, to have an audio connection output (HDMI to VGA/Audio):

Image

Raspberry GPIO pin-outs (needed for connecting MAX7219-LED matrix)
Image
Last edited by pilovis on Fri Oct 05, 2018 1:40 pm, edited 96 times in total.
too old to rock 'n' roll too young to die.

User avatar
pilovis
Posts: 19
Joined: Sat Sep 29, 2018 4:32 pm
Location: Italy
Contact:

Re: Digital Clock with Matrix Led 4x8x8

Post by pilovis » Fri Oct 05, 2018 7:14 am

- Installation of RTC (real time clock module with backup battery):

https://thepihut.com/blogs/raspberry-pi ... spberry-pi

- MAX7219 Driver installation:

https://luma-led-matrix.readthedocs.io/ ... requisites

- Matrix pin-outs connections to Raspberry:

https://luma-led-matrix.readthedocs.io/ ... o-pin-outs

- My Digital Clock Python script:

Code: Select all

sudo nano /root/clock.py

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017-18 Richard Hull and contributors
# See LICENSE.rst for details.
# MAX7219-LED-4x8x8-Matrix digital clock By Lovisolo Pier Maurizio - Italy

import re
import time
import argparse
import locale
import datetime

# NOTE: change the following according to your locale setting
locale.setlocale(locale.LC_ALL,'it_IT.UTF-8')

from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.render import canvas
from luma.core.virtual import viewport
from luma.core.legacy import text, show_message
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
from itertools import repeat

def demo(n, block_orientation, rotate):
    # create device for linear preassembled 4x8x8 max7219 matrix
    serial = spi(port=0, device=0, gpio=noop())
    device = max7219(serial, cascaded=4, block_orientation=-90)
    device.contrast(0x05)
    print("Created device")
    # note: change device.contrast value (0xXX) to change leds brightness

    # start scrolling text demo - just once at startup
    msg = "DIGITAL CLOCK by Lovisolo Pier Maurizio - 2018"
    print(msg)
    show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0.05)

    # MAX7219 4x8x8 LED Matrix Clock
    for _ in repeat(None):
     time.sleep(1)
     msg = time.asctime()
     msg= time.strftime("%H%M")
     with canvas(device) as draw:
      text(draw, (1, 0), msg, fill="white")
    time.sleep(1)
    pass


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='matrix_demo arguments',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('--cascaded', '-n', type=int, default=1, help='Number of cascaded MAX7219 LED matrices')
    parser.add_argument('--block-orientation', type=int, default=0, choices=[0, 90, -90], help='Corrects block orientation when wired vertically')
    parser.add_argument('--rotate', type=int, default=0, choices=[0, 1, 2, 3], help='Rotate display 0=0°, 1=90°, 2=180°, 3=270°')

    args = parser.parse_args()

    try:
        demo(args.cascaded, args.block_orientation, args.rotate)
    except KeyboardInterrupt:
        pass
Audio for alarm clock (MP3 player):

Code: Select all

sudo apt-get install madplay
My crontab for alarm wakeup and "hour/ half hour" bell (you might use any MP3 audio you like):

Code: Select all

crontab -e 
insert the following:

Code: Select all

# Big-Ben bell every hour from 09:00 to 22:00
00 09-22 * * * /usr/bin/madplay /root/westminster_classic.mp3
#
# haf hour bell 09:30 to 22:30
30 09-22 * * * /usr/bin/madplay /root/mezzora.mp3
#

# Wake up at 07:00 Mon to Fri
00 07 * * 1-5 /usr/bin/madplay /root/church.mp3
Last edited by pilovis on Fri Oct 05, 2018 1:39 pm, edited 26 times in total.
too old to rock 'n' roll too young to die.

User avatar
pilovis
Posts: 19
Joined: Sat Sep 29, 2018 4:32 pm
Location: Italy
Contact:

Re: Digital Clock with Matrix Led 4x8x8

Post by pilovis » Fri Oct 05, 2018 7:24 am

Digital Clock autostart at boot:

Code: Select all

sudo nano /etc/rc.local
put the following just before "exit 0" line:

Code: Select all

/usr/bin/python /root/clock.py &

Image

I also connected a blue led directly to GPIO 22 without any resistor :o ,
but I used this python script to reduce gpio output voltage by controlling the PWM duty cycle ;)
and also to start pulsing the led (dim to brighten and viceversa).
You might use this led as a visible alarm for something.

NOTE: never connect red, orange, yellow or green LEDs directly to the GPIO without a series resistor!
You might directly connect a blue or a white led to the gpio since those leds have higher drop voltage (at your own risk).

Code: Select all

#!/usr/bin/python3
# script to connect a "blue" or "white" led directly to gpio pin and control it
# never connect red, orange, yellow or green LED directly to the GPIO without a series resistor

import RPi.GPIO as GPIO # always needed with RPi.GPIO  
from time import sleep  # pull in the sleep function from time module  

GPIO.setmode(GPIO.BCM)  # choose BCM or BOARD numbering schemes. I use BCM  

GPIO.setup(22, GPIO.OUT)# set GPIO 22 as output for blue led  

white = GPIO.PWM(22, 100)    # create object white for PWM on port 22 at 100 Hertz  

white.start(0)              # start white or blue led on 0 percent duty cycle (off)  

# now the fun starts, we'll vary the duty cycle to   
# dim/brighten the led  

pause_time = 0.01           # you can change this to slow down/speed up  

try:
    while True:
        for i in range(0,50):      # 50% to reduce max output voltage  
            white.ChangeDutyCycle(i)
            sleep(pause_time)
        for i in range(50,-1,-1):      # from 50% to zero in steps of -1  
            white.ChangeDutyCycle(i)
            sleep(pause_time)

except KeyboardInterrupt:
    white.stop()            # stop the white PWM output  
    GPIO.cleanup()          # clean up GPIO on CTRL+C exit  
How to determine the right resistor value for different types of led:

Image

notes and translation:
volt caduta = led drop voltage
infrarosso = infrared
valori standard resistenze = standard resistor values in ohm (x10, x100, x1000, etc.)
all the colors are self-explanatory ;)
Raspberry GPIO output voltage is 3.3V
the drop resistor has to be connected in series to one of the two terminals of the led (better to the catode +)
LED catode (+) is the longer terminal
Last edited by pilovis on Fri Oct 05, 2018 1:30 pm, edited 46 times in total.
too old to rock 'n' roll too young to die.

User avatar
johnvick
Legend
Posts: 653
Joined: Wed Jun 08, 2016 10:53 am
Location: New Zealand

Re: Digital Clock with Matrix Led 4x8x8

Post by johnvick » Fri Oct 05, 2018 7:26 am

Nice project thanks for posting to this forum.

John

User avatar
MichaIng
Legend
Posts: 1626
Joined: Sat Nov 18, 2017 5:21 pm

Re: [How To] Digital Alarm Clock with Matrix Led 4x8x8

Post by MichaIng » Fri Oct 05, 2018 1:22 pm

Really great 8).

I tend to hate my alarm clock some mornings and do not tread it very well. Perhaps a fully safe made baby helps to enjoy the start into the day, when you're forced to see what you have created and think about what you could do next :D.

Btw.:
/etc/rc.local is not by default enabled (or even exists) on all systems any more. It seems to be seen deprecated for some reason: https://stackoverflow.com/a/44801337

However, since DietPi anyway needs to run some oneshots on startup, we added an alternative: /var/lib/dietpi/postboot.d/
- Place any script inside and it will be executed at the end of startup sequence.

User avatar
pilovis
Posts: 19
Joined: Sat Sep 29, 2018 4:32 pm
Location: Italy
Contact:

Re: [How To] Digital Alarm Clock with Matrix Led 4x8x8

Post by pilovis » Mon Oct 08, 2018 8:27 pm

Just an update:

I noticed that connecting the +Vcc of the mini audio amplifier to the 3.3V pin instead of the 5V pin of Raspberry, the high frequency digital noise is much less audible, probably because Raspberry has the 3.3V line more filtered and stabilized than the 5V.

By looking at the data sheet I found that this little amp can work with power supply voltage from DC 2.5 to 5.5V
too old to rock 'n' roll too young to die.

User avatar
pilovis
Posts: 19
Joined: Sat Sep 29, 2018 4:32 pm
Location: Italy
Contact:

Talking Clock

Post by pilovis » Tue Oct 09, 2018 5:02 am

Just an improvement:

Let the raspberry say current date and time:

1) install picotts, offline text to speech:

Code: Select all

sudo apt-get install libttspico-utils
2) create the code:

Code: Select all

sudo nano /root/time.sh
insert the following:

Code: Select all

#!/bin/sh
# Clock says in English: date, day of the week and hour
data=$(date +%d%B)
pico2wave -l en-GB -w /tmp/data.wav "<volume level='55'><pitch level='105'> today is the"$data
data1=$(date +%A)
pico2wave -l en-GB -w /tmp/data1.wav "<volume level='55'><pitch level='105'>"$data1
ora=$(date +"%H:%M")
cadena=$(echo $ora | sed 's/\:00/and zero/g')
pico2wave -l en-GB -w /tmp/ora.wav "<volume level='55'><pitch level='105'>the current time is"$cadena"minutes"
aplay -r 8000 -t wav -c 2 -f S16_LE -D plughw:0,0 /tmp/data.wav
aplay -r 8000 -t wav -c 2 -f S16_LE -D plughw:0,0 /tmp/data1.wav
aplay -r 8000 -t wav -c 2 -f S16_LE -D plughw:0,0 /tmp/ora.wav
3) give the right permisions to the script

Code: Select all

sudo chmod 755 /root/time.sh
4) launch the script:

Code: Select all

/bin/sh /root/time.sh

Note: Picotts supported languages : ‘en-US’, ‘en-GB’, ‘de-DE’, ‘es-ES’, ‘fr-FR’, ‘it-IT’.
On some Raspbian release, this package is missing but you can just copy the arm deb package from debian.
too old to rock 'n' roll too young to die.

Post Reply