Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

4.1.3 Speech Clock

Introduction

In this project, let’s make a voice clock with a speaker and a 4-digit 7-segment display. The 4-digit 7-segment display will display the time, and the speaker will broadcast the time every hour.

Required Components

In this project, we need the following components.

../_images/3.1.17components.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

Audio Module and Speaker

-

4-Digit 7-Segment Display

-

74HC595

BUY

Schematic Diagram

T-Board Name

physical

wiringPi

BCM

GPIO17

Pin 11

0

17

GPIO27

Pin 13

2

27

GPIO22

Pin 15

3

22

SPIMOSI

Pin 19

12

10

GPIO18

Pin 12

1

18

GPIO23

Pin 16

4

23

GPIO24

Pin 18

5

24

../_images/4.1.3_speechclock_schematic.png ../_images/3.1.17_schematic.png

Experimental Procedures

Step 1: Build the circuit.

../_images/3.1.17fritzing.png

Before this project, you need to make sure you complete 3.1.4 Text-to-speech.

Step 2: Use the command date to view the local time.

date

If the local time is different from the real time, you need to use the following command to set the time zone.

sudo dpkg-reconfigure tzdata

Choose your time zone.

../_images/tzdata.png

Step 3: Get into the folder of the code.

cd ~/raphael-kit/python/

Step 3: Run.

python3 4.1.3_SpeechClock.py

After the code runs:

  • A welcome message is spoken: “Clock system started. Welcome!”

  • The four-digit display shows the current time in HH:MM format.

  • At the start of each hour (minute = 0), the system announces the current time once.

  • The display updates continuously until Ctrl+C is pressed, after which GPIO resources are cleaned up.

Code

Note

You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python. After modifying the code, you can run it directly to see the effect.

import RPi.GPIO as GPIO
from tts import TTS
import time

# Initialize TTS
tts = TTS(engine="espeak")
tts.lang('en-US')

# GPIO pins
SDI = 24
RCLK = 23
SRCLK = 25

placePin = (10, 22, 27, 17)

# Seven-segment encoding
number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(SDI, GPIO.OUT)
    GPIO.setup(RCLK, GPIO.OUT)
    GPIO.setup(SRCLK, GPIO.OUT)
    for pin in placePin:
        GPIO.setup(pin, GPIO.OUT)

def clearDisplay():
    for _ in range(8):
        GPIO.output(SDI, 1)
        GPIO.output(SRCLK, GPIO.HIGH)
        GPIO.output(SRCLK, GPIO.LOW)
    GPIO.output(RCLK, GPIO.HIGH)
    GPIO.output(RCLK, GPIO.LOW)

def hc595_shift(data):
    for i in range(8):
        GPIO.output(SDI, (0x80 & (data << i)))
        GPIO.output(SRCLK, GPIO.HIGH)
        GPIO.output(SRCLK, GPIO.LOW)
    GPIO.output(RCLK, GPIO.HIGH)
    GPIO.output(RCLK, GPIO.LOW)

def pickDigit(digit):
    # Turn all digits off
    for pin in placePin:
        GPIO.output(pin, GPIO.LOW)
    # Turn selected digit ON
    GPIO.output(placePin[digit], GPIO.HIGH)

def loop():
    status = 0

    while True:
        now = time.localtime()
        hour = now.tm_hour
        minute = now.tm_min

        # Display minute (unit)
        clearDisplay()
        pickDigit(0)
        hc595_shift(number[minute % 10])

        # Display minute (tens)
        clearDisplay()
        pickDigit(1)
        hc595_shift(number[(minute // 10) % 10])

        # Display hour (unit)
        clearDisplay()
        pickDigit(2)
        hc595_shift(number[hour % 10])

        # Display hour (tens)
        clearDisplay()
        pickDigit(3)
        hc595_shift(number[(hour // 10) % 10])

        # Speak once every hour (at minute == 0)
        if minute == 0 and status == 0:
            tts.say(f'The time is now {hour} hours and {minute} minutes')
            time.sleep(3)   # Give time to finish speaking
            status = 1
        elif minute != 0:
            status = 0

        time.sleep(0.005)  # Prevent CPU overload

def destroy():
    GPIO.cleanup()

if __name__ == '__main__':
    setup()

    # ★ Welcome message at startup
    tts.say("Clock system started. Welcome!")
    time.sleep(3)

    try:
        loop()
    except KeyboardInterrupt:
        destroy()

Code Explanation

  1. Initializes the text-to-speech engine using espeak and sets the language to English.

    tts = TTS(engine="espeak")
    tts.lang('en-US')
    
  2. Defines the GPIO pins used for the shift register (74HC595) and the digit-select pins for the 4-digit display.

    SDI = 24
    RCLK = 23
    SRCLK = 25
    placePin = (10, 22, 27, 17)
    
  3. Stores the segment encoding values used to display digits 0–9 on the seven-segment display.

    number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)
    
  4. Clears the display by shifting eight “1” bits into the shift register.

    def clearDisplay():
        for _ in range(8):
            GPIO.output(SDI, 1)
            GPIO.output(SRCLK, GPIO.HIGH)
            GPIO.output(SRCLK, GPIO.LOW)
        GPIO.output(RCLK, GPIO.HIGH)
        GPIO.output(RCLK, GPIO.LOW)
    
  5. Sends one byte of segment data to the 74HC595 to update the digit being displayed.

    def hc595_shift(data):
        for i in range(8):
            GPIO.output(SDI, (0x80 & (data << i)))
            GPIO.output(SRCLK, GPIO.HIGH)
            GPIO.output(SRCLK, GPIO.LOW)
        GPIO.output(RCLK, GPIO.HIGH)
        GPIO.output(RCLK, GPIO.LOW)
    
  6. Selects one of the four digit positions (thousands, hundreds, tens, ones) for multiplexing.

    def pickDigit(digit):
        for pin in placePin:
            GPIO.output(pin, GPIO.LOW)
        GPIO.output(placePin[digit], GPIO.HIGH)
    
  7. Reads the current time from the system clock.

    now = time.localtime()
    hour = now.tm_hour
    minute = now.tm_min
    
  8. Updates each digit on the seven-segment display to show the current time in HH:MM format.

    hc595_shift(number[minute % 10])
    hc595_shift(number[(minute // 10) % 10])
    hc595_shift(number[hour % 10])
    hc595_shift(number[(hour // 10) % 10])
    
  9. Speaks the current time once at the start of each hour using text-to-speech.

    if minute == 0 and status == 0:
        tts.say(f'The time is now {hour} hours and {minute} minutes')
        status = 1
    elif minute != 0:
        status = 0
    
  10. Plays a welcome message when the program starts.

    tts.say("Clock system started. Welcome!")
    
  11. Cleans up all GPIO pins when the user stops the program with Ctrl+C.

    GPIO.cleanup()
    

Phenomenon Picture

../_images/4.1.3speech_clock.JPG