Silicon ChipSpeech Synthesiser with the Raspberry Pi Zero - July 2020 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions: PicoLog Cloud
  5. Publisher's Letter: Publisher's Statement
  6. Publisher's Letter
  7. Feature: Techno Talk by Mark Nelson
  8. Feature: Net Work by Alan Winstanley
  9. Back Issues: Net Work by Alan Winstanley
  10. Project: Speech Synthesiser with the Raspberry Pi Zero by Tim Blythman
  11. Project: AD584 Precision Voltage References by Jim Rowe
  12. Project: AM/FM/CW Scanning HF/VHF RF Signal Generator by Andrew Woodfield
  13. Project: High-current Solid-state 12V Battery Isolator by Bruce Boardman
  14. Feature: Circuit Surgery by Ian Bell
  15. Feature: AUDIO OUT by Jake Rothman
  16. Feature: Make it with Micromite by Phil Boyce
  17. Feature: PICn’Mix by Mike Hibbett
  18. Feature: Max’s Cool Beans by Max the Magnificent
  19. Feature: Electronic Building Blocks by Julian Edgar
  20. PCB Order Form
  21. Advertising Index

This is only a preview of the July 2020 issue of Practical Electronics.

You can view 0 of the 72 pages in the full issue.

Articles in this series:
  • Techno Talk (July 2020)
  • Techno Talk (July 2020)
  • Techno Talk (August 2020)
  • Techno Talk (August 2020)
  • Techno Talk (September 2020)
  • Techno Talk (September 2020)
  • Techno Talk (October 2020)
  • Techno Talk (October 2020)
  • (November 2020)
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
  • Techno Talk (June 2025)
  • Techno Talk (July 2025)
  • Techno Talk (July 2025)
