For this tutorial, you’ll need:
- Soil Moisture Sensor with module
- Arduino UNO
- 2 x Resistor 330E
- BreadBoard
- Jumper Wires
- Led 5MM
- BDW93C
- Water Pump
- 2x Plastic Tubes
- Cup of water
Watch this full video:
Needed Codes and files for this tutorial:
Soil Sensor Test:
int soilPin = A0; int outputValue = 0; void setup() { // put your setup code here, to run once: pinMode(soilPin, INPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: outputValue = analogRead(soilPin); Serial.println(outputValue); delay(1000); outputValue = map(outputValue, 550, 10, 0, 100); Serial.println(outputValue); delay(5000); }
Pump with Potentiometer Test:
int soilPin = A0; int outputValue = 0; int pump = 9; void setup() { // put your setup code here, to run once: pinMode(soilPin, INPUT); pinMode(pump, OUTPUT); digitalWrite(9, LOW); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: outputValue = analogRead(soilPin); Serial.println(outputValue); outputValue = map(outputValue, 0, 1023, 0, 255); Serial.println(outputValue); analogWrite(pump, outputValue); delay(1000); // outputValue = map(outputValue, 550, 10, 0, 100); // Serial.println(outputValue); // delay(5000); }
i2C Scanner Sketch:
include <wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning…");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); }
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
LCD Test:
include "LCD.h" include <LiquidCrystal_I2C.h> define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner define BACKLIGHT_PIN 3 define En_pin 2 define Rw_pin 1 define Rs_pin 0 define D4_pin 4 define D5_pin 5 define D6_pin 6 define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); int counter=0; void setup() { lcd.begin (16,2); // <<----- My LCD was 16x2 // Switch on the backlight lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //lcd.home (); // go home lcd.setCursor(0,0); } void loop() { lcd.setCursor(0,0); lcd.print("C.B.ELECTRONICS"); delay(2000); lcd.setCursor(0,1); lcd.print("Lebanese Park"); delay(2000); lcd.clear(); lcd.setCursor(0,0); lcd.print(counter); delay(2000); lcd.clear(); counter ++; }
Download LCD.h
Download LCD.cpp
Full Code:
include "LCD.h" include <LiquidCrystal_I2C.h> define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner define BACKLIGHT_PIN 3 define En_pin 2 define Rw_pin 1 define Rs_pin 0 define D4_pin 4 define D5_pin 5 define D6_pin 6 define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); int soilPin = A0; int outputValue = 0; int percentageSoil = 0; int pump =9; int PWM_pump = 0; int dry = 40; int wet = 65; void setup() { // put your setup code here, to run once: lcd.begin (16,2); // <<----- My LCD was 16x2 // Switch on the backlight lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //lcd.home (); // go home lcd.setCursor(0,0); pinMode(soilPin, INPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: lcd.setCursor(0,0); outputValue = analogRead(soilPin); Serial.println(outputValue); percentageSoil = map(outputValue, 550, 10, 0, 100); // pourcentage Soil Sensor Serial.println(percentageSoil); lcd.print("Humidity:"); lcd.print(percentageSoil); lcd.print("%"); if(percentageSoil<=dry) { lcd.setCursor(0,1); lcd.print("Pump ON"); // turn on the pumpPWM_pump = map(outputValue, 550, 10, 255, 0); analogWrite(pump, PWM_pump); //digitalWrite(pump, HIGH);
} if(percentageSoil>=wet) { lcd.clear(); lcd.print("Humidity:"); lcd.print(percentageSoil); lcd.setCursor(0,1); lcd.print("Pump OFF"); // do nothinganalogWrite(pump, 0);
} }