add sensor examples

pull/536/head
helgibbons 2022-10-12 16:54:48 +01:00
rodzic db170a7b76
commit 7014bcb749
3 zmienionych plików z 175 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,56 @@
import plasma
from plasma import plasma2040
import breakout_scd41 as scd
from pimoroni_i2c import PimoroniI2C
"""
Reads CO2 level from an SCD41 breakout...
... and changes the LED strip an appropriate colour.
https://shop.pimoroni.com/products/scd41-co2-sensor-breakout
"""
# set how many LEDs you have
NUM_LEDS = 50
BRIGHTNESS = 1.0
# the range of readings to map to colours
# https://www.kane.co.uk/knowledge-centre/what-are-safe-levels-of-co-and-co2-in-rooms
MIN = 400
MAX = 2000
# pick what bits of the colour wheel to use (from 0-360°)
# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/
HUE_START = 100 # green
HUE_END = 0 # red
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
# Start updating the LED strip
led_strip.start()
# set up I2C
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
# set up SCD41 breakout
scd.init(i2c)
scd.start()
print("Waiting for SCD41 to be ready")
while True:
if scd.ready():
co2, temperature, humidity = scd.measure()
print(f"""
CO2: {co2} ppm
Temperature: {temperature:.2f} °C
Humidity: {humidity:.2f} %""")
# calculates a colour
hue = HUE_START + ((co2 - MIN) * (HUE_END - HUE_START) / (MAX - MIN))
# set the leds
for i in range(NUM_LEDS):
led_strip.set_hsv(i, hue / 360, 1.0, BRIGHTNESS)

Wyświetl plik

@ -0,0 +1,55 @@
import plasma
from plasma import plasma2040
import time
from breakout_bme280 import BreakoutBME280
from pimoroni_i2c import PimoroniI2C
"""
Reads the temperature from a BME280 breakout...
...and changes the LED strip an appropriate colour.
https://shop.pimoroni.com/products/bme280-breakout
"""
# Set how many LEDs you have
NUM_LEDS = 50
BRIGHTNESS = 1.0
# The range of readings that we want to map to colours
MIN = 10
MAX = 30
# pick what bits of the colour wheel to use (from 0-360°)
# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/
HUE_START = 230 # blue
HUE_END = 359 # red
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
# Start updating the LED strip
led_strip.start()
# set up I2C
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
# set up BME280 breakout
bme = BreakoutBME280(i2c)
while True:
temperature, pressure, humidity = bme.read()
print(f"""
Temperature: {temperature:.2f} °C
Humidity: {humidity:.2f} %
Pressure: {pressure / 100:.2f} hPa
""")
# calculates a colour
hue = HUE_START + ((temperature - MIN) * (HUE_END - HUE_START) / (MAX - MIN))
# set the leds
for i in range(NUM_LEDS):
led_strip.set_hsv(i, hue / 360, 1.0, BRIGHTNESS)
time.sleep(0.5)

Wyświetl plik

@ -0,0 +1,64 @@
import plasma
from plasma import plasma2040
import machine
import time
"""
Reads the internal temperature sensor on the Pico W...
... and changes the LED strip an appropriate colour.
"""
# Set how many LEDs you have
NUM_LEDS = 50
BRIGHTNESS = 1.0
# The range of readings that we want to map to colours
MIN = 10
MAX = 30
# pick what bits of the colour wheel to use (from 0-360°)
# https://www.cssscript.com/demo/hsv-hsl-color-wheel-picker-reinvented/
HUE_START = 230 # blue
HUE_END = 359 # red
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
# Start updating the LED strip
led_strip.start()
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535) # used for calculating a temperature from the raw sensor reading
# The Pico's temperature sensor is not super accurate and readings jump around
# lets do some averaging to avoid annoying flashing
n = 0
temperature = 20 # a dummy value to fill the array
temperature_array = [temperature] * 10 # average over 10 readings (5 secs)
while True:
# read the sensor
# the following two lines do some maths to convert the number from the temp sensor into celsius
reading = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706) / 0.001721
# add the most recent reading to the array
if n >= len(temperature_array):
n = 0
temperature_array[n] = temperature
n += 1
temperature_average = sum(temperature_array)/len(temperature_array)
print(f"""
Average temperature: {temperature_average:.2f} °C
""")
# calculates a colour
hue = HUE_START + ((temperature_average - MIN) * (HUE_END - HUE_START) / (MAX - MIN))
# set the leds
for i in range(NUM_LEDS):
led_strip.set_hsv(i, hue / 360, 1.0, BRIGHTNESS)
time.sleep(0.5)