AN3248 Application note

Similar documents
AN4112 Application note

AN2979 Application note

STEVAL-ISQ010V1. High-side current-sense amplifier demonstration board based on the TSC102. Features. Description

EVAL-RHF310V1. EVAL-RHF310V1 evaluation board. Features. Description

AN4014 Application Note Adjustable LED blinking frequency using a potentiometer and STM8SVLDISCOVERY Application overview

AN3332 Application note

AN279 Application note

AN3134 Application note

2STA1695. High power PNP epitaxial planar bipolar transistor. Features. Applications. Description

STEVAL-ISA005V1. 1.8W buck topology power supply evaluation board with VIPer12AS. Features. Description. ST Components

AN1441 Application note

BD235 BD237. Low voltage NPN power transistors. Features. Applications. Description. Low saturation voltage NPN transistors

D44H8 - D44H11 D45H8 - D45H11

2STC4468. High power NPN epitaxial planar bipolar transistor. Features. Application. Description

STEVAL-CCA040V1. 4X10 Watt dual/quad power amplifier demonstration board based on the STA540SAN. Features. Description

Obsolete Product(s) - Obsolete Product(s)

2STR2215. Low voltage fast-switching PNP power transistor. Features. Applications. Description

Order codes Marking Polarity Package Packaging. MJD44H11T4 MJD44H11 NPN DPAK Tape and reel MJD45H11T4 MJD45H11 PNP DPAK Tape and reel

AN440 Application note

Obsolete Product(s) - Obsolete Product(s)

MJE182 Low voltage high speed switching NPN transistor Features Applications Description High speed switching NPN device

R 1 typ. = 15 kω. Order codes Marking Polarity Package Packaging. 2N6036 2N6036 NPN SOT-32 Tube 2N6039 2N6039 PNP SOT-32 Tube

AN2167 Application note

AN2333 Application note

AN2581 Application note

BD533 BD535 BD537 BD534 BD536

BUX87. High voltage NPN power transistor. Features. Applications. Description

SPV1001T40. Cool bypass switch for photovoltaic application. Features. Application. Description TO-220

STN9260. High voltage fast-switching PNP power transistor. Features. Applications. Description. High voltage capability Fast switching speed

BD241A BD241C. NPN power transistors. Features. Applications. Description. NPN transistors. Audio, general purpose switching and amplifier transistors

ST26025A. PNP power Darlington transistor. Features. Applications. Description

AN2837 Application note

Obsolete Product(s) - Obsolete Product(s)

ESDALCL6-4P6A. Multi-line low capacitance and low leakage current ESD protection. Features. Applications. Description

2STR SOT-23 Tape and reel 2STR1230G 130G SOT-23 Tape and reel

Order codes Marking Package Packaging. STX0560 X0560 TO-92 Bag STX0560-AP X0560 TO-92AP Ammopack. December 2010 Doc ID Rev 1 1/9

AN1756 Application note

2STD1665. Low voltage fast-switching NPN power transistor. Features. Applications. Description

2STN2540. Low voltage fast-switching PNP power bipolar transistor. Features. Applications. Description

BD243C BD244C. Complementary power transistors. Features. Applications. Description. Complementary NPN-PNP devices. Power linear and switching TO-220

R 1 typ. = 15 kω. Order codes Marking Polarity Package Packaging. STX112-AP X112 NPN TO92-AP Ammopack STX117-AP X117 PNP TO92-AP Ammopack

3STL2540. Low voltage high performance PNP power transistor. Features. Applications. Description

KF25B, KF33B KF50B, KF80B

2STC4468. High power NPN epitaxial planar bipolar transistor. Features. Application. Description

2STD1360 2STF1360-2STN1360

STPS140Z-Y. Automotive power Schottky rectifier. Features. Description

STN9360. High voltage fast-switching PNP power transistor. Features. Applications. Description. High voltage capability Fast switching speed

STN2580. High voltage fast switching NPN power transistor. Features. Applications. Description. High voltage capability Fast switching speed

2STA1943. High power PNP epitaxial planar bipolar transistor. Features. Application. Description

2N2219AHR. Hi-Rel NPN bipolar transistor 40 V A. Features. Description

