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!

3.1.4 Text-to-speech

Introduction

In many places, we can come into contact with TTS (Text-to-speech) technology, which converts text into natural-sounding speech and brings people a good interactive experience.

Let’s try to make your project speak.

Required Components

In this project, we need the following components.

../_images/audio2.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

Audio Module and Speaker

-

Experimental Procedures

Step 1: Build the circuit.

../_images/4.1.4fritzing1.png

After building the circuit according to the above diagram, then plug the audio cable into the Raspberry Pi’s 3.5mm audio jack.

../_images/audio41.png

Step 2: Install espeak module.

sudo apt-get install espeak -y

Step 3: Get into the folder of the code.

cd ~/raphael-kit/python/

Step 4: Run.

python3 3.1.4_Text-to-speech.py

Raspberry pi will greet you kindly after the code runs, and it will say goodbye to you when the code stops.

Note

If your speaker have no sound, it may be because the Raspberry Pi has selected the wrong audio output (The default is HDMI), you need to Change Audio Output to Headphones.

If you feel that the volume of the speakers is too low, you can Adjust Volume.

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. After confirming that there are no problems, you can use the Copy button to copy the modified code, then open the source code in Terminal via nano cammand and paste it.

from tts import TTS

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

def main():
    tts.say('Hello, nice to meet you!')

def destroy():
    tts.say('See you later')

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        destroy()

Code Explanation

from tts import TTS

tts = TTS(engine="espeak")

Import the TTS class and instantiate an object.

tts.lang('en-US')

Set the language.

Note

Currently the switchable language only supports English.

tts.say("Hello, nice to meet you!")

Fill in the text to be said as a parameter, after executing tts.say(), Raspberry Pi will say the text you wrote.

Phenomenon Picture

../_images/3.1.3audio.JPG