Reusable ESP32 utility library for PlatformIO and Arduino.
esp32-common is intended to be the low-level shared base for other projects. It now depends only on libraries provided by the Arduino ESP32 core and ESP-IDF integration used by PlatformIO.
Tested with:
esp32devesp32-s3-devkitc-1
CommonLogging macros, fast GPIO helpers, pin setup helpers, time helpers, and small parse/string helpers.TaskMinimal wrapper for FreeRTOS tasks pinned to a chosen core ortskNO_AFFINITY.PWMThin wrapper around the official Arduino-ESP32 LEDC API.BeeperSmall beeper helper built onPWM.AlerterSimple alert task for blinking an LED and/or toggling a beeper pin.EEPROMKVLightweight key/value storage on top ofEEPROM, without external JSON dependency.UtilsSmall utility helpers such asget_cpu_mhz().
From Git:
lib_deps =
https://github.com/gr82morozr/esp32-common.gitFrom a local checkout:
lib_deps =
file://../esp32-commonUse the umbrella header:
#include <esp32-common.h>Or include only the modules you need:
#include <Common/Common.h>
#include <Task/Task.h>
#include <PWM/ESP32PWM.h>
#include <Beeper/Beeper.h>
#include <Alerter/Alerter.h>
#include <Utils/eepromkv.h>
#include <Utils/utils.h>Set these in your app build_flags if your board uses different pins or serial speed:
build_flags =
-DESP32_COMMON_DEFAULT_LED_PIN=5
-DESP32_COMMON_DEFAULT_BEEPER_PIN=17
-DESP32_COMMON_ALERT_DEFAULT_LED_PIN=16
-DESP32_COMMON_ALERT_DEFAULT_BEEPER_PIN=17
-DESP32_COMMON_SERIAL_BAUD=921600You can also override PWM defaults:
build_flags =
-DESP32_COMMON_PWM_DEFAULT_FREQUENCY=5000
-DESP32_COMMON_PWM_DEFAULT_RESOLUTION_BITS=8
-DESP32_COMMON_PWM_DEFAULT_DUTY_CYCLE=0.25f
-DESP32_COMMON_BEEPER_TONE_DUTY_CYCLE=0.5f#include <esp32-common.h>
unsigned long last_log_ms = 0;
void setup() {
common_init();
setPinOutput(ESP32_COMMON_DEFAULT_LED_PIN, false);
LOG_INFO("cpu_mhz=%d", get_cpu_mhz());
}
void loop() {
toggleGPIO(ESP32_COMMON_DEFAULT_LED_PIN);
if (every_ms(last_log_ms, 2000)) {
LOG_INFO("still alive");
}
delay(500);
}For the fastest GPIO hot path, configure once and then use the GPIO helpers directly:
void setup() {
prepareGPIOOutput(ESP32_COMMON_DEFAULT_LED_PIN);
}
void loop() {
toggleGPIO(ESP32_COMMON_DEFAULT_LED_PIN);
}class BlinkTask : public Task {
public:
BlinkTask() : Task("BlinkTask", 4096, 1) {}
void run(void* data) override {
while (true) {
toggleGPIO(ESP32_COMMON_DEFAULT_LED_PIN);
Task::delay(250);
}
}
};ESP32PWM pwm(ESP32_COMMON_DEFAULT_LED_PIN, 5000, 8, 0.5f);
void setup() {
pwm.init();
pwm.setDutyPercent(50.0f);
}Beeper beeper(ESP32_COMMON_DEFAULT_BEEPER_PIN);
void setup() {
beeper.init();
beeper.toneReady();
beeper.beep(1200, 0.05f);
}Alerter alerter(ESP32_COMMON_ALERT_DEFAULT_LED_PIN, -1, ALERT_TYPE_LED);
void setup() {
alerter.setPattern(100, 400);
alerter.start();
}void setup() {
common_init();
const int count = EEPROMKV.readInt("boot_count", 0) + 1;
EEPROMKV.writeInt("boot_count", count);
EEPROMKV.writeBool("configured", true);
}Each example is a standalone PlatformIO project with esp32dev and esp32-s3-devkitc-1 environments.
examples/common_basicLogging, CPU utility, and GPIO helper usage.examples/task_basicRunning a simple FreeRTOS task using theTaskwrapper.examples/pwm_basicLEDC PWM output withESP32PWM.examples/beeper_basicSimple beeper patterns withBeeper.examples/alerter_basicBackground alert blinking withAlerter.examples/eepromkv_basicPersistent key/value storage withEEPROMKV.
Build an example with:
cd examples/eepromkv_basic
pio run -e esp32devOr:
cd examples/eepromkv_basic
pio run -e esp32-s3-devkitc-1common_init()initializesSerialonly if it has not already been started.Commonnow includes overflow-safeelapsed_ms,is_timeout, andevery_mshelpers, plus simple pin setup helpers.CommonincludesprepareGPIOOutput,writeGPIO,toggleGPIO, andreadGPIOas direct-register GPIO helpers. Configure output once before tight write/toggle loops.Taskdefaults totskNO_AFFINITY.Tasknow exposesstartOnce,restart, and basic getters for name/core/priority/stack.EEPROMKVstores escapedkey=valuerecords in EEPROM, and now includes typed read/write helpers plusexistsandremove.ESP32PWMnow includessetDutyPercent,getDutyCycle, andgetFrequency.esp32-commonno longer depends on the old customesp32-pwmlibrary.