6 years, 6 months ago.

Can't read ADC after WakeUp from deepsleep

Hi I am using a Nucleo L152RE.

I have had a lot of trouble with these low power modes...hope I can get some help with this problem too.

I am not able to read the ADC after a WakeUp from deepsleep. It returns 0.0V.

I have tried in sleep and this works fine. I have also tried adding a delay after waking up, and I am not using a callback function to read the adc, but calling it right after the call to deepsleep.

Any ideas?


Question relating to:

Erik - / WakeUp Featured
Wake-up timer library to wake from deepsleep/power-down KL25z, LPC800-MAX, LPC812, powerdown, Sleep, wake-up

Can you try what happens when you use InterruptIn to wake it up? That should verify if it is WakeUp (unlikely tbh) or the mbed deepsleep code (which isn't my problem :D). (No I can have a look at that too)

posted by Erik - 20 May 2015

Ok, I tried using an interruptIn but it didn't seem to fix it :/ The program actually didn't print anything after woken up when I tried reading the ADC, but if I just added a random printf it worked..

posted by Håkon Bryn 20 May 2015

This means the ADC does not properly function after waking from deepsleep. I'll see the coming days if I can have a look at it, but the problem is in the mbed lib itself, and in principle STM maintains that :).

I don't have an L152 myself, so if the problem does not happen for other Nucleos it will be probably close to impossible for me to solve it. If you want you can make a bug report here: https://github.com/mbedmicro/mbed/issues

posted by Erik - 20 May 2015

Before I forget: Important question: Are you printing in the interrupt you use to wake it, or in the main loop after the deepsleep() command? The first option is generally not a good solution, since clock settings are only restored after that interrupt.

posted by Erik - 20 May 2015

Okay I will make a bug report about it :) I am printing in the main loop after the deepsleep command, the clock is set because it is printing correct. But it is printing 0.00000 as my read ADC value, which is not correct. This was with your library; WakeUp

posted by Håkon Bryn 20 May 2015

2 Answers

5 years, 9 months ago.

You have to restart the HSI clock after wakeup with the L152. The ADC on the L152 needs to use the HSI for reading.

        // Enable the HSI (to clock the ADC)
        RCC_OscInitTypeDef RCC_OscInitStruct;
        RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
        RCC_OscInitStruct.HSIState       = RCC_HSI_ON;
        RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE;

Thanks!! Perhaps a bit cleaner:

        RCC_OscInitTypeDef RCC_OscInitStruct;
        if ( RCC_OscInitStruct.HSIState != RCC_HSI_ON ) {
            RCC_OscInitStruct.HSIState = RCC_HSI_ON;
            RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
            RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
            HAL_StatusTypeDef ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
            if ( ret == HAL_OK ) {
                printf("Fixed the HSI to clock the ADC");
            else {
                printf("Failed to fix the HSI to clock the ADC: %d", ret);
posted by Mark Ruys 06 Apr 2017
5 years, 9 months ago.

This problem probably stems from this issue here:


Depending on how your board clock source is configured, it can wake up without setting the clock sources correctly.

Running from external clock source input from the interface MCU can cause problems. I found using the external 8MHz crystal gave the best overall results for many reasons.

If you use the external crystal and change the clock setup order, the following simple example at the bottom of the page gives information on how to get low power and retain pin configurations using Eriks WakeUp. But bear in mind if you do not have the external 32KHz crystal (LSE) fitted the wake up timer may be very inaccurate.


I haven't tried AnalogIn but pretty sure it should work here. However AnalogIn may well increase that deepsleep current in the order of 10uA.