Skip to content

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 ​

  1. Memory check. memoryUsage() reports heap usage at startup. Useful for spotting leaks over time.

  2. 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).

  3. Main loop. Switches the pin between 0 and 1 every second.

  4. Async delay. await sleep(1000) pauses for 1 second.

Create project ​

sh
pnpm create mikro --template blinky
sh
npm create mikro -- --template blinky
sh
yarn create mikro --template blinky
sh
bun create mikro --template blinky

Run it ​

sh
pnpm install
pnpm mikro flash  # only needed once per board
pnpm mikro dev
sh
npm install
npx mikro flash  # only needed once per board
npx mikro dev
sh
yarn install
yarn mikro flash  # only needed once per board
yarn mikro dev
sh
bun install
bunx mikro flash  # only needed once per board
bunx mikro dev

The LED blinks, and the console prints memory usage every second.

View source on GitHub

Mikro.js is built with AI assistance, code and docs alike. Read the AI disclosure.