Obsolete Product(s) - Obsolete Product(s)

STD840DN40. Dual NPN high voltage transistors in a single package. Features. Applications. Description

STEVAL-CCA011V1. Filter-free stereo 2x2.5 W Class-D audio power amplifier demonstration board based on the TS2012FC. Features.

Obsolete Product(s) - Obsolete Product(s)

LET9060C. RF power transistor from the LdmoST family of n-channel enhancement-mode lateral MOSFETs. Features. Description

Obsolete Product(s) - Obsolete Product(s)

UM0890 User manual. 2-stage RF power amplifier with LPF based on the PD85006L-E and STAP85050 RF power transistors. Introduction

Obsolete Product(s) - Obsolete Product(s)

2STX2220. High Gain Low Voltage PNP Power Transistor. General features. Description. Internal schematic diagram. Applications.

Description. Part numbers Order codes Packages Output voltages

STPSC V power Schottky silicon carbide diode. Features. Description

AN3116 Application note

MD2310FX. High voltage NPN power transistor for standard definition CRT display. Features. Application. Description

Part numbers Order codes Packages Temperature range. LM137 LM137K TO-3-55 C to 150 C LM337 LM337K TO-3 0 C to 125 C LM337 LM337SP TO C to 125 C

2STC5242. High power NPN epitaxial planar bipolar transistor. Features. Application. Description

STD1802T4-A. Low voltage fast-switching NPN power transistor. Features. Description. Applications

STPS3045FP. Power Schottky rectifier. Features. Description

TIP2955 TIP3055. Complementary power transistors. Features. Applications. Description

Order codes Marking Package Packaging 2STF SOT-89 2STN2550 N2550 SOT-223. November 2008 Rev 1 1/8

STN High voltage fast-switching PNP power transistor. Features. Application. Description. High voltage capability Very high switching speed

BUL743. High voltage fast-switching NPN power transistor. Features. Applications. Description

STPS30H60-Y. Automotive power Schottky rectifier. Features. Description

BUL38D. High voltage fast-switching NPN power transistor. Features. Applications. Description

STB High voltage fast-switching NPN power transistor. Features. Applications. Description

LM323. Three-terminal 3 A adjustable voltage regulators. Features. Description

Obsolete Product(s) - Obsolete Product(s)

MJD122 MJD127 Complementary power Darlington transistors Features Applications Description

AN2961 Application note

2ST2121. High power PNP epitaxial planar bipolar transistor. Features. Applications. Description 1 2 TO-3

BUL39D. High voltage fast-switching NPN power transistor. Features. Application. Description

MD2009DFX. High voltage NPN power transistor for CRT TV. Features. Application. Description

STTH1210-Y. Automotive ultrafast recovery - high voltage diode. Features. Description

Obsolete Product(s) - Obsolete Product(s)

ST13003D-K High voltage fast-switching NPN power transistor Features Applications Description

TS522. Precision low noise dual operational amplifier. Features. Description

TR136. High voltage fast-switching NPN power transistor. Features. Applications. Description

STTH60W03C. Turbo 2 ultrafast high voltage rectifier. Features. Description

UM0791 User manual. Demonstration firmware for the DMX-512 communication protocol receiver based on the STM32F103Zx. Introduction

LM2903W. Low-power, dual-voltage comparator. Features. Description

LS1240. Electronic two-tone ringer. Features. Description. Pin connection (top view)

EVL6566B-40WSTB demonstration board 40 W wide input range flyback converter for digital consumer equipments using the L6566B

Order codes Packages Lead finish Marking Type EPPL Packaging. 2N5153SHR SMD.5 Gold ESCC Flight Yes Strip pack

Order code Temperature range Package Packaging Marking

Obsolete Product(s) - Obsolete Product(s)

UM0920 User manual. 4 W non-isolated, wide input-voltage range SMPS demonstration board based on the VIPer16. Introduction

AN4233 Application note

STTH200W06TV1. Turbo 2 ultrafast high voltage rectifier. Features. Description

ST1510FX. High voltage fast-switching NPN Power transistor. General features. Applications. Internal schematic diagram. Description.

