BME 280 with LED’s and a Push Button

These days if you wanted to know the temperature outside, or even the humidity levels you would probably just look it up on your phone or better still ask Alexa or Google about it. Here’s a project to challenge the maker in you. Instead of asking Google or Alexa the weather conditions how about building your own simple home weather monitoring project using a Raspberry Pi Pico and a BME 280 programmed with Micropython.

This tutorial is slightly different from the previous BME 280 tutorial. In this tutorial we will be using the PiStop PiMoroni module to light up some LED’s, include a push button which will be used to trigger reading of the value from the BME 280. If you do not have the PiStop PiMoroni module lying around you could consider using 3 LED’s and 3 x 10K Ohm resistors.

PiStop by PiMoroni : The PiStop module from PiMoroni is a simple traffic light module consisting of three LED’S i.e. Green, Amber, Red along with resistors on board to allow you directly interface with any maker board. In our case we chose to use PiStop from PiMoroni along with the Raspberry Pi Pico. The PiStop includes resistors in line with each of the LED’s along with a separate Vcc (power) and GND (ground) line, thus making it really easy for anyone to get started with their traffic light project. The entire package makes it very easy to work with the LED’s and allows embedding the PiStop into various maker projects where a visual traffic light setup might be required.

You can purchase the PiStop from PiMoroni from the following link – PiStop at PiMoroni

PiStop by PiMoroni (Image : courtesy Pi Moroni)

Raspberry Pi Pico – Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040, the flagship microcontroller chip designed by Raspberry Pi in the UK. From light displays and IoT devices to signage and manufacturing processes, Raspberry Pi Pico gives the maker the power to control countless home, hobby, and industrial projects. Programmable in C and MicroPython, the Pi Pico is adaptable to a vast range of applications and skill levels, and getting started is as easy as dragging and dropping a file. More experienced users can take advantage of Raspberry Pi Pico’s rich peripheral set, including SPI, I2C, and eight Programmable I/O (PIO) state machines for custom peripheral support.

Raspberry Pi Pico

Here’s the specifications (from Raspberry Pi Foundation website) of the Raspberry Pi Pico in case you are interested –

  1. 21 mm × 51 mm form factor
  2. RP2040 microcontroller chip designed by Raspberry Pi in the UK
  3. Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133 MHz
  4. 264kB on-chip SRAM
  5. 2MB on-board QSPI flash
  6. 2.4GHz 802.11n wireless LAN (Raspberry Pi Pico W and WH only)
  7. 26 multifunction GPIO pins, including 3 analogue inputs
  8. 2 × UART, 2 × SPI controllers, 2 × I2C controllers, 16 × PWM channels
  9. 1 × USB 1.1 controller and PHY, with host and device support
  10. 8 × Programmable I/O (PIO) state machines for custom peripheral support
  11. Supported input power 1.8–5.5V DC
  12. Operating temperature -20°C to +85°C (Raspberry Pi Pico and Pico H); -20°C to +70°C (Raspberry Pi Pico W and Pico WH)
  13. Castellated module allows soldering direct to carrier boards (Raspberry Pi Pico and Pico W only)
  14. Drag-and-drop programming using mass storage over USB
  15. Low-power sleep and dormant modes
  16. Accurate on-chip clock
  17. Temperature sensor
  18. Accelerated integer and floating-point libraries on-chip

Pinout diagram for the Raspberry Pi Pico – To help you work out which pins are located where and what they are called you can refer to the pinout diagram for the Raspberry Pi Pico provided below.

Raspberry Pi Pico Pinout Diagram

BME 280 sensor – The BME280 is a humidity sensor especially developed for mobile applications and wearables where size and low power consumption are key design parameters. The unit combines high linearity and high accuracy sensors and is perfectly feasible for low current consumption, long-term stability and high EMC robustness. The humidity sensor offers an extremely fast response time and therefore supports performance requirements for emerging applications such as context awareness, and high accuracy over a wide temperature range. Operational range for the BME280 is :

  1. Pressure: 300…1100 hPa
  2. Temperature: -40…85°C
BME 280 Temperature, Pressure, Humidity sensor

Bill Of Material – Here are the components you will need to build your home weather station –

  1. Raspberry Pi Pico microcontroller x 1
  2. BME280 (not a BMP280) x 1
  3. Dupont male to male wires x 4
  4. USB micro cable x 1
  5. 800 point Breadboard x 1
  6. 1 x Pi Stop PiMoroni board or 3 LED’s with 3 10K resistors
  7. 1 x Push button switch

Circuit diagram – Follow the circuit diagram provided below and assemble the circuit before you proceed with programming the Pi Pico to fetch temperature, pressure and humidity from the BME 280.

BME 280 with the Pi Pico

The above diagram was put together in Fritzing, not being able to access a PiStop module within Fritzing we have displayed the PiStop as three LED’s, each connected to a 10K Ohm resistors (in line with each LED). The connections for this tutorial are to be made as follows –

  1. PiStop Red LED connected to GPIO 18
  2. PiStop Amber LED connected to GPIO 17
  3. PiStop Green LED connected to GPIO 16
  4. PiStop Vcc connected to Vcc or 3V3 OUT
  5. PiStop GND (Ground) connected to GND
  6. BME 280 Vcc connected to Vcc or 3V3 OUT
  7. BME 280 GND (Ground) connected to GND
  8. BME 280 SCL connected to GPIO 1
  9. BME 280 SDA connected to GPIO 0
  10. 1 end of the push button connected to 3V3
  11. other end of the push button connected to GPIO 14

If you are using the PiStop PiMoroni module you do not have to use the resistors we’ve shown above. These PiStop module includes SMD resistors on the module. If you do not have the PiStop module you can just use 3 x 10K Ohm resistors instead.

Code for the tutorial – Open up your favourite Micropython editor and type in the following code. There are a number of editors out there for you to choose from. The two most commonly used editors for Micropython are Thonny and Mu. We tend to use Thonny a lot in class with our kids.

from machine import Pin, I2C    #importing relevant modules & classes
import time
import bme280
# Setting up the pin which we'll use to blink the onboard LED:
led_green = Pin(16, Pin.OUT)
led_amber = Pin(17, Pin.OUT)
led_red = Pin(18, Pin.OUT)
# Setting up the button with a Pull_Down resistor
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
# Setting up I2C pin for use with the BME280
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) 
while True:
    if button.value() == 1:
        
        bme = bme280.BME280(i2c=i2c)
        temp = bme.values[0]
        pressure = bme.values[1]
        humidity = bme.values[2]
        print("Temp is ",temp," || Pressure is ",pressure," || Humidity is ",humidity)
        #print(bme.values)
        
        # Blinking the Green LED
        led_green.on()
        time.sleep(.5)
        led_green.off()
    
        # Blinking the Amber LED
        led_amber.on()
        time.sleep(.5)
        led_amber.off()
    
        # Blinking the Red LED
        led_red.on()
        time.sleep(.5)
        led_red.off()
    
        # Sleeping for 3s
        time.sleep(2)  
    else:
        # Turning all the LED's off
        led_green.off()
        led_red.off()
        led_amber.off()

Summary – In this tutorial we used Micropython to program the Raspberry Pi Pico to grab temperature, pressure and humidity from the BME 280. The code is designed to run an infinite loop and grab data from the sensor every time the switch is pressed and light up the LED’s after reading the sensor.

We hope you enjoyed the tutorial. Drop us a note at trevor at hack2 dot live if you have any questions, comments or suggestions.

Leave a Reply

Your email address will not be published.