Monitoring voltage and current in real time is a fundamental skill for any embedded project, especially battery-powered designs. The STM32’s ADC can continuously scan multiple analog channels and transfer results to memory via DMA without any CPU involvement. In this lesson you will combine that with the analog watchdog, a hardware comparator that fires an interrupt the instant a reading crosses your threshold, so your firmware reacts to dangerous conditions within microseconds. #STM32 #ADC #AnalogWatchdog
What We Are Building
Voltage and Current Monitor with Threshold Alerts
A monitoring system that continuously reads voltage from a potentiometer (ADC channel) and current from an INA219 sensor module (I2C). The ADC runs in continuous scan mode with DMA, reading three channels: the potentiometer voltage, the internal temperature sensor, and the internal voltage reference. The analog watchdog monitors the potentiometer channel and triggers an alert (LED flash + serial warning) when the voltage exceeds a configurable threshold.
The ADC scan mode converts multiple channels in sequence, and DMA transfers each result to memory automatically. The CPU only needs to read the final values from the DMA buffer.
The STM32F103 has two ADC peripherals (ADC1 and ADC2), each with 12-bit resolution and up to 1 MHz sampling rate. ADC1 has a dedicated DMA channel (DMA1 Channel 1), making it the preferred choice for continuous background conversions. The ADC can convert a sequence of up to 16 channels in a single scan, and in continuous mode it repeats the sequence automatically.
ADC Clock Requirements
The ADC clock must be between 1 MHz and 14 MHz. It is derived from the APB2 clock through a prescaler:
APB2 Clock
Prescaler
ADC Clock
72 MHz
/6
12 MHz
72 MHz
/8
9 MHz
We use /6 for the fastest valid ADC clock (12 MHz).
The analog watchdog is a hardware feature that compares every ADC conversion result against programmable high and low thresholds.
When a conversion falls outside the allowed range, it sets a flag and optionally triggers an interrupt. This happens in hardware at ADC speed, so your firmware is notified within one conversion cycle (about 20 us at 12 MHz). Compare this to polling the ADC value in software, which depends on your loop speed and could miss brief spikes entirely.
Watchdog Configuration
#defineAWD_HIGH_THRESHOLD3000 /* ~2.4V at 3.3V reference */
The STM32F103 ADC does not have hardware oversampling (unlike newer STM32 families like the L4 or G4). However, you can implement it in software by averaging multiple samples. Oversampling by 4x and dividing by 2 gives you approximately 1 extra bit of resolution (from 12-bit to ~13-bit effective). Oversampling by 16x and dividing by 4 gives about 2 extra bits. The DMA circular mode makes this easy because the buffer continuously refills.
The INA219 is a high-side current and power monitor with an I2C interface. It measures the tiny voltage drop across a shunt resistor to determine current flow.
It measures the voltage drop across a shunt resistor to calculate current, and it also measures the bus voltage. Most INA219 breakout boards include a 0.1 ohm shunt resistor, giving a maximum measurement range of about 3.2A. The INA219 has a built-in 12-bit ADC, so it returns calibrated current and power values directly over I2C.
Converting raw ADC values to physical units (voltage, temperature)
DMA skills:
ADC-to-memory DMA with circular mode for continuous updates
Buffer sizing for multi-channel scans
Software oversampling using DMA circular buffers
Analog watchdog:
Hardware threshold monitoring with high/low limits
Single-channel watchdog configuration
Interrupt-driven alert response at ADC speed
Sensor integration:
INA219 current sensor initialization and calibration over I2C
Reading bus voltage and current from I2C registers
Combining internal ADC readings with external I2C sensor data
Comments
Loading comments...
Predict first
A program reads a battery voltage 4 times a second. The ADC itself does not stop: it converts the input about 16,000 times a second. The voltage rises above the safe limit for 2 ms and comes back. Does the program notice?
Check yourself
Three questions. Each wrong choice is a mistake people really make, so read the note after you answer.
1. A board runs from a battery with no regulator, so the battery voltage is also the ADC reference. With a new battery the code for the internal reference (a fixed 1.20 V) was 1489. Today it is 1638. What has happened?
2. You add a fourth channel to the scan. The ADC clock stays at 12 MHz and every channel keeps the sample time of 239.5 cycles. The analog watchdog still watches only channel 0. What is the longest time that channel 0 can be above its limit before the watchdog sees it?
3. A pressure sensor gives 0.5 V to 2.5 V. The ADC reference is 3.3 V. You want the analog watchdog to interrupt when the sensor output passes 1.5 V. What do you write to the high threshold register?
See it: a short spike between two looks
Main loop sees it
Watchdog checks in the spike
Alert after63 µs at most
Each pass of the sweep puts the spike at a new random time. Gold dots are the looks of the main loop; a dot inside the spike is a catch. The lower strip magnifies 6 ms around the spike.
Watch it build: from 72 MHz to adc_values[]
The ADC clock comes from APB2, 72 MHz, and must stay between 0.6 and 14 MHz.
Divide by 6: 12 MHz, the fastest clock inside the limit. Divide by 4 would give 18 MHz.
One conversion: 239.5 cycles of sampling and 12.5 to convert, 252 cycles, 21 µs at 12 MHz.
SQR3 sets the order CH0, CH16, CH17. One scan is 3 × 21 µs = 63 µs.
After each conversion DMA channel 1 copies DR into the next place of adc_values. CNDTR = 3.
Circular mode goes back to [0], so the array always holds the newest scan, 15,873 times a second.
Found this useful?
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
Text size
←→
Previous and next card. On a turned card: Not yet, I knew it
SpaceEnter
Turn the card, show the answer
123
Overview, Recap, Test me
HomeEnd
First and last card
Esc
Close
← → or scroll · N notes · F full screen · B blank0:00
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.
Comments