pdf-icon

Arduino Guide

Unit TimerPWR Arduino Tutorial

1. Preparation

2. Example Program

Example Explanation
The Unit TimerPWR is a timed power unit that can turn the HY2.0-4P Grove interface’s 5V output on and off based on a set cycle. In this example, we will configure the Unit TimerPWR to control the power supply cycle by writing I2C commands. This allows the main device to perform operations periodically and then completely turn off the power to achieve low power consumption.

Set Single Wake-up

Disable the cycle mode, and after touching the CoreS3, immediately turn off the power and restore the power after the off-time ends.

#include "M5Unified.h"
#include "Wire.h"
#include "m5_unit_timerpwr.hpp"

M5UnitTimerPWR timer_pwr;

void setup()
{
    M5.begin();
    Serial.begin(115200);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::lgfxJapanMinchoP_24);

    while (!timer_pwr.begin(&Wire, TIMERPWR_ADDR, 2, 1, 400000U)) {
        M5.Display.drawString("Unit TimerPWR init Fail!", M5.Display.width() / 2, M5.Display.height() / 2);

        delay(1000);
    };
    M5.Display.clear();
    M5.Display.drawString("Unit TimerPWR init OK", M5.Display.width() / 2, M5.Display.height() / 2 - 20);
    M5.Display.drawString("Touch To Sleep 10S", M5.Display.width() / 2, M5.Display.height() / 2 + 20);

    // do something before sleep...

    // disable cycle sleep mode
    timer_pwr.set_cycle_sleep(false);
}

void loop()
{
    M5.update();
    auto t = M5.Touch.getDetail();

    if (t.wasClicked() || M5.BtnA.wasClicked()) {
        M5.Display.clear();
        M5.Display.drawString("Sleep 10S!", M5.Display.width() / 2, M5.Display.height() / 2);
        delay(1000);
        timer_pwr.set_power_on_time(0, 0, 0);
        timer_pwr.set_power_off_time(0, 0, 10);
        timer_pwr.set_sleep();
    }
}

Set Cycle Wake-up

Enable the cycle mode, configure the on/off time, and once the cycle starts, it will begin counting. After the on-time runs out, it will immediately turn off the power and restore power after the off-time ends. The cycle will repeat until new configuration is written or the battery is fully drained.

#include "M5Unified.h"
#include "Wire.h"
#include "m5_unit_timerpwr.hpp"

M5UnitTimerPWR timer_pwr;

void setup()
{
    M5.begin();
    Serial.begin(115200);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setFont(&fonts::lgfxJapanMinchoP_24);

    while (!timer_pwr.begin(&Wire, TIMERPWR_ADDR, 2, 1, 400000U)) {
        M5.Display.drawString("Unit TimerPWR init Fail!", M5.Display.width() / 2, M5.Display.height() / 2);

        delay(1000);
    };
    M5.Display.clear();
    M5.Display.drawString("Unit TimerPWR init OK", M5.Display.width() / 2, M5.Display.height() / 2 - 20);
    M5.Display.drawString("ON 10S / OFF 5S", M5.Display.width() / 2, M5.Display.height() / 2 + 20);
}

void loop()
{
    M5.update();
    auto t = M5.Touch.getDetail();

    if (t.wasClicked() || M5.BtnA.wasClicked()) {
        M5.Display.clear();
        M5.Display.drawString("Start Cycle ON/OFF!", M5.Display.width() / 2, M5.Display.height() / 2);
        delay(1000);
        // enable cycle sleep mode
        timer_pwr.set_cycle_sleep(false);
        timer_pwr.set_power_on_time(0, 0, 10);
        timer_pwr.set_power_off_time(0, 0, 20);
        timer_pwr.save_flash();
        timer_pwr.set_cycle_sleep(true);
    }
    if (t.wasHold() || M5.BtnA.wasHold()) {
        // disable cycle sleep mode
        timer_pwr.set_cycle_sleep(false);
        M5.Display.clear();
        M5.Display.drawString("Stop Cycle ON/OFF!", M5.Display.width() / 2, M5.Display.height() / 2);
        delay(1000);
    }
}

3. Compile and Upload

    1. Download Mode: Different devices may require entering download mode before programming. The steps for this may vary depending on the main control device. For more details, refer to the Arduino IDE Getting Started Guide , which includes specific download tutorial lists for each device.
  • For CoreS3, press and hold the reset button (for about 2 seconds) until the internal green LED lights up, and then release it. This indicates the device is in download mode and ready for programming.

    1. Select the device port and click the compile and upload button in the top left corner of Arduino IDE, then wait for the program to compile and upload to the device.

4. Timer Wakeup

Use the Unit TimerPWR to provide periodic wake-up for the CoreS3. (Note: The effect can be achieved by disconnecting the CoreS3 base battery power connection).

Touch the CoreS3 screen to control the single sleep-wake operation.

Entering sleep mode.

On This Page