Wake Up - ADC - Hall Sensor

sslama
Posts: 5
Joined: Tue Feb 06, 2018 10:28 am

Wake Up - ADC - Hall Sensor

Postby sslama » Tue Feb 06, 2018 11:09 am

Hi,
for a prototyping project I'm looking for a developer that can help us.

It is an university project, improving the hygiene in the health care sector.

I need the ESP32 to wake up from the deep sleep mode if the hall sensor detects a measurement over a certain threshold. My understanding ist, that the ULP can read the current value of the hall sensor over the ADC.

As I don't have much experience in Assembler and the examples using the ULP are quite vague any help is very much appreciated.

Best regards,
Simon

brandobur
Posts: 1
Joined: Wed Jul 11, 2018 12:40 pm

Re: Wake Up - ADC - Hall Sensor

Postby brandobur » Wed Jul 11, 2018 12:45 pm

Hi,

maybe it will help, I have written it today (works also in Arduino IDE):

Code: Select all

#include "esp32/ulp.h"
#include "soc/rtc_cntl_reg.h"
#include "driver/rtc_io.h"
#include "driver/adc.h"

void ulp_adc_wake_up(unsigned int low_adc_treshold, unsigned int high_adc_treshold)
{
   adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11);
   adc1_config_width(ADC_WIDTH_BIT_10);
   adc1_ulp_enable();

   rtc_gpio_init(GPIO_NUM_36);

   const ulp_insn_t program[] = {
      I_DELAY(32000),              // Wait until ESP32 goes to deep sleep
      M_LABEL(1),                  // LABEL 1
        I_MOVI(R0, 0),             // Set reg. R0 to initial 0
        I_MOVI(R2, 0),             // Set reg. R2 to initial 0
      M_LABEL(2),                  // LABEL 2
        I_ADDI(R0, R0, 1),         // Increment cycle counter (reg. R0)
        I_ADC(R1, 0, 0),           // Read ADC value to reg. R1
        I_ADDR(R2, R2, R1),        // Add ADC value from reg R1 to reg. R2
      M_BL(2, 4),                  // If cycle counter is less than 4, go to LABEL 2
      I_RSHI(R0, R2, 2),           // Divide accumulated ADC value in reg. R2 by 4 and save it to reg. R0
      M_BGE(3, high_adc_treshold), // If average ADC value from reg. R0 is higher or equal than high_adc_treshold, go to LABEL 3
      M_BL(3, low_adc_treshold),   // If average ADC value from reg. R0 is lower than low_adc_treshold, go to LABEL 3
      M_BX(1),                     // Go to LABEL 1
      M_LABEL(3),                  // LABEL 3
      I_WAKE(),                    // Wake up ESP32
      I_END(),                     // Stop ULP program timer
      I_HALT()                     // Halt the coprocessor
   };

   size_t size = sizeof(program)/sizeof(ulp_insn_t);
   ulp_process_macros_and_load(0, program, &size);

   ulp_run(0);
   esp_sleep_enable_ulp_wakeup();
   esp_deep_sleep_start();
}
Best regards,
Marek

gabriel-milan
Posts: 2
Joined: Tue May 28, 2019 10:18 pm

Re: Wake Up - ADC - Hall Sensor

Postby gabriel-milan » Tue May 28, 2019 10:20 pm

Hey!

I was having a similar issue and realized there's few material on the internet for that.
I've written a very simple code for waking up ESP32 according to a threshold on hall sensor readings, hope it helps!

Link for the code: https://github.com/gabriel-milan/esp32_ulp_hall_wakeup

janbenes
Posts: 4
Joined: Wed Feb 17, 2021 2:18 pm

Re: Wake Up - ADC - Hall Sensor

Postby janbenes » Fri Feb 26, 2021 12:33 pm

Hello Marek,

your code does not compile for me on Arduino, any advice what could be wrong? Here is error message:

Code: Select all

