K O logo
IoT

IoT — Building an automated greenhouse Pt. I

Author

Kalle

Date Published

This AI-generated image depicts a futuristic, tech-enhanced greenhouse with plants organized on shelves. The space features digital displays and sensors monitoring growth, all powered by Raspberry Pi technology.

The course is coming slowly to an end, so it's time to bring all knowledge together and start building something useful. The goal is to have a little greenhouse to put on your window ledge, connected to sensors so you will get notified when it gets too hot/cold inside or the plants need new water.

The Raspberry Pi and the program running on it already is capable of reading the temperature and humidity values which are both important environmental values to grow plants. But till now it's not possible to have any information about the soil and how much water is left. To measure the moisture of the soil there are simple sensors available, basically two metal pieces which are put in the ground to measure the electric conductivity.

The image shows a soil moisture sensor with two metal prongs attached to a small black circuit board. The prongs are designed to be inserted into the soil to measure moisture levels. The circuit board has some text in a non-English language, and wires are connected to it. The sensor is placed on a wooden surface.

Using this sensor brings another problem to the table, the Raspberry Pi doesn't have any analog-in pins. So to measure analog values you have to use an analog-digital-converter (ADC). In this Example I will use the MCP3008 chip which can be connected to the RasPi via the Serial Peripheral Interface (SPI) bus. SPI is deactivated by default so connect to your Raspberry Pi via ssh and run the raspi-config command with admin privileges:

1sudo raspi-config
The image shows the "Raspberry Pi Software Configuration Tool (raspi-config)" interface on a Raspberry Pi Zero W Rev 1.1. It lists options like changing the user password, configuring network and boot options, localization, interfacing, overclocking, advanced settings, updates, and information about raspi-config. The highlighted option is "Change User Password." The interface has "Select" and "Finish" options at the bottom.

In the menu choose the Interfacing Options and activate SPI.

When SPI is enabled you can start wiring the MCP3008 chip to the Raspberry Pi. For orientation the chip has a small dent on one side. When aligned with the dent pointing to the left, the SPI connectors are on the top and the analog input pins are on the bottom.

The image shows the pinout diagram of the MCP3008, a 16-pin analog-to-digital converter (ADC) chip. The pins are labeled as follows: CH0-CH7 (analog inputs), VDD, VREF, AGND, CLK, DOUT, DIN, CS/SHDN, and DGND. The chip is rectangular with pins on both sides.

1VDD 3.3V
2VREF 3.3V
3AGND GROUND
4CLK BCM11(SCLK) Physical pin 23
5DOUT BCM09(MISO) Physical pin 21
6DIN BCM10(MOSI) Physical pin 19
7CS BCM08(CE0) Physical pin 24
8DGND GROUND

With the complete SPI bus connected you can use the other side of the MCP3008 to connect an analog input from a soil moisture sensor:

The image shows a breadboard circuit connected to a Raspberry Pi. Components include an MCP3008 ADC, a soil moisture sensor, a DHT11 temperature and humidity sensor, and an LED. Various colored jumper wires connect the components to the Raspberry Pi GPIO pins.

Now the only missing part is a little bit of software to collect the values and send them to the cloud. Like always with a node.js program the first step is choosing a library to make it as easy as possible to work with the SPI. This time the package of choice is mcp-spi-adc, start by running npm install mcp-spi-adc on the Raspberry Pi inside the directory of the firebase sensor project.

The following code snippet will read the analog input value on the pin 0 in an interval of 5 seconds:

1const mcpadc = require("mcp-spi-adc");
2
3const analogPin = 0;
4
5const analogInput = mcpadc.open(analogPin, (error) => {
6 if (error) {
7 console.error("error on initializing spi", error);
8 throw error;
9 }
10 setInterval(() => {
11 analogInput.read((error, reading) => {
12 if (error) {
13 console.error("error on reading the analog input", error);
14 }
15
16 console.log("analog input value", reading.value, reading.rawValue);
17 });
18 }, 5000);
19});

Tasks

  • activate the SPI bus on the Raspberry Pi
  • connect the MCP3008 chip to the Raspberry Pi via SPI
  • complete the wiring to connect a soil moisture sensor
  • read the soil moisture values and store them in the firestore
This AI-generated image shows a high-tech greenhouse filled with rows of plants. The space is equipped with advanced sensors, digital displays, and automated systems for monitoring plant growth, powered by Raspberry Pi.

IoT — Building an automated greenhouse Pt. II

Continue building an automated greenhouse by adding an interface to configure soil moisture reference values and implement authentication with Firebase. This guide explains protecting database access, setting up user authentication, and updating sensor data references in Firestore.

Mailing List

If you want to receive updates on new posts, leave your email below.