Beyond the beep: giving embedded projects a voice

An electronics setup featuring a blue microcontroller board wired to a small green circuit board, a BNC cable, and a round speaker resting on a foam block against a white background.

Ehrenpreis – Stage 0

A machine does not always need a screen to communicate what it is doing. Sometimes a short sound is all it takes to confirm an operation, signal a state change or let someone know that a process has finished.

But adding sound to an embedded project is not always as straightforward as it seems.

When working on something like a complex FPGA design or a custom CNC controller, there are usually two obvious options. The first is a buzzer: inexpensive, simple to drive and perfectly adequate for basic alerts. It is also rather limiting. You get the familiar microwave-style beeps, and not much room for expression.

The other option is a proper digital audio subsystem, typically involving an I2S interface, audio data and a suitable amplifier. That is a perfectly reasonable solution when the project already has an audio system, or when it runs on a single-board computer. But it can be excessive when all that is needed is a handful of clear, pleasant sound effects.

There is a gap between those two approaches. Ehrenpreis is an attempt to fill it.

The idea is to make a small, inexpensive microcontroller module capable of producing recognisable, configurable sounds. It can be used as a dedicated hardware module in another design, or as a firmware building block when a suitable microcontroller is already available.

Starting with a small microcontroller

The first decision was to keep the hardware requirements modest. There are several inexpensive 32-bit microcontroller families that could be suitable for this kind of task, including parts from Renesas (RA0E3), Nuvoton (M2003) and NXP (MCX C15).

I decided to use the LPC802 instead – an earlier generation NXP part with similar capabilities. I already had a few devices left over from earlier projects, as well as an OM40000 evaluation board. That made it a convenient platform for testing the concept without introducing another hardware platform into the process.

This was a practical choice rather than a claim that the LPC802 is the best possible device for this task. One consequence, however, is that the firmware should not become too dependent on one particular microcontroller. I am writing it in C with portability between 32-bit microcontroller platforms in mind.

Making audio with PWM

The central question was how to produce a useful audio signal without adding a DAC.

I considered mixing several channels externally, but settled on PWM instead. The principle is similar to the signal-generation approach used in early switching amplifiers: vary the PWM duty cycle to represent the audio waveform, then filter the switching component out.

The PWM carrier runs at approximately 47 kHz, comfortably above the intended audible range. A duty cycle of 50% represents zero audio amplitude, with the waveform moving above and below that midpoint as the audio signal changes.

The timing is worth distinguishing here. With the 15 MHz clock used in the proof of concept, a PWM period of 320 ticks gives a carrier frequency of approximately 46.9 kHz. The sample timer runs at half that rate, giving approximately 23.4 k samples per second. The carrier frequency is therefore not the same as the audio sample rate.

The output passes through a second-order passive RC low-pass filter. This removes the high-frequency PWM component and leaves a usable audio-frequency signal. The signal is then AC-coupled to the amplifier, forming a high-pass filter.

There are compromises here. This is not intended to be a high-fidelity audio player, and the available processing and memory resources are limited. The goal is to produce clear sound effects with enough character to be more useful than a simple buzzer.

After some experimentation, the most pleasing results came with a low-pass cutoff frequency of approximately 1.32 kHz and a high-pass cutoff around 200 Hz.

I also chose a separate audio amplifier rather than driving the speaker directly from the microcontroller. Since an external driver was already going to be necessary, using a dedicated amp isn't adding a BOM item – it's substituting one. This has the major advantage of keeping DC current away from the speaker when silent, bypassing the need for the LC filters typical of classic Class D setups.

The firmware: small building blocks, recognisable sounds

The proof-of-concept firmware is written in C. Its waveform generation uses a lookup table containing a quarter-cycle of a sine wave, together with an envelope curve that shapes the beginning, sustain and decay of a sound.

A quarter-wave table is a convenient way of storing the information needed for a sine wave without keeping a complete waveform in memory. The remaining portions can be reconstructed through symmetry. That is useful on a microcontroller with limited flash and RAM.

The implementation uses a 256-entry table for the positive quarter-cycle. The lookup function reconstructs the other three quadrants by mirroring the table and changing the sign where necessary:

static inline int_fast16_t sine(uint32_t phase) {
    uint32_t p = phase % 1024;

    if (p > 767)
        return -sineTable[255 - (p % 256)];
    else if (p > 511)
        return -sineTable[p % 256];
    else if (p > 255)
        return sineTable[255 - (p % 256)];
    else
        return sineTable[p % 256];
}

The phase is effectively divided into four 256-step quadrants. This gives the firmware a complete periodic waveform without storing all 1024 points.

The envelope is just as important as the waveform itself. This, along with the sinusoidal waveform, is the main difference from a simple buzzer. Shaping the amplitude over time makes it possible to create more pleasant and refined sounds.

The envelope is also implemented as a lookup table. Its values encode the attack, sustain and decay profile, while a small helper function keeps the table access within bounds:

