IoT Smart Garden Setup with MKR IoT Carrier

Build a smart garden setup with the MKR IoT Carrier, a pump, and a moisture sensor.

Smart garden setup with MKR IoT Carrier.
Smart garden setup with MKR IoT Carrier.

Decorating your home with plants is an easy way to bring some life into your day-to-day. The only problem is - those plants need water to survive, and if you forget to pay attention to them for a while you may need to start over. So instead of staying ever vigilant, why don't you spend an afternoon creating a setup that will let you both monitor the amount of moisture in your plants soil, and water your plants from afar using the Arduino IoT Cloud?

How It Works

The MKR IoT Carrier has built in relays that can let you control circuits that are powered separately. In this tutorial we will be using one of the relay modules on the carrier to control a pump that can provide one of your plants with water from the Arduino IoT Cloud thanks to the functionality of the Arduino MKR WiFi 1010.

We will also connect a soil moisture sensor, and together with the sensors onboard the MKR IoT Carrier, we will create a sophisticated smart garden setup, capable of:

  • Remote watering of a plant (with a pump).
  • Check the moisture of your plant (with a moisture sensor).
  • Check the temperature/humidity (using the onboard HTS221 sensor).

Live data streamed to the Arduino IoT Cloud.

Hardware & Software Needed

Apps and Online Services

Hardware & Circuit Assembly

Begin by connecting the MKR WiFi 1010 board to the MKR IoT Carrier. Then, we need to connect the moisture sensor via a Grove cable to the "A6" connector.

From the open ended USB cable, connect the positive wire (+) directly to relay 1 on the carrier, labelled "NO" (normally open). Connect the negative wire (-) together with the pump's negative wire.

Finally, connect the pump's positive wire, to the "COM" (common) pin on the relay. As a result, you have created a circuit that can be switched on/off via a relay. See next section for the full circuit.

Circuit

Below is the complete circuit for this setup.

Circuit for this project.
Circuit for this project.

To connect the wires to the relay, see the image below:

Connecting the wires to relay 1.
Connecting the wires to relay 1.

Tip: to connect the power, pump & relay, you can use a connector strip. This will make your connections much more reliable & sturdy.

IoT Cloud Setup

If you are new to the Arduino IoT Cloud, please refer to the Getting Started Guide or visit the full documentation to learn more about the service.

Begin by navigating to the Arduino IoT Cloud. You will need to have a registered account with Arduino to use it. Follow the steps below to set up the Arduino IoT Cloud.

1. Create a new Thing, and select/configure the MKR WiFi 1010 board. Note that the board needs to be connected to your computer during this setup.

2. Create variables according to the table below:

NameData TypeFunctionPermission
wateringbooleanActivate / de-activate pumpRead & Write
waterTimeintHow long the pump should run (seconds)Read & Write
moistureintRead moistureRead Only
temperaturefloatRead temperatureRead Only
humidityfloatRead humidityRead Only

3. Enter your credentials to your Wi-Fi network in the network section.

4. Your Thing overview should now look like the following:

Thing overview complete.
Thing overview complete.

5. Go to the sketch tab, and use the following code:

