Skip to main content

Tutorial: Your First HIL LISP Program

This tutorial walks you through writing, testing, and deploying your first HIL LISP program—from blinking an LED to reading sensors and controlling actuators.

Prerequisites​

Before starting, ensure you have:

  1. Hardware: Arduino Uno or Nano (ATmega328P)
  2. Software: Node.js ≥18 and npm installed
  3. HIL: npm install -g @srashwan/hilp
  4. Serial Cable: USB cable connected to your Arduino

Step 1: Verify Installation​

# Check hilp is installed
hilp --version
# → v2.0.0

# Check orchestrator is available
which hil-orchestrator
# → /usr/local/bin/hil-orchestrator

Step 2: Initialize a Project​

mkdir my-first-project
cd my-first-project
hilp init

This creates:

my-first-project/
├── hil.json # Project configuration
├── index.hil # Main entry point
└── lib/ # Package directory

Step 3: Write Your First Program​

Open index.hil and write a simple LED blink:

; My first HIL LISP program
; Blinks the built-in LED (pin 13) every 500ms

(SET led-pin 13)

(PIN-MODE D led-pin 1)

(WHILE 1
(DO
(WRITE D led-pin 1)
(LOG "LED ON")
(DELAY 500)
(WRITE D led-pin 0)
(LOG "LED OFF")
(DELAY 500)))

Step 4: Test in the REPL​

Open the web REPL at hil-os.org or connect via serial:

# Connect to device
hil-orchestrator --port /dev/ttyUSB0

Type each line in the REPL:

; Configure LED pin
(PIN-MODE D 13 1)

; Turn LED on
(WRITE D 13 1)

; Read analog sensor
(READ A 0)

; Turn LED off
(WRITE D 13 0)

Step 5: Read a Sensor​

Connect an LM35 temperature sensor to analog pin A0, then run:

; Read temperature from LM35
; LM35 outputs 10mV per degree Celsius
(SET raw (READ A 0))
(SET voltage (* raw 4.88)) ; Convert to millivolts (5000/1023 ≈ 4.88)
(SET temp (/ voltage 10)) ; Convert to Celsius
(LOG "Temperature: " temp " C")

Temperature Monitoring Loop​

; Continuous temperature monitoring
(WHILE 1
(DO
(SET raw (READ A 0))
(SET temp (/ (* raw 488) 10000))
(LOG "Temperature: " temp "C")
(DELAY 2000)))

Step 6: Control an Actuator​

Connect a servo motor to pin 9:

; Servo sweep animation
(FOR angle 0 180
(DO
(SERVO 9 angle)
(DELAY 15)))

(FOR angle 180 0
(DO
(SERVO 9 angle)
(DELAY 15)))

Step 7: Combined Sensor + Actuator​

Create a thermostat that reads temperature and controls a fan:

; Simple thermostat
; LM35 on A0, fan on D9 (PWM)

(SET target-temp 25)
(SET tolerance 2)

(WHILE 1
(DO
(SET temp (/ (* (READ A 0) 488) 10000))
(LOG "Current: " temp "C Target: " target-temp "C")

(IF (> temp (+ target-temp tolerance))
(DO
(WRITE P 9 255)
(LOG "Fan ON - full speed")))

(IF (< temp (- target-temp tolerance))
(DO
(WRITE P 9 0)
(LOG "Fan OFF")))

(DELAY 2000)))

Step 8: Install a Package​

# Search for sensor packages
hilp search lm35

# Install the LM35 package
hilp install lm35-temp

Now use the package in your code:

; Load the LM35 package
(DO (LOAD "lm35-temp/lib/main.hil"))

; Use the library function
(SET temp (LM35-READ 0))
(LOG "Temperature: " temp "C")

Step 9: Flash to Device​

Once your code is working, flash it to run autonomously:

# Transpile and flash
hil-cli firmware flash index.hil

The device will now run your program on boot without needing the host computer.

Next Steps​


Troubleshooting​

IssueSolution
Device not foundCheck USB connection, try different port
Serial timeoutVerify baud rate (115200), check cable
Sensor reads 0Verify wiring, check pin configuration
REPL not respondingClose other serial monitors

For more help, see the Troubleshooting guide.