Skip to content

Instantly share code, notes, and snippets.

Last active January 4, 2023 23:52
What would you like to do?
BlinkMs working in CircuitPython
# -- very simply demonstrate low-level BlinkM control
# 4 Jan 2023 - @todbot / Tod Kurt
# Note: BlinkMs are designed to be 5V devices, they may not work on 3.3V systems.
# You should really use a I2C level shifter like:
# If you're powering many BlinkMs or a BlinkM MaxM, you *definitely* need a level shifter,
# since you cannot risk sending 5V back to the 3.3V inputs of a CircuitPython board.
# Details of BlinkM protocol at
import time
import board
import rainbowio
i2c = board.I2C()
# Lock the I2C device before we use it
while not i2c.try_lock():
# Print addresses found
device_addrs = i2c.scan()
print("I2C addresses found:", [hex(addr) for addr in device_addrs])
# Hopefully found one
if len(device_addrs) == 0:
print("no devices!?")
# Assume first addr is blinkm
blinkm_addr = device_addrs[0]
# Do standard BlinkM setup
print("stop built-in script playing")
i2c.writeto(blinkm_addr, bytearray([ord('o'), 0x0]) )
print("set fadespeed to our liking")
i2c.writeto(blinkm_addr, bytearray([ord('f'), 0x10]) )
print("setting blinkm to black")
i2c.writeto(blinkm_addr, bytearray([ord('c'), 0x00,0x00,0x00]) )
time.sleep(1) # wait a bit for dramatic effect
# Do a loop of happy disco colors
while True:
c = rainbowio.colorwheel( (time.monotonic()*10000) % 255 )
r,g,b = (c>>16 & 0xff), (c>>8 & 0xff), (c>>0 & 0xff) # convert 24-bit number to r,g,b
print("setting color to %02x%02x%02x" % (r,g,b))
i2c.writeto(blinkm_addr, bytearray([ord('c'), r,g,b ]) )
Copy link

todbot commented Jan 4, 2023

Small demo video of the above script


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment