5. CircuitPython Getting Started - CRCibernetica/circuitpython-ideaboard GitHub Wiki
Adafruit CircuitPython Essentials
Adafruit has a great tutorial to begin working with CircuitPython called CircuitPython Essentials.
IdeaBoard Pin Names
To access the pin names for the IdeaBoard use the board
module:
Type this directly into the REPL
>>> import board
>>> dir(board)
['__class__', '__name__', 'I2C', 'IO0', 'IO1', 'IO10', 'IO11', 'IO12', 'IO13', 'IO14', 'IO15', 'IO16', 'IO17', 'IO18', 'IO19', 'IO2', 'IO20', 'IO21', 'IO22', 'IO23', 'IO25', 'IO26', 'IO27', 'IO3', 'IO32', 'IO33', 'IO34', 'IO35', 'IO36', 'IO39', 'IO4', 'IO5', 'IO6', 'IO7', 'IO8', 'IO9', 'MISO', 'MOSI', 'NEOPIXEL', 'RX', 'SCK', 'SCL', 'SDA', 'SPI', 'TX', 'board_id']
>>>
IdeaBoard I2C
The I2C bus is a great way to easily add devices to the IdeaBoard using the STEMMA QT / QWIIC connector or connecting directly to the SDA
, SCL
headers. Use the following code to connect and scan the I2C bus. In this example the code scans the I2C bus and finds the address (0x10) which is a GPS.
import board
i2c = board.I2C()
# Ensure exlusive access to i2c bus
while not i2c.try_lock():
pass
# scan the bus
for device in i2c.scan():
print(hex(device))
>>> %Run -c $EDITOR_CONTENT
0x10
Basic GPIO
Digital In / Out
The digitalio
module is used to control the digital inputs and outputs.
Digital output example on pin 27
import time
import board
from digitalio import DigitalInOut, Direction, Pull
# Digital Output on IO27
my_input = DigitalInOut(board.IO27) # Choose pin 27
my_output.direction = Direction.OUTPUT # Set the direction to output
while True:
my_output.value = 1 # Set value 1 (high/on)
time.sleep(2)
my_output.value = 0 # Set value 0 (low/off)
time.sleep(2)
Digtal Input example on pin 33
import time
import board
from digitalio import DigitalInOut, Direction, Pull
# Digital Output on IO27
my_input = DigitalInOut(board.IO33) # Choose pin 33
my_input.direction = Direction.INPUT # Optional - default is INPUT
my_input.pull = Pull.UP # Add the internal PULLUP resistor if needed
while True:
print(my_input.value)
time.sleep(0.5)
False
True
False
False
False
True
True
Analog In
The analogio
module is used to read analog inputs.
Note that pins 4, 25, 26, and 27 cannot be used as Analog Inputs if WIFI is being used.
import time
import board
from analogio import AnalogIn
# Connect potentiometer to pin 33
analog_in = AnalogIn(board.IO33)
while True:
# Values are from 0 to 65535 (2 ** 16)
print(analog_in.value)
time.sleep(0.1)
Wifi
Basic Connection
Change ssid
and password
to your WIFI credentials.
import wifi
print("Connecting...")
wifi.radio.connect("ssid", "password")
print("Connected to Wifi!")
ip = wifi.radio.ipv4_address
print(f"IP: {ip}"))
>>> %Run -c $EDITOR_CONTENT
Connecting...
Connected to Wifi!
IP: 192.168.84.20
>>>
HTTP GET with JSON
This example shows how to connect to WIFI, do an HTTP GET and parse the JSON received.
import socketpool
import ssl
import wifi
import adafruit_requests as requests
socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())
print("Connecting...")
wifi.radio.connect("ssid", "password")
print("Connected to Wifi!")
URL = "http://api.open-notify.org/iss-now.json"
data = https.get(URL).json()
print(data)
long = data["iss_position"]["longitude"]
lat = data["iss_position"]["latitude"]
print(f"The International Space Station is located at {lat}, {long}")
>>> %Run -c $EDITOR_CONTENT
Connecting...
Connected to Wifi!
{'iss_position': {'longitude': '30.5286', 'latitude': '49.6584'}, 'message': 'success', 'timestamp': 1669389718}
The International Space Station is located at 49.6584, 30.5286