- Interfacing a soil moisture sensor and DHT11 sensor to RPi3
- Data visualization using Thingspeak
For this project, you will need
- Raspberry Pi 3 [RPi3] (quantity: 1 no.)
- DHT11 sensor (quantity: 1 no.)
- Soil moisture sensor (quantity: 1 no.)
- Micro SD card (quantity: 1 no.)
- Power Adapter (quantity: 1 no.)
- Resistor 4.7K Ohm (quantity: 1 no.)
- Jumper Wires (quantity: 9 no.)
- VNC Viewer software on your computer
- Thingspeak account
IoT stands for Internet of Things which literally translates to the network of devices or connected things. Here things maybe anything to a water bottle to your washing machine. This smart network enables us to have control over many things and the data obtained by these devices can be used for data analytics. For example, a smart water bottle can remind you to drink water etc.
As backyard farming is getting a good attention in urban and suburban areas it is important to monitor the health of your farm. But it will be difficult to pay attention to your farm in a busy urban life. As the first experiment in IoT, we will build a Raspberry Pi based garden monitoring unit.
Here we use DHT 11 sensor and a soil moisture to monitor humidity, temperature and soil moisture level.
Interfacing the DHT 11 and soil moisture sensor to Raspberry Pi
We need to connect the soil moisture sensor and DHT11 to Raspberry Pi. The Soil Moisture Sensor uses capacitance to measure the dielectric permittivity of the surrounding medium. In soil, dielectric permittivity is a function of the water content. The sensor creates a voltage proportional to the dielectric permittivity, and therefore the water content of the soil.
Here we will read the analog voltage produced by the probe and then digitize that using an OPAMP comparator module. This will sense particular value to turn on and turn off i.e it converts analog voltage to 1 and 0. This High or Low digital input is fed to GPIO pin 14. This can be used to say whether the garden needs water or not.
We are keeping the circuit simple and building it on a breadboard using our components and jumper wires. Before connecting anything to your Raspberry PI, disconnect the power.
Warning – you can destroy your Raspberry Pi with a short circuit from a wrong connection. Just be careful and double check everything before powering back on.
To connect from the Raspberry PI to breadboard I like to use Dupont Cables, they are jumper wires that have a female side and a male side. The female side connects right to the male header pins of he Raspberry Pi and the male side plugs right into the Breadboard.
For this circuit, we need to use the 3.3v out from the Raspberry Pi Pin 1 (do not use the 5v on Pin 2) and we need Ground (GND) of course. Connect these from the Pi to the Breadboard.
The DHT 11 has 3 Pins. Pin 1 is VCC, Pin 2 is Data, Pin 3 is Ground.
- Connect DHT 11 Pin 1 to 3.3v
- Connect DHT 11 Pin 2 to Raspberry PI Pin 16/GPIO 23 and connect a 4.7k or 10k resistor from DHT 11 Pin 2 to DHT Pin 1
- Connect DHT 11 Pin 3 to Ground
DHT11 communicates to raspberry pi over one wire protocol which needs precise timing to communicate to the controller or processor.
Not all the libraries we need to make this project are pre-loaded on the Raspberry Pi. You will need the Adafruit GPIO Python library and the Adafruit DHT 11 library.
We will use Adafruit’s guide and library for setting up Python to communicate with the Raspberry Pi GPIO pins.
We also need Adafruit’s Python library for the DHT11 sensor, which you can find here:
Setting up Thingspeak for storage of Data
We need to send the data to the cloud so that we can view it from anywhere. So let’s send this data to the cloud i.e a service provider such as Thingspeak. Head over to http://www.thingspeak.com and create an account.
Click on “My Channels” and then click on New Channel. Name your new channel and name the fields. The order of the fields is important later when we post data. They can be in any order, but when you post the data the data you need to remember position.
You can decide if you want the channel to be public or not as well as publish information about its location. This is all up to you and will not affect our code. You will also need the Write API key for the channel as it will be required to post data to the channel.
Launch IDLE on the Raspberry Pi
To Launch IDLE on the Raspberry Pi go to MENU -> Programming -> Python 3
To start writing our script go to FILE -> NEW and a new, untitled window will appear. This is where we will actually write our code. Save and name the file before we begin. Go to FILE ->Save and the file IoT.py
Writing the Code
Let’s code. First, we need to
- Import all the required libraries and packages
- We need to setup GPIOs
- We should read sensor outputs to send the data to thingspeak.
In the above code the variables are assigned as follows,
TW = temperature in Celsius
TWF = temperature in Fahrenheit
RHW = Humidity
ST = Soil Moisture (Dry or Wet)
There you go!
Open LXTerminal in Raspberry Pi. Then type the following command to run the script.
sudo python IoT.py
This will show a message that the system is starting and then it will show the URL to where the data is being sent. It will show all the measured values of temperature, Humidity and soil moisture.
Now you can check thingspeak website that your channel has received the data and are visualised using the intuitive charts of Thingspeak.
The above picture is the sample output.