Try Embedded

Pico 2: a floating-point filter on the Cortex-M33

A Raspberry Pi Pico 2 smooths a noisy knob with a float low-pass filter on its FPU, works out the voltage in double precision on its coprocessor, and dims an…

Sensors · Intermediate

In a real product: Smoothed throttle input (Robotics): A motor controller filters a hand throttle so the motor follows the hand, not the noise on the wire.

The Pico 2's RP2350 has two Arm Cortex-M33 cores, and each has a floating-point unit: float maths is a single instruction, not a library call as on the Pico's Cortex-M0+. double maths goes to a second helper, the RP2350's double-precision coprocessor. This sketch reads a knob on GP26, smooths it with a first-order low-pass filter in float, works out the voltage in double, and sets an LED on GP15 to the filtered level.

What you'll learn

  • A first-order low-pass filter, y += alpha * (x - y), and how alpha trades smoothness for lag
  • That float runs on the Cortex-M33's FPU and double on the RP2350's coprocessor, both far faster than in software
  • Timing a piece of code with micros() to see what it costs

How it works

Every 10 ms the sketch reads the ADC with 12 bits (analogReadResolution(12)), 0 to 4095 for 0 to 3.3 V. The filter keeps a running value level and moves it a fraction alpha of the way to each new reading: with alpha = 0.1 a sudden jump takes about 22 readings (220 ms) to settle to 90 %. A small alpha smooths more and reacts more slowly.

The voltage is worked out in double precision and printed with four decimals. analogWrite takes 0 to 255 by default, so the 12-bit level is divided by 16 for the LED's PWM duty.

The sketch times 1000 filter steps with micros() once at the start and prints it: under 100 microseconds on the Pico 2, a few dozen clock cycles a step. The same loop on a Pico (RP2040) calls a software routine for each multiply and add.

Try this

  • Change alpha to 0.02 and turn the knob quickly: the LED follows slowly, as a heavily filtered throttle would.
  • Make level a double and compare the timing line: the coprocessor is slower than the FPU, but still fast.
  • Add a second knob on GP27 and filter both.

sketch.ino

// Raspberry Pi Pico 2 (Arduino core): a knob on GP26 (ADC0) filtered in float on the Cortex-M33's FPU, the volts in
// double on the RP2350's coprocessor, and an LED on GP15 dimmed to the filtered level.
const int KNOB = 26;
const int LED = 15;
const float ALPHA = 0.1f;

float level = 0;

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 3000) delay(10);   // the USB port open (up to 3 s), so the timing line is seen
  analogReadResolution(12);
  pinMode(LED, OUTPUT);
  level = analogRead(KNOB);

  // What 1000 filter steps cost.
  volatile float y = 0;
  uint32_t t0 = micros();
  for (int i = 0; i < 1000; i++) y += ALPHA * (2048.0f - y);
  Serial.printf("1000 float filter steps: %lu us\n", micros() - t0);
}

void loop() {
  int raw = analogRead(KNOB);
  level += ALPHA * (raw - level);
  double volts = level * 3.3 / 4095.0;
  analogWrite(LED, (int)(level / 16));
  Serial.printf("raw %4d  filtered %7.1f  %.4f V\n", raw, level, volts);
  delay(10);
}

Lessons behind it