Silicon ChipSpeech Synthesis using a Raspberry Pi Zero - July 2019 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: New motor vehicles should have built-in dashcams
  4. Feature: Radiation Hardening by Dr David Maddison
  5. Project: Dual Battery Isolator for 4WD, RVs, Caravans, etc by Bruce Boardman
  6. Feature: Modern PCBs – how they’re made by Tim Blythman
  7. Product Showcase
  8. Project: Speech Synthesis using a Raspberry Pi Zero by Tim Blythman
  9. Subscriptions
  10. Feature: El Cheapo Modules: AD584 Precision Voltage References by Jim Rowe
  11. Serviceman's Log: Repairs for a 'key' client by Dave Thompson
  12. Project: Building the RF Signal Generator (Part 2) by Andrew Woodfield, ZL2PD
  13. Project: DSP Active Crossover and 8-channel Parametric Equaliser by Phil Prosser & Nicholas Vinen
  14. Vintage Radio: The National Panasonic AKQ Walkabout portable by Ian Batty
  15. PartShop
  16. Market Centre
  17. Advertising Index
  18. Outer Back Cover

This is only a preview of the July 2019 issue of Silicon Chip.

You can view 37 of the 112 pages in the full issue, including the advertisments.

For full access, purchase the issue for $10.00 or subscribe for access to the latest issues.

Items relevant to "Dual Battery Isolator for 4WD, RVs, Caravans, etc":
  • Battery Isolator Control PCB [05106191] (AUD $7.50)
  • Battery Isolator Mosfet PCB [05106192] (AUD $10.00)
  • Battery Isolator PCB patterns (PDF download) [05106191-2] (Free)
  • Battery Isolator lid panel artwork and drilling template (PDF download) (Free)
Items relevant to "Speech Synthesis using a Raspberry Pi Zero":
  • Raspberry Pi Audio Breakout PCB [01106191] (AUD $5.00)
  • TDA1543A DAC IC (Component, AUD $2.00)
  • TDA1543 DAC IC (Component, AUD $3.00)
  • Raspberry Pi Speech Synthesis software (Free)
  • Raspberry Pi Audio Breakout PCB pattern (PDF download) [01106191] (Free)
