Analogue
PWM
Servos
I²C
SPI
UART
Script
Ready
Script reference
Basics
# comment x = adc.read 0 # command form: arguments separated by commas i2c.write 0x68, 0x6B, 0x00 if adc.read(0) > 512 # inside expressions use brackets io.set 12, 1 elif x == 0 io.set 12, 0 else print "x is", x, hex(x) end repeat 10 … end loop … end while cond … end for i = 0 to 255 step 5 … end for a in list … end def name(a, b) … return v … end break continue exit wait 100 # milliseconds
Maths uses JavaScript operators: + - * / % << >> & | ^ == != < >, plus and / or. ^ is XOR, use pow(a,b) for powers. Variables set inside a def are local to it.
Board commands
| io.set pin, v | Drive pin 0–17 high (1) or low (0). Also io.high, io.low, io.toggle. |
| io.get pin | Make pin an input and return 0 / 1. |
| adc.read ch | 10-bit reading (0–1023) from ADC0–2. adc.read8 gives 0–255, adc.volts gives volts. |
| dac.enable / dac.disable | Turn the DAC output on pin 3 on or off. |
| dac.set v | DAC level 0–31. |
| pwm.enable ch / pwm.disable ch | PWM channel 0 or 1. |
| pwm.duty ch, v | 8-bit duty 0–255. pwm.duty10 ch, v for 0–1023. |
| pwm.prescaler code | 0=1:1, 1=1:4, 2=1:16, 3=1:64. |
| servo.enable ch / servo.disable ch | Servo channel 0–5 (S0–S5). |
| servo.set ch, pos | Position 0–255. servo.angle ch, deg takes 0–180. |
| i2c.init | Initialise the I²C master. |
| i2c.write addr, reg, data… | Write bytes to a register. addr is the 7-bit address. Data can be numbers, lists or strings. Pass null as reg to skip it. |
| i2c.read addr, reg, n | Read n bytes from a register and return a list. i2c.readByte addr, reg returns one byte. i2c.readWord addr, reg returns 16 bits, MSB first. i2c.readRaw addr, n reads without a register. |
| i2c.probe addr / i2c.scan | Check one address and return true or false, or scan and return a list of addresses. |
| i2c.start / restart / stop | Low-level bus conditions. |
| i2c.send byte | Send a raw byte. Returns 0 for ACK, 1 for NACK. |
| i2c.recv last | Receive a byte. Use last=1 on the final byte to send a NACK. |
| spi.init / spi.prescaler code | Initialise SPI. Prescaler codes: 0=1:1, 1=1:4, 2=1:16. |
| spi.transfer byte | Full-duplex single byte. Chip select is not changed. |
| spi.xfer bytes… | Pull CS low, transfer every byte, release CS and return the received list. |
| spi.cs pin | Set the CS pin used by xfer (default 11, -1 for none). Also spi.select and spi.deselect. |
| uart.init / uart.baud rate | Initialise the UART or set its baud rate (1200–115200). |
| uart.write data… | Send numbers, lists or strings. |
| uart.available | Number of bytes waiting. |
| uart.read | Read one byte (255 if the buffer is empty). |
| uart.readAll / uart.readString | Read everything waiting, as a list or as text. |
| uart.readLine timeout | Wait for a line of text. Returns null on timeout (default 1000 ms). |
| board.raw cmd, [params], n | Send a raw protocol command and return n reply bytes. |
Helpers
| print a, b, … | Write to the console. clear empties it. |
| hex(v) bin(v) | Format a number, or a list of bytes, as hex or binary. |
| u16(msb, lsb) s16(msb, lsb) s8(v) | Combine bytes, or convert to a signed value. |
| bit(v, n) | Value of bit n. |
| str(list) bytes(…) | Convert bytes to text, or text and values to a list of bytes. |
| len(x) map(v,a,b,c,d) constrain(v,lo,hi) | Length, range mapping and clamping. |
| abs min max round floor ceil sqrt pow sin cos tan atan2 log exp random PI now() | Maths functions. now() returns milliseconds. |