Skip to main content

The Raspberry Pi Weather Station – In Ten Minutes Flat | DesignSpark

The weather is a hot topic again in the UK, where we are experiencing something of a heatwave. This is generally referred to as ‘summer’ elsewhere in the world but heat in summer is something of an outlandish concept to the people of this island!

Parts list

Qty Product Part number
1 Raspberry Pi 3 Model B+ 1373331
1 Raspberry Pi Bridge 1743694
1 XinaBox MD01 for use with Blank Placeholder 1743711
1 AVX BTB Series, Male Edge Connector For use with Xinabox XChips, SMT, 10 Way, 2 Row, 2mm Pitch, 2.5A 1744977
1 XinaBox SW01, Advanced Weather Sensor Module for BME280 1743744


The weather is a hot topic again in the UK, where we are experiencing something of a heatwave. This is generally referred to as ‘summer’ elsewhere in the world but heat in summer is something of an outlandish concept to the people of this sceptred isle. Much like that other aberrant period, winter, we are woefully unprepared for the kind of weather that is generally considered standard for the season.

But fear not, friends. Whether you live on ‘this precious stone set in the silver sea’ or elsewhere in the world, it has never been easier to keep track of how inclemently the weather is behaving. You can do it with just your Raspberry Pi, a couple of low-cost Xinabox parts and ten minutes of your time.

If you are new to XinaBox, you will find that these little, square modules are great for clipping together super-fast prototypes for all sorts of circuit functionality.

The SW01 module sports a Bosch BME280 digital sensor that will give us accurate readings for temperature, humidity and air pressure. XinaBox modules use I2C to interface with each other and the BME280 is an I2C device, so we can use existing Python BME280 libraries to drive the sensor from our Raspberry Pi. For this experiment, I will be using Richard Hull’s Python module, though there is also an Adafruit module you could use.

The BR01 Rasperry Pi bridge fits onto the GPIO pins furthest away from the USB sockets on your Pi:


We are using the MD01 spacer (or in my case, the MD03) to make sure our sensor is not reading the temperature directly above the Broadcom processor. As regular Pi users will know, the processor can get quite warm, even when it isn’t doing all that much:


I used a quick shell script to get the processor temperatures and even doing nothing, the processor is at 45 degrees C. Moving the sensor away from this heat source, using the spacer, should (mostly) mitigate any distorted readings.

Prerequisite Setup

The first thing we need to do is make sure the I2C kernel driver is enabled on our Raspberry Pi. We can do this by running dmesg or lsmod from the command line in a terminal:


If we find, unlike in the picture above, that we have no listed I2C kernel modules then we will need to enable them.

In your terminal type:

$ sudo raspi-config


Then use the down arrow key to move down to ‘Advanced Options’ and hit <Enter>:

config_011_ffb1cd40220280279278febcaa81c0b914407b85.jpgWe then need to move down to ‘Interfacing Options’ and <Enter>:


Then select P5 I2C:


And we get:


Select <Yes> which should produce:


Hit the <OK> which takes us back to the main menu. Use the right arrow key to get to <Finish>:


If we reboot and check dmesg or lsmod again, we should see the kernel modules are now loaded.

The next thing we need to do is install some tools and an essential pre-requisite Python library:


With the I2C tools installed, we can check that our weather sensor is visible on the bus:


There it is at address 0x76. Great.

Now we can install our Python module:

$ sudo pip install RPi.bme280

If you want to use Python 3, then you will need to use pip3:

$ sudo pip3 install RPi.bme280


At this point, we are ready to begin writing our Python Program. We will start with a really rough n’ ready program, just to make sure our hardware is behaving as expected. Open the text editor or use Geany to enter a test script:

import smbus2
import bme280

bus = smbus2.SMBus(1)

bme280.load_calibration_params(bus, 0x76)

print(bme280.sample(bus, 0x76))

We can save this as

Open a terminal and run the script from the command line:


Good. Our hardware has produced a sensible output, so we know it’s all working as it should. Now we can go on and do something useful, like logging our readings over time into a file. For this we are going to need a slightly more complicated Python script:

import time
import smbus2
import bme280

bus = smbus2.SMBus(1)
address = 0x76

calibration_params = bme280.load_calibration_params(bus, address)
layout = '{0:5d}:  {1},  {2:0.3f} deg C,  {3:0.2f} hPa,  {4:0.2f} %'
counter = 1

while True:
    data = bme280.sample(bus, address, calibration_params)
    with open("sample.log","a+") as f:
        f.write(layout.format(counter, data.timestamp, data.temperature, data.pressure, data.humidity) + "\n")
    counter += 1

When we save and run this, not much happens in the terminal. Instead, our output is appended to a file on each loop:


I have used only a 5-second interval between each loop in order to get some quick data output, but in a real-world logging situation, you would probably want to have an interval of something more like 5 to 15 minutes between log entries. The log file ends up looking something like this:


Final Thoughts

The XinaBox x-chips make it incredibly fast to try out any ideas you may have. You don’t even need to know much about electronics – you just need to know the functionality you want. Then you can clip these functions together in seconds and get straight to programming. This is ideal for super-quick prototyping or makeshift measurement instruments that you can take apart and re-use for other purposes when you’re done.

There are over 80 x-chips covering a host of different processor, sensor and communications functions. More are being added all the time, so I’m excited by the prospect of being able to build and deploy almost any kind of IoT or autonomous embedded system that I can imagine, pretty much at the drop of a hat. You can’t help feeling the future looks bright!

Mark completed his Electronic Engineering degree in 1991 and worked in real-time digital signal processing applications engineering for a number of years, before moving into technical marketing.