/tmp/arduino_build_150223/core/core.a(main.cpp.o):(.literal._Z8loopTaskPv+0x8): undefined reference to `setup()'
/tmp/arduino_build_150223/core/core.a(main.cpp.o):(.literal._Z8loopTaskPv+0xc): undefined reference to `loop()'
/tmp/arduino_build_150223/core/core.a(main.cpp.o): In function `loopTask(void*)':
/home/janbenes/.arduino15/packages/esp32/hardware/esp32/1.0.5/cores/esp32/main.cpp:32: undefined reference to `setup()'
/home/janbenes/.arduino15/packages/esp32/hardware/esp32/1.0.5/cores/esp32/main.cpp:35: undefined reference to `loop()'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board ESP32 Dev Module.
Best regards, Jan

brandobur wrote:
Wed Jul 11, 2018 12:45 pm
Hi,

maybe it will help, I have written it today (works also in Arduino IDE):

Code: Select all

#include "esp32/ulp.h"
#include "soc/rtc_cntl_reg.h"
#include "driver/rtc_io.h"
#include "driver/adc.h"

void ulp_adc_wake_up(unsigned int low_adc_treshold, unsigned int high_adc_treshold)
{
   adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11);
   adc1_config_width(ADC_WIDTH_BIT_10);
   adc1_ulp_enable();

   rtc_gpio_init(GPIO_NUM_36);

   const ulp_insn_t program[] = {
      I_DELAY(32000),              // Wait until ESP32 goes to deep sleep
      M_LABEL(1),                  // LABEL 1
        I_MOVI(R0, 0),             // Set reg. R0 to initial 0
        I_MOVI(R2, 0),             // Set reg. R2 to initial 0
      M_LABEL(2),                  // LABEL 2
        I_ADDI(R0, R0, 1),         // Increment cycle counter (reg. R0)
        I_ADC(R1, 0, 0),           // Read ADC value to reg. R1
        I_ADDR(R2, R2, R1),        // Add ADC value from reg R1 to reg. R2
      M_BL(2, 4),                  // If cycle counter is less than 4, go to LABEL 2
      I_RSHI(R0, R2, 2),           // Divide accumulated ADC value in reg. R2 by 4 and save it to reg. R0
      M_BGE(3, high_adc_treshold), // If average ADC value from reg. R0 is higher or equal than high_adc_treshold, go to LABEL 3
      M_BL(3, low_adc_treshold),   // If average ADC value from reg. R0 is lower than low_adc_treshold, go to LABEL 3
      M_BX(1),                     // Go to LABEL 1
      M_LABEL(3),                  // LABEL 3
      I_WAKE(),                    // Wake up ESP32
      I_END(),                     // Stop ULP program timer
      I_HALT()                     // Halt the coprocessor
   };

   size_t size = sizeof(program)/sizeof(ulp_insn_t);
   ulp_process_macros_and_load(0, program, &size);

   ulp_run(0);
   esp_sleep_enable_ulp_wakeup();
   esp_deep_sleep_start();
}
Best regards,
Marek

pipi61
Posts: 60
Joined: Fri Dec 23, 2016 10:58 pm

Re: Wake Up - ADC - Hall Sensor

Postby pipi61 » Wed Mar 23, 2022 10:43 pm

Hi!
Dear Brandobur!
I tried this example in Arduino.
in setup () I call ulp_adc_wake_up (unsigned int low_adc_treshold, unsigned int high_adc_treshold) with different parameters, but it always wakes up the processor without a magnet.
The arduino hallread() return integer value, i look 80-100 without magnet, with magnet one pole 560, other pole -440 negative value
You use unsigned integer, I tried (1, 4200000000) and vice versa, and many other values.
What can be the usable range?
Thanks

xien551
Posts: 69
Joined: Wed Feb 02, 2022 4:04 am

Re: Wake Up - ADC - Hall Sensor

Postby xien551 » Wed Mar 23, 2022 11:43 pm

Excellent project, I think it would work for all the ultra-low power application. I will try the code in my project in the future.

atmoz_
Posts: 6
Joined: Tue May 03, 2022 2:59 pm

Re: Wake Up - ADC - Hall Sensor

Postby atmoz_ » Tue May 03, 2022 3:04 pm

brandobur wrote:
Wed Jul 11, 2018 12:45 pm
Hi,

maybe it will help, I have written it today (works also in Arduino IDE):

Code: Select all

#include "esp32/ulp.h"
#include "soc/rtc_cntl_reg.h"
#include "driver/rtc_io.h"
#include "driver/adc.h"

void ulp_adc_wake_up(unsigned int low_adc_treshold, unsigned int high_adc_treshold)
{
   adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11);
   adc1_config_width(ADC_WIDTH_BIT_10);
   adc1_ulp_enable();

   rtc_gpio_init(GPIO_NUM_36);

   const ulp_insn_t program[] = {
      I_DELAY(32000),              // Wait until ESP32 goes to deep sleep
      M_LABEL(1),                  // LABEL 1
        I_MOVI(R0, 0),             // Set reg. R0 to initial 0
        I_MOVI(R2, 0),             // Set reg. R2 to initial 0
      M_LABEL(2),                  // LABEL 2
        I_ADDI(R0, R0, 1),         // Increment cycle counter (reg. R0)
        I_ADC(R1, 0, 0),           // Read ADC value to reg. R1
        I_ADDR(R2, R2, R1),        // Add ADC value from reg R1 to reg. R2
      M_BL(2, 4),                  // If cycle counter is less than 4, go to LABEL 2
      I_RSHI(R0, R2, 2),           // Divide accumulated ADC value in reg. R2 by 4 and save it to reg. R0
      M_BGE(3, high_adc_treshold), // If average ADC value from reg. R0 is higher or equal than high_adc_treshold, go to LABEL 3
      M_BL(3, low_adc_treshold),   // If average ADC value from reg. R0 is lower than low_adc_treshold, go to LABEL 3
      M_BX(1),                     // Go to LABEL 1
      M_LABEL(3),                  // LABEL 3
      I_WAKE(),                    // Wake up ESP32
      I_END(),                     // Stop ULP program timer
      I_HALT()                     // Halt the coprocessor
   };

   size_t size = sizeof(program)/sizeof(ulp_insn_t);
   ulp_process_macros_and_load(0, program, &size);

   ulp_run(0);
   esp_sleep_enable_ulp_wakeup();
   esp_deep_sleep_start();
}
Best regards,
Marek
Hi Marek,

Thanks for your code.
Can you please tell me how I must run this?
What has to be in the setup() function for example, and what has to be in de main loop?

Do I have to call ulp_adc_wake_up(0, 3500); only once or (very fast) in a loop?

I want to read ADC values from an IR-sensor, so I want to check the ADC-value as much as possible in order to detect things properly.

Thanks for thinking with me :-)

Best regards,

Atmoz

atmoz_
Posts: 6
Joined: Tue May 03, 2022 2:59 pm

Re: Wake Up - ADC - Hall Sensor

Postby atmoz_ » Thu May 05, 2022 6:04 am

Does anyone here on this forum how I should run this code?

The only thing I want is to read ADC in ULP and if a specific value is reached --> wake up the main processor.

Any help is appreciated 8-)


Thanks in advance!

Atmoz

Who is online

Users browsing this forum: No registered users and 95 guests