Skip to content

Sensors & Signal Processing

How to make a peak hold meter in code

A peak hold shows the highest recent value of a reading instead of the reading itself. Each sample, if the reading beats the stored peak, copy it in; otherwise let peak fall a little. A flick too quick to see jumps straight to the top and then slides back slowly, so it stays on display long enough to read, and the display is ready for the next one.

Hold

The hold alone is one line:

if (reading > peak) peak = reading;

Nothing ever lowers peak, so a flick of the knob to full lights the lamp, and it stays lit for good. That's also the problem: once it's at the top, a smaller second flick can never show.

Let it fall

Take a little off peak every sample, never below 0:

decay → hold → show
peak = peak - 3;                 // decay
if (peak < 0) peak = 0;
if (reading > peak) peak = reading;   // hold
analogWrite(9, peak / 4);        // show: 0 to 1023 becomes 0 to 255
delay(10);

At a sample every 10 ms, 3 a sample is 300 a second. From full, 1023, back to a resting 200 takes (1023 − 200) ÷ 300, about 2.7 s. A steady reading props the peak up every pass, so the lamp holds steady; a flick leaps up and fades.

Live experiment

Two flicks, four decays

The lamp shows peak ÷ 4, and the knob is flicked twice for a tenth of a second: to full at 0.5 s and to 70 % at 3 s. With no decay the lamp sticks at full and misses the second flick. 1 a pass is still too high when it comes. 3 a pass falls back in time and shows both. 10 a pass is gone in under a second.

Try it yourself: free lesson

“Hold the peak” is lesson 4 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.

Choose the decay

Too slow and a second event hides behind the first. At 1 a sample, 100 a second, a full-scale flick at 0.5 s has only fallen to around 780 by 3 s, so a 70 % flick reading 716 to 740 never beats it. Too fast and it's gone before you can read it: 30 a sample, 3000 a second, empties in about a third of a second. Pick the fall time first, then work the decay out from your sample rate.

Where you'll meet it

Audio mixers show the loudest recent moment as a dot hanging above the level bar, then dropping. Radar guns hold the fastest speed of a pitch. In hardware, a diode charging a capacitor with a resistor across it is the same thing: a peak detector, with the resistor setting the decay.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Hold the peak” is lesson 4 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3A peak hold with no decay at all…
    Show the answer

    Answer: Shows the biggest reading ever, and never shows a smaller one again. It can only go up. The slow decay lets it fall back so the next flick shows too.

    • Why not “Follows every reading”? Following every reading is what a plain display does. A peak hold only ever copies a reading that's higher.
    • Why not “Shows zero”? It shows the biggest reading so far, not zero.
    • Why not “Flickers”? With no decay the peak only ever rises, so it can't flicker back and forth.
  2. Question 2 of 3Taking 1 off the peak each 10 ms pass, a flick to full at 0.5 s, then a flick to 70 % at 3 s. The second flick…
    Show the answer

    Answer: Doesn't show: the peak is still near 790, above 70 %. 1 a pass is 100 a second, so in 2.5 s the peak only falls about 250, from 1023 to near 790. The 70 % flick reads about 716 to 740, below that, so it never beats the peak. 3 a pass falls fast enough.

    • Why not “Resets the peak to zero”? Nothing in a peak hold resets to zero. It only falls by the decay each pass.
    • Why not “Shows, at 716”? The flick reads 716 to 740, but the peak is still near 790, so the compare never copies it.
    • Why not “Doubles the peak”? A peak hold either copies a higher reading or falls by the decay. Nothing in it doubles.
  3. Question 3 of 3The decay is 30 a sample instead of 3. After a flick, the lamp…
    Show the answer

    Answer: Is dim again within a third of a second. 30 a sample is 3000 a second: from 1023 to rest in a quarter of a second. Too fast to read.

    • Why not “Stays bright”? It only stays bright with no decay. Any decay pulls it down.
    • Why not “Fades over three seconds”? Three seconds is the fade at 3 a sample. At 30 a sample it falls ten times as fast.
    • Why not “Never lights”? The flick still lifts the peak and lights the lamp. It just doesn't stay lit long.
Keep practising: free lesson

“Hold the peak” is lesson 4 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.

Questions people ask

What is a peak hold?

A display that shows the highest recent value instead of the current one. It jumps up instantly to any new high and falls back slowly, so a moment too brief to see stays on screen long enough to read. Audio level meters and radar speed guns use it.

How fast should a peak hold decay?

Slow enough to read, fast enough to be ready for the next event. Falling from full to rest in about three seconds suits a meter you glance at. Work it out as decay per sample times samples per second: 3 a sample at 100 samples a second is 300 counts a second.

Is a peak hold the same as a peak detector circuit?

It's the same idea in code. A peak detector circuit uses a diode to charge a capacitor to the highest voltage it sees, and a resistor across the capacitor to let it fall back slowly. The diode is the compare, the resistor is the decay.

Every topic