Main Content

Arduino Based Smart Watering of Plants

This example shows how to use Simulink® Support Package for Arduino® Hardware to create a Simulink model to implement a smart plant watering system based on ThingSpeak.

Supported Hardware:

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Due

Available versions of this example:

Arduino Mega 2560 board: arduino_smart_plant

The provided model is pre-configured for Arduino Mega 2560. You can run this model on any of the board listed in the "Supported Hardware" section by changing the "Hardware board" parameter as described in Task 7 of this example.

Introduction

In this example, we will develop a Simulink model to implement a smart plant watering system.

The watering requirements for a plant can be decided by the ideal soil moisture range required for it. If the soil moisture is not in the ideal range then the plant is over watered or not watered enough.

By measuring the soil moisture level, we can decide if the plant needs to be watered. To measure the soil moisture, we will use a soil moisture sensor. The sensor measures the percentage of moisture present in the soil.

Another vital resource for growth of the plant is the amount of sunlight it receives. By measuring the intensity of light reaching the plant, we can determine if the requirements of the plant is met. An ambient light intensity sensor can be used to measure the amount of light incident on the plant.

The smart plant watering system would consist of an Arduino connected with a soil moisture sensor and a light intensity sensor.

  • Depending on the measured value of the moisture level, a water pump connected to Arduino through a relay shield is powered on/off.

  • The user is informed about a low-light condition using a LED if the light intensity measured is less than the threshold level set by the user.

To provide further versatility to the user, we will connect the Arduino to ThingSpeak. Thingspeak is the MathWorks analytics IoT platform.

ThingSpeak is an IoT platform that enables you to collect, store, analyze, visualize, and act on data from sensors or actuators, such as Arduino®, Raspberry Pi™, BeagleBone Black, and other hardware. The primary element of ThingSpeak activity is the channel, which contains data fields, location fields, and a status field. After you create a ThingSpeak channel, you can write data to the channel, process and view the data with MATLAB® code, and react to the data with tweets and other alerts.

Connecting the system to ThingSpeak provides us the following capabilities

  • We will periodically send the value of moisture and light intensity to be stored on ThingSpeak. This would enable the user to monitor the performance of the system online from any part of the world.

  • The threshold value for soil moisture and light intensity can be stored in ThingSpeak. Arduino would read the threshold values from ThingSpeak and take the necessary actions. User can at any point of time change the threshold remotely by just setting the value in ThingSpeak to a different one. The change will then take effect in the system when Arduino reads from ThingSpeak during the subsequent cycles of operations.

Prerequisites

Before you start with this example, we recommend you complete the following:

Required Hardware

To run this example, you will need the following hardware:

Task 1 - Connect Sensors to Arduino Hardware

In this task, you will connect the Soil moisture sensor and the Ambient light sensor to the Arduino board.

1. Connect the Soil moisture sensor to the Arduino board using the following circuit diagram:

Soil moisture sensor pin |   Arduino Mega 2560 pin
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
        VCC              |           5 V
        GND              |           GND
        SIG              |           A0
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

2. Connect the Ambient light sensor to the Arduino board using the following circuit diagram:

Ambient light sensor pin |   Arduino Mega 2560 pin
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
        VCC              |           5 V
        GND              |           GND
        SIG              |           A1
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

3. Connect the Relay Shield to the Arduino board using the following circuit diagram:

     Relay Shield         |   Arduino Mega 2560 pin
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
         VCC              |           5 V
         GND              |           GND
         IN1              |           Pin 4
         COM              |           12 V
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

4. Connect the Water pump to relay shield using the following circuit diagram

      Water Pump          |       Relay Shield
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
         VCC              |           NO
         GND              |           GND
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Task 2 - Create Simulink Model to Implement Smart Watering System

In this task, you will develop a Simulink model to implement a smart watering system. We will use Arduino algorithm template from the Arduino template library to develop the Simulink model.

1. Open the Simulink Start Page.

2. From the Simulink Support Package for Arduino Hardware templates, create a new Simulink model using Algorithm template.

This template is best suited for implementation of systems involving simple or moderately complex algorithms. The template is categorised into three main region, inputs, algorithm and output section.

The model that we develop will be used for both Simulation and deployment mode. In the simulation mode, we will use Simulink source blocks to act/simulate the soil moisture sensor and the ambient light sensor. The output of the algorithm can be viewed using a display or scope block.

After the algorithm is simulated and verified, we can configure the model for Arduino hardware, build and deploy the model on to the hardware.

3. Open Simulink library browser and add two Analog Input blocks from the Simulink Supportpackage for Arduino library. The output of both the moisture sensor and the light sensor are Analog signals. Configure the Analog Input blocks to read the sensors on pin A0 for Ambient light sensor and pin A1 for soil Moisture sensor.

