Friday, September 26, 2025

Pico audio 7: Build a Zwitscherbox

For an index to all my stories, click this text

My girlfriend got a birthday present from a friend. Nothing to write about you would think. But it was a Zwitscherbox. Never heard of it ??? Neither did I. Read on.



A Zwitscherbox is a simple concept. It is a box with some kind of a microcontroller inside (no I did not take it apart), a speaker and a PIR. Every time you pass by, the PIR detects movement, and the box plays the sound of birds singing.

Simple, but effective and fun. And most of the time it brings a smile to your face. So a real fun present.

I looked it up on the internet and was amazed at the price. It was a rather expensive gift at 50 to 60 Euro (USD).

That got me thinking. How difficult would it be to build one myself. Well actually it is dead simple.

Playing audio with a microcontroller.

There are several ways to play audio with a microcontroller. You can add an MP3 player like I did it this story: http://lucstechblog.blogspot.com/2019/10/web-controlled-mp3-player.html

Another method is to use the Raspberry Pi Pico as an audio player. I have written a few stories on using a Raspberry Pi Pico as an audio player. You just need a few cheap components. It can be build in 15 minutes or so. And like stated before, the audio quality is very good. I chose this option for this project.

Raspberry Pi Pico audio player.

To build an audio player with the Raspberry Pi pico you will need a few simple components. You will need a few resistors, some capacitors and (for larger audio files) an SD card with an adapter. If you can solder, you can use the SD adapter that is included with most micro-sd cards. If you can not solder you can use an SD-card adapter for microcontrollers.

I wrote several stories on this subject and it would be wise to read these first before continuing this story.

The audio file has to be in WAV format and you can use Audacity to convert an MP3 file to a 8K wave file. The following link takes you to my story on how to achieve this:
https://lucstechblog.blogspot.com/2024/10/audacity-pico-audio-part-1.html

The Raspberry Pi Pico audio player hardware can be found here:
https://lucstechblog.blogspot.com/2024/10/audio-on-pico-part-2-hardware.html

The Raspberry Pi Pico software (Micropython) can be found here:
https://lucstechblog.blogspot.com/2024/10/pico-audio-part-3.html

Attaching an SD card:
https://lucstechblog.blogspot.com/2025/01/pico-sdcard-part-1-hardware.html

Software for Pico and SD card:
https://lucstechblog.blogspot.com/2025/01/pico-sd-card-part-2-software.html

For this project we need to add a PIR.
You can read the basics on how a PIR works here: https://lucstechblog.blogspot.com/2017/01/pir-basics-movement-detection.html

Alternatively you could use a radar module like this one:
http://lucstechblog.blogspot.com/2018/08/motion-detection-with-rcwl-0516-radar.html

They both work the same way so rummage through your stock and use what you have got.

The breadboard setup.

Basically it is the setup from the Pico audio player. You can use that for testing. When everything works as planned just remove the buttons and the Oled screen. If you want to use that setup just look at this story: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx




I just added one thing to this setup and that is a PIR.
The VCC of the PIR MUST be connected to the VBUS connection of the Pico as it needs 5 Volt.
The PIR is further connected to GP21 and to GND.
If you are not building this on top of the Pico Audio Player you can attach the PIR to any GPIO you like. Just make sure you adjust the program to use the selected pin.

And that's all for the hardware side.

The audio file.

For replicating the Zwitscherbox I needed an audiofile of a bird singing. There are many sites on the internet that offer royalty free sounds. I found a nice audio file that plays a blackbird singing. It runs for about 2 minutes. Ideal for this project. You can download it here:

https://quicksounds.com/sound/4769/blackbird-in-town-3

Next step is to import the file into (free software) Audacity and convert it to an 8K wav file. In a previous story in this series I described how to do that. You can find that story here: 
https://lucstechblog.blogspot.com/2024/10/audacity-pico-audio-part-1.html

When the audio file was converted I stored it on the SD card that is attached to the Pico in the directory music with the name "Blackbird.wav"

Zwitscherbox software

Basically this is the same program that was used for the Talking Dice. The program is written in MicroPython.

import os as uos
import time
from machine import SPI, Pin
import sdcard
from wavePlayer import wavePlayer

spi = SPI(1,sck=Pin(14), mosi=Pin(15), miso=Pin(12))
cs = Pin(13)
sd = sdcard.SDCard(spi, cs)

pir=machine.Pin(21, machine.Pin.IN)

