m5stack_pbhub

CircuitPython driver for M5Stack PbHub

  • Author(s): Dario Cammi

Implementation Notes

Hardware:

Software and Dependencies:

# * Adafruit’s Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice

class m5stack_pbhub.NeoPixels(pbHub: PbHub, channel: int, number_of_leds: int, brightness: float = 0.5)

Drive an array of NeoPixels connect to the PbHub

Parameters:
  • pbHub – Instance of PbHub where the leds are connected

  • channel – Hub channel where the leds are connected

  • number_of_leds – Number of leds connected to the channel

  • brightness – Leds brightness

Here is an example of driving a strip with 10 leds connected on channel 0:

import time
import board
import m5stack_pbhub

i2c = board.I2C()
hub = m5stack_pbhub.PbHub(i2c)
strip = m5stack_pbhub.NeoPixels(hub, 0, 10)

strip[0] = 0xFF0000 # Set the color a single led
strip[3] = 0xFF00FF # Set the color a single led
time.sleep(1)
strip[1:3] = 0x00FFFF # Set the color of two leds
time.sleep(1)
strip.fill(0xFFFF00) # Set the color of the wall strip
time.sleep(1)
property brightness: float

Leds brightness. Range 0 to 1

A change in the brightness take effect only on the subsequent leds color write

property channel: int

Channel where the leds are connected

fill(color: int | Annotated[list[int], 3]) None

Fill all the leds with the same color

Parameters:

color – Color to apply at the leds

property number_of_leds: int

Number of leds connected to PbHub

class m5stack_pbhub.PbHub(i2c: I2C, addr: int = 0x61)

CircuitPython driver for M5Stack PbHub

Parameters:
  • i2c (I2C) – The I2C bus PbHub is connected to

  • device_address (int) – The I2C bus addres. Default to 0x61

Quickstart: Importing and using the hub

Here is an example of using the PbHub class. First you will need to import the libraries, get an I2C bus and create an hub instance

import board
import m5stack_pbhub

i2c = board.I2C()
hub = m5stack_pbhub.PbHub(i2c)

Now you can create an I/O on one of PbHub channel. For example let create a digital input

din = m5stack_pbhub.PbHubDigitalInput(hub, channel = 1, io = 1)
input_value = din.value
property firmware_version: int

Return the hub firmware version

class m5stack_pbhub.PbHubAnalogInput(pbHub: PbHub, channel: int)

PbHub analog input

Parameters:
  • pbHub (PbHub) – PhHub to use

  • channel (int) – Channel of the hub. Range 0 to 5

Analog inputs are always on IO pin 0. The resolution is 12 bit (0 - 4095)

Here is an example of reading an analog input

import board
import m5stack_pbhub

i2c = board.I2C()
hub = m5stack_pbhub.PbHub(i2c)
ain = m5stack_pbhub.PbHubAnalogInput(hub, channel = 0)
print(ain.value)
property value: int

Input value. Range 0 to 4095

class m5stack_pbhub.PbHubDigitalInput(pbHub: PbHub, channel: int, io: int)

PbHub digital input

Parameters:
  • pbHub (PbHub) – PhHub to use

  • channel (int) – Channel of the hub. Range 0 to 5

  • io (int) – Input pin of the channel. Range 0 to 1

Here is an example of reading a digital input

import board
import m5stack_pbhub

i2c = board.I2C()
hub = m5stack_pbhub.PbHub(i2c)
din = m5stack_pbhub.PbHubDigitalInput(hub, channel = 0, io = 0)
print(din.value)
property value: bool

Return the digital input value

class m5stack_pbhub.PbHubDigitalOutput(pbHub: PbHub, channel: int, io: int)

PbHub digital output

Parameters:
  • pbHub (PbHub) – PhHub to use

  • channel (int) – Channel of the hub. Range 0 to 5

  • io (int) – Input pin of the channel. Range 0 to 1

Here is an example of driving a digital output

import board
import m5stack_pbhub

i2c = board.I2C()
hub = m5stack_pbhub.PbHub(i2c)
dout = m5stack_pbhub.PbHubDigitalOutput(hub, channel = 0, io = 0)

dout.value = 1 # set the output
print(dout.value) # read the output value
property value: bool

Digital output value

class m5stack_pbhub.PbHubPwmOutput(pbHub: PbHub, channel: int, io: int)

PbHub PWM output

Parameters:
  • pbHub (PbHub) – PhHub to use

  • channel (int) – Channel of the hub. Range 0 to 5

  • io (int) – Input pin of the channel. Range 0 to 1

The output resolution is 8 bit (0 - 255)

Here is an example of driving PWM output

import board
import m5stack_pbhub

i2c = board.I2C()
hub = m5stack_pbhub.PbHub(i2c)
pwm = m5stack_pbhub.PbHubPwmOutput(hub, channel = 0, io = 0)
pwm.value = 127
property value: int

PWM output value

class m5stack_pbhub.PbHubServo(pbHub: PbHub, channel: int, io: int)

PbHub output to drive RC servo

Parameters:
  • pbHub (PbHub) – PhHub to use

  • channel (int) – Channel of the hub. Range 0 to 5

  • io (int) – Input pin of the channel. Range 0 to 1

Here is an example of driving an RC servo

import board
import m5stack_pbhub

i2c = board.I2C()
hub = m5stack_pbhub.PbHub(i2c)
servo = m5stack_pbhub.PbHubServo(hub, channel = 0, io = 0)
servo.angle = 45 # set servo angle in degrees
servo.pulse = 1500 # set servo angle as pulse with. 1500µs is equivalent to 90 degrees
property angle: int

RC servo angle in degree. Range 0 to 180

property pulse: int

RC servo angle in pulse width

Pulse width is expressed in microseconds and the frequency is 50 Hz Allowed range: 500 to 2500