Blinky
The classic first program: blink an LED on and off. Uses the gpio and sleep APIs.
Hardware
- Any ESP32 board with a built-in LED, or an external LED connected to a GPIO pin
- USB cable
Code
ts
import {DigitalOut} from 'mikro/gpio'
import {sleep} from 'mikro/sleep'
import {memoryUsage} from 'mikro/sys'
const mem = memoryUsage()
console.log('free heap: %dKB', (mem.heapTotal - mem.heapUsed) / 1000)
// GPIO 15 is the built-in LED on XIAO ESP32C6. Replace with your board's LED pin.
const led = DigitalOut(15).orPanic('Failed to configure LED pin')
let level: 0 | 1 = 0
while (true) {
level = level ? 0 : 1
console.log(level ? 'HIGH' : 'LOW')
const mem = memoryUsage()
console.log('free memory: %dKB', (mem.heapTotal - mem.heapUsed) / 1000)
led.write(level)
await sleep(1000)
}Walkthrough
Memory check.
memoryUsage()reports heap usage at startup. Useful for spotting leaks over time.Pin setup.
DigitalOut(15)claims GPIO 15 (the LED on XIAO ESP32C6) and configures it as an output..orPanic()crashes with a clear message if this fails (for example on an invalid GPIO number, or a GPIO pin that is already in use).Main loop. Switches the pin between
0and1every second.Async delay.
await sleep(1000)pauses for 1 second.
Create project
sh
pnpm create mikro --template blinkysh
npm create mikro -- --template blinkysh
yarn create mikro --template blinkysh
bun create mikro --template blinkyRun it
sh
pnpm install
pnpm mikro flash # only needed once per board
pnpm mikro devsh
npm install
npx mikro flash # only needed once per board
npx mikro devsh
yarn install
yarn mikro flash # only needed once per board
yarn mikro devsh
bun install
bunx mikro flash # only needed once per board
bunx mikro devThe LED blinks, and the console prints memory usage every second.