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.

It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
Experimental Procedures¶
Step 1: Build the circuit.

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

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.