Introduction

Documentation Status Gitter

SimpleIO features a number of helpers to simplify hardware interactions. Many of the functions and classes are inspired by Arduino APIs to make it easier to move to CircuitPython from Arduino.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Usage Example

TODO

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

API Reference

simpleio - Simple, beginner friendly IO.

The simpleio module contains classes to provide simple access to IO.

class simpleio.DigitalIn(pin)[source]

Simple digital input that is valid until soft reset.

value

The digital logic level of the input pin.

class simpleio.DigitalOut(pin)[source]

Simple digital output that is valid until soft reset.

value

The digital logic level of the output pin.

class simpleio.Servo(pin, min_pulse=0.5, max_pulse=2.5)[source]

Easy control for hobby (3-wire) servos

Parameters:
  • pin (Pin) – PWM pin where the servo is located.
  • min_pulse (int) – Pulse width (microseconds) corresponding to 0 degrees.
  • max_pulse (int) – Pulse width (microseconds) corresponding to 180 degrees.

Example for Metro M0 Express:

import simpleio
import time
from board import *

pwm = simpleio.Servo(D9)

while True:
    pwm.angle = 0
    print("Angle: ", pwm.angle)
    time.sleep(2)
    pwm.angle = pwm.microseconds_to_angle(2500)
    print("Angle: ", pwm.angle)
    time.sleep(2)
angle

Get and set the servo angle in degrees

deinit()[source]

Detaches servo object from pin, frees pin

microseconds_to_angle(us)[source]

Converts microseconds to a degree value

simpleio.bitWrite(x, n, b)[source]

Based on the Arduino bitWrite function, changes a specific bit of a value to 0 or 1. The return value is the original value with the changed bit. This function is written for use with 8-bit shift registers

Parameters:
  • x – numeric value
  • n – position to change starting with least-significant (right-most) bit as 0
  • b – value to write (0 or 1)
simpleio.map_range(x, in_min, in_max, out_min, out_max)[source]

Maps a number from one range to another. Note: This implementation handles values < in_min differently than arduino’s map function does.

Returns:Returns value mapped to new range
Return type:float
simpleio.shift_in(data_pin, clock, msb_first=True)[source]

Shifts in a byte of data one bit at a time. Starts from either the LSB or MSB.

Warning

Data and clock are swapped compared to other CircuitPython libraries in order to match Arduino.

Parameters:
  • data_pin (DigitalInOut) – pin on which to input each bit
  • clock (DigitalInOut) – toggles to signal data_pin reads
  • msb_first (bool) – True when the first bit is most significant
Returns:

returns the value read

Return type:

int

simpleio.shift_out(data_pin, clock, value, msb_first=True)[source]

Shifts out a byte of data one bit at a time. Data gets written to a data pin. Then, the clock pulses hi then low

Warning

Data and clock are swapped compared to other CircuitPython libraries in order to match Arduino.

Parameters:
  • data_pin (DigitalInOut) – value bits get output on this pin
  • clock (DigitalInOut) – toggled once the data pin is set
  • msb_first (bool) – True when the first bit is most significant
  • value (int) – byte to be shifted

Example for Metro M0 Express:

import digitalio
import simpleio
from board import *
clock = digitalio.DigitalInOut(D12)
data_pin = digitalio.DigitalInOut(D11)
latchPin = digitalio.DigitalInOut(D10)
clock.direction = digitalio.Direction.OUTPUT
data_pin.direction = digitalio.Direction.OUTPUT
latchPin.direction = digitalio.Direction.OUTPUT

while True:
    valueSend = 500
    # shifting out least significant bits
    # must toggle latchPin.value before and after shift_out to push to IC chip
    # this sample code was tested using
    latchPin.value = False
    simpleio.shift_out(data_pin, clock, (valueSend>>8), msb_first = False)
    latchPin.value = True
    time.sleep(1.0)
    latchPin.value = False
    simpleio.shift_out(data_pin, clock, valueSend, msb_first = False)
    latchPin.value = True
    time.sleep(1.0)

    # shifting out most significant bits
    latchPin.value = False
    simpleio.shift_out(data_pin, clock, (valueSend>>8))
    latchPin.value = True
    time.sleep(1.0)
    latchpin.value = False
    simpleio.shift_out(data_pin, clock, valueSend)
    latchpin.value = True
    time.sleep(1.0)
simpleio.tone(pin, frequency, duration=1, length=100)[source]

Generates a square wave of the specified frequency on a pin

Parameters:
  • Pin (Pin) – Pin on which to output the tone
  • frequency (float) – Frequency of tone in Hz
  • length (int) – Variable size buffer (optional)
  • duration (int) – Duration of tone in seconds (optional)