uos.mount(sd, '/sd')

player = wavePlayer()

print(uos.listdir('/sd/Music'))
time.sleep(10)

try:
    while True:
      print(pir.value())   
      if (pir.value() == 1):
        player.play('/sd/Music/Blackbird.wav')
        time.sleep(5)
      time.sleep(0.3)
except KeyboardInterrupt:
    player.stop()
    print("wave player terminated")


For testing purposes I build a keyboard interrupt into the program. Pressing CTRL-C on the keyboard, while the Pico is connected to Thonny, stops the program and shuts the audio player down.

Like said before and as can be seen in the schematics: the PIR is connected to GPIO 21 on the Raspberry Pi Pico.

Almost everything is identical to the previous audio programs except for this part:

      if (pir.value() == 1):
        player.play('/sd/Music/Blackbird.wav')
        time.sleep(5)
      time.sleep(0.3)

First the program tests if the PIR signal is high. If that is the case then someone is passing by. The player then plays the blackbird sound witch is stored on the attached SD card in the directory Music. The program then waits 5 seconds so the PIR can settle down and then runs again.

Concluding.

The Raspberry Pi Pico with MicroPython is a golden combination that is easy to program and enormous versatile.
The Zwitscherbox clone is an easy build and great fun. It makes a fantastic present for a fraction of the price of the original.

Next step is to design and build a nice casing.........

Till next time
Have fun


Luc Volders



Friday, September 12, 2025

Javascript Speech Synthesises

For an index to all my stories click this text.

I like Javascript. I really do. The language is easy to learn, versatile and extensive. You just make a simple (or complicated) webpage, put some Javascript code in it and it runs on your PC, Raspberry, phone or tablet. Interfacing with the ESP8266, ESP32, your home automation system or a myriad of other devices and services is generally not difficult to achieve.

And there is speech synthesises.......

Have your computer speak to you.

The Javascript commands for having your computer/phone/tablet speak to you are really easy.

var msg = new SpeechSynthesisUtterance();

This starts a new Speechsynthesis instance and we call it msg.

msg.text = "this is my text"

Obviously the text that the message will speak = "this is my text"

speechSynthesis.speak(msg);

And this speaks out the message.

That is really all there is to it.

Now let us look to the next html code with a Javascript example.

<!DOCTYPE html>
<html lang="en-US">
<body>

<h2>Javascript Speech</h2>

<script>

var msg = new SpeechSynthesisUtterance();
msg.text = 'I have put the lamp on';

speechSynthesis.speak(msg);

</script>
</body>
</html>

Copy this code in your favorite text editor (I use notepad) and save it as speech01.html Make sure not to save it as a txt file. So use "Save as" for that. Save the file in a directory of your choice.



As you can see the file was saved as speech01 and seen as a Firefox HTML document. If your default browser is Chrome you should see the file as a Chrome HTML file.

Double-click the file and your browser will open and the words 'I have put the lamp on' will be spoken. Naturally you will have to make sure that your speakers are on and the volume is up. The text shows where this is going.......

Neat huh ???

