Design

The goal of my MusicBox project is to have fun creating my own microcontroller-based hardware and software to play sound effects and music on small speakers. I wanted to use the smallest number of parts possible while still getting reasonably good audio quality. Preferably I wanted to use parts I already had on hand in my toolbox.

Turning digital signals into sound

A microcontroller is a digital device. Its output pins only produce binary 1's and 0's, represented as 3.3 volts for a 1 and ground for 0. Sound is analog, continuous in nature, not binary. A speaker is driven by electrical currents that need to vary in proportion to the sounds it is trying to produce. To bridge the divide between digital and analog, we need to do digital-to-analog conversion. The RP2040 microcontroller chip I'm using is really good at PWM (Pulse Width Modulation), so that was what I used for to drive the speaker.

What is PWM?

PWM stands for Pulse Width Modulation. It means rapidly turning a digital output signal on and off for varying amounts of time (pulse widths).

For example, most light dimmers use PWM to control the brightness of a lamp or LED light. The PWM signal turns the light on and off 60 times per second or faster, quicker than the eye can notice. The percent of the time the PWM signal is turned on is called the duty cycle. If the PWM signal is on 10% of the time and off 90% of the time, it has a 10% duty cycle. Applied to the light dimmer, it means the light shines at 10% of its maximum brightness.

See: What is PWM: Pulse Width Modulation

How did I use PWM to drive a speaker?

The Suggested RP2040 PWM Audio Design

I looked at the RP2040 hardware design notes section 3.4.1 PWM Audio. Their suggested design uses a PWM output signal connected to a filter circuit consisting of two capacitors and three resistors:

RP2040 recommended PWM audio design

This design does not meet my requirements:

  • It produces a signal too weak to drive a speaker
  • It therefore needs to be connected to an audio amplifier
  • Buying an amplifier doesn't give me as much DIY joy as making my own
  • Buying an amplifier doesn't make use of the parts I already have

My PWM Speaker Driver Design

I researched audio amplifiers to find out how to build my own audio amplifier to turn a weak, filtered PWM signal into a strong one that could drive a speaker. I found that modern audio amplifiers use a class D amplifier design. The class D design has benefits over the older class A and AB designs because it doesn't experience crossover distortion.

See: What is a a Class-D Amplifier, and What Are They Useful For?

This example class D amplifier design comes from an Analog Devices technical article Class D Amplifiers: Fundamentals of Operation and Recent Developments:

Basic half-bridge Class D amplifier

When I looked at this diagram, that's when I had my "aha!" moment. A class D amplifier turns an analog input signal into a PWM signal, and then uses that PWM signal to rapidly switch a speaker "off" and "on", just like a light switch dimmer or a PWM variable-speed motor controller.

Why use a capacitor filter to turn my PWM output signal into a weak analog signal just so a class D amplifier can turn it back into a PWM signal to drive a speaker? I already have a PWM signal!

My design combines analog to digital conversion and signal amplification in one step, by using the classic H-bridge technique used for controlling motors.

This diagram comes from an article titled "What is an H-Bridge?" and shows the basic layout of an H-bridge motor controller circuit:

H-bridge motor controller with BJT transistors

An H-bridge circuit lets you run a motor backward or forward by changing the direction of current through the motor. By using PWM signals as the inputs to the H-bridge you can control not only the direction but the speed of the motor, by varying the PWM duty cycle.

A motor and a speaker have a lot in common: they are inductive in nature because they basically involve putting a electrical current through a coil of wire. In my case instead of making a motor run backwards and forwards I'm sending current in alternating directions through a speaker to make the speaker diaphram move forwards or backwards. I vary the duty cycle to vary the strength of the current and therefore the power of the audio signal.

Here is my speaker driver circuit design: Speaker Driver Schematic

This design only requires 4 transistors, 4 diodes, 4 resistors, 2 1.5V batteries, and a speaker. I already had these parts on hand.

Why are there diodes in the circuit? As I mentioned above, the speaker has an inductive coil and therefore I need diodes to protect the circuit from inductive kickback.

Avoiding the Hazard of Transistor Burnout

People experienced in electronics might notice that this design requires very careful timing to ensure that for example transistors Q1 and Q2 are never both turned on at the same time. That hazard is called "shoot through" and will cause your transistors to overheat and burn out.

These particular BJT transistors take 225 ns to turn off but only 15 ns to turn on. I programmed the microcontroller to include "dead zone" timing to make sure that for each pair of transistors, one transistor was turned off for 225 ns before the other transistor starts turning on. I carefully checked the timing of the PWM signals with an oscilloscope before connecting the transistor outputs together.

H-bridge deadzone check 1

In the oscilloscope screenshot above:

  • Yellow (channel 1) is Pico pin 6 controlling transistor Q1
  • Red/violet (channel 2) is Pico pin 7 controlling transistor Q2
  • Blue (channel 3) is Pico pin 16 controlling transistor Q3
  • Green (channel 4) is Pico pin 17 controlling transistor Q4

Q1 and Q3 are PNP transistors, which means that a low signal turns them on.

Q2 and Q4 are NPN transistors, which means that a high signal turns them on.

In this oscilloscope trace you can see that the yellow and red signals start out high, meaning Q1 is off and Q1 is on. (If you are confused please go back and read the previous two paragraphs about PNP vs NPN transistors.) At the left vertical dashed line the red signal goes low, meaning Q2 starts to turn off. At the right vertical dotted line, you see that the yellow signal goes low, meaning Q1 starts to turn on. The time difference between the two vertical dashed lines is the "dead zone" when both are turned off. The oscilloscope shows that the dead zone is 230 ns, which meets the requirement that it be at least 225 ns. (N.B. ns = nanosecond, one billionth of a second. These are precise timing requirements!)