Quick reference

This quick reference is meant to help engineers to quickly start coding and testing alpha:ONE with MicroPython.



alpha:ONE comes with MicroPython preinstalled. alpha:ONE’s Micropython is a branch out of MicroPython v1.13 for pyboard v1.1. This quick reference leans therefore very much on the quick reference for the pyboard.

General board control

import pyb

pyb.repl_uart(pyb.UART(1, 9600))  # duplicate REPL on UART(1)
pyb.wfi()  # pause CPU, waiting for interrupt
pyb.freq()  # get CPU and bus frequencies
pyb.freq(60000000)  # set CPU freq to 60MHz
pyb.stop()  # stop CPU, waiting for external interrupt

Delay and timing

import time

time.sleep(1)  # sleep for 1 second
time.sleep_ms(500)  # sleep for 500 milliseconds
time.sleep_us(10)  # sleep for 10 microseconds
start = time.ticks_ms()  # get value of millisecond counter
delta = time.ticks_diff(time.ticks_ms(), start)  # compute time difference

Status LEDs

The module status_LEDs.py controls the status LEDs on the alpha:ONE board.

from status_LEDs import status_LEDs
import time

status_LEDs = status_LEDs()

# there are LED_nr 1, 2 and 3, max brightness is 31
status_LEDs.set_LED_color(LED_nr=1,color='red', brightness=2)  # max brightness is 31
status_LEDs.set_LED_rgb(LED_nr=1, red=167, green=23, blue=255, brightness=3)


There are twelve 24V inputs, from input_1 to input_12. They are defined in boot.py. When an input gets a high, the corresponding input LED switches on.

input_1.value() # get value, 0 or 1


There are six power outputs, from output_1 to output_6. They are defined in boot.py. When an output is switch on, the corresponding output LED switches on.

Set output high with:

output_1.high()  # output_1 is switched on

Set output low with:

output_1.low()  # output_1 is switched off

External interrupts

from pyb import Pin, ExtInt

callback = lambda e: print("intr")
ext_gpio_1 = ExtInt(GPIO_1, ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
ext_input_1 = ExtInt(input_1, ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)


from pyb import Timer

tim = Timer(1, freq=1000)
tim.counter()  # get counter value
tim.freq(0.5)  # 0.5 Hz
tim.callback(lambda t: pyb.LED(1).toggle())

RTC (real time clock)

from pyb import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0))  # set a specific date and time
rtc.datetime()  # get date and time

Pins and GPIO

Applies only to the multifunction pins.

from pyb import Pin

p_out = Pin('F6', Pin.OUT_PP)

p_in = Pin(GPIO_2, Pin.IN, Pin.PULL_UP)
p_in.value()  # get value, 0 or 1

PWM (pulse width modulation)

Applies only to the multifunction pins.

from pyb import Pin, Timer

p = Pin('F8')  # 'F8' or GPIO_3
# F8 has TIM13, CH1
tim = Timer(13, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)

ADC (analog to digital conversion)

Applies only to the multifunction pins.

from pyb import Pin, ADC

adc = ADC(Pin('F9'))  # 'F9' or GPIO_4
adc.read()  # read value, 0-4095

DAC (digital to analog conversion)

Applies only to the multifunction pins.

from pyb import Pin, DAC

dac = DAC(Pin('A4'))  # 'A4' or GPIO_8
dac.write(120)  # output between 0 and 255

UART (serial bus)

Applies only to the multifunction pins.

from pyb import UART

uart = UART(4, 9600)
uart.read(5)  # read up to 5 bytes

SPI bus

Applies only to the multifunction pins.

from pyb import SPI

spi = SPI(5, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
spi.recv(5)  # receive 5 bytes on the bus
spi.send_recv('hello')  # send and receive 5 bytes

I2C bus

Applies only to the multifunction pins.

from machine import I2C, SoftI2C

i2c = I2C(id=2, scl='B10', sda='B11', freq=400000) # create hardware I2c object
i2c = SoftI2C(scl=GPIO_1, sda=GPIO_2, freq=100000)  # create software I2C object

i2c.scan()  # returns list of slave addresses
i2c.writeto(0x42, 'hello')  # write 5 bytes to slave with address 0x42
i2c.readfrom(0x42, 5)  # read 5 bytes from slave

i2c.readfrom_mem(0x42, 0x10, 2)  # read 2 bytes from slave 0x42, slave memory 0x10
i2c.writeto_mem(0x42, 0x10, 'xy')  # write 2 bytes to slave 0x42, slave memory 0x10


import network
import pyb


eth = network.WIZNET5K(pyb.SPI('W5500'), W5500_cs, W5500_rst)



print('eth.status = {}'.format(eth.status()))
print('eth.active = {}'.format(eth.active()))
print('eth.isconnected = {}'.format(eth.isconnected()))
print('eth.ifconfig = {}'.format(eth.ifconfig()))

Instantiate a socket:

import usocket as socket
print('socket.getaddrinfo(\'www.systematica.ch\',80) = {}'.format(socket.getaddrinfo('www.systematica.ch',80)))

Ping a website / ip address:

from uping import ping
ping('www.systematica.ch', count=4, interval=250)

CAN bus (controller area network)

from pyb import CAN

can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
can.send('message!', 123)  # send a message with id 123
can.recv(0)  # receive message on FIFO 0


RS are defined in boot.py

# RS232_485 test in RS232 mode
RS232_485 = pyb.UART('RS232_485', baudrate=9600)
RS232_485_n232.low()  # pin low enables RS232 mode
RS232_485_DE485_F232.low()  # pin low enables low-EMI mode for <= 250 kBit/s
RS232_485.write('Test RS232')


There are two RS422/485 interfaces, RS422/485 1 and RS422/485 2.

# RS485_1 test
RS485_1 = pyb.UART('RS485_1', baudrate=9600)  # to use RS485 2 set 'RS485_2'
RS485_1_nRE.low()  # pin low enables the receiver
RS485_1_DE.high()  # pin high enables the driver permanently, only ok in full-duplex mode (4 wires)
RS485_1_nSLOW.low()  # pin low enables low-EMI mode for <= 250 kBit/s
RS485_1.write('Test RS485 1')