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

QtyPartWhat it does
1STM32 Blue Pill (STM32F103C8)the microcontroller board
1PotentiometerVCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1red LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1STM32 Blue Pill (STM32F103C8) 3V3Potentiometer VCC
2STM32 Blue Pill (STM32F103C8) GNDPotentiometer GND
3STM32 Blue Pill (STM32F103C8) PA0Potentiometer SIG
4STM32 Blue Pill (STM32F103C8) PA1220 Ω resistor leg 1
5220 Ω resistor leg 2red LED anode (A, long leg)
6STM32 Blue Pill (STM32F103C8) GNDred LED cathode (C, short leg)

Step by step

  1. 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.
  2. Add the parts. Add a potentiometer, a 220 Ω resistor and a red LED from the parts palette.
  3. Wire 1. Connect STM32 Blue Pill (STM32F103C8) 3V3 to Potentiometer VCC.
  4. Wire 2. Connect STM32 Blue Pill (STM32F103C8) GND to Potentiometer GND.
  5. Wire 3. Connect STM32 Blue Pill (STM32F103C8) PA0 to Potentiometer SIG.
  6. Wire 4. Connect STM32 Blue Pill (STM32F103C8) PA1 to 220 Ω resistor leg 1.
  7. Wire 5. Connect 220 Ω resistor leg 2 to red LED anode (A, long leg).
  8. Wire 6. Connect STM32 Blue Pill (STM32F103C8) GND to red LED cathode (C, short leg).
  9. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  10. 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