For an index to all my stories click this text
This is an ongoing series about audio on the Raspberry Pi Pico. This time I am going to build a talking dice. Just like the previous is made in MicroPython.
Actually this is the sixth story on how to play audio on the Raspberry Pi Pico with MicroPython. In this story shows how to build a talking dice. The audio on the Pico does not sound like the crappy audio on the ESP32 with Talkie. That sounded like voice synthesizes on the Commodore 64 and Atari computers if you still remember that. No this is real clear audio. And for the talking dice we are going to use your own recorded voice.
I urge you to read the proceeding stories in which audio recording, the necessary hardware and software were discussed.
Audacity. This is the first story in this series. With Audacity we can record our own voice using a microphone or a build-in microphone in your webcam. You can also convert mp3 files to the required 8K wav files for this project. Read that story here: http://lucstechblog.blogspot.com/2024/10/audacity-pico-audio-part-1.html
Picoaudio 2 the hardware. This story shows what hardware you need. Don't worry its just a few resistors and some capacitors. The total cost will be around 1 Euro/USD without the Raspberry Pi Pico. And as you know the Pico itself will set you back around 5 Euro/USD for the non-wifi version and about 8 Euro/USD for the wifi version.
http://lucstechblog.blogspot.com/2024/10/audio-on-pico-part-2-hardware.html
Picoaudio 3 the software. This story describes which MicroPython drivers you will need. In total 5 drivers. The story gives you the links to the download site. Thye story also shows a small program that will play your first sounds.
http://lucstechblog.blogspot.com/2024/10/pico-audio-part-3.html
There are 2 more stories in this series about building a talking thermometer and a talking clock. You can find those here:
http://lucstechblog.blogspot.com/2024/11/pico-audio-part-4-talking-thermometer.html
http://lucstechblog.blogspot.com/2024/11/pico-audio-part-5-talking-clock.html
To store the audio clips Pico's memory is rather limited. So the audio is stored on an SD card. Therefore you will need to attach an SD card to the Pico.
It is really easy to attach an SD card to the Raspberry Pi Pico. This story shows how to do that: http://lucstechblog.blogspot.com/2025/01/pico-sdcard-part-1-hardware.html
Next to physically attaching the SD card you will also need to install the drivers in MicroPython. This story shows how to do that:
http://lucstechblog.blogspot.com/2025/01/pico-sd-card-part-2-software.html
So what you will need to build the talking dice are the following components:
- A Raspberry Pi Pico or Pico W
- 1 push button
- 2 10K resistors.
- 2 1K resistors
- 4 2K2 resistors
- 2 47nF capacitors.
- 1 SD card module or SD card adapter
- 1 large or several small breadboards
- Dupont wires foir the breaboard
Extra
- An active speaker (like a computer speaker) or
- a pair of earplug headphones.
- a 3.5mm contra connector for the headphone or speakers
- an optional small amplifier so you do not need an active speaker
- a pair of small speakers when using the amplifier
- alligator clips for connecting to the headphone or speakers
- Power Bank or USB power supply
The breadboard setup
As the breadboard setup was extensive discussed in the previous stories I will not go into details here. Just follow those stories.
Back to Audacity
In the first story in this series I showed how to record audio and convert it to an 8K wav file with Audacity. If you did not read that story please do it now.
Our program will speak out the words: one, two, three, four, five and six. Those we already recorded in the story about Audacity :
lucstechblog.blogspot.com/2024/10/audacity-pico-audio-part-1.html
We do want to record some extra sentences. The first one is: "Here we go". And the second one is: "You threw an eight"
So please record these sentences and save them on your SD card in the directory where the number.wav files are stored. Name these files "Here.wav"and "Threw.wav". Chose any name you might like better but keep in mind to alter the names in the program also.
If you do not know how to load and transfer files to an SD card please read this story:
http://lucstechblog.blogspot.com/2025/01/pico-sd-card-part-2-software.html
and this story:
http://lucstechblog.blogspot.com/2023/07/a-few-small-thonny-tips.html
I also added a sound effect of rolling dice which I found on Pixabay. You can find it here: https://pixabay.com/sound-effects/search/dice_roll/
It is called Dice_roll and lasts 1 second. Great for our project.
Make sure you convert it to an 8K wav file before transferring it to the Pico's
SD card. Name this file "Roll.wav"
The talking dice program
Here is the full program in MicroPython. Don't forget to put the required libraries in the lib folder.
''' Talking dice by Luc Volders http://lucstechblog.blogspot.com/ ''' import os as os import machine from wavePlayer import wavePlayer from machine import SPI, Pin, Timer import sdcard import random random.seed(None) but01=machine.Pin(18, machine.Pin.IN) player = wavePlayer() spi = SPI(1,sck=Pin(14), mosi=Pin(15), miso=Pin(12)) cs = Pin(13) sd = sdcard.SDCard(spi, cs) os.mount(sd, '/sd') while True: if (but01.value()==0): player.play("/sd/Sounds/Here.wav") player.play("/sd/Sounds/Roll.wav") player.play("/sd/Sounds/Threw.wav") number = random.randint(1,6) strnum = str(number) soundstr = "/sd/Sounds/" soundstr = soundstr + strnum soundstr = soundstr + ".wav" player.play(soundstr)
If you have read the previous stories in this series the program speaks for itself. The number.wav files and the "Here.wav", "Roll.wav" and "Threw.wav" files are all in the Sounds directory on the SD card.
The only part that needs some explanation is this part in the While True loop:
number = random.randint(1,6)
strnum = str(number)
soundstr = "/sd/Sounds/"
soundstr = soundstr + strnum
soundstr = soundstr + ".wav"
player.play(soundstr)
The player.play() command accepts a string to point to the file we want to play.
First step is to create a random number from 1 to 6.
We make a string from that number with the str() command.
The first part of the string is the location of the files which is here: "/sd/Sounds/"
We add to this the string with the number and then we add ".wav"
And then the string is put into the player.play() command.
That is all there is to it.
Sidenote
If you are building a dedicated project for this there is enough memory in the Pico to put all the files in just like we did in the first part of this series:
http://lucstechblog.blogspot.com/2024/11/pico-audio-part-4-talking-thermometer.html picoaudio4 talking thermometer
That way you can leave the SD card and the accompanying resistor out.
Till next time.
Have fun
Luc Volders