1/*
2 MKR IoT Carrier Smart Garden Setup Project
3
4 A setup that allows for remote/local control of
5 a pump, as well as reading sensors (moisture,
6 temperature, humidity).
7
8 Built using the Arduino IoT Cloud service.
9
10 Components used:
11 - MKR WiFi 1010
12 - MKR IoT Carrier
13 - Moisture Sensor
14 - 5V water pump
15 - USB Adapter with 2x slots
16 - Micro USB Cable
17 - Open ended USB Cable
18 - Grove cable
19
20 Code by (c) Alessandro Ranelucci for Arduino.
21*/
22
23#include "arduino_secrets.h"
24#include "thingProperties.h"
25
26#include <Arduino_MKRIoTCarrier.h>
27#include <Arduino_OplaUI.h>
28
29const int moistPin = A6;
30
31unsigned long startedWatering;
32
33MKRIoTCarrier opla;
34CycleWidgetsApp app;
35Gauge2_Widget moistureWidget;
36Bool_Widget wateringToggleWidget;
37
38void setup() {
39 Serial.begin(9600);
40 delay(1500);
41
42 // Make sure the pump is not running
43 stopWatering();
44
45 // Connect to Arduino IoT Cloud
46 initProperties();
47 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
48 setDebugMessageLevel(4);
49 ArduinoCloud.printDebugInfo();
50
51 // Configure widgets
52 moistureWidget.attachValue(moisture);
53 moistureWidget.setTitle("MOISTURE");
54 moistureWidget.setRange(0, 100);
55 moistureWidget.setDigits(0);
56 moistureWidget.setSuffix(" %");
57 moistureWidget.setReadOnly(true);
58
59 wateringToggleWidget.attachValue(watering);
60 wateringToggleWidget.setTitle("PUMP");
61 wateringToggleWidget.onValueChange(onWateringChange);
62
63 // Initialize Oplà
64 CARRIER_CASE = true;
65 opla.begin();
66
67 // Initialize the widget application
68 app.begin(opla);
69 app.addWidget(moistureWidget);
70 app.addWidget(wateringToggleWidget);
71}
72
73void loop() {
74 ArduinoCloud.update();
75 app.loop();
76
77 // Read the sensor and convert its value to a percentage
78 // (0% = dry; 100% = wet)
79 int raw_moisture = analogRead(moistPin);
80 moisture = map(raw_moisture, 0, 1023, 0, 100);
81
82 temperature = opla.Env.readTemperature();
83 humidity = opla.Env.readHumidity();
84
85 // Set the LED color according to the moisture percentage
86 if (moisture > 40) {
87 opla.leds.setPixelColor(1, 50, 0 , 0); // green
88 } else if (moisture > 10) {
89 opla.leds.setPixelColor(1, 50, 50 , 0); // yellow
90 } else {
91 opla.leds.setPixelColor(1, 0, 50 , 0); // red
92 }
93 opla.leds.show();
94
95 // Stop watering after the configured duration
96 if (watering && (millis() - startedWatering) >= waterTime*1000) {
97 stopWatering();
98 }
99
100 delay(100);
101}
102
103// This function is triggered whenever the server sends a change event,
104// which means that someone changed a value remotely and we need to do
105// something.
106void onWateringChange() {
107 if (watering) {
108 startWatering();
109 } else {
110 stopWatering();
111 }
112}
113
114void startWatering () {
115 watering = true;
116 startedWatering = millis();
117 opla.Relay2.open();
118}
119
120void stopWatering () {
121 watering = false;
122 opla.Relay2.close();
123}
124
125void onWaterTimeChange() {
126 // Add your code here to act upon WaterTime change
127}

6. Upload the code. When successful, you can navigate over to the "Dashboards" section. Create a new dashboard.

7. Inside the dashboard view, click on "Add" then "Things" and select your Thing. This will generate a list of widgets and you can click on "Create Widget" to complete it. You should now see something similar to this dashboard:

Dashboard overview.
Dashboard overview.

Once you see the values changing, we know that the connection is successful, and we can monitor and interact with our device.

In this dashboard, we replaced some of the widgets with nicer representations, like gauges and percentage.

Final Setup

We have now assembled the hardware + configured the Arduino IoT Cloud, and we are ready to start using our setup. Now, let's start using it.

1. If you have confirmed that the connection works, we can unplug the setup from the computer, and move it to the plant we want to monitor.

2. Place the moisture sensor into the soil of the plant.

Moisture sensor in a plant.
Moisture sensor in a plant.

3. Place the pump inside a water container. Attach the plastic pipe to the pump, and place the other end into the plant pot. Place the MKR IoT Carrier next to the plant. Your setup could now look like this:

Plant setup.
Plant setup.

4. Finally, plug in the USB adapter into the wall. This will now power the MKR IoT Carrier, which should now connect to the IoT Cloud, via your Wi-Fi network. And that is it, you now have a Smart Garden setup!

Usage

Let's take a look at what our Smart Garden can do. To control it, we can either use the dashboard in the Arduino IoT Cloud, or the Arduino Remote app (Playstore / Appstore).

Control and monitor your Smart Garden!

In this dashboard, we have also added a chart widget for each of the variables we monitor.

Watering: to activate the pump, do the following:

  • Select number of seconds that you want the pump to run for.
  • Click on the switch widget. The pump will now run for
    x
    amount of seconds, and then it will turn off.
  • You can also activate the pump through the MKR IoT Carrier. This is done via the touch buttons.

Moisture: monitor the moisture of your plant: if it is low, turn on the pump, and watch the moisture levels rise. The moisture of your plant can be viewed in the cloud dashboard, on the carrier's display or through the LED indiciator (red is bad, green is good).

Display, buttons: you can also view the moisture level locally, and activate the pump through the capacitive buttons.

Temperature: check temperature levels. Note that this may be inaccurate if placed directly in a sunny window!

Humidity: measure the relative humidity of your room. Some plants like it dry; some doesn't!

Conclusion

With a smart garden setup, you can easily monitor the environment of your plant, and water it remotely. In this tutorial, we have gone through the basic elements needed for achieving just that: but there are more things you can do. Below is a list of some fun ideas that you can do with your plant:

  • Automatic watering - instead of watering your plant remotely, you can also activate the pump automatically whenever moisture drops too low. We do however think it is more fun to control it from a phone, but the choice is yours.
  • Cooling/heating fan - you can use the other relay to connect a cooling/heating fan. This can help you bring the temperature to a perfect level (some plants like it cold, some hot).
  • Humidifier - a humidifier is an awesome component that increases the humidity (a perfect combo with the humidity sensor).
  • UV lights - a UV light allows you to grow plants even when there's no natural sun light.

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.