How To Use Sleep Modes In Arduino IDE

Liked this post? Share with others!

To upload code while the main processor is in deep sleep mode, unplug the device and simultaneously press the reset button on the board. While holding the reset button, plug the device back in. This action puts the board into download mode, allowing for code uploading.

RTC based sleep:

esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds

esp_deep_sleep_start();

GPIO touch based sleep and wakeup:

The RTC IO module is designed to trigger a wakeup in response to a touch sensor interrupt. To enable wakeup from a touch sensor interrupt, users must configure the touchpad interrupt before the chip enters Deep-sleep or Light-sleep modes.

#include <esp_sleep.h>
#include <Arduino.h>
#define TOUCH_PIN 2 // Replace T0 with the specific touchpad pin you want to use
void setup() {
Serial.begin(115200);
 // Configure touchpad wakeup
esp_sleep_enable_touchpad_wakeup();
touchAttachInterrupt(TOUCH_PIN, touchInterrupt, 50); // Attach touch interrupt to the specified pin
}

void loop() {
// Your main code here
// Enter deep sleep
Serial.println(“Entering deep sleep…”);
esp_deep_sleep_start();
}
void touchInterrupt() {

// Callback function for touchpad interrupt
 // This will be called when touch is detected on the specified pin
}

External wakeups:

The RTC IO module has the capability to initiate a wakeup when one of the RTC GPIOs reaches a specific logic level. As part of the RTC peripherals power domain, RTC peripherals remain powered on during Deep-sleep if this wakeup source is activated.

esp_sleep_enable_ext0_wakeup(GPIO_NUM_27, LOW); // Replace GPIO_NUM_27 with the GPIO pin number
esp_deep_sleep_start();

Light sleep:
esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
esp_light_sleep_start();

Deep sleep:
esp_sleep_enable_timer_wakeup(10 * 1000000); // 10 seconds
esp_deep_sleep_start();

ULP Coprocessor Programming

To use the ULP (Ultra Low Power) coprocessor on ESP32-S2 for tasks like blinking an LED, we must write a program in ULP assembly language and then load and run it from the main application. Here’s an example of how to do this with the ESP-IDF framework:

#include <Arduino.h>
#include “ulp_main.h” // Header file generated by ESP-IDF build system
RTC_DATA_ATTR int bootCount = 0;

void setup() {
Serial.begin(115200);
delay(1000);


bootCount++
Serial.printf(“Boot count: %d\n”, bootCount);

 // Initialize LED pin
pinMode(LED_BUILTIN, OUTPUT);


// Load and run the ULP program
if (bootCount == 1) {
Serial.println(“Starting ULP program”);
ulpLoadBinary();
ulpRun();
}}

void loop() {
// Enter deep sleep
Serial.println(“Entering deep sleep…”);
esp_deep_sleep_start();
}

In this example, we include the ulp_main.h header file, which is generated by the ESP-IDF build system and contains function prototypes for loading and running the ULP program. We use a bootCount variable stored in RTC memory to ensure that the ULP program is loaded and run only on the first boot by keeping track of the number of boots.

Now, let’s create the ULP assembly program ulp_main.S:

#include “ulp_macros.h”

.section .ulp.text,”ax”,%progbits

.global entry

entry:

    move r3, 1      // Set GPIO to output

    wr_reg rtc_gpio_out, r3

loop:

    set r3, 1       // Toggle GPIO

    xor r3, r3, r3  // Delay

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    wr_reg rtc_gpio_out, r3

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    set r3, 1       // Delay

    nop

    nop

    nop

    nop

    nop

    nop

    nop

    j loop          // Loop indefinitely

How to power IndusBoard Coin with battery

When using the WiFi feature, it’s important to note that the USB connection may not provide sufficient power for extended periods due to the higher power consumption of WiFi. In such cases, it is advisable to switch to a battery for prolonged WiFi usage.

The board can be powered using a 3V, 3.3V, or 3.6V battery by connecting it to the 3V and GND pins on the board. For safety, ensure that you use either the USB or the battery for power, but not both simultaneously. If you need to update the code, disconnect the battery, upload the code, and then reconnect the battery.

Additionally, you can incorporate a battery charging circuit by utilizing the 5V and GND pins on the board.

Learn how we helped 100 top brands gain success