Some Fun with CircuitPython
CircuitPython Day 2024 will be next Friday (Agoust 16), so here is a simple project! I am using a few modules I got in my drawers:
- An RP2040 Feather board. This is a nice RP2040 board with 8 MB of Flash and a battery charger (not used in this project).
- A BMP390 sensor. I chose this particular board because it has a STEMMA QT connector, just like the Feather board.
- A 128x64 pixels OLED display, with I2C interface.
- A 4 by 4 WS2812 RGB LED matrix.
This project aims to use the sensor to measure temperature, air pressure, and altitude, show this information in the OLED display and set the RGB LEDs to a color based on the temperature.
Hardware
The connections between the modules are:
- Pins +3.3V, GND and A0 are connected to the RGB LED matrix (Vcc, GND an IN, respectively).
- A STEMMA cable connects the Feather to the sensor.
- The OLED display is connected to the sensor board (to make wiring simpler). The pins used are Vin, GND, SCL and SDA.
Software Setup
The first step is to install the latest CircuitPython firmware on the Feather. Just download the uf2 file from https://circuitpython.org/board/adafruit_feather_rp2040/, connect the Feather to a PC, press boot and reset to put it in the bootloader and copy the uf2 file to the RPI-RP2 drive that will show up.
The board will reboot at the end of the copy and a CIRCUITPY drive will appear.
Now go to https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest and download the Adafruit CircuitPython Bundle for the CircuitPython version you installed (in my case, 9.x). Expand the zip in your PC and copy the following files/directories to CIRCUITPY/lib:
- adafruit_bus_device directory
- adafruit_register directory
- adafruit_bmp3xx.mpy file
- adafruit_ssd1306.mpy file
- neopixel.mpy file
Last, download https://github.com/adafruit/Adafruit_CircuitPython_framebuf/blob/main/examples/font5x8.bin and place it at the root of the CIRCUITPY drive.
Software
Here is the full application:
# BMP390 Sensor Example import board from busio import I2C from time import sleep import neopixel from adafruit_bmp3xx import BMP3XX_I2C import adafruit_ssd1306 np_onboard = neopixel.NeoPixel(board.NEOPIXEL, 1) np_onboard.fill((0, 0, 0)) np = neopixel.NeoPixel(board.A0, 16) np.fill((0, 0, 0)) temp_colors = ( (12, 28, 32), # < 0 light blue (0, 18, 32), # 0 to 10 blue (32, 29, 8), # 10 to 20 yellow (32, 13, 0), # 20 to 30 orange (32, 7, 2), # 30 to 40 light red (32, 0, 0) # > 50 dark red ) i2c = board.STEMMA_I2C() bmp = BMP3XX_I2C(i2c) oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c) oled.fill(0) oled.show() def color_temp(temp): temp = -9 if temp < -9 else 49 if temp > 49 else temp return temp_colors[int(temp+10) // 10] while True: # Read Sensor press = bmp.pressure temp = bmp.temperature alt = bmp.altitude print ('Temperature: {:.1f}C Pressure: {:.0f}Pa Altitude: {:.1f}m'.format( temp, press*100, alt)) # Show Temperature and Pressure on display oled.fill(0) oled.text('Temperature: {:.1f}C'.format(temp), 0, 0, 1) oled.text('Pressure: {:.0f}Pa'.format(press*100), 0, 10, 1) oled.text('Altitude: {:.1f}m'.format(alt), 0, 20, 1) oled.show() # Choose color based on temperature np.fill(color_temp(temp)) # Wait before updating sleep(2)
Comments
Post a Comment