Black Pill two-channel voltmeter
Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the knobs
Outer legs to 3.3 and GND, wipers to PA0 and PA1.
Wire the display
SDA to PB7, SCL to PB6, power at 3.3 V.
Wire the button
PB0 to GND.
Upload the sketch
Both voltages appear.
Turn each knob
Its channel changes alone.
Press the button
Readings freeze, press again to release.
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
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| Pot A outer legs | Black Pill 3.3 and GND | Voltage divider |
| Pot A wiper | Black Pill PA0 | Channel A input |
| Pot B outer legs | Black Pill 3.3 and GND | Voltage divider |
| Pot B wiper | Black Pill PA1 | Channel B input |
| OLED VCC and GND | Black Pill 3.3 pin and GND | Power at 3.3 V |
| OLED SDA and SCL | Black Pill PB7 and PB6 | I2C data and clock |
| Hold button | Black Pill PB0 and GND | Freezes 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.
| Check | Expected |
|---|---|
| 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 |
| Hold pressed | Values stay put |
| Compared with a multimeter | Within 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 →
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.
