Showing posts with label BME280. Show all posts
Showing posts with label BME280. Show all posts

Saturday, 3 April 2021

Pimoroni Enviro board

 The Pimoroni Enviro pHAT format board combines  a BME280 temperature, pressure and humidity sensor, LTR559 light and proximity sensor, a microphone and small colour LCD.

The Enviro board comes packed in a neatly printed anti-static bag (I was too excited to photograph it before opening it up)




Pimoroni have a Python library and a bunch of examples.


Assembly was easy, and a a pair of stand-offs secured the sandwich.

Here is an example program that displays the temperature, humidity, atmospheric pressure, lux level and the current time.

import ST7735
from PIL import Image, ImageDraw, ImageFont
from fonts.ttf import RobotoMedium as UserFont
from bme280 import BME280
import time
import datetime

import os

try:
    # Transitional fix for breaking change in LTR559
    from ltr559 import LTR559
    ltr559 = LTR559()
except ImportError:
    import ltr559

print("Starting")
bme280 = BME280()

display_angle = 270 # 90 for upside dow relative to screen print

# Set up the display
disp = ST7735.ST7735(
    port=0,
    cs=1,
    dc=9,
    backlight=12,
    rotation=display_angle,
    spi_speed_hz=10000000
)

# Initialize display.
disp.begin()

# Width and height to calculate text position.
WIDTH = disp.width
HEIGHT = disp.height

# Calculate text position
x = 5
y = 0

# Set up canvas and font
img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
path = os.path.dirname(os.path.realpath(__file__))
font_size = 20
font = ImageFont.truetype(UserFont, font_size)
small_font = ImageFont.truetype(UserFont, 16)
large_font = ImageFont.truetype(UserFont, 38)

text_colour = (255, 255, 255)
back_colour = (0, 50, 170)

def display_data():
    temperature = bme280.get_temperature()
    pressure = bme280.get_pressure()
    humidity = bme280.get_humidity()
    lux = ltr559.get_lux()
    prox = ltr559.get_proximity()
    draw.rectangle((0, 0, WIDTH, HEIGHT), back_colour)
    draw.text((x, y), "{:04.1f}C".format(temperature), font=font, fill=text_colour)
    draw.text((x, y+25), "{:04.1f}%".format(humidity), font=font, fill=text_colour)
    draw.text((x, y+50), "{:4.0f}hPa".format(pressure), font=font, fill=text_colour)
    # alternating seconds flash the colon separator
    if int(time.time()) % 2 == 0:
        draw.text((x+60,y), datetime.datetime.now().strftime("%H:%M"),
                  font=large_font,fill=text_colour)
    else:
        draw.text((x+60,y), datetime.datetime.now().strftime("%H %M"),
                  font=large_font,fill=text_colour)
    draw.text((x+100,y+55),"{:05.02f}lx".format(lux), font=small_font,fill=text_colour)
    disp.display(img)

# Main loop
while True:
    #print(bme280.get_temperature())
    display_data()
    time.sleep(1)
    







Saturday, 13 June 2020

Pimoroni Envirobit

Pimoroni Envirobit


The Pimoroni Envirobit is a set of sensors for the BBC Micro:Bit .


As you can see, it is equipped with a slot to take the Micro:Bit, so no soldering is required.

The Envirobit is fitted with the following sensors:

  • BME280 environmental sensor - which measures temperature, pressure, humidity and can calculate the altitude based on a supplied base pressure level (discuss).
  • tcs3472 RGB sensor - which measures Red Green and Blue light levels as well as “white” light levels. Also includes two illuminating (white) LEDs,
  • Sound - a small microphone allows the sound level to be measured on one of the Micro:Bit’s analogue pins

Assembly

Assembly is simple. Take the Envirobit board with the sensors facing forward, and insert the Micro:Bit with the LEDs also facing forward.
Due to the nature of the connection, you can swap the Microbits if the colour scheme does not match your needs.

Software

The main software support for the Envirobit is orientated towards the Microsoft MakeCode block based system.
There is some support for MicroPython. There is a GitHub link here: https://github.com/pimoroni/micropython-envirobit

There are three python files in the Library.
  • sound.py
  • bme280.py
  • tcs3472.py

The files can be transferred to your Micro:Bit using the Files function in Mu.

Sound

Contrary to the description on GitHub, this is not a class, just three methods.

  • sound.read() - This takes a reading of the sound level and returns a value between 0 and 440. There is an offset value in the code to set the minimum sensitivity.
  • sound.wait_for_double_clap() - listen for two high level sound events in a second, returns True if detected
  • sound.wait_for_clap() - listen for a single high sound level event in a second, returns True if detected

tcs3472

This uses a class to access the TCS3472 sensor via I2C.
To use the sensor, import the module (having transferred it to the Micro:Bit) and instantiate an instance.
import tcs3472
light_sensor = tcs3472.tcs3472() 
Methods:

  • r, g, b = light_sensor.rgb() - returns a tuple of the corrected levels of red, green and blue out of 255
  • r, g, b = light_sensor.scaled() - return a tuple of the amounts of red, green and blue on a scale of 0-1
  • level = light_sensor.light() - return a raw reading of light level on a scale of 0-65535
  • light_sensor.set_leds(0) - Turn the LEDs off
  • light_sensor.set_leds(1) - Turn the LEDs on

BME280

This uses a class to access the BME280 sensor via I2C.
The instructions on GitHub are incorrect, there is a missing () on the end of the class instantiation. Python can be very unforgiving if you make a mistake of this kind.
import bme280
bme = bme280.bme280()

The bme280 class has the following methods:

  • temp = bme.temperature() - return the temperature in degrees C
  • pressure = bme.pressure() - return the pressure in hectopascals
  • humidity = bme.humidity() - return the relative humidity in %
  • alt = bme.altitude() - return the altitude in feet, calculated against the current QNH value
  • bme.set_qnh(value) - set the QNH value for calculating altitude

QNH is the atmospheric pressure adjusted to sea level (what the pressure sensor should read at sea level).
https://en.wikipedia.org/wiki/QNH

References

https://github.com/pimoroni/micropython-envirobit
https://en.wikipedia.org/wiki/QNH