Examples

See the CircuitPython docs for extensive API documentation which should (mostly) work with Blinka.

examples/analog_in.py
 1# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4"""Analog in demo"""
 5import time
 6import board
 7from analogio import AnalogIn
 8
 9analog_in = AnalogIn(board.A1)
10
11
12def get_voltage(pin):
13    return (pin.value * 3.3) / 4096
14
15
16while True:
17    print((get_voltage(analog_in),))
18    time.sleep(0.1)
examples/bbb_digitalio.py
 1#!/usr/bin/env python3
 2# -*- coding: utf-8 -*-
 3
 4# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 5#
 6# SPDX-License-Identifier: MIT
 7
 8# Example of blinking LED on BeagleBone Black
 9# https://www.adafruit.com/product/1876
10#
11# Wire the circuit as follows:
12# 1) connect anode (+) lead of LED to P9.12 pin
13# 2) connect cathode (-) lead to 1K Ohm resistor
14# 3) connect that 1K Ohm resistor to DGND (P9.1)
15#
16# NOTE: the pin mode can be verified with the command line
17# utility config-pin on the BeagleBoard.org Debian image
18#
19# To verify the pin is in GPIO mode:
20# debian@beaglebone:~$ config-pin -q p9.12
21# P9_12 Mode: gpio Direction: out Value: 0
22#
23# To set pin to GPIO mode:
24# $ config-pin p9.12 gpio
25
26import time
27import board
28import digitalio
29
30print("hello blinky!")
31
32led = digitalio.DigitalInOut(board.P9_12)
33led.direction = digitalio.Direction.OUTPUT
34
35while True:
36    led.value = True
37    time.sleep(0.5)
38    led.value = False
39    time.sleep(0.5)
examples/mcps_busio_i2c.py
 1# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4import time
 5import hid
 6import busio
 7
 8from adafruit_blinka.microcontroller.mcp2221.mcp2221 import mcp2221 as _mcp2221
 9from adafruit_blinka.microcontroller.mcp2221.mcp2221 import MCP2221 as _MCP2221
10from adafruit_blinka.microcontroller.mcp2221.i2c import I2C as _MCP2221I2C
11
12MLXADDR = 0x33
13ADDRID1 = 0x2407
14
15
16class MCP2221(_MCP2221):  # pylint: disable=too-few-public-methods
17    def __init__(self, address):
18        self._hid = hid.device()
19        self._hid.open_path(address)
20        print("Connected to " + str(address))
21        self._gp_config = [0x07] * 4  # "don't care" initial value
22        for pin in range(4):
23            self.gp_set_mode(pin, self.GP_GPIO)  # set to GPIO mode
24            self.gpio_set_direction(pin, 1)  # set to INPUT
25
26
27class MCP2221I2C(_MCP2221I2C):  # pylint: disable=too-few-public-methods
28    def __init__(self, mcp2221, *, frequency=100000):
29        self._mcp2221 = mcp2221
30        self._mcp2221.i2c_configure(frequency)
31
32
33class I2C(busio.I2C):  # pylint: disable=too-few-public-methods
34    def __init__(self, mcp2221_i2c):
35        self._i2c = mcp2221_i2c
36
37
38addresses = [mcp["path"] for mcp in hid.enumerate(0x04D8, 0x00DD)]
39
40i2c_devices = []
41i2c_devices.append(I2C(MCP2221I2C(_mcp2221)))
42
43for addr in addresses:
44    try:
45        i2c_device = I2C(MCP2221I2C(MCP2221(addr)))
46        i2c_devices.append(i2c_device)
47    except OSError:
48        print("Device path: " + str(addr) + " is used")
49
50
51while True:
52    for i2c in i2c_devices:
53        addrbuf = bytearray(2)
54        addrbuf[0] = ADDRID1 >> 8
55        addrbuf[1] = ADDRID1 & 0xFF
56        inbuf = bytearray(6)
57        i2c.writeto_then_readfrom(MLXADDR, addrbuf, inbuf)
58        print("Device " + str(i2c_devices.index(i2c)) + ": ")
59        print(inbuf)
60        time.sleep(0.5)
examples/pb_digitalio.py
 1#!/usr/bin/env python3
 2# -*- coding: utf-8 -*-
 3
 4# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 5#
 6# SPDX-License-Identifier: MIT
 7
 8# Example of blinking LED on PocketBeagle
 9# https://www.adafruit.com/product/4179