4. From the Simulink Supportpackage for Arduino library, add two Digital output blocks. These blocks will be used to switch on/off the relay controlling the water pump and to turn on/off the LED (in this case the on-board LED of Arduino which is in pin 13). Configure the Digital output block corresponding to the relay to output on pin 4 and configure the other Digital output block to output on pin 13.

5. Open the subsystem in the 'Algorithm' area by double clicking on it. Add two 'Relational operator' blocks from the Simulink 'Logic and Bit operations' library. These will be used to decide if the water pump has to be turned on/off and if the user needs to be informed about low-light conditions.

Task 3 - Create ThingSpeak Channel

In this task, you will create a new Thingspeak channel to store the sensor data of the system and the value of threshold associated with them.

1. Complete the Get Started with ThingSpeak (ThingSpeak). This step will introduce you to ThingSpeak and its features.

2. Create a channel in your ThingSpeak account to collect the data associated with the Smart Plant watering system. Follow the steps mentioned in Collect Data in a New Channel (ThingSpeak) help page.

3. In the channel created, add four fields. These will be used to store the following

  • Field 1 - Ambient light intensity. This field will store the value of the ambient light incident on the plant.

  • Field 2 - Soil moisture level. This field will store the value of the soil moisture level of the plant.

  • Field 3 - Ambient light intensity threshold. This field will be used to set the threshold level of the light intensity.

  • Field 4 - Soil moisture threshold. This field will be used to set the threshold level of the soil moisture level.

Task 4 - Add and Configure ThingSpeak Blocks to Simulink Model

In this task, you will add ThingSpeak block to your model. ThingSpeak Write block will be used to send the moisture level and light intensity level to the ThingSpeak channel. ThingSpeak read block will be used to read the threshold value for the light intensity and soil moisture level.

1. Add two ThingSpeak read blocks to your model from the Simulink Supportpackage for Arduino library. This block can be found in both the Ethernet and WiFi group in the Arduino block library. Depending on which shield you are using in the example, choose the ThingSpeak read block from the corresponding group.

2. Configure the ThingSpeak read block to read the threshold values stored in the ThingSpeak channel you have created in task 4. Set the 'Channel ID' parameter of the two blocks with the Channel ID of the channel you have created in Task 4. Depending on the channel Access you have set in Task 4, set the 'Channel Access' parameter for the two blocks. If the access type is private, provide the 'READ API KEY' associated with your channel. The threshold for light intensity is stored in field 3 and the threshold for soil moisture level is stored in field 4.

3. Add a ThingSpeak write block. This block is placed under the Ethernet and the WiFi group of the Arduino library. Similar to step 1, select the appropriate block corresponding to the type of shield you have used for the example.

4. Configure the ThingSpeak write block. Set the 'Channel ID' parameter of the block with the Channel ID of the channel you have created in Task 4. Provide the Write API Key in the 'Write API Key' parameter. As we are storing the value of light intensity and soil moisture, set the 'number of variables to send' parameter to 2.

5. Save your model.

Task 5 - Store Initial Threshold Values for Soil Moisture and Ambient Light Intensity on ThingSpeak

The Threshold value for soil moisture and ambient light intensity can be stored in field 3 and 4 of your channel by executing the following command in your MATLAB command prompt

thingSpeakWrite(Channel ID, {light intensity threshold, Soil moisture threshold}, 'Fields', [3, 4], 'WriteKey', Channel Write API Key)

For further help regarding thingSpeakWrite, see thingSpeakWrite (ThingSpeak).

Task 6 - Configure Simulink Model for Deployment on Arduino Hardware

In this task, you will configure the model to be deployed on the Arduino hardware. As we started with an Arduino template model, the model is already configured for Arduino MEGA 2560.

1. Open the arduino_smart_plant Simulink model.

2. On the Hardware tab, click Hardware Settings. In the Configuration Parameters dialog box, review the parameters in the Hardware Implementation pane. If you are using an Arduino board other than Arduino MEGA 2560, set the required Arduino board in the Hardware board parameter.

3. If you have used an Ethernet or a WiFi shield in this example, set the Network details under Ethernet shield properties or WiFi properties in the Configuration Parameters > Hardware Implementation.

4. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start. The Simulink model will be built and deployed on to the Arduino board connected.

Other Things to Try

1. Act on your data : Follow the steps mentioned in Act on Your Data (ThingSpeak) help page to react on a low-light condition by sending you a tweet.

2. You can replace the relay shield by using a transistor arrangement.

3. Create a mobile app using Simulink Support Package for Android Devices to set the value of the thresholds for light and soil moisture. This support package provide a ThingSpeak block similar to the one available in the Arduino library to store the data on Thingspeak channel.