Projects / Black Pill two-channel voltmeter

Black Pill two-channel voltmeter

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM32

    Wire the knobs

    Outer legs to 3.3 and GND, wipers to PA0 and PA1.

  3. STM32

    Wire the display

    SDA to PB7, SCL to PB6, power at 3.3 V.

  4. STM32

    Wire the button

    PB0 to GND.

  5. STM32

    Upload the sketch

    Both voltages appear.

  6. STM32

    Turn each knob

    Its channel changes alone.

  7. STM32

    Press the button

    Readings freeze, press again to release.

In short

How do you build a black Pill two-channel voltmeter?

A Black Pill reads two knobs with the ADC and shows both voltages on an OLED together with the difference between them. You need STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), Potentiometer 10 kΩ × 2, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ST-Link GNDBoard GNDGround
ST-Link SWDIOBoard DIODebug data
ST-Link SWCLKBoard DCLKDebug clock
ST-Link 3.3VBoard 3.3 pinPower from the programmer (do not also plug in USB)
Pot A outer legsBlack Pill 3.3 and GNDVoltage divider
Pot A wiperBlack Pill PA0Channel A input
Pot B outer legsBlack Pill 3.3 and GNDVoltage divider
Pot B wiperBlack Pill PA1Channel B input
OLED VCC and GNDBlack Pill 3.3 pin and GNDPower at 3.3 V
OLED SDA and SCLBlack Pill PB7 and PB6I2C data and clock
Hold buttonBlack Pill PB0 and GNDFreezes and releases the readings

Code

A single ADC can read many inputs by switching a multiplexer between them, so one chip gives you several channels, taking turns. The sketch reads each channel as an average of sixteen samples to calm noise, converts the count to volts and shows both with their difference, which is the number you would need to look at the voltage across a part. The hold button freezes the readings so you can write them down. The Black Pill ADC reads only between 0 V and 3.3 V, so never connect anything higher, and to measure more you would add a divider and clamp. The reference is the board 3.3 V supply, which is not exact, so measure it with a multimeter and set VREF to match. Treat the last digit as indicative only, since a bare ADC is noisy in its lowest bits and the two channels share one converter.

Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int CH_A = PA0, CH_B = PA1, BTN = PB0;
const float VREF = 3.3;                          // measure the 3.3 V pin and adjust

float va = 0, vb = 0;
bool hold = false, prevBtn = HIGH;
unsigned long lastDraw = 0, lastEdge = 0;

float readVolts(int pin) {
  long sum = 0;
  for (int i = 0; i < 16; i++) sum += analogRead(pin);   // average 16 reads
  return (sum / 16.0) * VREF / 4095.0;
}

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  analogReadResolution(12);
  Wire.begin();
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
}

void loop() {
  bool b = digitalRead(BTN);
  if (prevBtn == HIGH && b == LOW && millis() - lastEdge > 40) { hold = !hold; lastEdge = millis(); }
  prevBtn = b;

  if (!hold) { va = readVolts(CH_A); vb = readVolts(CH_B); }

  if (millis() - lastDraw >= 150) {
    lastDraw = millis();
    oled.clearDisplay();
    oled.setTextColor(SSD1306_WHITE);
    oled.setTextSize(1);
    oled.setCursor(0, 0); oled.print("Two-channel voltmeter");
    if (hold) { oled.setCursor(104, 0); oled.print("HOLD"); }
    oled.setTextSize(2);
    oled.setCursor(0, 14); oled.print("A "); oled.print(va, 3);
    oled.setCursor(0, 32); oled.print("B "); oled.print(vb, 3);
    oled.setTextSize(1);
    oled.setCursor(0, 54); oled.print("A-B "); oled.print(va - vb, 3); oled.print(" V");
    oled.display();
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Both knobs at one endAbout 0 V on both
Both at the other endAbout 3.3 V on both
Knobs apartThe difference line shows the gap
Hold pressedValues stay put
Compared with a multimeterWithin a few tens of millivolts after setting VREF

If it does not work

A and B change together

Check each wiper goes to its own pin.

Readings jump

Keep wires short and add 100 nanofarad from each input to GND.

Values do not match the meter

Measure the 3.3 V pin and update VREF.

Blank screen

Check the OLED address and I2C wires.

Upload fails

Check the ST-Link wires and the board settings.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the black Pill two-channel voltmeter?

STM32 Black Pill (STM32F411CEU6), ST-Link V2 programmer (clone), Potentiometer 10 kΩ × 2, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the black Pill two-channel voltmeter take to build?

About 150 minutes for a intermediate maker, in 7 steps.

How do I know the black Pill two-channel voltmeter works?

Both knobs at one end: About 0 V on both. Both at the other end: About 3.3 V on both. Knobs apart: The difference line shows the gap.

A and B change together: what should I check?

Check each wiper goes to its own pin.

Readings jump: what should I check?

Keep wires short and add 100 nanofarad from each input to GND.

Values do not match the meter: what should I check?

Measure the 3.3 V pin and update VREF.

Blank screen: what should I check?

Check the OLED address and I2C wires.