I’ve been using Pimoroni’s 1.12" white/black i2c OLED display (128x128 pixels) on a Pico. I haven’t used one in Python on a Pi though.
This a file I’m running on a weather station build. It’s Micro Python, not Python. The Oled is just there as a quick hookup to check my sensor data.
import time, math, os
import network
import pimoroni_bus
import veml6075
import breakout_icp10125
import breakout_bh1745
from machine import ADC, Pin, PWM, RTC
from pimoroni import Analog
from pimoroni_i2c import PimoroniI2C
from breakout_ltr559 import BreakoutLTR559
from breakout_bme280 import BreakoutBME280
from breakout_rtc import BreakoutRTC
i2cbus = PimoroniI2C(4, 5)
bme = BreakoutBME280(i2cbus)
ltr = BreakoutLTR559(i2cbus)
bh1745 = breakout_bh1745.BreakoutBH1745(i2cbus)
uv = veml6075.VEML6075(i2cbus)
connected = uv.initUV()
icp10125 = breakout_icp10125.BreakoutICP10125(i2cbus)
bh1745.leds(False)
from picographics import PicoGraphics, DISPLAY_I2C_OLED_128X128
display = PicoGraphics(display=DISPLAY_I2C_OLED_128X128, bus=i2cbus)
display.clear()
display.set_font("bitmap8")
display.set_pen(15) # White
rtc = BreakoutRTC(i2cbus)
if rtc.is_12_hour:
rtc.set_24_hour()
start_time = time.time()
temp, press, hum = bme.read()
time.sleep(0.3)
temp, press, hum = bme.read()
time.sleep(0.3)
temp, press, hum = bme.read()
time.sleep(0.3)
if rtc.read_periodic_update_interrupt_flag():
rtc.clear_periodic_update_interrupt_flag()
if rtc.update_time():
rtc_date = rtc.string_date()
rtc_time = rtc.string_time()
RAIN_MM_PER_TICK = 0.2794
WIND_CM_RADIUS = 7.0
WIND_FACTOR = 0.0218
wind_direction_pin = Analog(26)
wind_speed_pin = Pin(9, Pin.IN, Pin.PULL_UP)
rain_pin = Pin(10, Pin.IN, Pin.PULL_DOWN)
last_rain_trigger = False
def wind_speed(sample_time_ms=1000):
# get initial sensor state
state = wind_speed_pin.value()
# create an array for each sensor to log the times when the sensor state changed
# then we can use those values to calculate an average tick time for each sensor
ticks = []
start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) <= sample_time_ms:
now = wind_speed_pin.value()
if now != state: # sensor output changed
# record the time of the change and update the state
ticks.append(time.ticks_ms())
state = now
# if no sensor connected then we have no readings, skip
if len(ticks) < 2:
return 0
# calculate the average tick between transitions in ms
average_tick_ms = (time.ticks_diff(ticks[-1], ticks[0])) / (len(ticks) - 1)
if average_tick_ms == 0:
return 0
# work out rotation speed in hz (two ticks per rotation)
rotation_hz = (1000 / average_tick_ms) / 2
# calculate the wind speed in metres per second
circumference = WIND_CM_RADIUS * 2.0 * math.pi
wind_m_s = rotation_hz * circumference * WIND_FACTOR
return wind_m_s
def wind_direction():
# adc reading voltage to cardinal direction taken from our python
# library - each array index represents a 45 degree step around
# the compass (index 0 == 0, 1 == 45, 2 == 90, etc.)
# we find the closest matching value in the array and use the index
# to determine the heading
ADC_TO_DEGREES = (0.9, 2.0, 3.0, 2.8, 2.5, 1.5, 0.3, 0.6)
closest_index = -1
last_index = None
# ensure we have two readings that match in a row as otherwise if
# you read during transition between two values it can glitch
# fixes https://github.com/pimoroni/enviro/issues/20
def get_vsys():
conversion_factor = 3 * 3.3 / 65535
wlan = network.WLAN(network.STA_IF)
wlan_active = wlan.active()
try:
wlan.active(False)
Pin(25, mode=Pin.OUT, pull=Pin.PULL_DOWN).high()
Pin(29, Pin.IN)
vsys = ADC(29)
return vsys.read_u16() * conversion_factor
finally:
Pin(29, Pin.ALT, pull=Pin.PULL_DOWN, alt=7)
wlan.active(wlan_active)
power = Pin('WL_GPIO2', Pin.IN)
charging = Pin(22, mode=Pin.IN, pull=Pin.PULL_UP)
full_battery = 4.2
empty_battery = 2.8
while True:
display.set_pen(0)
display.clear()
display.set_pen(15)
time_elapsed = time.time() - start_time
hour = rtc.get_hours()
minute = rtc.get_minutes()
month = rtc.get_month()
date = rtc.get_date()
if rtc.read_periodic_update_interrupt_flag():
rtc.clear_periodic_update_interrupt_flag()
if rtc.update_time():
rtc_date = rtc.string_date()
rtc_time = rtc.string_time()
#print("Date: ", rtc_date, ", Time: ", rtc_time, sep="")
display.text(rtc_date, 0, 0, scale=2)
#display.text(rtc_time, 30, 18, 1)
if hour == 0:
display.text(f"{12}:{minute:02}:AM", 0, 18, scale=2)
elif 0 < hour < 10:
display.text(f"{hour:1}:{minute:02}:AM", 0, 18, scale=2)
elif 10 <= hour < 12:
display.text(f"{hour:2}:{minute:02}:AM", 0, 18, scale=2)
elif hour == 12:
display.text(f"{hour:2}:{minute:02}:PM", 0, 18, scale=2)
elif hour > 12:
hour = hour - 12
if hour <10:
display.text(f"{hour:1}:{minute:02}:PM", 0, 18, scale=2)
elif 10 <= hour < 12:
display.text(f"{hour:2}:{minute:02}:PM", 0, 18, scale =2)
elif hour == 12:
display.text(f"{hour:2}:{minute:02}:AM", 0, 18, scale=2)
temp, press, hum = bme.read()
reading = bme.read()
pressmb = press / 100
#print('Temperature {:05.2f}*C'.format(temp))
#print('Humididty {:05.2f}%'.format(hum))
#print('Pressure {:05.2f}hPa'.format(pressmb))
t, p, status = icp10125.measure(icp10125.NORMAL)
if status == icp10125.STATUS_OK:
icptemp = t
icppress = p / 100
#print('ICP Pressure {:05.2f}mb'.format(icppress))
display.text("{:.0f}C".format(icptemp), 0, 36, scale=2)
display.text("{:.0f}%".format(hum), 50, 36, scale=2)
display.text("{:.0f}mb".format(icppress), 0, 54, scale=2)
rgbl_raw = bh1745.rgbc_raw()
rgbl_clamped = bh1745.rgbc_clamped()
rgb_scaled = bh1745.rgbc_scaled()
print("Raw: {}, {}, {}, {}".format(*rgbl_raw))
print("Clamped: {}, {}, {}, {}".format(*rgbl_clamped))
print("Scaled: #{:02x}{:02x}{:02x}".format(*rgb_scaled))
'''
display.set_pen(BLACK)
display.clear()
display.set_pen(WHITE)
display.text("Raw: R, G, B, L", 0, 20, scale = 3)
display.text("{}, {}, {}, {}".format(*rgbl_raw), 0, 50, scale = 3)
display.text("Clamped: R, G, B, L", 0, 110, scale = 3)
display.text("{}, {}, {}, {}".format(*rgbl_clamped), 0, 140, scale = 3)
display.text("Scaled: {:02x} {:02x} {:02x}".format(*rgb_scaled), 0, 210, scale = 3)
display.update()
'''
#display.text("Lux {:.0f}".format(reading[BreakoutLTR559.LUX]), 0, 90, scale=2)
UVI, UVIA, UVIB = uv.readUV()
print('UV {:05.1f} '.format(UVI))
print('UVA {:05.1f}'.format(UVIA))
print('UVB {:05.1f}'.format(UVIB))
display.text("UV {:.0f}".format(UVI), 0, 72, scale=2)
percentage = 100 * ((get_vsys() - empty_battery) / (full_battery - empty_battery))
if percentage > 100:
percentage = 100.00
if power.value() == True:
if charging.value() == 0:
#print ("Charging!")
display.text("Charging!", 0, 108, scale=2)
else:
#print ("USB Powered")
display.text("USB Powered", 0, 108, scale=2)
else:
#print('{:.2f}V'.format(get_vsys()))
#print('{:.0f}%'.format(percentage))
display.text("{:.1f}V".format(get_vsys()), 0, 108, scale=2)
display.text("{:.0f}%".format(percentage), 50, 108, scale=2)
display.update()
time.sleep(1)