|
| 1 | +# SPDX-FileCopyrightText: 2023 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Pi Pico & Picowbell with SSD1306 display & BME280 sensor |
| 4 | +# Coded for Circuit Python 8.1 |
| 5 | + |
| 6 | +import time |
| 7 | +import board |
| 8 | +import displayio |
| 9 | +import busio |
| 10 | +import terminalio |
| 11 | +from adafruit_display_text import label |
| 12 | +from adafruit_bme280 import basic as adafruit_bme280 |
| 13 | +import adafruit_displayio_ssd1306 as ssd1306 |
| 14 | + |
| 15 | +# Reinitalizes display upon any soft reboot or hard reset |
| 16 | +displayio.release_displays() |
| 17 | + |
| 18 | +# Pi Pico RP2040 I2C0 bus initialization (SSD1306 display) |
| 19 | +i2c0 = busio.I2C(board.GP3, board.GP2) |
| 20 | +# Pi Pico RP2040 I2C1 bus initialization (temp sensor from Stemma port) |
| 21 | +i2c1 = busio.I2C(board.GP5, board.GP4) |
| 22 | +# i2c = board.I2C() # other boards use board.SCL and board.SDA |
| 23 | + |
| 24 | +# Initialize BME280 sensor |
| 25 | +bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c1) |
| 26 | + |
| 27 | +# Configure display size |
| 28 | +ssd_width = 128 |
| 29 | +ssd_height = 32 |
| 30 | + |
| 31 | +# Ensure the physical address of your SSD1306 is set here: |
| 32 | +ssd_bus = displayio.I2CDisplay(i2c0, device_address=0x3C) |
| 33 | +display = ssd1306.SSD1306(ssd_bus, width=ssd_width, height=ssd_height) |
| 34 | + |
| 35 | +# Manually set your sea_level_pressure to your area |
| 36 | +# This can change houly, lookup NOAA data or automate it. |
| 37 | +# On a normal sunny day it's generally 1013-1015 |
| 38 | +bme280.sea_level_pressure = 1013.4 |
| 39 | +# If you live at 0 sea level this is a nice workaround |
| 40 | +# bme280.sea_level_pressure = bme280.pressure |
| 41 | + |
| 42 | +# Quick Colors for Labels |
| 43 | +TEXT_BLACK = 0x000000 |
| 44 | +TEXT_BLUE = 0x0000FF |
| 45 | +TEXT_CYAN = 0x00FFFF |
| 46 | +TEXT_GRAY = 0x8B8B8B |
| 47 | +TEXT_GREEN = 0x00FF00 |
| 48 | +TEXT_LIGHTBLUE = 0x90C7FF |
| 49 | +TEXT_MAGENTA = 0xFF00FF |
| 50 | +TEXT_ORANGE = 0xFFA500 |
| 51 | +TEXT_PURPLE = 0x800080 |
| 52 | +TEXT_RED = 0xFF0000 |
| 53 | +TEXT_WHITE = 0xFFFFFF |
| 54 | +TEXT_YELLOW = 0xFFFF00 |
| 55 | + |
| 56 | +# Text labels for display |
| 57 | +hello_label = label.Label(terminalio.FONT) |
| 58 | +hello_label.anchor_point = (0.0, 0.0) |
| 59 | +hello_label.anchored_position = (0, 0) |
| 60 | +hello_label.scale = 1 |
| 61 | +hello_label.color = TEXT_WHITE |
| 62 | + |
| 63 | +temp_label = label.Label(terminalio.FONT) |
| 64 | +temp_label.anchor_point = (0.0, 0.0) |
| 65 | +temp_label.anchored_position = (0, 10) |
| 66 | +temp_label.scale = 1 |
| 67 | +temp_label.color = TEXT_WHITE |
| 68 | + |
| 69 | +humidity_label = label.Label(terminalio.FONT) |
| 70 | +humidity_label.anchor_point = (0.0, 0.0) |
| 71 | +humidity_label.anchored_position = (0, 20) |
| 72 | +humidity_label.scale = 1 |
| 73 | +humidity_label.color = TEXT_WHITE |
| 74 | + |
| 75 | +pressure_label = label.Label(terminalio.FONT) |
| 76 | +pressure_label.anchor_point = (1.0, 1.0) |
| 77 | +pressure_label.anchored_position = (ssd_width, 10) |
| 78 | +pressure_label.scale = 1 |
| 79 | +pressure_label.color = TEXT_WHITE |
| 80 | + |
| 81 | +# Create DisplayIO Group Layer |
| 82 | +layer1 = displayio.Group() |
| 83 | +layer1.append(hello_label) |
| 84 | +layer1.append(temp_label) |
| 85 | +layer1.append(humidity_label) |
| 86 | +layer1.append(pressure_label) |
| 87 | +display.show(layer1) |
| 88 | + |
| 89 | +while True: |
| 90 | + # Convert temp C to F |
| 91 | + temperature = f"{bme280.temperature * 1.8 + 32:.1f}" |
| 92 | + |
| 93 | + # Displays labels on the SSD1306 display |
| 94 | + hello_label.text = "Pico W SSD1306" |
| 95 | + temp_label.text = temperature |
| 96 | + humidity_label.text = f"{bme280.relative_humidity:.0f}" |
| 97 | + pressure_label.text = f"{bme280.pressure:.0f}" |
| 98 | + |
| 99 | + # Prints Serial Data to REPL console (good for debugging) |
| 100 | + print(f"\nTemperature: {temperature} F") |
| 101 | + print("Humidity: %0.1f %%" % bme280.relative_humidity) |
| 102 | + print("Pressure: %0.1f hPa" % bme280.pressure) |
| 103 | + print("Altitude = %0.2f meters" % bme280.altitude) |
| 104 | + time.sleep(2) |
0 commit comments