12-bit voltmeter on a Blue Pill
Build it step by step
0 of 8 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the knob
Outer legs to 3.3 and GND, wiper to PA0.
Wire the display
SDA to PB7, SCL to PB6, power at 3.3 V.
Install the libraries
Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
Volts and counts appear.
Turn the knob slowly
Watch the 10-bit and 12-bit counts.
Measure the 3.3 V pin
Set VREF to the measured value.
Compare the display with the multimeter
Note the difference.
How do you build a 12-bit voltmeter on a Blue Pill?
A Blue Pill reads a knob with its 12-bit ADC and shows the counts, the voltage and what a 10-bit board would read for the same input. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), Potentiometer 10 kΩ, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 8 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 5
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 | Blue Pill GND | Ground |
| ST-Link SWDIO | Blue Pill DIO | Debug data |
| ST-Link SWCLK | Blue Pill DCLK | Debug clock |
| ST-Link 3.3V | Blue Pill 3.3 pin | Power from the programmer (do not also plug in USB) |
| Pot outer legs | Blue Pill 3.3 and GND | Voltage divider |
| Pot wiper | Blue Pill PA0 | Analog input |
| OLED VCC | Blue Pill 3.3 pin | Power at 3.3 V |
| OLED GND | Blue Pill GND | Common ground |
| OLED SDA | Blue Pill PB7 | I2C data |
| OLED SCL | Blue Pill PB6 | I2C clock |
Code
An ADC turns a voltage into a number. A 10-bit one, like the Uno's, gives 1024 steps and a 12-bit one gives 4096, so each step is four times smaller. On a 3.3 V range that is about 3.2 millivolts per step against 0.8 millivolts. The sketch averages sixteen reads, shows the volts and prints both counts, so you can turn the knob slowly and see the 10-bit count change in steps while the 12-bit count changes in finer ones. Dividing the count by 4095 and multiplying by the reference gives the voltage. The reference is the board's 3.3 V supply, which is not exact, so measure it with a multimeter and change VREF to match. The ADC can read only 0 to 3.3 V: never connect anything higher to a pin. To measure bigger voltages you would add a resistor divider and a clamp, which is a project of its own. Readings from a bare ADC are noisy in the last bits, so treat the last digit as indicative only.
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 ADC_PIN = PA0;
const float VREF = 3.3; // check the 3.3 V pin with a multimeter and adjust
unsigned long lastDraw = 0;
void setup() {
analogReadResolution(12);
Wire.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
}
void loop() {
if (millis() - lastDraw >= 200) {
lastDraw = millis();
long sum = 0;
for (int i = 0; i < 16; i++) sum += analogRead(ADC_PIN); // average 16 reads to calm the noise
int raw12 = sum / 16;
int raw10 = raw12 >> 2; // what a 10-bit ADC would report
float volts = raw12 * VREF / 4095.0;
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("12-bit voltmeter (max 3.3 V)");
oled.setTextSize(3);
oled.setCursor(0, 14); oled.print(volts, 3);
oled.setTextSize(1);
oled.setCursor(0, 46); oled.print("12-bit: "); oled.print(raw12);
oled.setCursor(0, 56); oled.print("10-bit: "); oled.print(raw10);
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 |
|---|---|
| Wiper at GND | About 0 V and 0 counts |
| Wiper at 3.3 V | About 3.3 V and near 4095 |
| Knob in the middle | About 1.65 V |
| Small knob movements | 12-bit count changes four times as finely |
| Compared with a multimeter | Within a few tens of millivolts after setting VREF |
If it does not work
Value stuck at maximum
Check the wiper goes to PA0.
Reading jumps around
Keep wires short and add a 100 nanofarad capacitor from PA0 to GND.
Volts 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 boot jumpers.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the 12-bit voltmeter on a Blue Pill?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), Potentiometer 10 kΩ, SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the 12-bit voltmeter on a Blue Pill take to build?
About 150 minutes for a intermediate maker, in 8 steps.
How do I know the 12-bit voltmeter on a Blue Pill works?
Wiper at GND: About 0 V and 0 counts. Wiper at 3.3 V: About 3.3 V and near 4095. Knob in the middle: About 1.65 V.
Value stuck at maximum: what should I check?
Check the wiper goes to PA0.
Reading jumps around: what should I check?
Keep wires short and add a 100 nanofarad capacitor from PA0 to GND.
Volts 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.
