8 months, 3 weeks ago.

How to use the analog comparators of this processor on Mbed?

I see in that PeripheralPins.c has no reference to analog comparators. I am testing an application with a reflective sensor where I need to generate an interrupt routine when the sensor detects the passage by a different color strip on a mechanism. I am using the QRD1114 sensor and its output voltage depends on the reflectance of the object. Has someone a suggestion how to use the comparator peripheral on Mbed environment?

Question relating to:

Affordable and flexible platform to ease prototyping using a STM32L432KCU6 microcontroller.
Comment on this question

6 Answers

8 months, 1 week ago.

I think you could do something like this:

#include "mbed.h"

// Initialize a pins to perform analog input and digital output functions
AnalogIn   ain(A0);

void do_something();

int main(void)
    while (1) {
        // test the voltage on the initialized analog pin
        //  and if greater than 0.3 * VCC set the digital pin
        //  to a logic 1 otherwise a logic 0
        if(ain > 0.3f) {

Here is the reference for AnalogIn API -

Thank you Sarah, but, this is ADC and has no interrupt associated.

Some ARM microcontrollers, like de STM32 L432KC, has the comparator peripheral that has an associated interrupt line and takes much less processing time and consumes much less energy than an ADC operation. I saw in the MBED files of board I am using that already exists a HAL file for the comparator peripheral, but I did'nt see anywhere nothing about this peripheral on MBED platform.

Best regards.

posted by Branilson Luiz 06 Nov 2017
8 months, 1 week ago.


Interrupts for analogIn are not supported in mbed.

Either you can use directly the STM_HAL functions (from STM32Cube_FW_L4\Projects\STM32L4KC-Nucleo\Examples\COMP\COMP_Interrupt)

or you can use a timer interrupt to read the value from your analogin . It would look like :

#include "mbed.h"
// Initialize a pins to perform analog input
AnalogIn   ain(A0);
Ticker ticker1;
volatile float value; // set as volatile as modified in an interrupt

void read_analog_in() 
    value =; // set the global value in the interrupt
int main(void)
   ticker1.attach(&read_analog_in, 0.5); // interrupt will occur every 0.5

    while (1) {
        printf("value = %f\n", &value);

You will not be able to use the comparator interrupt in a simple manner within mbed, but at least it will save some power

Hello Armelle, thank you for your answer.

It still is an ADC polling. Imagine if the application is battery operated and has time restriction to detect an analog event as soon as it happens. This approach has two inconvenient. First, the MCU has to be waked up periodically even if nothing has happened, so, energy waste. Second, if the voltage spike the system is waiting for occurs between two polls, it would not be detected or it can be a delayed detection.

Just because these two problems the comparator interrupt is interesting. The comparator interrupt is capable to wake up the MCU from most of the low power modes. During a low power state, the comparator peripheral consumes only 350 nA and the MCU can be sleeping all the time, so it is very energy efficient. Additionally, the comparator interrupt works as simple and quick as a digital input interrupt.


posted by Branilson Luiz 07 Nov 2017

Hello Branilson,

I fully agree with your point of view, but the analog in interrupt is not available within mbed-os.

You can always access TARGET_STM/TARGET_STM32L4/device/stm32l4xx_hal_adc.c functions within mbed. They will offer the granularity you want to reach, even so they are not ported at the top level of AnalogIn api.

In the meanwhile, I'll ask a change request in the github repository of mbed-os.

Kind regards


posted by Armelle DubocST 07 Nov 2017
1 month, 1 week ago.

An analog computer is anything but a universally useful information preparing machine, however a mini-computer. It utilizes some quantifiable amount in reality to remain for numbers. Normally these are separate, in the event that it is a mechanical number cruncher, or voltage in the event that it is an electronic one. Dissimilar to advanced PCs, it doesn't speak to numbers as discrete twofold qualities, yet as separations or voltages. They are ordinarily intended for a solitary reason.

6 months, 1 week ago.


I'm trying to use the HAL for the COMP of my STM32 NUCLEO-L432KC with med.

Which version of HAL does mbed use? I've found two descriptions: "Description of STM32L4/L4+ HAL and low-layer drivers" (UM1884)

The Revision 5 has a HAL COMP Generic Driver (page 152ff) The newer Revision 7 doesn't have a HAL COMP Generic Driver - only a LL-Version

So how to use the COMP HAL functions to activate the comparator within mbed?

I couldn't find any examples. Only those from STM32Cube_FW_L4

Any hints?

4 weeks, 1 day ago.

A simple PC is definitely not an all-around valuable data getting the ready machine, anyway a smaller than normal PC. It uses some quantifiable sum in all actuality to stay for numbers. Ordinarily, these are particular, if it is a mechanical analyst, or voltage if it is an electronic one. Not at all like propelled PCs, it doesn't address numbers as discrete twofold characteristics, yet as partitions or voltages. They are usually proposed for a singular reason.

5 months, 3 weeks ago.


We recently put the STM32L4xx HAL V1.8.2 in mbed-os. The stm32l4xx_hal_comp.c/h and stm32l4xx_ll_comp.c/h files are both present and you can use them.

There is no COMP examples on mbed, you have to see those in the STM32CubeL4 package.

If you have questions/issues using the COMP API, please ask them on STM32 forum:


To post an answer, please log in.