Home › Embedded / firmware › Arduino / PlatformIO rules

Arduino / PlatformIO rules

Cursor rules for microcontroller projects with PlatformIO and the Arduino framework: non-blocking loops, memory, pins.

CursorEmbedded / firmware

How to use

Save this as .cursor/rules/platformio.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/platformio.mdcDownload  
---
description: Arduino / PlatformIO conventions
globs: ["src/**/*.{cpp,h,ino}","include/**/*.h","platformio.ini"]
alwaysApply: false
---

# PlatformIO + Arduino framework

- Build with `pio run`, test with `pio test -e native` (host) or the board env; upload/monitor (`pio run -t upload`, `pio device monitor`) only when the user asks.
- Boards and libraries are declared in `platformio.ini` (`lib_deps` with pinned versions). Don't vendor libraries into `src/`.
- Code in `src/` split into modules (`sensors.cpp/.h`, `display.cpp/.h`); `main.cpp` has only `setup()` and `loop()` calling them.
- `loop()` must never block: no `delay()` beyond a few ms. Use `millis()`-based timers (`if (millis() - last >= interval)`) or a small state machine per task. Handle `millis()` rollover by subtracting, never comparing absolute times.
- Pins are named `constexpr uint8_t PIN_LED = 2;` in a `pins.h`, never magic numbers.
- Memory on small MCUs: avoid `String` concatenation in loops (heap fragmentation); use fixed `char` buffers with `snprintf`. Keep constant strings in flash (`F("...")` on AVR).
- Interrupt handlers are tiny, set `volatile` flags, and are attached with `digitalPinToInterrupt`.
- Debounce buttons in software; check sensor read return values and timeouts.
- Serial logging behind a `DEBUG` macro so release builds stay quiet.
- Secrets (Wi-Fi, API keys) in a gitignored `secrets.h` with a committed `secrets.example.h`.

## Done when
`pio run` builds every env with no warnings and RAM/flash usage from the build output is reported.

More: all Embedded / firmware rules · all Cursor rules

Related rules

Embedded C (bare metal / RTOS) AGENTS.md

AGENTS.md for embedded C firmware (bare metal, Zephyr, FreeRTOS): safety-minded C rules, memory, and hardware access.

AGENTS.mdEmbedded / firmware

ESP32 ESP-IDF firmware CLAUDE.md

CLAUDE.md for ESP32 firmware on ESP-IDF 5.x in C: components, FreeRTOS tasks, error handling, and logging.

Claude Code (CLAUDE.md)Embedded / firmware

Modern C++ with CMake rules

Cursor rules for modern C++ (C++20/23) projects built with CMake presets: RAII, ownership, warnings, and tests.

CursorC++

Solidity + Foundry Copilot path instructions

Path-specific Copilot instructions for Solidity 0.8.x contracts with Foundry: security patterns, fuzz tests, and gas.

GitHub CopilotSolidity / smart contracts

Solidity smart contract security CLAUDE.md

CLAUDE.md for reviewing and writing Solidity contracts safely: threat checklist, Foundry tests, and no-deploy rules.

Claude Code (CLAUDE.md)Solidity / smart contracts

Zig AGENTS.md

AGENTS.md for Zig 0.15 projects: build.zig, explicit allocators, error unions, defer cleanup, and zig test.

AGENTS.mdZig

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.