STM32 Blue Pill ADC Potentiometer and PWM LED: Wiring, Code and Simulator
To build this STM32 Blue Pill ADC Potentiometer and PWM LED project you need an STM32 Blue Pill (STM32F103C8) and a potentiometer, a 220 Ω resistor and a red LED. analogRead(PA0) reads a potentiometer with the 12-bit ADC; analogWrite(PA1) sets an LED’s brightness with hardware PWM from TIM2. Make the 6 connections in the wiring table, upload the Arduino C++ code, and turn the knob while it runs: the millivolts follow, and the LED dims and brightens.
Open this circuit in the free STM32 Blue Pill (STM32F103C8) simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | STM32 Blue Pill (STM32F103C8) | the microcontroller board |
| 1 | Potentiometer | VCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023. |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | red LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | STM32 Blue Pill (STM32F103C8) 3V3 | Potentiometer VCC |
| 2 | STM32 Blue Pill (STM32F103C8) GND | Potentiometer GND |
| 3 | STM32 Blue Pill (STM32F103C8) PA0 | Potentiometer SIG |
| 4 | STM32 Blue Pill (STM32F103C8) PA1 | 220 Ω resistor leg 1 |
| 5 | 220 Ω resistor leg 2 | red LED anode (A, long leg) |
| 6 | STM32 Blue Pill (STM32F103C8) GND | red LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the STM32 Blue Pill (STM32F103C8). No install or account is needed.
- Add the parts. Add a potentiometer, a 220 Ω resistor and a red LED from the parts palette.
- Wire 1. Connect STM32 Blue Pill (STM32F103C8) 3V3 to Potentiometer VCC.
- Wire 2. Connect STM32 Blue Pill (STM32F103C8) GND to Potentiometer GND.
- Wire 3. Connect STM32 Blue Pill (STM32F103C8) PA0 to Potentiometer SIG.
- Wire 4. Connect STM32 Blue Pill (STM32F103C8) PA1 to 220 Ω resistor leg 1.
- Wire 5. Connect 220 Ω resistor leg 2 to red LED anode (A, long leg).
- Wire 6. Connect STM32 Blue Pill (STM32F103C8) GND to red LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Turn the knob while it runs: the millivolts follow, and the LED dims and brightens.
Arduino C++ code
// Read a potentiometer on PA0 with the 12-bit ADC and set an LED's
// brightness on PA1 with hardware PWM (TIM2 channel 2).
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 0..4095
pinMode(PA1, OUTPUT);
}
void loop() {
int raw = analogRead(PA0);
int mv = raw * 3300L / 4095;
analogWrite(PA1, raw / 16); // 0..255
Serial.print("raw=");
Serial.print(raw);
Serial.print(" ");
Serial.print(mv);
Serial.println(" mV");
delay(300);
}
What to try
Turn the knob while it runs: the millivolts follow, and the LED dims and brightens.
Frequently asked questions
How do I wire the ADC Potentiometer and PWM LED project on an STM32 Blue Pill (STM32F103C8)?
Make 6 connections: STM32 Blue Pill (STM32F103C8) 3V3 to Potentiometer VCC; STM32 Blue Pill (STM32F103C8) GND to Potentiometer GND; STM32 Blue Pill (STM32F103C8) PA0 to Potentiometer SIG; STM32 Blue Pill (STM32F103C8) PA1 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to red LED anode (A, long leg); STM32 Blue Pill (STM32F103C8) GND to red LED cathode (C, short leg).
What parts do I need for this STM32 Blue Pill project?
An STM32 Blue Pill (STM32F103C8) and a potentiometer, a 220 Ω resistor and a red LED.
Can I simulate this STM32 Blue Pill project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is built with the STM32duino core and runs on an STM32F103 emulated instruction by instruction: its ARM Cortex-M3 core, NVIC and peripherals (GPIO, EXTI, USART, timers, ADC and I²C).
What does the code do?
analogRead(PA0) reads a potentiometer with the 12-bit ADC; analogWrite(PA1) sets an LED’s brightness with hardware PWM from TIM2.
More STM32 Blue Pill (STM32F103C8) projects
- STM32 Blue Pill HC-05 Bluetooth on USART2: Wiring, Code and Simulator
- STM32 Blue Pill LED Blink and Serial: Wiring, Code and Simulator
- STM32 Blue Pill Button Interrupt (EXTI): Wiring, Code and Simulator
- STM32 Blue Pill I2C BMP280 with Wire: Wiring, Code and Simulator
- STM32 Blue Pill Serial UART Echo: Wiring, Code and Simulator
- STM32 Blue Pill Servo Motor Sweep: Wiring, Code and Simulator