Pages

Friday, November 1, 2024

Pico Audio part 4 - Talking thermometer

For an index to all my stories click this text

This is the fourth story on how to play audio on the Raspberry Pi Pico with MicroPython. In this story I am showing how to build a talking thermometer. 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 thermometer we are going to use your own recorded voice.

I urge you to read the proceeding stories in which audio recording, the neccesary 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. The story also shows a small program that will play your first sounds.
http://lucstechblog.blogspot.com/2024/10/pico-audio-part-3.html

The thermometer.

To get the temperature we will use the readily available DS18B20 digital thermometer. In case you do not have one at hand, read further on for a simple solution.



To attach the DS18B20 to a Pico you only need the chip itself and a 4.7k pull-up resistor. If you are not familiar with this digital thermometer chip and how to use it with MicroPython you can read all about this in one of my books: Raspberry Pi Pico Simplified or Raspberry Pico W Simplified.



Here is the breadboard setup. The Dallas DS18B20 is attached (with a 4.7K pull-up resistor) to GP17 of the Pico. The rest of the setup is taken from my second story in this series: http://lucstechblog.blogspot.com/2024/10/audio-on-pico-part-2-hardware.html

First test

Before we are going to have our Pico speak out the temperature we first are going to do a test if everything works as we want.

Copy the following program in Thonny's editor and run it.

'''
Copy from Luc's book
Dallas on pin 17
'''

import machine
import onewire
import ds18x20
import time
 
dallaspin = machine.Pin(17)
sensor = ds18x20.DS18X20(onewire.OneWire(dallaspin))
 
roms = sensor.scan()
print('A ds18x20 is present')
 
while True:
  sensor.convert_temp()
  time.sleep(1)
  print(sensor.read_temp(roms[0]))
  time.sleep(2)


Thonny's shell will show you something like this:



The figures in your output will definitely be different. It was really cold in my mancave !!! As long as you get figures you know the DS18B20 is working fine. On to the next step.

Copy the Audacity files

In the first chapter in this series I asked you to make recordings of your own voice saying the words 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, It is now, Degrees. If you did not do that please do that now. Make sure to record these words in 8K wav format. If you do not know how to do that please read the first story in this series: http://lucstechblog.blogspot.com/2024/10/audacity-pico-audio-part-1.html (Audacity)

Create a new directory in Thonny, call it "Sounds" and copy the recorded wav files to that directory. If you are not sure how to do this you can read a small tutorial here:
http://lucstechblog.blogspot.com/2023/07/a-few-small-thonny-tips.html (thonny tips).



When done your Pico's directory will look like this.

The talking thermometer program

The preparations are done meaning that the breadboard setup is build and all the audio files are copied to the Pico's memory. The last part is the program that glues all this together.

'''
Talking thermometer program
http://lucstechblog.blogspot.com/
'''

import machine
import onewire
import ds18x20
import time
from wavePlayer import wavePlayer

dallaspin = machine.Pin(17)
sensor = ds18x20.DS18X20(onewire.OneWire(dallaspin))
 
roms = sensor.scan()
print('A ds18x20 is present')

player = wavePlayer()

while True:
  sensor.convert_temp()
  time.sleep(.5)
  temp = int(sensor.read_temp(roms[0]))
  print (temp)
  player.play("/Sounds/itsnow.wav")
  if (temp<=20):
     player.play("/Sounds/"+str(temp)+".wav")
  if (temp>20 and temp<30):
      player.play("/Sounds/"+str(20)+".wav")
      temp = temp-20
      player.play("/Sounds/"+str(temp)+".wav")
  if (temp>30 and temp<40):
      player.play("/Sounds/"+str(30)+".wav")
      temp = temp-30
      player.play("/Sounds/"+str(temp)+".wav")
  player.play("/Sounds/degrees.wav")
  time.sleep(3)

As usual on this weblog (and in my books) I'll give a short explanation on some highlights of the program.

while True:
  sensor.convert_temp()
  time.sleep(.5)
  temp = int(sensor.read_temp(roms[0]))
  print (temp)
  player.play("/Sounds/itsnow.wav")

After the necessary libraries have been loaded an endless loop is started. Inside the loop the Dallas DS18B20 is accessed to get the temperature. The temperature is then printed in Thonny's shell as a check.
Then you can hear your own voice (or the one you recorded) through the speaker saying "it is now"

  if (temp<=20):
     player.play("/Sounds/"+str(temp)+".wav")

If the temperature is equal to or below 20 degrees the program converts the temperature's value into a string and plays XX.wav in which the xx's are the value of the temperature. Make sure that the .wav files are in the /Sounds/ directory. Otherwise change the name of the directory in this line.

  if (temp>20 and temp<30):
      player.play("/Sounds/"+str(20)+".wav")
      temp = temp-20
      player.play("/Sounds/"+str(temp)+".wav")

If the temperature is higher than 20 degrees and lower than 30 degrees first the word "twenty" is spoken, found in the file "20.wav". Then 20 is subtracted from the temperature's value and the remaining figure is spoken.

  if (temp>30 and temp<40):
      player.play("/Sounds/"+str(30)+".wav")
      temp = temp-30
      player.play("/Sounds/"+str(temp)+".wav")

The same we did for values between 20 and 30 happens here when the temperature is higher as 30 degrees and lower as 40.

  player.play("/Sounds/degrees.wav")
  time.sleep(3)

The player then speaks the word "degrees" and finaly there is a pause of 3 seconds after which the sequence repeats.

And that is all that is needed to build a talking clock.

Expansions for the talking thermometer.

This talking thermometer speaks the temperature out loud every 5 seconds or so. This is really annoying. So you can alter the pause so that it reads out the temperature every half our or so.

Add a button. Alter the software in such a way that the temperature is only proclaimed when the button is pressed.

You could alter the software in such a way that the temperature is only pronounced when it reaches a certain critical value.

If you do not know how to add a button to this program or test for a critical value in MicroPython please consider buying one of my books: Raspberry Pi Pico Simplified or Pico W simplified.

Coming up: a talking clock.

Till next time
have fun

Luc Volders