Skip to content

Instantly share code, notes, and snippets.

@stonehippo
stonehippo / micropython_tools.md
Created August 27, 2024 16:39
Tools for working withMicropython

ESP8266 and Micropython Tools

I use Micropython sometimes. It's a great port of Python to microcontrollers, and the basis of CircuitPython. Unlike CircuitPython, MicroPython doesn't typically provide a mounted drive via native USB, which means that you need some tooling to best interact with the board. For example, I am using MP on some ESP8266 dev boards, because CP support there isn't great (no UF2 bootloader, no native USB).

  • esptool.py - Needed for flashing the board
  • mpremote - a handy toolkit for working with MP boards, including console access, package management, and file management
  • ampy - a handy tool for copying files to and from a MicroPython board. This is somewhat redundant if you use mpremote
@stonehippo
stonehippo / display.py
Created August 14, 2024 14:10
Code from my little CircuitPython display
import board
import time
import adafruit_bme680
import adafruit_veml7700
import neopixel
from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.color import RED
from ulab import numpy as np
i2c = board.I2C()
@stonehippo
stonehippo / using_circuitpython_native_used_for_serial_data.md
Last active May 12, 2025 02:59
A note to self about using the CircuitPython native USB for serial data with usb_cdc

Using the CircuitPython native USB for serial data

I was trying to get some CP code to talk to Processing over a serial port interface. Of course, the CP REPL is normally available on whatever serial port the board mounts by default. This is a little complicated, because although you can use something like print from the REPL (or code.py) to print a value to the console, sys.stdin and sys.stdout are text-based streams, which may not be what you want. And you'll get REPL noise to boot. Not ideal.

This is where usb_cdc comes in. It's a handy library for managing the USB CDC (serial) on most CircuitPython boards. First, usb_cdc can be used to control how many serial ports CP will provide at startup. You can modify boot.py to make this work:

import usb_cdc

usb_cdc.enable(console=True, data=False)
@stonehippo
stonehippo / RGB_strip_control.py
Created November 16, 2023 15:30
Some CircuitPython to control an RGBW LED strip.
"""
Simple CircuitPython control of an RGB strip via Mosfetti or plain MOSFETS.
Copyright (c) 2023 George White <[email protected]>
Written for the Adafruit Metro M0, but will work with any board with
4 PWM-capable pins.
"""
import board
from pwmio import PWMOut
@stonehippo
stonehippo / update_circuitpy.sh
Created September 4, 2023 20:08
shell script to update CircuitPython build files
cd ~/circuitpython
git pull
git submodule sync
git submodule update --init
cd $OLDPWD

Hallowing M4: Enable audioio & touchio

The Adafruit Hallowing M4 is a feature-filled development board, including four capacitve touch "teeth" at the bottom of it's skull-shaped circuit board.

CircuitPython has a nifty module for working cap touch pins, touchio. However, the Hallowing M4 uses an Atmel SAMD51, which doesn't handle cap touch in hardware (unlike the SAMD21 in the Hallowing M0). If you try to use the cap touch pins without some additional setup, you'll get an error.

import board
import touchio
@stonehippo
stonehippo / micropython_motobit.md
Last active November 28, 2024 19:31
A class for the SparkFun moto:bit for use with BBC micro:bit micro python

SparkFun moto:bit driver for BBC micro:bit micropython

The SparkFun moto:bit is a motor controller carrier for the BBC micro:bit. This board is also available as part of the SparkFun micro:bot kit.

SparkFun provides a MakeCode extension for using this board, but no driver for working with the board using micro:bit-flavored micropython. Given the spiffy new micro:bit Python Editor, it would nice to be able to program this motor controller in micropython. This class makes that easy to do.

The code has been moved to a new repo: https://github.com/stonehippo/micropython_motobit.

Usage

@stonehippo
stonehippo / README.md
Last active December 29, 2023 00:49
Making a little dice roller in modern Python

A tiny dice roller in modern Python

I have been reading Fluent Python, and I can tell that it's going to help me be a better Python coder right away.

A while ago, I started a Javascript library for rolling common dice types, which I intended to use for various games. While reading Fluent Python, I realized that I could write the same logic in a much more compact form, thanks to listcomps, generator functions, and unpacking. In fact, the core implementation takes only four lines of code:

sides = [4, 6, 8, 10, 12, 20]
d4, d6, d8, d10, d12, d20 = [range(1, count + 1) for count in sides]
@stonehippo
stonehippo / knock_knock.ino
Last active November 27, 2024 23:06
A simple Arduino piezo knock sensor that cycles through a series of RGB LED patterns
// define pins for the RGB LED
byte led_red_pin = 3;
byte led_green_pin = 5;
byte led_blue_pin = 6;
int piezo_pin = A0;
int piezo_threshold = 250; // This may have to be tuned for your piezo
int wait = 10000; // the time before the last night and the cycle resets
int debounce = 200; // the number of milliseconds we'll wait to reject bad input
@stonehippo
stonehippo / ssd1306_I2C_setup.py
Last active January 7, 2025 22:50
Basic setup of an SSD1306-based I2C display using CircuitPython Display
import board
import displayio
import terminalio
import adafruit_displayio_ssd1306
from adafruit_display_text import label
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
# release any displays that have already been set up
displayio.release_displays()