This guide was created on behalf of the Arm Software Developers team, follow us on Twitter: @ArmSoftwareDev and YouTube: Arm Software Developers for more resources!
IntroductionDo you forgot to water your plants? What if your plant was able to text you when it needed water? This project will use a Raspberry Pi Pico W board along with a Pimoroni Grow HAT Mini to make this a reality!
Why stop there... we can expand the project and make it more fun by giving the plant a personality by including additional messages:
- Good morning βοΈ and good night π΄ - based on a light sensor and time of the day
- Random π² messages
- I need some water π§ and thank you for the water π - based on a soil moisture sensor
The Pico W board will use the Twilio Programmable SMS API to send SMS messages to your phone.
MicroPython will be used to develop the code that runs on the Raspberry Pi Pico W board.
"MicroPython is a lean and efficient implementation of the Python 3 programming language..."
It runs on several Arm Cortex-M based microcontrollers (MCUs) and development boards, including the Raspberry Pi Pico W.
Overview of HardwareWe'll be using the Raspberry Pi Pico W board as the main development board in this project. The board is based on the Raspberry Pi RP2040 microcontroller (MCU), which contains a dual-core Arm Cortex-M0+ processor. The board is also equipped with an Infineon CYW43439 chip that is used for 2.4 GHz Wi-Fi communications and contains a Arm Cortex-M3 processor.
A Pimoroni Grow Kit will be used to monitor the plant and its surrounding environment. The kit contains the following items:
- A Pimoroni Grow HAT Mini - which has an on-board LTR559 light and proximity sensor and a 0.96" 160x80 pixel IPS LCD display with ST7735R LCD controller. The Grow HAT Mini also contains a piezo buzzer and buttons, however these features will not be used in this project.
- Grow Moisture Sensors (pack of 3)
- 3-pin JST-SH cables for Grow moisture sensors (pack of 3)
Details of sensors (inputs):
- The LTR559 light and proximity sensor has an I2C interface which can be used to read the surrounding environments light level in Lux.
- The moisture sensor outputs a digital pulse signal with a frequency between
1 Hz
to30 Hz
. Higher frequencies indicate the sensor is dry, while lower frequencies indicate the sensor is wet.
For example, when the moisture sensor is left in open air, it outputs a pulse of ~25 Hz.
If the moisture sensor is left in water, it will output a pulse of ~1 Hz.
Details of actuators (outputs):
- The ST7735R LCD controller has an SPI interface, that allows a MCU to control the pixels displayed on the LCD screen. The screen will be used to display a name tag and status of the board's Wi-Fi connection.
Soldering
Solder male headers onto your Raspberry Pi Pico W board. See The MagPi's "How to solder GPIO pin headers to Raspberry Pi Pico" guide for more details on soldering pin headers to the Raspberry Pi Pico W board.
The Pimoroni Grow HAT Mini included with the Pimoroni Grow Kit is intended to be used with a normal Raspberry Pi board. We will need to create an adapter board using an Adafruit Perma-Proto HAT for Pi Mini Kit - No EEPROM board to connect the Grow HAT Mini to the Raspberry Pi Pico W board.
Solder the GPIO Header for Raspberry Pi A+/B+/Pi 2/Pi 3/Pi 4/Zero - 2x20 Female Header included with the Perma-Proto HAT to the top of the board. Then solder two Adafruit 20-pin 0.1" Female Headers to the board in the position displayed in the diagram below.
After this, 10 wires will need to be soldered for the Pico W to use the Grow Hat Mini's built-in SPI LCD display, I2C light sensor, and moisture sensor connector. Please use the diagram and tables below to solder the wires into the appropriate locations.
Power:
+--------------------+--------+-----------------------------------+
| Perma-Proto Pi HAT | Pico W | Notes |
+====================+========+===================================+
| GND | GND | - Ground |
+--------------------+--------+-----------------------------------+
| +3V | 3V3 | - Power for light sensor, |
| | | LCD display, and |
| | | moisture sensor |
+--------------------+--------+-----------------------------------+
| +5V | VBUS | - Power for LCD display backlight |
+--------------------+--------+-----------------------------------+
Data:
+--------------------+--------+-----------------------------------+
| Perma-Proto Pi HAT | Pico W | Notes |
+====================+========+===================================+
| SDA | GPIO4 | - I2C0 SDA for light sensor |
+--------------------+--------+-----------------------------------+
| SCL | GPIO5 | - I2C0 SCL for light sensor |
+--------------------+--------+-----------------------------------+
| MOSI | GPIO19 | - SPI0 TX for LCD display |
+--------------------+--------+-----------------------------------+
| MISO | GPIO16 | - D/C control for LCD display |
+--------------------+--------+-----------------------------------+
| CLK | GPIO18 | - SPI0 SCK for LCD display |
+--------------------+--------+-----------------------------------+
| CE0 | GPIO8 | - Soil moisture sensor (S2) data |
| | | output |
+--------------------+--------+-----------------------------------+
| CE1 | GPIO17 | - SPI0 CSn for LCD display |
+--------------------+--------+-----------------------------------+
More detailed pinout information for each component can be found on the following pages:
Once soldered, the top of the board should look something like this:
We recommend you use a multimeter to verify all the headers and wires you soldered are in the correct position and not shorted to one another. You can also, optionally, place electrical tape on the back of the board to protect the electrical connections.
Assembly
Plug the Raspberry Pi Pico W board into the white headers with USB connector facing the rightside of the board.
Then, plug in the Raspberry Pi GPIO Male Header - 2x20, with the longer pins facing down, into the 2x20 Female Header we soldered earlier to the top - this will give us extra height for stacking the Grow HAT mini.
Next plug in the Pimoroni Grow HAT mini on top of the male headers.
Connect one of the 3-pin JST-SH cables for Grow moisture sensors included in the Pimoroni Grow Kit to the middle (S2) port of the Grow HAT Mini board.
Then attach the other end of the cable to one of the soil moisture sensors.
Your setup will look like this once complete:
Download and install MicroPython on the board
Download the Pico W MicroPython UF2 image with networking support: rp2-pico-w-latest.uf2
While holding the white boot
button on the Pico W board, plug in a micro USB B cable to the board and then plug the other end of the cable to your computer. If done correctly, a new RPI-RP2
boot volume will appear on your computer:
Next, copy the .uf2
MicroPython UF2 file you downloaded earlier to the RPI-RP2
boot volume. The board will then reset and start running MicroPython.
Download and install Thonny
The Thonny IDE will be used to upload code to the Raspberry Pico W board. Download the operating system (OS) specific version of Thonny from the Thonny home page for your computer. At the time of writing this guide Thonny 3.3.13
was the latest release.
For more details on how to use Thonny and MicroPython with your Raspberry Pi Pico board, please see the Chapter 2 of the "Get Started with MicroPython on Raspberry Pi Pico" book from HackSpace.
Setup Thonny
Next, open the Thonny application and click the "Python 3.7.9" label on the bottom right-hand side of the window:
Then select "MicroPython (Raspberry Pi Pico)" from the menu:
Twilio's Programmable SMS API will be used to send an SMS text message from the board to your phone. You will need to create and setup a Twilio account to use this API.
1. Start by creating a free account and validating your email address.
2. Next enter your phone number for verification. Twilio trial accounts only allow you to send SMS messages to phone numbers which you have verified.
3. Fill in responses to the questions and click the "Get Started with Twilio" button.
4. Click the "Get a Twilio phone number" button to get a phone number to use for sending SMS messages.
5. Scroll down on the page to view your Account SID, Auth Token and Twilio phone number from the "Account Info" section of the Twilio console. These values will be needed further down in the guide.
The Account SID, Auth Token, and Twilio phone number will be used to send an SMS message from the board, by sending an HTTP POST request to the Twilio "Messages list resource URI":
https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages.json
Twilio uses HTTP Basic Access Authentication to authentic the HTTP requests - the Account SID will be used as the username
and Auth Token will be used as the password
.
For more information on the SMS API, please see Twilio's "Create a Message resource" documentation.
Note: SMS messages sent from Twilio trial accounts will be prefixed with "Sent from your Twilio trial account -". You will need to upgrade to a paid Twilio Account to remove this prefix.
Software Application OverviewThe MicroPython application that runs on the Cortex-M0+ processor will consist of several components:
- A driver for the LTR559 light and proximity sensor, this driver will use the MicroPython I2C class to read and write registers on the LTR559 sensor (on address 0x23) over I2C.
- A driver for the ST7735R LCD controller, that will use the MicroPython SPI class and Pin class to control the ST7735R LCD controller over SPI. It will also use the MicroPython FrameBuffer class to handle drawing operations and buffer pixel data before transferring the pixel data to the LCD display over SPI.
- A driver for the Moisture Sensor, that will use MicroPython's RP2040 PIO related functions to read the incoming pulse signal from the moisture sensor using an RP2040 Programmable Input/Output (PIO) state machine instance.
- A TwilioClient Python class to handle interactions with the Twilio SMS API. The class will use MicroPython's urequests module to send an HTTP POST request to Twilio.
A JSON file will be used to store a pre-defined set of messages sent by the board over SMS. Configuration details - including: the Wi-Fi SSID and password, Twilio Account SID and Auth Token - will be stored in a separate Python config.py file.
When the application starts up it will:
- Load the configuration values and pre-configured messages.
- Initialize the drivers (LCD display, light sensor, moisture sensor), Twilio Client, and Wi-Fi network interface.
After initialization, the main application will loop continuously and perform the following operations:
- If the Wi-Fi connection is not established, connect to the Wi-Fi network and synchronize the boards time with a Network Time Protocol (NTP) server.
- Send any scheduled messages depending on the current time of day, light and proximity sensor readings.
- Read the soil moisture using the moisture sensor, then if necessary: a) send a "need water" message if the value has crossed the configured threshold, or b) send a "thank you" message if the soil was previously dry but no longer is.
- If applicable, schedule the next set of messages.
Download code from GitHub
The MicroPython code used in this project can be found on GitHub. You can download it using git
:
git clone https://github.com/ArmDeveloperEcosystem/smart-plant-example-for-pico-w.git
If you are unfamiliar with git
, a zip file version can also be downloaded and extracted.
Upload code to the board
1. In Thonny, click View -> Files
in the top menu bar. Then press the Stop
button in the toolbar to refresh the files on the Pico W board.
2. In the "This computer" section, navigate to the src
folder of the code downloaded from GitHub earlier.
4. While pressing the shift
key on your keyboard, select all the files in the "This computer" section. Then right click and select Upload to /
to upload the files to the Pico W board.
Application Configuration
Open the config.py
file in the Raspberry Pi Pico
section.
Then fill in the following configuration information:
WIFI_SSID
is your Wi-Fi networks SSIDWIFI_PASSWORD
is your Wi-Fi networks passwordTWILIO_ACCOUNT_SID
is your Twilio Account SID (obtained earlier in the guide)TWILIO_AUTH_TOKEN
is your Twilio Auth Token (obtained earlier in the guide)TWILIO_FROM
is your Twilio phone number (obtained earlier in the guide)TWILIO_TO
is the phone number you would like messages to be sent toTIMEZONE_OFFSET
is the offset in hours from UTC for your location
The following items can also be optionally configured with customized values:
NAME
- name to display on the name tag on the LCD displayGOOD_MORNING_HOUR
- hour in 24-hour format when "good morning" messages can start being sentGOOD_NIGHT_HOUR
- hour in 24-hour format when "good night" messages can start being sentLUX_BRIGHT_THRESHOLD
- threshold in Lux for when it is bright enough to send a "good morning" messageLUX_DARK_THRESHOLD
- threshold in Lux for when it is dark enough to send a "good night" messagePROXIMITY_THRESHOLD
- proximity sensor value to use as a threshold to avoid triggering "good night" messages due to something blocking light to the light sensorSOIL_DRY_THRESHOLD
- moisture sensor threshold to report the soil as being drySOIL_WET_THRESHOLD
- moisture sensor threshold to report the soil as being wet
Open the main.py
file in the Raspberry Pi Pico
section, and then click the green Run
button to run the current script on the board.
The board will then attempt to connect to your Wi-Fi network.
After the board has successfully connected to the Wi-Fi network it will display a name tag on the screen.
The application will print out the sensor values once a second, these values can be used to fine tune the configuration settings in config.py.
Note: You will need to press the Thonny IDE's "STOP" button before you are able to edit files on the board, the main.py
file must then be opened and run manually.
Once you have successfully setup the hardware and software for this project you are now ready to attach it to your plant!
A USB power adapter can be used to power the board without a computer. MicroPython will automatically start running the code in main.py
when the board is powered on.
This project used MicroPython on an Arm Cortex-M0+ based Raspberry Pi Pico W board with a Pimoroni Grow HAT Mini to monitor soil moisture and light sensor values.
The board's built-in Wi-Fi capabilities were then used to send SMS messages to your phone via Twilio's Programmable SMS API based on the time of day and sensor values. This gave your plant a personality!
Next steps...
The project can be customized and expanded further by:
- Customizing the list of text messages bundled with the MicroPython application.
- Adding additional messages and application logic to handle day of week specific messages like "Happy Hump day Wednesday! πͺ" or "Ready to rock it's the weekend! π€".
- Using the proximity sensor to detect when someone is close by and displaying a message on the Pimoroni Grow HAT Mini's LCD screen.
- Soldering additional wires to use the Pimoroni Grow HAT Mini's buttons, piezo buzzer, remaining two moisture sensors, and water pump connectors. Followed by updating the MicroPython application to use the additional hardware features.
Please share any customizations you make on Twitter and tag @ArmSoftwareDev.
Arm DevSummit 2022Build your future on Arm.
Arm's foundational technology is defining the future of computing β and so are you. Join software developers from the most advanced technology ecosystem in the world to learn, collaborate, and celebrate β live, and in person in San Francisco, Oct 26-27 2022.
Additional Raspberry Pi RP2040 resourcesFor more tutorials using the Raspberry Pi RP2040, check out these projects below:
Comments