pdf-icon

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Stamp-S3Bat Battery

Stamp-S3Bat battery and power input status reading related APIs and example programs.

Example Programs

Compilation Requirements

  • M5Stack Board Manager Version >= 3.2.6
  • Board Option = M5StampS3Bat
  • M5PM1 Library Version >= 1.0.7
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <Arduino.h>
#include <M5PM1.h>
#include <Wire.h>

M5PM1 pm1;

static const uint8_t PIN_SCL            = 47;
static const uint8_t PIN_SDA            = 48;
static const uint8_t LED_COUNT          = 1;
static const uint8_t LED_BRIGHTNESS     = 64;
static const m5pm1_rgb_t LED_COLOR      = {0, LED_BRIGHTNESS, 0};
static const uint32_t PRINT_INTERVAL_MS = 2000;

static void printVoltages()
{
    uint16_t mv = 0;

    if (pm1.readVref(&mv) == M5PM1_OK) {
        Serial.printf("[PM1][I] Vref: %u mV (%.3f V)\r\n", mv, mv / 1000.0f);
    }
    if (pm1.readVbat(&mv) == M5PM1_OK) {
        Serial.printf("[PM1][I] VBAT: %u mV (%.3f V)\r\n", mv, mv / 1000.0f);
    }
    if (pm1.readVin(&mv) == M5PM1_OK) {
        Serial.printf("[PM1][I] VIN: %u mV (%.3f V)\r\n", mv, mv / 1000.0f);
    }
    if (pm1.read5VInOut(&mv) == M5PM1_OK) {
        Serial.printf("[PM1][I] 5V IN/OUT: %u mV (%.3f V)\r\n", mv, mv / 1000.0f);
    }
}

static bool setupLedDemo()
{
    pm1.gpioSetFunc(M5PM1_GPIO_NUM_0, M5PM1_GPIO_FUNC_OTHER);
    pm1.gpioSetDrive(M5PM1_GPIO_NUM_0, M5PM1_GPIO_DRIVE_PUSHPULL);
    pm1.gpioSetOutput(M5PM1_GPIO_NUM_0, true);
    pm1.pinMode(M5PM1_GPIO_NUM_0, M5PM1_OTHER);
    pm1.setLedEnLevel(true);
    pm1.setLedCount(LED_COUNT);
    pm1.setLedColor(0, LED_COLOR);
    pm1.refreshLeds();
    Serial.println("[DEMO] RGB on");
    delay(700);
    return true;
}

void setup()
{
    Serial.begin(115200);
    delay(300);
    Serial.println("[DEMO] Boot");

    Wire.end();
    Wire.begin(PIN_SDA, PIN_SCL, 100000U);
    if (pm1.begin(&Wire, M5PM1_DEFAULT_ADDR, PIN_SDA, PIN_SCL, M5PM1_I2C_FREQ_400K) != M5PM1_OK) {
        Serial.println("[DEMO] PM1 init failed");
        return;
    }
    Serial.println("[DEMO] PM1 init ok");
    setupLedDemo();
}

void loop()
{
    static uint32_t lastMs = 0;
    if (millis() - lastMs >= PRINT_INTERVAL_MS) {
        lastMs = millis();
        printVoltages();
    }
}
On This Page