4. IdeaBoard Library Usage - CRCibernetica/circuitpython-ideaboard GitHub Wiki
Overview
The IdeaBoard library contains helper functions to use the onboard motor drivers, the onboard RGB LED and servos. Additionally, the map_range function has been included for convenience.
Basic Usage
Create the instance ib using the following code
from ideaboard import IdeaBoard
ib = IdeaBoard()
Motor control
The two motors (motor_1 and motor_2) can be controlled using the throttle property.
The throttle values can range from full reverse (-1.0) to full speed ahead (1.0).
A throttle of 0 turns uses the brake. A throttle of None allows the motor to roll freely.
from ideaboard import IdeaBoard
ib = IdeaBoard()
# motor speed is from -1.0 (reverse) to 1.0 (forward)
# 0 is stopped with brake, None = roll freely
ib.motor_1.throttle = 1.0
ib.motor_2.throttle = -1.0
RGB LED (Neopixel)
The onboard RGB LED can be easily controlled using pixel or arcoiris.
ib.pixel
The ib.pixel property can be used to assign a RGB value to the LED. The values of each component are between 0 and 255.
The ib.brightness property of between (0.0 and 1.0) changes the LED brightness accordingly.
from ideaboard import IdeaBoard
ib = IdeaBoard()
# Set brightness from 0.0 to 1.0
ib.brightness = 0.2
# colors are RGB (Red, Green, Blue)
BLUE = (0,0,255)
GREEN = (0,255,0)
RED = (255,0,0)
PURPLE = (255,0,255)
YELLOW = (255,255,0)
ib.pixel = BLUE
ib.arcoiris
ib.arcoiris uses a value of between 0 and 255 to display different colors in a color wheel.
from ideaboard import IdeaBoard
from time import sleep
ib = IdeaBoard()
# ib.acroiris(0-255) creates a color wheel
# from 0 (RED) to 255 (RED)
while True:
for i in range(256):
ib.arcoiris = i
sleep(0.01)
Servo Motors
Several servo motors can be easily controlled using the IdeaBoard. The angle property changes the angle of the servo from 0 to 180 degrees.
import board
from ideaboard import IdeaBoard
from time import sleep
ib = IdeaBoard()
servo1 = ib.Servo(board.IO4)
while True:
# Set servo angle to 10 degrees
servo1.angle = 10
sleep(2)
# Set servo angle to 170 degrees
servo1.angle = 170
sleep(2)
Digital In
To read a digital input signal use the DigitalIn(pin, pullup mode) function. The first parameter is the physical pin name (ie. board.IO27). The second parameter, pull, sets the pullup resistor either ib.UP or ib.DOWN. The default is None. The property value contains the current state of the pin.
import board
import time
from ideaboard import IdeaBoard
ib = IdeaBoard()
#Digital In
# entrada = ib.DigitalIn(board.IO27, pull=ib.UP)
# pull can be ib.UP or ib.DOWN, default None)
entrada = ib.DigitalIn(board.IO27)
while True:
print(entrada.value)
time.sleep(0.5)
Digital Out
To create a digital output signal, use the DigitalOut(pin) function. The only parameter required is the pin name. (ie. board.IO27). Write to the output of the pin using the value property. The values may be any "truthy" or "falsey" value. (ie True or 1, False or 0)
import board
import time
from ideaboard import IdeaBoard
ib = IdeaBoard()
#Digital Out
salida = ib.DigitalOut(board.IO27)
while True:
salida.value = True
time.sleep(0.5)
salida.value = False
time.sleep(0.5)
Analog In
To read an analog input signal use the AnalogIn(pin) function. The only parameter required is the pin name. The property value contains the current value of the pin. The analog value is represented as a 16-bit number from 0 to 65535.
import board
import time
from ideaboard import IdeaBoard
ib = IdeaBoard()
#Analog In
entrada = ib.AnalogIn(board.IO33)
while True:
print(entrada.value)
time.sleep(0.5)
Analog Out
To create an analog output signal use the AnalogOut(pin) function. The only parameter required is the pin name. The property value sets the output level. The analog value is represented as a 16-bit number from 0 to 65535 and corresponds to the voltage 0-3.3V. Only two pins are available in the IdeaBoard Analog Out: board.IO25 and board.IO26
import time
import board
from ideaboard import IdeaBoard
ib = IdeaBoard()
# Note that there are only two pins on the IdeaBoard
# that support AnalogOut: board.IO25 and board.IO26
dac = ib.AnalogOut(board.IO26)
dac.value = 32768 # 1.60V on pin board.IO26
Map Range function
The map_range function is used to map a range of numbers to another range. The example maps a potentiometer with values between 0 and 65535 to the range of a servo (0 to 180).
import board
from ideaboard import IdeaBoard
from analogio import AnalogIn
ib = IdeaBoard()
pot = AnalogIn(board.IO33)
servo = ib.Servo(board.IO4)
while True:
val = pot.value
val = ib.map_range(val, 0, 65535, 0, 180)
servo.angle = val