Obsolete Product(s) - Obsolete Product(s)

PD54003L-E. RF power transistor, LdmoST plastic family N-channel enhancement-mode, lateral MOSFETs. Features. Description

Transcription:

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 Analog voltage monitoring.................................... 4 2 Analog watchdog during Stop mode............................ 7 3 Pulse width measurement.................................... 9 4 PWM signal control......................................... 11 5 Capacitance measurement................................... 12 6 Brightness control using a light dependent resistor (LDR)..................................................... 14 7 Revision history........................................... 16 2/17 Doc ID 17758 Rev 3

List of figures List of figures Figure 1. Sensor output connection to COMP2.......................................... 4 Figure 2. Sensor output connection to COMP1.......................................... 5 Figure 3. Power consumption in an analog voltage monitoring application..................... 5 Figure 4. COMP2 configuration...................................................... 6 Figure 5. Analog comparators combined in window mode................................. 7 Figure 6. Analog watchdog during Stop mode........................................... 8 Figure 7. COMP2 with output redirection feature......................................... 9 Figure 8. Pulse width measurement: COMP2 output redirection to timer..................... 10 Figure 9. PWM signal control: COMP2 output redirection to timer.......................... 11 Figure 10. RC network connection for capacitance measurement........................... 12 Figure 11. Capacitance measurement using COMP2..................................... 13 Figure 12. Connecting an LDR resistor to an STM32L1 device............................. 14 Figure 13. Comparator output behavior versus light intensity............................... 15 Doc ID 17758 Rev 3 3/17

Analog voltage monitoring AN3248 1 Analog voltage monitoring Note: 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. 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 reenters 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 Sensor STM32L1 device Analog voltage 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

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 Sensor Analog voltage Amplifier (2) Filter (2) STM32L1 device 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 MCU state S R S R S R S MCU current consumption Few ma Few μa 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

Analog voltage monitoring AN3248 COMP2 wakes up the device from Stop mode through external interrupt line 22 (EXTI line 22). Figure 4 shows the available configurations for inverting and non inverting inputs. Figure 4. COMP2 configuration GR6 Wakeup EXTI line 22 PB4 PB5 GR6 Input voltage PB3 COMP2 + - CMP2OUT DAC_OUT1 Analog threshold: multiple sources DAC_OUT2 V REFINT 3/4 V REFINT 1/2 V REFINT 1/4 V REFINT ai17493 1. Legend for Figure 4 DAC_OUT1: DAC channel 1 output DAC_OUT2: DAC channel 2 output V REFINT : Internal reference voltage CMP2OUT: Comparator 2 output (internal output) When the device enters Stop mode, only COMP2 and the internal reference voltage, V REFINT, remain powered on. Note: 1 Refer to the device datasheet for power consumption values. 2 In High-density devices, pins PB6 and PB7 can be used also as COMP2 non-inverting inputs. 3 If the analog threshold corresponds to the internal reference voltage, V REFINT (1.22 V), COMP1 can be used instead of COMP2 since it consumes much less power. In this case, the input analog voltage can be connected to any channel among the 24 ADC channels. 6/17 Doc ID 17758 Rev 3

Analog watchdog during Stop mode 2 Analog watchdog during Stop mode The ADC in the STM32L1 family can be used as an analog watchdog with programmable high and low thresholds. Nevertheless, the MCU must be kept in Run mode to be able to watch analog voltage on input since the ADC is powered off in Stop mode. For ultra low power STM32L1 devices, two analog comparators, COMP1 and COMP2, can be combined in window mode and used as an analog watchdog that remains powered on while the MCU is stopped. Consequently, lower consumption is achieved and power is saved. Figure 5 displays the configuration of two such analog comparators in window mode. Threshold1 is set to the internal reference voltage, V REFINT, and threshold2 is configurable among V REFINT, 3/4 V REFINT, 1/2 V REFINT, 3/4 V REFINT, DAC_OUT1, DAC_OUT2, or the external pin PB3. The analog input voltage can be applied on group 6 of the analog switches (PB4 or PB5). Figure 5. Analog comparators combined in window mode COMP1 Wakeup EXTI line 21 Threshold1: V REFINT (1.22 V) + - CMP1OUT PB4 PB5 GR6-1 GR6-2 WNDE Input voltage PB3 COMP2 + - Wakeup EXTI line 22 CMP2OUT DAC_OUT1 Threshold2: multiple sources DAC_OUT2 V REFINT 3/4 V REFINT 1/2 V REFINT 1/4 V REFINT ai17494 Note: In High-density devices, pins PB6 and PB7 can be used also as COMP2 non-inverting inputs. DAC_OUT1 and DAC_OUT2 cannot be used in Stop mode since the DAC peripheral is powered off. Doc ID 17758 Rev 3 7/17

