Vin <--------------6V or 12V
+V-------->3V3 Bus
-- ---------->GND Bus
Signal------>A0 (D14)
Pin 3(emitter) --- ------->GND Bus
Pin 1(base). <-------------Resistor-------
Pin 2(collector) <--------- Pump(-)
+Vcc-------------->3V3 Bus
GND-------------->GND Bus
SCL---------------->SCL (D19)
SDA--------------->SDA( D18)
Simplified Code( no Wifi or OLED):
// Simplified Irrigation Controller Program
#define pumpPin 6 //Output pin to turn on pump with transistor
int moisture;
int threshold= 300;
void setup() {
pinMode(pumpPin, OUTPUT);
moisture=0;
Serial.begin(9600);
}
void loop() {
moisture = analogRead(A0);
Serial.println(moisture);
if (moisture < threshold){ // if water needed, pump runs for 10 seconds, then shuts off for 10 seconds.
digitalWrite(pumpPin, HIGH);
delay (10000);
digitalWrite(pumpPin, LOW);
delay(10000);
} else {
digitalWrite(pumpPin, LOW);
}
}
Code Example-Irrigation Controller
#include "thingProperties.h"
#include <Wire.h>. //code for OLED display
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#include <Adafruit_Sensor.h>
void setup() {
pinMode(6, OUTPUT);
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
}
void loop() {
ArduinoCloud.update();
moisture = analogRead(A0);
Serial.println(moisture);
delay(2000);
// OLED Display:
display.clearDisplay();
delay(2000);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println(moisture);
display.display();
delay(500);
}
void onPumpChange() {
// Do something
if (pump) {
digitalWrite(LED, HIGH);
digitalWrite(6, HIGH);
}
else {
digitalWrite(LED, LOW);
digitalWrite(6, LOW);
}
}
Features:
POWER:
Entire unit is powered using a 5V USB charger cube. The 5V goes to Vin/GND on the top left, and also powers the water pump.
3V/GND output at bottom left powers a DHT 11 humidity/temp module(the blue thing), and a photocell for readings of daylight.
Both top and bottom ground rails are tied together.
Signals:
The signal from the moisture sensor(yellow) goes to Pin D34(A0)
The signal from the DHT 11(green) goes to Pin D32
The signal from the photocell(yellow) goes to Pin D35
The output to the pump(blue) is on Pin D33
One wire of the pump goes to +5V; the other pin goes to the collector of the TIP 120 transistor; when the transistor is turned on by the signal from the ESP32, the coilector is grounded, turning on the pump.
-------------------------------
Code:
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/b3bb7643-a022-4018-9309-cb4b16a98177
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
int humidity;
int moisture;
int temperature;
int light;
bool pump;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include "DHT.h"
#define DHTPIN 32 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
#define pumpPin 33. //pin that drives the transistor
DHT dht(DHTPIN, DHTTYPE);
int threshold = 1200; // moisture threshold value to be determined by experiment
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
dht.begin();
// initialize pins
pinMode(32, INPUT); //input from DHT 22
pinMode(33, OUTPUT); // output to turn on transistor
pinMode(34, INPUT); //input from moisture sensor
pinMode(35, INPUT); //input from light sensor
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
moisture = analogRead(34);
light = analogRead(35); //optional photoresistor
humidity = dht.readHumidity();
temperature = dht.readTemperature();
Serial.println(moisture);
Serial.println(humidity);
Serial.println(temperature);
Serial.println(light);
delay(2000);
if (moisture < threshold)
{
digitalWrite(pumpPin, HIGH);
delay(5000);
digitalWrite(pumpPin, LOW);
delay(10000);
} else {
digitalWrite(pumpPin, LOW);
}
}
/*
Since Pump is READ_WRITE variable, onPumpChange() is
executed every time a new value is received from IoT Cloud.
// Add your code here to act upon Pump change
*/
void onPumpChange() {
if (pump) {
digitalWrite(pumpPin, HIGH);
}
else {
digitalWrite(pumpPin, LOW);
}
}
//-------------- Properties Tab
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "e294cc78-3001-453f-a8b2-697c51a3b140";
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = SECRET_DEVICE_KEY; // Secret device password
void onPumpChange();
int humidity;
int light;
int moisture;
int temperature;
bool pump;
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(humidity, READ, 15 * SECONDS, NULL);
ArduinoCloud.addProperty(light, READ, 15 * SECONDS, NULL);
ArduinoCloud.addProperty(moisture, READ, 20 * SECONDS, NULL);
ArduinoCloud.addProperty(temperature, READ, 15 * SECONDS, NULL);
ArduinoCloud.addProperty(pump, READWRITE, ON_CHANGE, onPumpChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);