static inline uint_fast8_t decay(int32_t step) {
    return decayTable[(size_t)MIN(255, MAX(0, step))];
}

The initial entries give the sound a short attack, followed by a sustain period and a exponential release. Changing the table changes the character of the sound without requiring a different waveform generator.

The sound definitions are parameterised as well. A chord contains up to four sine-wave components, each with its own phase increment and envelope offset, plus a duration limit:

struct chord {
    uint_fast8_t x1, x2, x3, x4;
    int32_t p1, p2, p3, p4;
    uint_fast16_t smax;
};

/* Three-tone SAE800-style chime */
static const struct chord three_tone = {
    .x1 = 24, .x2 = 20, .x3 = 16, .x4 = 0,
    .p1 = 0,  .p2 = 50, .p3 = 100,
    .p4 = INT32_MAX,
    .smax = 360
};

The x values control the oscillator phase increments, implementing *just intonation*, while the p values determine when their respective envelopes begin. This makes it possible to build different chimes from the same small set of functions. A melody is simply a sequence of these chord definitions.

The initial sound was inspired by the now discontinued SAE 800 integrated circuit from Siemens, as well as the notification tones heard aboard passenger aircraft. The implementation is deliberately customisable, so the same basic generator can produce different tones without changing the hardware.

First sounds on the bench

For the first bring-up, I connected the OM40000 evaluation board to a rather improvised amplifier board and a small speaker.

A prototype green circuit board with a yellow capacitor, a small breakout chip, and yellow wires connected to a small speaker resting on a piece of foam.

There was some debugging involved. The interesting part was not just getting the PWM output running, but finding a reasonable combination of PWM parameters, frequencies and audio sample rates given the limited performance of the LPC802.

These parameters are related, but they are not the same thing: the PWM carrier has to be high enough for filtering, while the audio sample rate determines the highest available frequency. A major factor is the PWM, which has to have enough bit depth to avoid quantization noise, and still fit in the 32-bit calculations. All three need to be considered within the timing budget of the microcontroller.

Once the basic signal path worked, I started experimenting with different tones. The core of the generator is quite simple: calculate the individual oscillators, apply their envelopes, add the results together and normalise the sum around the PWM midpoint.

For a chime, the essential per-sample calculation looks like this:

int32_t y =
    sine(cc->x1 * i) * decay(s - cc->p1) +
    sine(cc->x2 * i) * decay(s - cc->p2) +
    sine(cc->x3 * i) * decay(s - cc->p3) +
    sine(cc->x4 * i) * decay(s - cc->p4);

/* Normalise and centre around 50% duty cycle */
y *= ymax / 2;
y /= sineAmpl * amax;
y += ymax / 2;

CTIMER_SetShadowValue(
    CTIMER0_PERIPHERAL,
    CTIMER0_PWM_0_CHANNEL,
    (uint32_t)y
);

The amax value is calculated for each chime in advance. It represents the maximum combined envelope amplitude, allowing the result to be scaled to the available PWM range. Adding half the PWM period afterwards shifts the signed audio waveform into the unsigned duty-cycle range.

One practical detail is how the sample generation is paced. The firmware uses the MRT interrupt (SysTick would also work) to signal when the next sample is due. The handler itself does very little:

void MRT0_IRQHANDLER(void) {
    MRT_ClearStatusFlags(
        MRT0_PERIPHERAL,
        MRT0_CHANNEL_0,
        kMRT_TimerInterruptFlag
    );

    nextSample = 1;
}

The foreground code writes the next PWM value and waits for that flag before proceeding. This keeps the interrupt short and leaves the actual waveform calculations outside it.

I ended up writing more than a dozen variations. I also converted La Cumparsita into a form the hardware could play. In the firmware, the tune is represented as a sequence of 83 chord entries, using the same oscillator and envelope machinery as the individual sound effects. It's not the best rendition, as I had to round some values to integer.

It was a very satisfying moment: a small, inexpensive microcontroller producing recognisable sound through a circuit assembled largely to test an idea.

What comes next

The proof of concept established that the approach works well enough to justify turning it into a reusable module. It also provided practical information about the signal levels, filter characteristics and firmware requirements that would otherwise have been easy to get wrong on paper.

The next step is to move away from the evaluation board and design a dedicated PCB. That board will include the microcontroller, the signal-conditioning circuit and a separate audio amplifier, with the intention of making it useful both as a demonstration platform and as a module for other projects.

That is the subject of the next stage of Ehrenpreis.

The project is being developed in the open, with the firmware and hardware work collected in the Ehrenpreis GitHub repository.


A quick note on AI: The hardware design, PCB layout, and core firmware logic are 100% my own manual work, with AI only assisting briefly with firmware code cleanup. The heaviest use of AI was actually writing the blog posts — I fed my personal bullet points into a free-tier LLM to generate the prose, then went through and edited it myself to make sure everything reads accurately.