Speech Synthesiser with the Raspberry Pi Zero by Tim Blythman Most electronic devices communicate with us via blinking lights. But humans use speech to communicate virtually any concept easily and clearly. So wouldn’t it be better if your electronic gadgets spoke to you? Now you can make them do just that, with a low-cost Raspberry Pi and our simple hardware and software, in just about any language. They can even play music! W e have published several projects over the years which can be used to play back sounds, and many of these can be (and have been) used to play back recorded voice samples to indicate to a user what is going on inside an electronic device. But you’re usually limited to just a handful of voice samples, restricting the information that you can convey with such devices. Not so with this one, which can generate a virtually unlimited number of different phrases, short or long. They broadcast clearly, in the language of your choice, and with the option of several different accents. You just need to feed in text over a serial port (eg, from just about any microcontroller or computer), and it will be translated into sound. These days, pretty much every portable electronic device (and some which Shown above: the completed Speech Synthesiser consists of a small PCB fitted to a Raspberry Pi Zero board, and measures only 65mm by 31mm and is capable of directly driving a small pair of stereo speakers. We show it here connected to a Arduino board, although any microcontroller or computer which provides a serial interface can be used to control the Speech Synthesiser. 14 are intended to be placed around the home) can speak to users. We wanted to be able to add that capability to any microcontroller-based project in a compact and low-cost package, and that is what we have achieved. Various speech options Single-chip ‘speech solutions’ do exist; for example, the SpeakJet at: www.magnevation.com While capable of generating speech and other sound effects, it still requires an external filter and amplifier. The SpeakJet IC costs aroun £20, and while impressive in what it does for its size, we think our solution is competitive on cost and versatility, even if it is slightly larger. We’ve also seen an Arduino speech shield, closer to £50 in cost, which is more expensive than our solution and also larger. Enter the Raspberry Pi Zero These days, the Raspberry Pi 3B+ can be bought for around £34 plus postage from several resellers (eg, https:// thepihut.com). But the Pi 3B+ is overkill for what we need. So we’ve turned to a smaller relative, the Raspberry Pi Zero. Remarkably, the Raspberry Pi Zero can be had for under £5 from PiHut. It is actually a small form-factor variant of the earlier Raspberry Pi Model B, but the Raspberry Pi Zero lacks features such as Wi-Fi or even a headphone socket. The Pi Zero W adds Wi-Fi, and there is also the Pi Zero WH, which adds WiFi and soldered headers to the mix. It retails for around £13, over twice the cost of the basic Pi Zero. However, all of these choices are excellent value for money. To turn our Pi Zero (of whatever flavour) into a Speech Synthesiser, we need to get audio out and amplify it. For this, we’ve created a small DAC and amplifier board to provide direct stereo speaker drive. Our DAC/amplifier board is the same shape as a Raspberry Pi Zero and sits directly above it. You’ll also need some speakers and a microSD card to create a finished, working Speech Synthesiser, as well as some means of supplying serial commands to the completed unit, so it knows what to say. Advanced users could even program the Raspberry Pi directly in a language such as Python, but you would need to be reasonably confident using a Linux command line. We have also provided some code to allow an Arduino board to control the Speech Synthesiser. Practical Electronics | July | 2020 CON2 +5V 1 +5V 100nF 2 100nF 3 10 F 4 SERIAL 1 3 5 (GP03) 7 (GP04) GND 9 11 (GP17) 13 (GP27) 15 (GP22) 17 (+3.3V) 19 (GP10) 21 (GP09) 23 (GP11) GND 25 27 (GP00) 29 (GP05) 31 (GP06) 33 (GP13) WS 35 37 CON1 (+3.3V) (GP02) 39 470 2 +5V 470 4 +5V 6 (GND) 8 TXD 10 RXD 12 BitCLK 14 GND 16 (GP23) 18 1k (GP24) 20 GND 22 (GP25) 24 (GP08) 26 (GP07) 28 (GP01) 30 GND 32 (GP12) 34 GND 36 (GP16) 38 (GP20) 40 GP21 DATA 1 2 3 BitCLK W Sel DATA 5 Vdd AoutR 8 1k 10nF 3 2 AoutL 1 IC1 LM386N 8 5 100 F CON3 1 2 7 TO RIGHT SPEAKER 4 IC3 7 TDA1543 VrefO GND 4 6 6 1k 10nF 10 F 3 2 6 +5V 1 IC2 LM386N 8 5 100 F 7 CON4 1 2 TO LEFT SPEAKER 4 CON5 1 2 TO/FROM RASPBERRY PI 3 SC SPEECH Speech Synthesiser / Audio Playback HatFOR for RASPBERRY RaspberryPI Pi SYNTHESISER/AUDIO PLAYBACK HAT LEFT LINE OUT GND RIGHT LINE OUT 20 1 9 Fig.1: the circuit of the Raspberry Pi hat which adds the ability to drive two speakers. It can be used for speech synthesis or general audio playback. Audio data comes from the Pi via header socket CON1 in I2S digital format and is fed to DAC IC3. The analogue audio signals are then coupled to amplifiers IC1 and IC2 and on to headers CON3 and CON4, which connect to the speaker(s). The resistor shown in red is left off for 3.3V signal levels on CON2. Why try Pi? The Raspberry Pi series of single-board computers are astonishingly cheap for what they are, and this project would work with any current variant of the Raspberry Pi. The larger models will result in a less compact finished product, but would provide a great way to experiment with speech synthesis before committing to buying another, smaller Pi. The speech synthesis software we are going to use is an open-source project called ‘espeak-ng’, see: https://github. com/espeak-ng/espeak-ng The nice thing about espeak-ng is that it includes many different languages and voices, so it is perfect if you need speech in English or just about any other language. You can download variants of espeak-ng for different operating systems, such as Windows, if you would like to hear what it sounds like first. You can find these downloads at: https://github. com/espeak-ng/espeak-ng/releases Since the Pi Zero is capable of running a wide range of advanced software, we’ve included some extra features in our Speech Synthesiser. We’ve also included another opensource program, ‘madplay’, just visit: https://sourceforge.net/projects/mad/ files/madplay/ It can decode and play MP3 files, so if you also want to use your Speech Synthesise as a simple sound-effects module, you can do that. If you have one of the Wi-Fi-enabled Pi variants, the Speech Synthesiser can Practical Electronics | July | 2020 also become a very simple internet radio. Instead of playing a file, madplay can decode and play an internet radio station using a single command. We developed the software for this project using a Raspberry Pi WH, as the Wi-Fi allowed us to download the necessary software packages directly from the Internet. This also lets us use SSH (secure shell) via Wi-Fi to tweak our settings remotely. So while the Pi Zero is the cheapest option, and requires the least power to operate, you do give up some interesting possibilities compared to the WiFi-enabled variants. Hardware overview The Speech Synthesiser consists of a few parts, primarily the Raspberry Pi itself, plus a ‘hat’ that we have designed, which plugs into it and allows it to drive one or two speakers. This is necessary because the Raspberry Pi Zero does not have any onboard analogue audio outputs. The circuit for this ‘hat’ is shown in Fig.1. It connects to the pin header of the Raspberry Pi via CON1, a 2x20 pin socket. CON2 is a 4-pin header which makes the 5V supply from the Raspberry Pi available (eg, to power an Arduino board or similar), plus a 2-wire serial interface for control. The three resistors between CON2 and CON1 allow a 5V device like an Arduino to communicate with the Raspberry Pi’s serial port, which operates at 3.3V. If you will be controlling the Speech Synthesiser from a 3.3V micro board or similar, then you should replace the two 470resistors with wire links (or fit them anyway, it won’t matter) and omit the 1kresistor to disable the voltage conversion. This UART serial port is the primary means of control and communication between the external microcontroller and the Raspberry Pi microcomputer, which handles all the speech synthesis and audio playback tasks. IC3 is a TDA1543 16-bit digital-to-analogue converter (DAC). It is fed digital audio data, in I2S format, from the Raspberry Pi on pins 12, 35 and 40 of CON1. These are the bit clock, word clock and serial data pins respectively. Pins 6 and 8 of IC3 are the analogue audio outputs, which act as current sinks. The current flow is proportional to the desired audio signal voltage levels for the two stereo channels. These currents are converted into voltages by the two 1kresistors connected between those pins and the voltage reference output, pin 7, which sits at around 2.2V and can supply up to 2.5mA. DAC switching artefacts are attenuated by filtering from the 10nF capacitors across these resistors. The resulting voltage signals are coupled to the non-inverting inputs of audio amplifiers IC1 and IC2 via 10µF non-polarised capacitors. IC1 and IC2 are LM386 amplifiers which need few external components. 15 Fig.2: the Pi audio hat is quite compact and easy to build, with relatively few components. Take care with the orientation of the ICs and electrolytic capacitors. CON1 is mounted on the underside and plugs into the GPIO header on the Raspberry Pi host. CON2 is for serial communications. The resistor shown in red is left off for 3.3V signal levels on CON2. Speaker wires could be soldered directly to the board, rather than fitting headers CON3 and CON4. The dotted outline at left shows the size of the regular Raspberry Pi PCB, giving an idea of how the board would fit on one. Their 5V supply from the Raspberry Pi is bypassed by a shared 100nF capacitor. Their outputs are AC coupled to the speaker terminals, CON3 and CON4, by a pair of 100µF electrolytic capacitors which remove the DC bias in the signal. This is provided by IC1 and IC2, to keep the signals within their supply rails. With pins 1 and 8 of IC1 and IC2 left open, each amplifier provides a voltage gain of 20. They can both deliver around 250mW into an 8load. The line-level signals are fed separately to pin header CON5 in case you need to feed them elsewhere, but keep in mind that these signals are not ground-referenced, but instead have a DC bias of around 1V. Software The software for this project can be downloaded from the July 2020 page of the PE website. This is a large download, around 400MB because the software is supplied as a complete installation of the Raspbian Lite operating system, with some extra packages and settings incorporated. Raspbian Lite dispenses with the graphical user interface normally included with Raspian, reducing the install size (and therefore download size) substantially. Fig.3: Win32diskimagewriter is a Windows program used to write the Pi software to the microSD card. You can start with our pre-configured image, or a basic Raspbian Lite installation if you are customising your software. Take great care using Win32 diskimagewriter, as it can overwrite your data if used incorrectly. 16 You can fit the software on a 2GB microSD card, although larger cards can be used. You can either write our supplied image directly to your card, or follow the instructions below to set up the operating system in a step-by-step fashion. The step-by-step method is more involved and requires a bit more knowledge of the Linux command line. One disadvantage of using our 2GB image is that your file system will be limited to 2GB, even if you use a larger card, and much of the space is already taken up by the operating system. If you need more than 2GB (eg, you want to store a large number of audio files on the card), then you should use the step-by-step process and a highercapacity card. The step-by-step approach is also best if you wish to customise your setup, but note that you will need a Raspberry Pi variant with Wi-Fi to download the packages. As noted above, we’re using espeakng and madplay to provide the audio functions. We also need to apply some custom settings to enable the UART serial control interface and the I2S (digital audio) interface. Plus, if you’re using a Wi-Fi-enabled variant, it’s necessary to set up the Wi-Fi interface. We’re also configuring the Pi to boot from the microSD card in a read-only mode. This allows us to simply remove power when we’re finished with the unit, rather than having to send a serial command to perform a ‘clean shutdown’, as would be necessary if the card was writeable during use. This does not permanently make the card read-only, as you can easily add a jumper to enable write access temporarily. Building the DAC and amplifier board The DAC/amplifier ‘hat’ board is a handy little device that can be fitted to any variant of the Raspberry Pi. Use the PCB overlay diagram, Fig.2, as a guide during construction. Start by fitting the resistors. As mentioned earlier, leave out the 1k resistor at upper-right if you will be controlling the Raspberry Pi from a microcontroller that has 3.3V I/O levels. Follow with the ICs, which can either be soldered directly to the board or plugged into sockets. Regardless, ensure they are oriented correctly, with their pin 1 indicators towards the bottom of the board, as shown in Fig.2. Next are the MKT and ceramic capacitors, which are not polarised, then the electrolytic capacitors, which are. Their longer leads indicate the positive end and this must face towards the right side of the board, as shown by the ‘+’ signs on the overlay diagram and PCB itself. The stripe on the capacitor cans indicates the negative end and so should face away from the ‘+’ signs. Finally, fit the pin headers, with the 2x20 pin socket mounted on the underside of the board as shown. You might like to plug it into the Raspberry Pi board before soldering it, to ensure it sits correctly. You could use a stackable header here, which would be useful if you plan to connect any of the other Raspberry Pi I/O or supply pins to external circuitry (other than the serial port, which is already wired to CON2 for you). Practical Electronics | July | 2020 Reproduced by arrangement with SILICON CHIP magazine 2020. www.siliconchip.com.au The DAC board simply plugs into the header socket on the Pi board, as seen at left and above. The complete assembly is quite compact. If you require an even smaller footprint, the stackable header can be replaced by a simple female header, or even omitted and the DAC and amplifier board soldered directly to the Raspberry Pi. Alternatively, you could dispense with CON1 entirely and solder the hat directly to the Pi. But if you do this, take care that the underside of the DAC and amplifier board does not touch the top of the Pi. You may like to slide a strip of plastic or insulating card between the two to ensure separation. Keep in mind that you will need access to the microSD card slot. 5V DC power can be fed to the Pi through CON2 if necessary. Similarly, you could solder wires directly to the speakers rather than fit headers for CON3 and CON4. Once the board is complete, plug it into the Raspberry Pi, and you are ready to install the software. Simple software setup The simplest way to set up the software for the Speech Synthesiser is to download our firmware image. This is a .img file which has been put into a .zip archive to make it smaller. The .img file is a byte for byte ‘snapshot’ of the SD card. Unfortunately, that means it’s not possible to do a simple copy and paste, as the file needs to overwrite everything including the existing file system on the card. So we need to use a program called Win32diskimagewriter to write the image to the SD card. Win32diskimagewriter is written to work on Windows computers and can be downloaded from: https://sourceforge. net/projects/win32diskimager/ If you have a different operating system, then alternatives such as Etcher (www.balena.io/etcher) or the ‘dd’ command under Linux perform the same task. Other programs will have their own instructions for writing images to cards. Connect the microSD card to your PC; if your computer does not have a card slot, use a USB card reader/writer (eg, Jaycar Cat XC4740). Install Win32diskimagewriter and open it. Extract the .img file from the .zip file and click on the folder icon under Practical Electronics | July | 2020 ‘Image File’ to select the image file. Double check that the ‘Device’ setting matches your microSD card. Win32diskimagewriter is capable of writing to almost all sorts of media, so make sure that you aren’t telling it to overwrite your USB stick or hard drive. This is very important! Fig.3 shows an example of what the Win32diskimagewriter program looks like just before writing to the card. Finally, click ‘Write’. This process may take ten minutes or even longer, depending on the speed of the card and other factors. Once the write has completed successfully, remove the microSD card from your computer and insert it into the Raspberry Pi. If you want to set up the software from scratch, refer to the panel overleaf with the step-by-step procedure. Connecting to a host To control the Pi and trigger speech synthesis and audio playback, you need a device which can communicate over a serial UART interface. We used an Arduino Leonardo microcontroller board, as it has two serial ports; one is a virtual serial port connected to the USB interface, while the other is a hardware-based serial port which is connected to a pair of accessible I/O pins. Initially, we’ll just use the Leonardo as an interface between your PC and the Raspberry Pi for testing purposes. Later, you can program the Leonardo to trigger speech and sounds by itself. Start by programming the Leonardo with our USB-Serial_for_Leonardo sketch (also available for download from the July 2020 page of the PE website). This makes the Leonardo equivalent to a simple USB/serial converter. It won’t work on Uno boards, as they only have one hardware serial port. If you don’t have a Leonardo, any other Arduino board based on the ATmega32U4 microcontroller should work. For example, you could use a small ‘Beetle’ board, like the one we used for the PC Remote Control Interface in the August 2019 issue. Connect the Leonardo as shown in Fig.4. This allows it to supply 5V to the Raspberry Pi board. While there will inevitably be a voltage drop across the jumper wires supplying current to the Pi, we did not find this to cause any problems. If you do find you have power problems on the Pi, or noise in the audio, you may be able to solve this by powering the Pi directly using its own micro USB socket and an external USB plugpack. In this case, don’t connect the 5V supply wire. The Arduino board can still get its power from the computer. Another option for the test procedure is to use a CP2102 USB/serial converter. To do that, simply wire up the converter to CON2 on the hat, but note that you will need to leave out or remove the 1k resistor at upper right as these devices operate with 3.3V signalling levels. Terminal software While it’s possible to use the Arduino serial monitor to communicate with the Pi via the Arduino, other terminal programs such as PuTTY or TeraTerm have better terminal emulation support which suits the Raspberry Pi interface. In particular, if you wish to do any file editing on the Pi (which may be necessary to enable specific settings), a proper terminal program is mandatory. Regardless of which terminal software you use, you will need to connect to the Pi at 115,200 baud with eight bits and no parity (8-N-1). Generating speech If you have chosen the step-by-step setup, you will have already tested out the Speech Synthesiser. If you have installed the pre-configured card image, then you will want to see what the Speech Synthesiser is capable of before setting up your controller. After the Pi has booted, you need to log in using the username ‘pi’ and password ‘raspberry’. Later, if you set up an 17 Step-by-step software set-up procedure This process is more involved than simply using the image file, as described in the main body of this article, but gives you a lot more options. We don’t recommend doing this with a Raspberry Pi variant that lacks Wi-Fi since that is a lot more fiddly. But you could set up the SD card on a Raspberry Pi equipped with Wi-Fi and then plug it into a Pi Zero. The first step involves writing a Raspbian Lite image to the card, which is practically the same process as we described for our custom image. These files are available for download from: www.raspberrypi.org/downloads/raspbian/ We used the November 2018 version of Raspbian Lite. Write the Raspbian Lite image to the card using Win32diskimagewriter, Etcher or dd, as described in the text. Under Windows, there should be two drives created, one named ‘boot’ and another that Windows cannot recognise. Windows will say that it wants to format the unrecognised partition, but do not let it! The initial contents of the boot drive are as shown in Fig.6. Open this drive and find the file called ‘config.txt’, then open it with a text editor such as Wordpad or Notepad++. Some versions of Notepad do not recognise the line endings that Linux uses, and may not display the file correctly, so we do not recommend that you use it. Now scroll to the end of the file and make the four changes shown in Fig.7. ssid=“network” psk=“password” key_mgmt=WPA-PS } Change the ‘country’, ‘ssid’ and ‘psk’ values to match those of your own Wi-Fi network, and then save the file. If you think you might want to use SSH to access the Pi, create a file named ‘ssh’ in the root of the boot drive. The file doesn’t need to contain anything; it merely needs to exist. Now safely remove the microSD card from your PC and insert it into the Pi’s microSD card slot. Connect it to your host microcontroller, or whatever you are using to communicate with the Pi over its UART serial port. Power it up and open to the serial port on the Pi at 115,200 baud. After about five seconds, you should see the screen fill with boot messages. When the Pi connects to your Wi-Fi network, a message showing its IP address can be seen; this is handy if you wish to use SSH for further communication. Fig.8: if you can see the login prompt in your terminal window, the Pi is booting correctly, and serial communication is working. Fig.7: we’ve made four changes to the ‘config.txt’ file on our image, as shown here. These set up the Pi to send audio to our DAC and amplifier board, and to turn on the UART to enable serial communications. The first and third enable the I2S output, to send data to the DAC on the hat, and disable the default audio output (which is via the HDMI display connector). The second configures the I2S output to suit the DAC we are using. The fourth change allows the console to be accessed over the UART serial port. If you want to make any more changes to this file, now is the time, as it will be easier to perform edits on a PC than on the Pi. Save the file when finished. Now you need to create a text file on the boot drive named ‘wpa_supplicant.conf’, and edit it to contain the following lines: country=AU ctrl_interface=DIR=/var/run/wpa_ supplicant GROUP=netdev update_config=1 network={ Arduino (or another device) to control the Pi directly, you will need to program it to wait for the login prompt and then send these strings, followed by newline characters, so that it can log in automatically. Our sample software demonstrates how to do this. The espeakng program we’re using for speech 18 After a minute, you will see the login prompt, as shown in Fig.8. The default username is ‘pi’ and the default password is ‘raspberry’. Enter these, and you will end up at the command prompt, from which we can continue to set up the Pi. Run the following command to update the package list, by typing the command and then pressing Enter. It may take a few minutes, or even longer: sudo apt update Then run: sudo apt-get install espeak-ng raspi-gpio madplay This installs the espeak-ng, raspi-gpio and madplay programs. You may be prompted during the install; press ‘y’ and Enter to proceed. While the raspi-gpio program is not necessary for the Speech Synthesiser, it will be handy if you wish to use the Pi’s other GPIO (general purpose input/output) pins. At this point, everything should be working sufficiently to allow the Speech Synthesiser to function. It can be tested by running this command at the prompt: espeak-ng “testing” You should hear the word ‘testing’ coming through the speakers. synthesis has a multitude of options, and a full list of command parameters can be listed by typing the command: The parameters start with a dash and are usually listed before the text to be spoken. For example, type: espeak-ng - - help espeak-ng -ven-us “testing” For example, using the voice parameter, we can apply a different accent. You should then hear the word ‘testing’ in an American accent. Or try: Practical Electronics | July | 2020 The next step is to set the microSD card to be read-only. Before you do this though, you may wish to install more programs or copy other files, as it will be easier now than later. We say we are setting the microSD card to be read-only, note that this is only a software setting this is used by the Pi and does not affect whether or not it can be written by other systems. There also some utilities installed which allow the Pi to use a ramdisk overlay, for any programs that expect to be able to write to the disk. If you wish to write files to the ramdisk for your own application, the easiest way is to create a file in the /tmp folder, which exists on the ramdisk. But note that its contents will be lost the next time the Pi is rebooted or powered down. To set up the read-only SD card, run the command: wget https://raw.githubusercontent.com/adafruit/ Raspberry-Pi-Installer-Scripts/master/read-only-fs.sh This downloads the required script. When the download completes successfully, run this command: sudo bash read-only-fs.sh This will provide several prompts to be answered before applying its settings. There are options to set a GPIO pin as a jumper to GND, to allow write access (the jumper is only read at boot time and applies until the next reset). We suggest setting this to GPIO21, as it can easily be jumpered to GND by placing a jumper across two pins of the GPIO header. This is actually one of the pins used for the I2S audio data, but the jumper only needs to be placed long enough to be detected at boot time, so will not interfere with the audio. Fig.9 shows the pin allocations for the Raspberry Pi header, including the suggested jumper location. GPIO16 can be set to allow a jumper or external transistor to shut down the Pi. Both of these pins can be configured differently in the script. Just follow the prompts. You can also choose to force the Pi to reboot on a kernel panic (ie, an unrecoverable operating system fault), which may be handy, although that is unlikely to happen. Now that’s all done, download and install some packages and apply the settings you have chosen. You can reboot after this by running the command: sudo reboot The software on the Pi has now been set up and is ready to use. Parts list (audio hat) 1 double-sided PCB coded 01106191, 65 x 31mm – available from the PE PCB Service 1 2x20 way header socket (CON1) [Jaycar HM3228 or Altronics P5387 for stackable variant] 1 4-way header or socket (CON2) for connection to the host microcontroller 2 2-way male header (CON3, CON4) [optional, for speaker connections] 1 3-way male header (CON5) [optional, line out] Semiconductors 2 LM386 audio amplifier ICs (IC1,IC2) 1 TDA1543 stereo DAC IC (IC3) (available on eBay – see below)* Capacitors 2 100µF 10V electrolytic 2 10µF multi-layer ceramic [eg, Digi-key Cat 445-181284-ND] 2 100nF MKT or multi-layer ceramic 2 10nF MKT Resistors (all 1/4W 1% metal film) 3 1kΩ 2 470Ω Other parts for complete Speech Synthesiser 1 Raspberry Pi Zero, Zero W or Zero WH [eg, from https://thepihut.com] 1 power supply to suit Raspberry Pi 1 microSD card, 2-32GB 1 or 2 small 8Ω speakers [eg, Jaycar AS3004] 1 microcontroller board (eg, Arduino Leonardo) 4 jumper wires to connect a microcontroller to Speech Synthesiser board Wire or jumper wires to connect speakers * Note that the TDA1543A, which is now much more common to find than the TDA1543, is not directly compatible. It expects a different digital audio format. We have revised software, which allows you to use the TDA1543A, available for download. Playing MP3 files and internet radio As we noted earlier, you can also use ‘madplay’ to play MP3 files or internet radio streams. Using this software is straightforward. For example, issue the following command: madplay file.mp3 This will play the ‘file.mp3’ track, assuming it is located in the current directory of the Pi. If the file name has spaces or other special characters in it, put the name in quotes (single or double). You can issue this command: madplay - -help Fig.9: pinout of the Raspberry Pi’s 2x20 way header, with the functions used by our software shown in red (I2S audio data) and blue (serial transmit/receive), along with the recommended shutdown and write-enable jumper locations. If you fit a stackable header to the hat board, jumpers and other accessories can still be easily connected to the Pi. espeak-ng -s 125 -v en+f5 “testing” This will also say ‘testing’, but in a female-sounding voice. Of course, you can modify the text inside the quotes to make it say different words and phrases. Other parameters such as reading speed, voice pitch and volume can also be adjusted similarly. See the output of the ‘help’ command mentioned above. Practical Electronics | July | 2020 This lists the command line parameters that madplay accepts. To play an internet radio stream, you will need a version of the Pi with Wi-Fi, and that Wi-Fi needs to be configured to connect to the internet via your router. For this task, we’re combining two Linux commands: the aforementioned madplay, to play the audio, plus a package called ‘wget’, which downloads the audio stream over the internet. These are combined in a single command, with the content of the stream being piped by the wget command from its source URL to the input of madplay. The stream will continue unless there is an error, or it can be stopped early by pressing Ctrl-C. For example: wget -O - “http://us5.internet-radio.com:8487/” | madplay 19 Fig.4: connect the Leonardo board to the Speech Synthesiser as shown, for testing or to develop your own Arduino code to drive the Synthesiser. Note that the Pi will draw a few hundred milliamps from the 5V supply, so ensure that it can get the power it needs or you may have glitches. It isn’t always obvious what the URL is for the actual radio stream, as you’re expected to use an online directory to find and play the streams. We found it useful to visit www.internet-radio.com and then opening up each .m3u file in a text editor (eg, notepad) to determine each station’s stream URL. Putting this URL into the above command should then allow you to play that station using the Pi. Controlling this all automatically Our final goal was to be able to use the Arduino board to control the Speech Synthesiser and audio playback automatically. To this end, we’ve created a basic sample sketch which communicates with the Pi, including the login process. Any text sent to the Arduino over the regular serial monitor is then sent to the Pi as a command, to be spoken. Note though, if the Pi is still booting when you send the text, you will have to wait for it to finish before hearing it spoken. The sample sketch is called Pi_TTS_Interface and is again available for download from the PE website. Upload this to the Leonardo board using the usual procedure and open a serial terminal or the serial monitor. The sketch will report on its status and prompt for text to be spoken when ready. An example of the output of this sketch is shown in Fig.5. You can use this sketch as a starting point for your own voice control schemes. As the cliche says: the sky’s the limit! What else can you do? As a small computer in its own right, the Pi is capable of much more than what we’ve outlined here, especially the versions equipped with Wi-Fi such as the Pi Zero W. There’s a lot of information available on the internet on how to program the Raspberry Pi, so if you’re keen to make yours do more, head over to your favourite search engine and start investigating the possibilities. You’ll learn a lot more by ‘doing’ than by ‘reading!’ Fig.5 (above): our sample program logins into the Pi’s console and then sends commands to speak whatever is typed into the serial monitor. When the ‘Ready: type speech’ prompt appears, it is ready for speech synthesis. Fig.6 (right): some files on the microSD card for the Pi can be edited on a PC as the ‘boot’ volume uses the common FAT file system. This is much easier to do than using the Pi’s inbuilt text editor. The config.txt file contains many settings, including which services are started at boot time. 20 Practical Electronics | July | 2020