A modern, ESM-compatible, TypeScript implementation of the STK500v1 protocol for programming Arduino boards directly from Node.js or the browser.
- Full JavaScript/TypeScript implementation of the STK500v1 programmer
- ESM (ECMAScript Modules) compatible
- Can be used in Node.js or browser environments
- No dependency on avrdude or the Arduino IDE
- TypeScript support for improved developer experience
- Built-in Intel HEX parsing (no need for external parsing libraries)
npm install stk500-esm
Here's a basic example of how to use stk500-esm to program an Arduino:
import { SerialPort } from "serialport";
import fs from "fs/promises";
import STK500, { type Board } from "stk500-esm";
const board: Board = {
name: "Arduino Uno",
baudRate: 115200,
signature: new Uint8Array([0x1e, 0x95, 0x0f]),
pageSize: 128,
timeout: 400,
};
async function upload(path: string) {
let serialPort;
try {
const hexData = await fs.readFile("path/to/your/sketch.hex");
serialPort = new SerialPort({ path, baudRate: board.baudRate });
const stk = new STK500(serialPort, board);
await stk.bootload(hexData);
console.log("Programming successful!");
} catch (error) {
console.error("Programming failed:", error);
} finally {
if (serialPort) {
await serialPort.close();
}
}
}
upload("/dev/ttyACM0"); // Replace with your Arduino's serial port
For more detailed examples, please check the examples
folder in the repository. It contains several TypeScript files demonstrating how to use stk500-esm with different Arduino boards:
avr4809.ts
: Example for AVR4809 based boardsdiecimila-duemilanove168.ts
: Example for Arduino Diecimila and Duemilanove (ATmega168)duemilanove328.ts
: Example for Arduino Duemilanove (ATmega328)lg8f328.ts
: Example for LGT8F328 boardsnano.ts
: Example for Arduino Nanouno.ts
: Example for Arduino Uno
These examples show how to set up the board configuration, read hex files, and upload them to the respective Arduino boards.
To run an example, use:
npx tsx examples/uno.ts /dev/ttyACM0
Replace uno.ts
with the appropriate example file and /dev/ttyACM0
with your Arduino's serial port.
The main class STK500
provides the following methods:
constructor(stream: Duplex, board: Board, opts?: STK500Options)
bootload(hexData: string | Uint8Array, progressCallback?: BootloadProgressCallback): Promise<void>
sync(attempts: number): Promise<Uint8Array>
verifySignature(): Promise<Uint8Array>
upload(hexData: string | Uint8Array, progressCallback?: (percentage: number) => void): Promise<void>
verify(hexData: string | Uint8Array, progressCallback?: (percentage: number) => void): Promise<void>
For more detailed API information, please refer to the TypeScript definitions or the source code.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.