Pi Pico Environmental Sensing Workout
by tonygo2 in Circuits > Microcontrollers
1786 Views, 0 Favorites, 0 Comments
Pi Pico Environmental Sensing Workout
I've been using the Raspberry Pi Pico for several months, programming it with MicroPython using the Thonny editor. I've spent most of this time with graphics programs and several different displays. Using sensors has been difficult as board suppliers have not usually provided suitable MicroPython drivers for their boards.
WaveShare have recently made available a new Environmental Sensor Board for the Pi Pico, with five sensor chips allowing the user to monitor a wide range of environmental values.
The board contains the following features:
- Onboard TSL25911FN digital ambient light sensor, for measuring IR and visible light
- Onboard BME280 sensor, for measuring temperature, humidity and air pressure
- Onboard ICM20948 motion sensor - accelerometer, gyroscope and magnetometer
- Onboard LTR390-UV-1 sensor for measuring UV rays
- Onboard SGP40 sensor, for detecting ambient air quality/VOC
- I2C bus - allows retrieving data by using just two wires/pins (SDA & SCL)
- MicroPython drivers supplied and easy to include in your programs.
The package can be plugged directly onto the Pi Pico pins.
Supplies
Raspberry Pi Pico
USB cable
Thonny Editor
WaveShare Environmental Sensor Module for Pi Pico
WaveShare 1.8" LCD Display for Raspberry Pi Pico (160x128 pixels)
(I live in the UK and got them from ThePiHut.com)
The Top of the Module
My board was set up to use pins GP20 and GP21 for the I2C0 connections. If this clashes with something else you want to use (display or additional sensor) you could move the R0 connectors at the left and use pins GP6 and GP7 for I2C1 connection.
The chips are arranged along the centre of the board with the motion sensor in the middle. There is also a red power LED to show the board is live.
The Bottom of the Module
The table shows the I2C addresses of the various chips.
With this setup we will be able to monitor
- Visible light
- IR light
- UV rays
- Temperature
- Humidity
- Air Pressure
- Air Quality/VOC (Volatile Organic Compounds - ethanol equivalence)
- Motion and position:
3-axis Accelerometer + 3-axis Gyroscope + 3-axis Magnetometer
You can find all the drivers, and information about the board and sensors here:
https://www.waveshare.com/wiki/Pico-Environment-S...
I downloaded the resources and found the zipped demonstrations to be a .7z file and slightly difficult to extract without paying for additional software. However, I managed it and all the drivers are available in the following software in a slightly modified form. (I changed time to utime and run the main part of the code in a slightly different way.) WaveShare drivers are not in separate libraries but the necessary code can easily be copied and inserted into your own code, usually near the top of your program.
I like this method as you do not have to keep changing the libraries in the lib folder as different programs are loaded and executed. (Much easier at 'Show & Tell' events.)
The data sheets for the sensors make a very interesting read. You can understand why it has taken so long for sensor drivers to appear.
The Light sensor driver only provided the visible light value so I have added the IR part, already calculated within the driver. (This will provide a value for comparison purposes - not sure what units, if any, apply.)
Please bare in mind that just because the temperature, or other reading, is shown with several decimal places that may not be the actual temperature. The documentation for the BME280 says that the resolution is +/- 0.01 degrees C and the accuracy is only +/- 1 degree C.
Connecting Up
I used a Pimoroni Pico Decker which made things really easy. I just pushed the Pico, display and sensor module onto the Decker and plugged in the USB cable.
Demonstration Program to Read All the Sensor Data
I've included the drivers for all the sensors in the following program which makes it very long - more than 1360 lines! Each driver is 'topped & tailed' with comments to make them easy to isolate, copy and extract or delete if not needed. This program does a great deal of work. After the title a 'splash screen' lists the environmental sensors and the readings they can supply. They are accessed in turn and the readings are displayed on the screen and in the Serial Monitor at the bottom of the Thonny Window. This is followed by the readings from the '9-Degrees of Freedom' sensor, which takes 2 display screens. This last sensor is very complicated and making sense of the readings is quite difficult. (I've not got my head round all the values displayed but the accelerometer is the easiest. More about this later. If you have the full kit just run the code. If you have a different display you may need to alter the screen text lines, or comment them out and just look at the Serial Monitor.
The Demonstration Program
The full program (48 K bytes) can be downloaded below. Most of the code is the drivers for the Environmental Sensors and the Display, with re-sizeable characters.
- Display driver: 22 - 384
- BME280 driver: 285 - 524
- SPG40 driver: 526 - 630
- TSL2591 driver: 631 - 776
- LTR390 driver 777 - 847
- ICM20948 driver 848 - 1249 very complicated!
The main program starts at line 1251.
I suggest you download the program and take a look at the code. The main part is easy to understand as it just calls to drivers to provide values, and then prints and displays them.
The circuit was horizontal and stationary on my desktop during the filming but some of the 9-DoF values moved about more than I expected.
I've not yet worked out how to use the magnetic and gyroscopic values but the accelerometer is easier and the next section provides a 'Tilt Game' as an example.
Downloads
The Tilt Game
This is a simple game where the player has to tilt the screen to move the yellow square until it covers the blue target square. (The movement makes this very difficult to film a suitable video but I'm sure you understand the idea.)
Downloads
Game Code #1
This shows the start of the program
Game Code #2
Here are 3 helper routines:
wait_shake() this waits until the sensor registers that the circuit is being shaken. SQRT(x*x + y*y + z*z) provides a value from the 3 accelerator readings to register how much the circuit is moving about.
colour(R,G,B) converts 3 byte colours to 2 byte colours (RGB565) for the display screen.
ctext(text,y,c) centres a text string on a screen line.
The next few line start the display and the accelerometer
Game Code #3
Here is the code for the instructions and the game.(Plenty of comments)
Game Code #4
This displays the score turns off the screen.
=================================================================
I hope you have found this 'Workout' interesting and helpful. I am happy to receive comments and questions.
The sensor board is quite expensive (£27.50 in UK) but excellent value with its 5 different sensors (with working drivers) and convenient packaging using so few pins. I will be spending some time investigating and trying to understand the use of the gyroscope and magnetic aspects and will probably try to answer the questions, "When the fridge door closes, does the light go out?" and "How long to get back to normal temperature if I leave the fridge door open for 5 minutes?"
Have fun with your coding.