BlinkMs working in CircuitPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# blinkm_simple_demo.py -- 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: https://www.adafruit.com/product/5637 | |
# 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 http://thingm.com/s/BlinkM_datasheet.pdf | |
# | |
import time | |
import board | |
import rainbowio | |
i2c = board.I2C() | |
# Lock the I2C device before we use it | |
while not i2c.try_lock(): | |
pass | |
# 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 ]) ) | |
time.sleep(0.2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small demo video of the above script
blinkm_circuitpython.mp4