Articles in this series:
  • El Cheapo Modules From Asia - Part 1 (October 2016)
  • El Cheapo Modules From Asia - Part 1 (October 2016)
  • El Cheapo Modules From Asia - Part 2 (December 2016)
  • El Cheapo Modules From Asia - Part 2 (December 2016)
  • El Cheapo Modules From Asia - Part 3 (January 2017)
  • El Cheapo Modules From Asia - Part 3 (January 2017)
  • El Cheapo Modules from Asia - Part 4 (February 2017)
  • El Cheapo Modules from Asia - Part 4 (February 2017)
  • El Cheapo Modules, Part 5: LCD module with I²C (March 2017)
  • El Cheapo Modules, Part 5: LCD module with I²C (March 2017)
  • El Cheapo Modules, Part 6: Direct Digital Synthesiser (April 2017)
  • El Cheapo Modules, Part 6: Direct Digital Synthesiser (April 2017)
  • El Cheapo Modules, Part 7: LED Matrix displays (June 2017)
  • El Cheapo Modules, Part 7: LED Matrix displays (June 2017)
  • El Cheapo Modules: Li-ion & LiPo Chargers (August 2017)
  • El Cheapo Modules: Li-ion & LiPo Chargers (August 2017)
  • El Cheapo modules Part 9: AD9850 DDS module (September 2017)
  • El Cheapo modules Part 9: AD9850 DDS module (September 2017)
  • El Cheapo Modules Part 10: GPS receivers (October 2017)
  • El Cheapo Modules Part 10: GPS receivers (October 2017)
  • El Cheapo Modules 11: Pressure/Temperature Sensors (December 2017)
  • El Cheapo Modules 11: Pressure/Temperature Sensors (December 2017)
  • El Cheapo Modules 12: 2.4GHz Wireless Data Modules (January 2018)
  • El Cheapo Modules 12: 2.4GHz Wireless Data Modules (January 2018)
  • El Cheapo Modules 13: sensing motion and moisture (February 2018)
  • El Cheapo Modules 13: sensing motion and moisture (February 2018)
  • El Cheapo Modules 14: Logarithmic RF Detector (March 2018)
  • El Cheapo Modules 14: Logarithmic RF Detector (March 2018)
  • El Cheapo Modules 16: 35-4400MHz frequency generator (May 2018)
  • El Cheapo Modules 16: 35-4400MHz frequency generator (May 2018)
  • El Cheapo Modules 17: 4GHz digital attenuator (June 2018)
  • El Cheapo Modules 17: 4GHz digital attenuator (June 2018)
  • El Cheapo: 500MHz frequency counter and preamp (July 2018)
  • El Cheapo: 500MHz frequency counter and preamp (July 2018)
  • El Cheapo modules Part 19 – Arduino NFC Shield (September 2018)
  • El Cheapo modules Part 19 – Arduino NFC Shield (September 2018)
  • El cheapo modules, part 20: two tiny compass modules (November 2018)
  • El cheapo modules, part 20: two tiny compass modules (November 2018)
  • El cheapo modules, part 21: stamp-sized audio player (December 2018)
  • El cheapo modules, part 21: stamp-sized audio player (December 2018)
  • El Cheapo Modules 22: Stepper Motor Drivers (February 2019)
  • El Cheapo Modules 22: Stepper Motor Drivers (February 2019)
  • El Cheapo Modules 23: Galvanic Skin Response (March 2019)
  • El Cheapo Modules 23: Galvanic Skin Response (March 2019)
  • El Cheapo Modules: Class D amplifier modules (May 2019)
  • El Cheapo Modules: Class D amplifier modules (May 2019)
  • El Cheapo Modules: Long Range (LoRa) Transceivers (June 2019)
  • El Cheapo Modules: Long Range (LoRa) Transceivers (June 2019)
  • El Cheapo Modules: AD584 Precision Voltage References (July 2019)
  • El Cheapo Modules: AD584 Precision Voltage References (July 2019)
  • Three I-O Expanders to give you more control! (November 2019)
  • Three I-O Expanders to give you more control! (November 2019)
  • El Cheapo modules: “Intelligent” 8x8 RGB LED Matrix (January 2020)
  • El Cheapo modules: “Intelligent” 8x8 RGB LED Matrix (January 2020)
  • El Cheapo modules: 8-channel USB Logic Analyser (February 2020)
  • El Cheapo modules: 8-channel USB Logic Analyser (February 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules (May 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules (May 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules, Part 2 (June 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules, Part 2 (June 2020)
  • El Cheapo Modules: Mini Digital Volt/Amp Panel Meters (December 2020)
  • El Cheapo Modules: Mini Digital Volt/Amp Panel Meters (December 2020)
  • El Cheapo Modules: Mini Digital AC Panel Meters (January 2021)
  • El Cheapo Modules: Mini Digital AC Panel Meters (January 2021)
  • El Cheapo Modules: LCR-T4 Digital Multi-Tester (February 2021)
  • El Cheapo Modules: LCR-T4 Digital Multi-Tester (February 2021)
  • El Cheapo Modules: USB-PD chargers (July 2021)
  • El Cheapo Modules: USB-PD chargers (July 2021)
  • El Cheapo Modules: USB-PD Triggers (August 2021)
  • El Cheapo Modules: USB-PD Triggers (August 2021)
  • El Cheapo Modules: 3.8GHz Digital Attenuator (October 2021)
  • El Cheapo Modules: 3.8GHz Digital Attenuator (October 2021)
  • El Cheapo Modules: 6GHz Digital Attenuator (November 2021)
  • El Cheapo Modules: 6GHz Digital Attenuator (November 2021)
  • El Cheapo Modules: 35MHz-4.4GHz Signal Generator (December 2021)
  • El Cheapo Modules: 35MHz-4.4GHz Signal Generator (December 2021)
  • El Cheapo Modules: LTDZ Spectrum Analyser (January 2022)
  • El Cheapo Modules: LTDZ Spectrum Analyser (January 2022)
  • Low-noise HF-UHF Amplifiers (February 2022)
  • Low-noise HF-UHF Amplifiers (February 2022)
  • A Gesture Recognition Module (March 2022)
  • A Gesture Recognition Module (March 2022)
  • Air Quality Sensors (May 2022)
  • Air Quality Sensors (May 2022)
  • MOS Air Quality Sensors (June 2022)
  • MOS Air Quality Sensors (June 2022)
  • PAS CO2 Air Quality Sensor (July 2022)
  • PAS CO2 Air Quality Sensor (July 2022)
  • Particulate Matter (PM) Sensors (November 2022)
  • Particulate Matter (PM) Sensors (November 2022)
  • Heart Rate Sensor Module (February 2023)
  • Heart Rate Sensor Module (February 2023)
  • UVM-30A UV Light Sensor (May 2023)
  • UVM-30A UV Light Sensor (May 2023)
  • VL6180X Rangefinding Module (July 2023)
  • VL6180X Rangefinding Module (July 2023)
  • pH Meter Module (September 2023)
  • pH Meter Module (September 2023)
  • 1.3in Monochrome OLED Display (October 2023)
  • 1.3in Monochrome OLED Display (October 2023)
  • 16-bit precision 4-input ADC (November 2023)
  • 16-bit precision 4-input ADC (November 2023)
  • 1-24V USB Power Supply (October 2024)
  • 1-24V USB Power Supply (October 2024)
  • 14-segment, 4-digit LED Display Modules (November 2024)
  • 0.91-inch OLED Screen (November 2024)
  • 0.91-inch OLED Screen (November 2024)
  • 14-segment, 4-digit LED Display Modules (November 2024)
  • The Quason VL6180X laser rangefinder module (January 2025)
  • TCS230 Colour Sensor (January 2025)
  • The Quason VL6180X laser rangefinder module (January 2025)
  • TCS230 Colour Sensor (January 2025)
  • Using Electronic Modules: 1-24V Adjustable USB Power Supply (February 2025)
  • Using Electronic Modules: 1-24V Adjustable USB Power Supply (February 2025)
Items relevant to "Building the RF Signal Generator (Part 2)":
  • AM/FM/CW Scanning HF/VHF RF Signal Generator PCB [04106191] (AUD $15.00)
  • ATmega328P programmed with the firmware for the RF Signal Generator [0410619A.HEX] (V14) (Programmed Microcontroller, AUD $10.00)
  • Pulse-type rotary encoder with pushbutton and 18t spline shaft (Component, AUD $3.00)
  • Firmware for the RF Signal Generator [0410619A.HEX] (Software, Free)
  • AM/FM/CW Scanning HF/VHF RF Signal Generator PCB pattern (PDF download) [05105191] (Free)
  • AM/FM/CW Scanning HF/VHF RF Signal Generator panel artwork, cutting diagrams and STL files (downloads) (Free)
Articles in this series:
  • An AM/FM/CW Scanning HF/VHF RF Signal Generator (June 2019)
  • An AM/FM/CW Scanning HF/VHF RF Signal Generator (June 2019)
  • Building the RF Signal Generator (Part 2) (July 2019)
  • Building the RF Signal Generator (Part 2) (July 2019)
Items relevant to "DSP Active Crossover and 8-channel Parametric Equaliser ":
  • DSP Crossover CPU PCB [01106193] (AUD $5.00)
  • DSP Crossover LCD Adaptor PCB [01106196] (AUD $2.50)
  • DSP Crossover front panel control PCB [01106195] (AUD $5.00)
  • DSP Crossover DAC PCB [01106192] (AUD $7.50)
  • DSP Crossover power supply PCB [01106194] (AUD $7.50)
  • DSP Crossover ADC PCB [01106191] (AUD $7.50)
  • DSP Active Crossover complete PCB set (7 boards) [01106191-6] (AUD $40.00)
  • PIC32MZ2048EFH064-250I/PT programmed for the DSP Active Crossover/Equaliser (V8) (Programmed Microcontroller, AUD $30.00)
  • Pulse-type rotary encoder with pushbutton and 18t spline shaft (Component, AUD $3.00)
  • 128x64 Blue LCD screen with KS0108-compatible controller (Component, AUD $30.00)
  • Firmware and source code for the DSP Active Crossover/Equaliser project [0610619A.HEX] (Software, Free)
  • DSP Active Crossover/DDS/Reflow Oven PCB patterns (PDF download) [01106191-6] (Free)
Articles in this series:
  • DSP Active Crossover and 8-channel Parametric Equaliser (May 2019)
  • DSP Active Crossover and 8-channel Parametric Equaliser (May 2019)
  • DSP Active Crossover and 8-channel Parametric Equaliser, part two (June 2019)
  • DSP Active Crossover and 8-channel Parametric Equaliser, part two (June 2019)
  • DSP Active Crossover and 8-channel Parametric Equaliser (July 2019)
  • DSP Active Crossover and 8-channel Parametric Equaliser (July 2019)

Purchase a printed copy of this issue for $10.00.

Speech Synthesiser Speech    with the Raspberry Pi Zero 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 are intended to be placed around the home) can speak to its users. We wanted to be able to add that capability to any microcontrollerbased project in a compact and lowcost package, and that is what we have achieved. Various speech options Single-chip ‘speech solutions’ do exist, such as the SpeakJet (www. magnevation.com/SpeakJet.swf). While capable of generating speech and other sound effects, it still requires an external filter and amplifier. The SpeakJet IC costs over $50, 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 $100 in cost, which is more expensive than our solution and also larger. 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. by Tim Blythman 44 Silicon Chip Australia’s electronics magazine siliconchip.com.au 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 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 470 +5V +5V 470 (GND) TXD 1 2 3 RXD BitCLK W Sel DATA BitCLK GND (GP23) (GP24) GND 5 Vdd IC3 TDA1543 GND 4 AoutR VrefO AoutL 8 1k 10nF 3 2 6 1 IC1 LM386N 8 5 100 F 2 7 SC TO RIGHT SPEAKER 4 7 6 1k 1k 10nF 10 F 3 (GP25) (GP08) 2 (GP07) 6 +5V 1 IC2 LM386N 8 5 100 F 7 CON4 1 2 TO LEFT SPEAKER 4 (GP01) GND (GP12) GND (GP16) (GP20) GP21 CON5 1 DATA 2 TO/FROM RASPBERRY PI 20 1 9 CON3 1 3 SPEECH SYNTHESISER/AUDIO PLAYBACK HAT FOR RASPBERRY PI LEFT LINE OUT GND RIGHT LINE OUT 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 analog audios signals are then coupled to amplifiers IC1 & 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. Enter the Raspberry Pi Zero These days, the Raspberry Pi 3B+ can be bought for around $60 plus postage from several resellers. 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 $10 from Core Electronics (https://core-electronics.com. au/raspberry-pi-zero.html). It is actually a small form-factor variant of the earlier Raspberry Pi Model B. Of course, there is a catch, and the Raspberry Pi Foundation has limited sales of the Raspberry Pi Zero to one per customer. The Raspberry Pi Zero also lacks features such as WiFi or even a headphone socket. The Pi Zero W adds WiFi, but is still subject to the one per customer limit. We tried to buy a Pi Zero and a Pi Zero W at the same time and were not allowed. There is also the Pi Zero WH, which adds WiFi and soldered headers to the mix. There are no limits on the sale of these, fortunately, although it does retail for around $20, or 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 siliconchip.com.au flavour) into a Speech Synthesiser, we need to get audio out and amplify it, and 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. Why try Pi? The Raspberry Pi series of singleboard 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. Australia’s electronics magazine The speech synthesis software we’re using is an open-source project called “espeak-ng” (see https://github.com/ espeak-ng/espeak-ng). 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/espeakng/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” (https:// sourceforge.net/projects/mad/files/ madplay/). It can decode and play MP3 files, so if you also want to use your Speech Synthesiser as a simple sound effects module, you can do that. If you have one of the WiFi-enabled Pi variants, the Speech Synthesiser can 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 July 2019  45 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 & 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. project using a Raspberry Pi WH, as the WiFi allowed us to download the necessary software packages directly from the internet. This also lets us use SSH (secure shell) via WiFi 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 WiFienabled 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 as the Raspberry Pi Zero does not have any onboard analog 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 470resistors with wire links (or fit them anyway, it won’t matter) and omit the 1kresistor 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-toanalog 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 analog audio outputs, which act as current sinks. The current flow is proportional to 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. 46 Silicon Chip desired audio signal voltage levels for the two stereo channels. These currents are converted into voltages by the two 1kresistors connected between those pins and the voltage reference output, pin 7, which sits at around 2.2V and can supply up to 2.5mA. The DAC switching artefacts are attenuated due to the filtering action of the 10nF capacitors across these resistors, and 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 amplifier ICs which need minimal external components. 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 times.They can both deliver around 250mW into an 8load. 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-reference, but instead have a DC bias of around 1V. Software The software for this project can be downloaded from the SILICON CHIP website. It is a large download, around 400MB. That’s because the software is supplied as a complete installation of the Raspbian Lite operating system, Australia’s electronics magazine 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. 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. 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-bystep 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 WiFi 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 WiFi-enabled variant, it’s necessary to set up the WiFi 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 sesiliconchip.com.au rial command to perform a ‘clean shutdown’, as would be necessary if the card was writeable during use. This does not permanenly 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 orientated 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 RaspAustralia’s electronics magazine berry Pi I/O or supply pins to external circuitry (other than the serial port, which is already wired to CON2 for you). 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 siliconchip.com. au/link/aaps If you have a different operating system, then alternatives such as Etcher (www.balena.io/etcher) or the “dd” July 2019  47 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 WiFi since that is a lot more fiddly. But you could set up the SD card on a Raspberry Pi equipped with WiFi 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 Win32-diskimagewriter, 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. The first and third enable the I2S output, to send data to the key_mgmt=WPA-PS } Change the “country”, “ssid” and “psk” values to match those of your own WiFi 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 WiFi network, a message showing its IP address can be seen; this is handy if you wish to use SSH for further communication. After around 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, 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. 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 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={ ssid=”network” psk=”password” 48 Silicon Chip 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. 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. When we say we are setting the microSD card to be read-only, note Australia’s electronics magazine siliconchip.com.au Parts list (audio hat) 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. Fig.9: the 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. siliconchip.com.au 1 double-sided PCB coded 01106191, 65 x 31mm 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) [SILICON CHIP ONLINE SHOP Cat SC3029] 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 1kW 2 470W Other parts for complete Speech Synthesiser 1 Raspberry Pi Zero, Zero W or Zero WH [eg, from Core Electronics] 1 power supply to suit Raspberry Pi 1 microSD card, 2-32GB 1 or 2 small 8W 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 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 which costs a princely $5). Install Win32diskimagewriter and open it. Extract the .img file from the .zip file and click on the folder icon under “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 at left 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 Australia’s electronics magazine July 2019  49 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. 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 SILICON CHIP 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 PC Remote Control Interface in the August 2018 issue (see siliconchip.com.au/Article/11196). 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 50 Silicon Chip 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 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 espeak-ng program we’re using for speech synthesis has a multitude of options, and a full list of command parameters can be listed by typing the command: espeak-ng - - help For example, using the voice parameter, we can apply a different accent. The parameters start with a dash and are usually listed before the text to be spoken. For example, type: espeak-ng -ven-us “testing” You should then hear the word “testing” in an American accent. Or try: 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. There are currently no Australian or New Zealand accents available, but a clever choice of spelling can be used to emulate regional pronunciation. Australia’s electronics magazine siliconchip.com.au 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. Other parameters such as reading speed, voice pitch and volume can also be adjusted similarly. See the output of the “help” command mentioned above. Playing MP3 files and internet radio As we noted earlier, you can also use “madplay” to play MP3s or internet radio streams. Using this software is straightforward. For example, issuing the command: madplay file.mp3 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 to list the command line parameters which madplay accepts. To play an internet radio stream, you will need a version of the Pi with WiFi, and that WiFi 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 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. siliconchip.com.au 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 that 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 our 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 is 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 WiFi 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 SC more by “doing” than by “reading!” Australia’s electronics magazine July 2019  51