For the next step I urge you to change your default browser to Chrome or Edge as these browsers have more posibillities for speech synthesises as Firefox has. You can always left-click on the html file and choose open with Chrome or Edge provided these are installed on your computer. You might try with other browsers (I haven't) to see if that works.

Changing the language and voice

Now we know how simple it is to have the computer speak to us we might want to change some parameters. Again: this works in Chrome and Edge but not in Firefox.

var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[0];
msg.volume = 1; // From 0 to 1
msg.rate = 1; // From 0.1 to 1
msg.pitch = 1; // From 0 to 2
msg.lang = "en";

These are the parameters you can change.

msg.voice = voices[0];

You can most of the time choose between 0 and 1. This will change the voice sometimes from female to male or between different male/female voices. Choose what sounds best to you.

msg.volume = 1;

Set this to 0 and speechsynthesises is set off. Set this to any value between 0 and 1 (like 0.2) and you can have the voice whisper without having to change the volume settings on your computer.

msg.rate = 1;

This will set the speech rate. from 0.1 which is veeerrrrrryyyyyy ssssssslllllloooooowwwwwww to 1 which is the normal speech rate.

msg.pitch = 1;

This will set the pitch of the voice. 0 is very low and 2 is high. 1 Is the standard value.

msg.lang = "en";

And finally this allows us to choose the language in which the text will be spoken.

msg.text = 'I have put the lamp on';

This one we have seen behore in the first (small) program. And this is where to put the text that has to be spoken.  So if you change msg.lang into another language you need to alter the text in msg.text in the text for that language.

If you want to change the text in Dutch (hey that's my native language) you would need to set the following parameters:

msg.lang = 'nl'
msg.text = 'Ik heb de lamp aangezet'


Another example

Here is another example that would speak out a fake temperature setting. In this example all parameters are available so you can change them to your liking.

<!DOCTYPE html>
<html lang="en-US">
<body>

<h2>Javascript Speech</h2>

<script>

var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[1];
msg.volume = 1; // From 0 to 1
msg.rate = 1; // From 0.1 to 1
msg.pitch = 1; // From 0 to 2
msg.lang = "en";

msg.text = 'The temperature is' + 18 + 'degrees';

speechSynthesis.speak(msg);

</script>
</body>
</html>

Just copy this into your editor and save it as speech02.html Go to the directory where you saved it and double-click on it and the webpage will open and speak the text. Alter some settings in the editor, save it again and reload the page.

What languages are available ???


Javascript uses the BCP 47 Language Codes and here is the complete list of languages that are available:

ar-SA Arabic Saudi Arabia
cs-CZ Czech Czech Republic
da-DK Danish Denmark
de-DE German Germany
el-GR Modern Greek Greece
en-AU English Australia
en-GB English United Kingdom
en-IE English Ireland
en-US English United States
en-ZA English South Africa
es-ES Spanish Spain
es-MX Spanish Mexico
fi-FI Finnish Finland
fr-CA French Canada
fr-FR French France
he-IL Hebrew Israel
hi-IN Hindi India
hu-HU Hungarian Hungary
id-ID Indonesian Indonesia
it-IT Italian Italy
ja-JP Japanese Japan
ko-KR Korean Republic of Korea
nl-BE Dutch Belgium
nl-NL Dutch Netherlands
no-NO Norwegian Norway
pl-PL Polish Poland
pt-BR Portuguese Brazil
pt-PT Portuguese Portugal
ro-RO Romanian Romania
ru-RU Russian Russian Federation
sk-SK Slovak Slovakia
sv-SE Swedish Sweden
th-TH Thai Thailand
tr-TR Turkish Turkey
zh-CN Chinese China
zh-HK Chinese Hong Kong
zh-TW Chinese Taiwan


So if you are Belgian or Dutch and like the soft touch of the Belgian voices set:

msg.lang = "nl-BE";
msg.text = 'Ik heb de lamp aangezet';


Fun to play with.

Remember the Javascript pages made to control an ESP8266 and get information from that ESP ??? Next time we are going to incorporate the speech synthesises into that page so you will have an audible feedback on the commands you have send to the ESP and sensor readings from that ESP.

For now you have something funny and usefull to play with.

Till next time
have fun

Luc Volders


Friday, September 5, 2025

Autostarting a program with Raspberry Bookworm OS

For an index to all my stories click this text

October 2018 I wrote a story on how to build an internet radio with the Raspberry Pi Zero. You can find that story here: https://lucstechblog.blogspot.com/2018/10/raspberry-pi-internet-radio-part-3.html

As you might know (as a faithfull follower of this weblog) I am building a second version for using in my living room. The old one is still working in my mancave, and stays there.

The old one uses the, at that time, current Raspberry OS. And of course I am now going to use the latest version. And that posed a small problem.

The previous version (which still works flawlessly) modified the LXDE-pi file in a way that made the internet radio program start automatically when the Pi Zero booted. And that does not work anymore.

For those that are interested I'll show you how to make a Python program autostart with Raspberry Pi's Bookworm OS.

Modify .bashrc

There are several ways to make a program, any program, autostart but this is one of the easiest.

Open the console and type:

sudo nano /home/pi/.bashrc

This opens the .bashrc file in the Nano editor. Please not the . at the beginning of the .bashrc filename. This makes the file normally hidden.

This is a fairly large file. Scroll down to the end of the file, and at put down the next two lines:

echo Running at boot
python /home/pi/radio.py


Then press CTRL-O to save the file and CTRL-X to quit the Nano editor.

You can alter python /home/pi/radio.py in any program you want to start when the Pi boots.

Reset the Pi with:

Sudo reboot now

And the Pi will reboot and automatically runs your program.

That's all for now

Till next time
Have fun

Luc Volders