Hi Rui and everyone,
I’m using an ESP32 board and some sensors to measure parameters. I’m developing this device to be used with batteries, so optimizing the energy consumption is very important in this porject.
In this project I take values every 10-30 minutes. The rest of the time the ESP32 is in DeepSleep mode. The problem is that some of the sensors/modules I’m using don’t have a sleep mode, so keeping them powered while the ESP32 is in DeepSleep mode result in a high lost of energy. I’m planning on using a BS170 transistor as a relay in order to power these sensors/modules only when the ESP32 wakes up and I’m going to take the values. But here I have the other problem: some of the sensors need preheat time (around 1-3 minutes) before taking the values. This means I need to power on these sensors 1-3 minutes before measuring the parameters.
Let’s give an example where I want to take values every 20 minutes:
- The ESP32 goes to DeepSleep for 17 minutes.
- The ESP32 wakes up after 17 minutes, and turns on the BS170 transistor to power on the sensors.
- The ESP32 goes to DeepSleep for 3 minutes.
- The ESP32 wakes up and takes the values of the sensors.
- The ESP32 goes to DeepSleep for 17 minutes again.
The problem is the 3 minutes after powering on the transistor. I can control it setting a digital output high, but when I send the ESP32 to sleep, the digital output will go down and turn off the transistor again.
The question is: is there a way to keep a concrete digital output high while the ESP32 is in DeepSleep mode?
Or is there a better way you would do what I need to do, in order to save as much energy as possible?
Thank you very much in advance!
Any suggestion or recommendation will be appreciated 🙂
Xabi
Hi Xabi, what do you mean? Are you talking about controlling outputs while the ESP32 is in Deep Sleep mode?
Sorry Rui, I sent the question before writing it. I edited it 🙂
If you need more information, please let me know
Hi Xabi,
Sorry for taking so long to get back to you, but I’ve been receiving a high volume of emails and I wasn’t able to keep up with everything.
I would also like to know how to do that. According to the documentation it should be very easy… You would call this function to hold a state during deep sleep:
esp_err_t rtc_gpio_hold_en(GPIO_NUM_XX);
And you would call this function to disable/unlock states after a deep sleep (so, you could change the pin value):
esp_err_trtc_gpio_hold_dis(gpio_num_tgpio_num)
According to a source on the ESP32 Forum:Â http://bbs.esp32.com/viewtopic.php?t=4040
Something like this code should keep GPIO 14 HIGH during deep sleep:
void setup() { pinMode(14, OUTPUT); digitalWrite(14, HIGH); esp_err_t rtc_gpio_hold_en(GPIO_NUM_14); delay(1000); esp_deep_sleep(10000000000); }
However, I can’t make it work for me… I’ve also found this repository with some examples, but using the ESP IDF:Â https://github.com/krzychb/ulp-loop.
I’ve tried to run these commands on Arduino IDE, but it also didn’t work for me:
esp_err_t rtc_gpio_init(gpio_num_t GPIO_NUM_14); esp_err_t rtc_gpio_set_direction(gpio_num_t GPIO_NUM_14, gpio_mode_t RTC_GPIO_MODE_OUTPUT_ONLY); esp_err_t rtc_gpio_set_level(gpio_num_t GPIO_NUM_14, uint32_t t=1); esp_err_t rtc_gpio_hold_en(gpio_num_t GPIO_NUM_14); esp_deep_sleep(10000000000);
Unfortunately I don’t have a working example for you… But I’m sure something small is missing to make it work.
I hope that helps. Regards,
Rui
Hi Rui,Â
Thanks for the information! I tried it but I still couldn’t make it work. Did you make it work? Or do you know anyone that made it work?Â
Thank you very much for your support 🙂
Xabi
I didn’t have the time to make it work either. In theory, it should work, but I’m sure it’s something lower level that is missing that doesn’t allow it to make it work.
Regards,
Rui
Hi Rui,
Good news here! I made it work! Here you have the simple code that worked for me:
#include "driver/rtc_io.h" gpio_num_t pin_MOSFET = GPIO_NUM_15; void setup() { rtc_gpio_init(pin_MOSFET); rtc_gpio_set_direction(pin_MOSFET,RTC_GPIO_MODE_OUTPUT_ONLY); rtc_gpio_set_level(pin_MOSFET,0); //GPIO LOW delay(5000); rtc_gpio_set_level(pin_MOSFET,1); //GPIO HIGH esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); esp_sleep_enable_timer_wakeup(60*1000*1000); esp_deep_sleep_start(); } void loop() { // put your main code here, to run repeatedly: }
On the other hand, I’m having problems with my transistor. The BS170 is not giving enough current for a Vgs of 3.3V. I made a little research over the internet and, for micros like ESP32 which give 3.3V through their outputs, it’s needed MOSFETScalled logic level MOSFET (work for Vgs=3.3V). Could you give me a good reference of a logic level MOSFET that would work well with the ESP32 and is going to be used to control a sensor that needs up to 150mA?
I hope you find that code useful 🙂
Regards,
Xabi
Thanks for sharing your code and I’m glad you made it work. I’ll definitely give it a try.
Have you tried the 2N7000? I often use that N−Channel MOSFET with the ESP8266.
That’s exactly the one I ordered from Aliexpress to try! I’m still waiting for it, but from it’s datasheet curves I’m not sure if it will give me 150mA for 3.3V in Vgs. At least you confirmed I’m not in the wrong direction 😉
I’ll close this question as resolved.
Thanks for the support Rui.
Regards,
Xabi
I’m also not 100% sure… so you’ll need to test, but I think it should work for your needs. Thanks. Regards,
Rui
I have just tried the 2N7000 to switch a red led (in line with a 70 ohm resistor) using a 3.3v power source and it only worked after I had to put a 10K ohm pull down resistor on the gate. A very strange result occurred that perhaps someone can explain was that every time I touched the gate with a wire of any sort it turned on the transistor switch, this wire was not connected to a power source. I did make sure the transistor was the right way around, I found that if you switch drain with source it just powers it on permanently like reversing a diode. My only interpretation (I am a novice) was that the floating voltage in the wire was activating the transistor, but when I used a 10K resistor as a pull down all worked fine. The 2N7000 does have a min gate activation voltage of 0.8, so perhaps the floating voltage was enough to activate it???. Any one else had this occur to them or do they have an sound explanation?