Analog watchdog during Stop mode AN3248 In an analog watchdog application, COMP1 is configured through external interrupt line 21 (EXTI line 21) to exit the MCU from Stop mode when the analog input voltage exceeds V REFINT. COMP2 is set, through EXTI line 22, to exit the MCU from Stop mode when the analog voltage goes below the lower threshold. Throughout the time the analog voltage is within the defined thresholds, the MCU is in Stop mode and power consumption is reduced. When the analog voltage exceeds the defined thresholds, average power consumption can be reduced by switching to Run mode. Figure 6 gives an overview of an analog watchdog application with threshold1 higher than threshold2. Figure 6. Analog watchdog during Stop mode Sensor output voltage Threshold1: V REFINT MCU exits Stop mode MCU enters Stop mode MCU exits Stop mode Threshold2 0 V MCU state ai17495 1. While the MCU is in Stop mode, the input voltage exceeds threshold1 and the MCU exits Stop mode. 2. While the MCU is in Run mode, the input voltage goes below threshold1 and the MCU enters Stop mode. 3. While the MCU is in Stop mode, the input voltage goes below threshold2 and the MCU exits Stop mode. Note: 1 In Stop mode, only COMP1, COMP2 and V REFINT continue to consume power. Please refer to the specific device datasheet for power consumption values. 2 No hysteresis is implemented on either comparator inputs. 8/17 Doc ID 17758 Rev 3

Pulse width measurement 3 Pulse width measurement In STM32L1 devices, the COMP2 output can be redirected to the input capture of the embedded timers: TIM2, TIM3, TIM4, and TIM10. Redirecting the COMP2 output allows a signal width or frequency with specific low and high levels (for example, a shifted signal) to be measured. Figure 7 displays all the possible output redirections of the COMP2 output. The input signal, whose signal width should be measured, is connected to any I/O of analog switches group 6 (PB4 or PB5). The reference signal can be powered by: an internal reference (V REFINT, 3/4 V REFINT, 1/2 V REFINT, or 1/4 V REFINT ) the built-in DAC (channel 1 or channel 2) an external pin through PB3 The COMP2 output redirection is achieved through the OUTSEL[2:0] bits. The timer input capture channel is configured to save the timer counter at both rising and falling edges. When the input signal goes above the reference voltage, COMP2 output is at a high level generating a rising edge on the timer input capture. When the input signal goes under the reference voltage, COMP2 output is at low level generating a falling edge. The elapsed time between the two consecutive events (falling then rising edge or rising then falling edge) represents the pulse width. Hence, the pulse width measurement is performed by simple subtraction of the counter values. Figure 8 gives an overview of the pulse width measurement as measured by COMP2. Figure 7. COMP2 with output redirection feature GR6-1 PB4 COMP2 Input voltage GR6-2 + PB5 PB3 - DAC_OUT1 DAC_OUT2 Threshold2: multiple sources V REFINT 3/4 V REFINT 1/2 V REFINT 1/4 V REFINT Wakeup EXTI line 22 CMP2OUT TIM2 IC4 TIM2 OCRECLR TIM3 IC4 TIM3 OCREFCLR TIM4 IC4 TIM4 OCRECLR TIM10 IC1 OUTSEL[2:0] ai18706 Note: 1. Legend for Figure 7 DAC_OUT1: DAC channel 1 output DAC_OUT2: DAC channel 2 output V REFINT : Internal reference voltage CMP2OUT: Comparator 2 output (internal output) TIMx ICy: r x input capture channel y TIMx OCREFCLR: r x output compare reference clear In High-density devices, pins PB6 and PB7 can be used also as COMP2 non-inverting inputs. Doc ID 17758 Rev 3 9/17

