Raspberry Pi Plant Pot Moisture Sensor via Analogue Signals
Before we step into this tutorial, it’ll be good to have a small understanding of the difference between analogue and digital signals.
Both types of signals can be represented as wave forms, the difference being that analogue waves are smooth and curvy and have an infinite value, whereas digital waves are square and go in steps and have a finite set of values, most commonly one of two values, on or off, 1 or 0.
The main advantage of analogue signals is they can give an extremely accurate value, however because of this they are also prone to interference which can disrupt the signal.
The main advantage of digital signals is that because they are either one value or another, they are much harder to disrupt, the disadvantage of course is their values are limited meaning less accurate results.
If you’d like to know more about the differences, there is plenty of information on the subject out there. Just use your favourite search engine :)
So, on to the actual tutorial….
Last year, boy that sounds like a long time ago, I showed you how to wire up a moisture sensor to your Raspberry Pi and Plant Pot to monitor the moisture levels of the soil. That was relatively simple to do as the output of the sensor was simply 1 or 0 (on or off).
Today we are going to look at a more advanced technique of getting data from our sensor, and that is by using the moisture sensors Analogue output. By using the Analogue output, we can get more precise data. We won’t be receiving an either “yes I’m moist” or “help I’m dry!” but instead a value based on the moisture.
The Raspberry Pi itself doesn’t actually have an analogue GPIO pin, so we will need to use an analogue to digital converter or ADC for short.
Once the data has passed through our ADC it will become a digital reading. This reading will simply be a number representing the analogue value.
Here’s a list of things you’ll need to follow this tutorial:
- 1x Raspberry Pi – Any Model
- Moisture Sensor
- Breadboard – Half Size or Full Size
- Cobbler – Either T or Normal
- Analogue to Digital Convert
- If you are using a Pi Zero, you’ll need an Extra Tall 40 Pin Header Extender to fit the ribbon cable on from the cobbler
Start by connecting the Pi to the cobbler, using the ribbon cable, and then plug the cobbler into the breadboard. Make sure each row of pins on the cobbler sit either side of the gap that runs down the middle of the breadboard.
Now we can wire up our moisture sensor to the breadboard. Start by connecting the pin marked VDD on the sensor to the 3V3 pin on the breadboard (GPIO Pin 1). Now wire up the pin marked GND on the sensor to any of the GND pins on the breadboard. We also need to attach the sensor prongs to the sensor board. On the side of the sensor board that only has 2 pins, wire each pin from the board to the prong. It doesn’t matter which pin on the board goes to which pin on the prong.
Time to hook up the ADC. Like the sensor, wire up the VDD and GND pins to the breadboard (VDD -> 3V3, GND -> GND). Then wire up the SCL and SDA pins to the corresponding pins on the breadboard (SCL – GPIO Pin 5, SDA – GPIO Pin 3).
Now we need to connect the analogue output from our sensor, to the input on the ADC. So connect A0 from our sensor to A0 on our ADC. (The ADC we are using has multiple analogue inputs, but we just need the one for this tutorial)
Now with everything wired up, go grab your plant and poke the prongs of our moisture sensor into the soil.
That’s it for the hardware setup, now on to some coding!!
To start with, we need to download some python code from Adafruit which will allow us to easily use their ADC.
Adafruit have all of their example code in a single Git repository, so we’ll have to download it all, then find the code we actually need for our ADC.
Start by making sure you have git installed on your raspberry Pi
sudo apt-get install git
Now we can download or “clone” the repository to our Pi (this simply downloads all the files from the repository, to our Pi)
Now we have all Adafruit’s code downloaded we need to find the code for our ADC. Move into the newly downloaded directory
Then from here, if we list the contents of the directory we should be able to find what we are looking for
The Adafruit ADC we are using in this tutorial is an ADS1015. The first directory in the list is “Adafruit_ADS1x15”. That’s the one we want! So now we need to hop on in to that directory to see the code provided
In this directory we can see a few different python scripts. The one we are interested in is called:
We’ll be using this as our starting point to write our own script. If we run that script we should be able to see the voltage that our moisture sensor is reporting. To run the script simply enter
sudo python ads1x15_ex_singleended.py
To double check everything is working we can test the moisture sensor in the two extremes. Dry and wet.
Start by removing the sensor probes from our plant, and suspend them in mid-air, so that nothing is touching them. Now run the above script. You should get a value > 3.00
Now grab a glass of water, and carefully place the sensor probe in the water. DO NOT SUBMERGE THE WHOLE THING. Just make sure at least half of the two prongs are submerged. Run the above script again, and you should get a value < 0.50
Hopefully you got similar readings. If you didn’t just go back and double check all the connections are secure and correct.
If you did, then everything is setup correctly and you are ready to expand the example script to meet your requirements.
How about adapting the digital moisture sensor to send the email alerts based on the value of the analogue reading?