10#
11# Wire the circuit as follows:
12# 1) connect anode (+) lead of LED to P1_33 pin
13# 2) connect cathode (-) lead to 1K Ohm resistor
14# 3) connect that 1K Ohm resistor to GND
15#
16# NOTE: the pin mode can be verified with the command line
17# utility config-pin on the BeagleBoard.org Debian image
18#
19# To verify the pin is in GPIO mode:
20# debian@beaglebone:~$ config-pin -q p1.33
21# P1_33 Mode: gpio Direction: out Value: 0
22#
23# To set pin to GPIO mode:
24# $ config-pin p1.33 gpio
25
26import time
27import board
28import digitalio
29
30print("hello blinky!")
31
32led = digitalio.DigitalInOut(board.P1_33)
33led.direction = digitalio.Direction.OUTPUT
34
35while True:
36    led.value = True
37    time.sleep(0.5)
38    led.value = False
39    time.sleep(0.5)
examples/pi_busio_i2c.py
 1# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4import time
 5import sys
 6import board
 7import busio
 8
 9print("hello blinka!")
10
11i2c = busio.I2C(board.SCL, board.SDA)
12
13print("I2C devices found: ", [hex(i) for i in i2c.scan()])
14
15if not 0x18 in i2c.scan():
16    print("Didn't find MCP9808")
17    sys.exit()
18
19
20def temp_c(data):
21    value = data[0] << 8 | data[1]
22    temp = (value & 0xFFF) / 16.0
23    if value & 0x1000:
24        temp -= 256.0
25    return temp
26
27
28while True:
29    i2c.writeto(0x18, bytes([0x05]), stop=False)
30    result = bytearray(2)
31    i2c.readfrom_into(0x18, result)
32    print(temp_c(result))
33    time.sleep(0.5)
examples/pi_busio_spi.py
 1# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4import time
 5import board
 6import busio
 7
 8spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
 9while not spi.try_lock():
10    pass
11spi.configure(baudrate=16000000)
12spi.unlock()
13
14while True:
15    spi.write(bytes(range(64)))
16    time.sleep(0.1)
examples/pi_digitalio.py
 1# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4import time
 5import board
 6import digitalio
 7
 8print("hello blinka!")
 9
10led = digitalio.DigitalInOut(board.D4)
11led.direction = digitalio.Direction.OUTPUT
12
13button = digitalio.DigitalInOut(board.D18)
14button.direction = digitalio.Direction.INPUT
15button.pull = digitalio.Pull.DOWN
16
17while True:
18    led.value = button.value
19    time.sleep(0.1)
examples/piblinka.py
 1# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4import sys
 5import time
 6from adafruit_blinka import agnostic
 7import board
 8import digitalio
 9
10# from Adafruit_GPIO import Platform
11# print("Platform = ", Platform.platform_detect(), Platform.pi_version())
12
13print("hello blinka!")
14
15print(
16    "Found system type: %s (sys.platform %s implementation %s) "
17    % (agnostic.board_id, sys.platform, sys.implementation.name)
18)
19
20print("board contents: ", dir(board))
21
22
23led = digitalio.DigitalInOut(board.D4)
24led.direction = digitalio.Direction.OUTPUT
25
26button = digitalio.DigitalInOut(board.D18)
27button.direction = digitalio.Direction.INPUT
28button.pull = digitalio.Pull.DOWN
29
30while True:
31    led.value = button.value
32    time.sleep(0.1)