Pulse width measurement AN3248 Figure 8. Pulse width measurement: COMP2 output redirection to timer Input signal duration (1) duration (1) Reference voltage COMP2 output high level Low level r counter 65535 0 1st capture 2nd capture 1st capture 2nd capture 1st capture ai18707 1. The duration that should be measured 2. In the pulse width measurement application, COMP1 cannot be used since its output CMP1OUT (internal output) is not connected to the embedded timers. Note: 1 Signal frequency can be achieved by configuring the timer input capture channel to save the counter value on only a rising or falling edge. 2 DAC outputs (DAC_OUT1 or DAC_OUT2) can be used as inverting inputs to allow the reference voltage level (threshold) to be internally provided and programmable by software from 0 V to V DD.. 10/17 Doc ID 17758 Rev 3

PWM signal control 4 PWM signal control In STM32L1 devices, the COMP2 output can be redirected to the output compare reference clear signal (OCREFCLR) of the embedded timers: TIM2, TIM3, and TIM4 (refer to Figure 7: COMP2 with output redirection feature). The possibility of redirecting the COMP2 output can be used to provide a fast response time that is independent from the system frequency when an analog event occurs. This application case controls a PWM signal for motor control when the current sensor output is connected to the COMP2 non-inverting input. In this situation, the reference voltage is connected to the COMP2 inverting input. When the current sensor output exceeds the selected threshold, the COMP2 output goes high and the PWM signals switch to safe state. Figure 9. PWM signal control: COMP2 output redirection to timer PWM signal PWM signal at safe state (low level) High level COMP2 output COMP2 output is at high level Low level Current sensor output Reference voltage ai18708 Note: 1. In the pulse width measurement application, COMP1 cannot be used since its output CMP1OUT (internal output) is not connected to the embedded timers. When the current sensor voltage reaches the reference voltage, the COMP2 output goes high. Consequently, the PWM and output compare reference signals go low (to safe state). Doc ID 17758 Rev 3 11/17

Capacitance measurement AN3248 5 Capacitance measurement The ability to connect the COMP2 output to the input capture channels of the timers allows the capacitance value to be measured. The principle is based on measuring the charge time of a resistor-capacitor (RC) network as follows: the charge time is measured the charge resistor (R) is already known the unknown capacitance (C) can be computed Figure 10 shows the hardware connection of the RC network to an STM32L1 device. Figure 10. RC network connection for capacitance measurement STM32L1 device TIMx OC R Input voltage C Threshold COMP2 + - TIMx IC ai18709 The capacitance measurement procedure consists of charging and discharging the capacitor through the resistor. The charge/discharge function follows an exponential curve. The charge function is given by Example 1. Example 1 Input voltage = V DD ( 1 exp( t T) ) where: V DD is the positive supply voltage t is the time T is the RC constant Charging and discharging the RC network is ensured by the timer output compare channel (TIMx OC) configured in PWM mode. The timer channel is connected to the resistor. The input voltage is connected to the COMP2 non-inverting input while the threshold is connected to the COMP2 inverting input. When the input voltage crosses the threshold, the COMP2 output switches to high level and a capture event occurs saving the counter value. Figure 11 shows the capacitance measurement. 12/17 Doc ID 17758 Rev 3

Capacitance measurement Figure 11. Capacitance measurement using COMP2 TIMx OCy Input voltage Threshold COMP2 output r counter 65535 0 Capture event Capture event Capture event ai18710 At the moment where the input voltage crosses the threshold and the COMP2 output switches to high level, the charge function is given by Equation 1. Equation 1 Threshold = VDD( 1 exp( -tc T) ) where tc is the time when the input voltage crosses the threshold Using Equation 1 the capacitance value can be computed by Equation 2. Equation 2 C = t ( R ln( 1-threshold ( V DD ))) Usually R, the threshold, and V DD are constant, so, measuring the capacitance is reduced to solving for Equation 3. Equation 3 C = t K where K is solved using Equation 4 Equation 4 K = R ln( 1-threshold ( VDD) ) Doc ID 17758 Rev 3 13/17

