AN3248
Application note
Using STM32L1 analog comparators in application cases
Introduction
This document describes six application cases of the two analog comparators embedded in
the ultra low power STM32L1 product line. The application cases are:
●
●
●
●
●
●
Analog voltage monitoring
Analog watchdog during Stop mode
Pulse width measurement
Pulse width modulation (PWM) signal control
Capacitance measurement
Brightness control using a light dependent resistor (LDR)
The six application cases demonstrate the usefulness of analog comparators and show how
they are integrated with other peripherals, for example, the digital-to-analog-converter (DAC)
and timers.
To ensure a quick start, four application cases presented in this document are implemented
in C language and are available in
Project\STM32L1xx_StdPeriph_Examples\COMP
within
the
STM32L1xx_StdPeriph_Lib
package.
Please note that this document is not intended to replace the routing interface (RI) and
comparator sections in the product reference manual RM0038 (for STM32L1xx Ultra Low
Power devices).
The peripheral power consumption should be consulted in the device datasheets.
February 2012
Doc ID 17758 Rev 3
1/17
Contents
AN3248
Contents
1
2
3
4
5
6
Analog voltage monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Analog watchdog during Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Pulse width measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
PWM signal control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Capacitance measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Brightness control using a light dependent resistor
(LDR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7
2/17
Doc ID 17758 Rev 3
AN3248
List of figures
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Figure 13.
Sensor output connection to COMP2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Sensor output connection to COMP1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Power consumption in an analog voltage monitoring application . . . . . . . . . . . . . . . . . . . . . 5
COMP2 configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Analog comparators combined in window mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Analog watchdog during Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
COMP2 with output redirection feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Pulse width measurement: COMP2 output redirection to timer . . . . . . . . . . . . . . . . . . . . . 10
PWM signal control: COMP2 output redirection to timer . . . . . . . . . . . . . . . . . . . . . . . . . . 11
RC network connection for capacitance measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Capacitance measurement using COMP2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Connecting an LDR resistor to an STM32L1 device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Comparator output behavior versus light intensity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Doc ID 17758 Rev 3
3/17
Analog voltage monitoring
AN3248
1
Analog voltage monitoring
Ultra low power STM32L1 devices embed a 12-bit analog-to-digital converter (ADC) which
is very fast with a sampling rate of 1 Msample/s. However, with a 1.45 mA typical
consumption, it can jeopardize battery life time if left powered-on continuously. It is therefore
recommended to use analog comparators in application cases when analog input voltage
(sensor output) needs to be measured as soon as a pre-defined threshold is exceeded.
In STM32L1 devices, analog comparators are useful for monitoring the analog input voltage
and powering on the ADC when it is required. While monitoring the analog voltage, the
device can enter Stop mode at the same time that both comparators are still powered on.
Consequently, better consumption is achieved and power is saved.
Note:
Analog comparators are powered by the internal reference voltage, V
REFINT
, which is still
powered on in Stop mode, and can be disabled by configuration. Once V
REFINT
is disabled,
the comparators can no longer be used.
In an analog voltage monitoring application, where the sensor output voltage is lower than
the threshold, the MCU remains in Stop mode thereby saving power. As soon as the sensor
output exceeds the threshold, the MCU is woken up, the ADC is powered on, and the analog
input voltage is measured. When the sensor output is under the threshold, the MCU re-
enters Stop mode.
Average power consumption is dramatically reduced when compared with an application
that continuously measures the analog voltage whatever the input value.
Figure 1
shows how to connect a sensor output (temperature sensor, pressure sensor,
pyroelectric infrared detector, photodiode sensor) to an STM32L1 device in an analog
voltage monitoring application using comparator 2 (COMP2). COMP2 monitors the analog
voltage in Stop mode while the ADC measures it in Run mode.
Figure 1.
Sensor output connection to COMP2
STM32L1 device
Analog
voltage
Sensor
Amplifier
(2)
Filter
(2)
ADC_CHx
(1)
COMP2_INP
(1)
Ai17491
1. Legend for
Figure 1
ADC_CHx: AC channel x
COMP2_INP: comparator 2 non-inverting input
2. Only if required.
4/17
Doc ID 17758 Rev 3
AN3248
Analog voltage monitoring
Figure 2
shows how to connect a sensor output to an STM32L1 device using comparator 1
(COMP1). COMP1 shares the same inputs as the ADC which reduces the number of
required pins. Nevertheless, the threshold is fixed to
V
REFINT
.
Figure 2.
Sensor output connection to COMP1
STM32L1 device
Analog
voltage
Sensor
Amplifier
(2)
Filter
(2)
COMP1_INP/
ADC_CHx
(1)
Ai18734
1. Legend for
Figure 2
COMP1_INP/ADC CHx: comparator 1 non-inverting input shared with ADC channel x
2. Only if required.
Figure 3
shows the gain in power consumption in an analog monitoring application.
Figure 3.
Power consumption in an analog voltage monitoring application
Input analog voltage
Analog threshold
Time
MCU state
S
R
S
R
S
R
S
Time
MCU current consumption
Few mA
Few μA
Time
ai17492
1. Legend for
Figure 3
S: Stop mode
R: Run mode
The input analog voltage can be connected either to PB4 or PB5. The analog threshold can
be provided internally through V
REFINT
and its submultiples or via an external pin through
PB3. DAC channel 1 and channel 2 (DAC_OUT1 and DAC_OUT2 respectively) cannot be
used in such application cases since the DAC channels are powered off in Stop mode.
Doc ID 17758 Rev 3
5/17