Brightness control using a light dependent resistor (LDR) AN3248 6 Brightness control using a light dependent resistor (LDR) In some battery operated applications, the microcontroller needs to be powered if the environment is lit; otherwise, it must be kept powered-off. For such applications, a light dependent resistor (LDR), whose resistance depends on light intensity, is useful to control the microcontroller state. Using an LDR sensor, the microcontroller can switch to/from Lowpower mode depending on the voltage provided by the LDR resistor. Figure 12 shows how to connect an LDR resistor to an STM32L1 device. COMP2 non-inverting input can be connected to an LDR resistor through a voltage divider (V IN ). The threshold can be set externally to PB3 or set internally to V REFINT, 3/4 V REFINT, 1/2 V REFINT, or 1/4 V REFINT. The COMP2 output (CMP2OUT) can be internally connected to EXTI line 22 which, when configured to detect both rising and falling edges on CMP2OUT, can be used as an interrupt source to switch to/from Low power mode. Figure 12. Connecting an LDR resistor to an STM32L1 device V DD STM32L1 device Resistor (R) V IN PB4/PB5 (1) + COMP2 EXTI line 22 PB3 - CMP2OUT V REFINT Light dependent resistor (LDR) ¾ V REFINT ½ V REFINT ¼ V REFINT Other possibilities ai18728 1. PB4 or PB5 can be used as COMP2 non-inverting input. Thus, V IN can be connected to PB4 or PB5. The voltage V IN can be computed using Equation 5. Equation 5 V IN LDR = ( --------------------------- LDR + R) V DD As the LDR resistance decreases with increasing light intensity, the voltage V IN decreases as more light shines on the LDR. 14/17 Doc ID 17758 Rev 3

Brightness control using a light dependent resistor (LDR) The top part of Figure 13 shows the evolution of V IN as a function of light variation. The selected threshold (COMP2 inverting input) defines the limit of dark/light. The bottom part of Figure 13 shows that COMP2 output (CMP2OUT) level depends on V IN and consequently on light intensity. Using EXTI line 22 (which is internally connected to CMP2OUT), the microcontroller can detect the CMP2OUT level switch (from a high level to a low level or vice versa). Figure 13. Comparator output behavior versus light intensity Light Dark Light VIN VIN > threshold VIN < threshold Threshold High level CMP2OUT EXTI22 Microcontroller enters Low power mode Microcontroller exits Low power mode Low level ai18729 The LDR resistor can be used in other microcontroller-based applications rather than darkness control (for example: DC relay control and AutoFocus). For other uses of analog comparators using LDR resistors, refer to the STM32L152-EVAL (for STM32L1xx Ultra Low Power Medium-density devices) or to STM32L152D-EVAL (for STM32L1xx Ultra Low Power High-density devices) demonstration firmware. In this demonstration, the LCD glass contrast is adjusted according to the luminosity detected using the LDR resistor. Doc ID 17758 Rev 3 15/17

Revision history AN3248 7 Revision history Table 1. Document revision history Date Revision Changes 10-Jan-2011 1 Initial release. 12-Jan-2012 2 07-Feb-2012 3 Document updated to include Ultra Low Power High-density device features. Updated notes under Figure 4: COMP2 configuration, Figure 5: Analog comparators combined in window mode and Figure 7: COMP2 with output redirection feature. 16/17 Doc ID 17758 Rev 3

Please Read Carefully: Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries ( ST ) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice. All ST products are sold pursuant to ST s terms and conditions of sale. Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein. UNLESS OTHERWISE SET FORTH IN ST S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPROVED IN WRITING BY TWO AUTHORIZED ST REPRESENTATIVES, ST PRODUCTS ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER S OWN RISK. Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST. ST and the ST logo are trademarks or registered trademarks of ST in various countries. Information in this document supersedes and replaces all information previously supplied. The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners. 2012 STMicroelectronics - All rights reserved STMicroelectronics group of companies Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan - Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America www.st.com Doc ID 17758 Rev 3 17/17