Components and supplies
Ultrasonic Sensor - HC-SR04 (Generic)
Water pump
OLED Character Display Module 16 x 2 Green
Solderless Breadboard Full Size
piezo speaker
Resistor 330 ohm
Arduino UNO
Relay Module (Generic)
BME280/BMP280
Gravity: Analog Soil Moisture Sensor For Arduino
LED (generic)
Dual bidirectional I2C-bus and SMBus voltage-level translator(PCA9306)
Apps and platforms
Arduino IDE
Project description
Code
AutoWatering.ino
c_cpp
It says a lot, but I'm just measuring the soil humidity and watering it. It's so easy!
1#include <Wire.h> 2//BME280 3#include <Adafruit_BME280.h> 4//OLED 5#include <I2CLiquidCrystal.h> 6 7//------------------------------ 8 9//BME280 10#define I2C_SCL 5 11#define I2C_SDA 4 12Adafruit_BME280 bme; 13 14//OLED 15#define I2C_ADDR 0x3c 16#define BRIGHT 127 17I2CLiquidCrystal oled(I2C_ADDR, (uint8_t)BRIGHT); 18 19//Music 20#define BEAT 300 21#define PIN 10 22 23#define DO 262 24#define RE 294 25#define MI 330 26#define FA 349 27#define SO 392 28#define RA 440 29#define SI 494 30#define HDO 523 31 32//---------------------- 33 34//Timer 35int counter = 0; 36 37//Ultrasonic Sensor 38int Trig = 8; 39int Echo = 9; 40int Duration; 41float Distance; 42 43//LED 44int led = 7; 45 46//misture sensor 47int water_count = 0; 48 49//relay 50int relay = 13; 51 52void setup() { 53 //BME280 54 Serial.begin(9600); 55 Wire.begin(); 56 if (!bme.begin(0x76)) { 57 Serial.println("Could not find a valid BME280 sensor, check wiring!"); 58 while (1); 59 } 60 delay(100); 61 62 //OLED 63 oled.begin(16, 2); 64 oled.display(); 65 oled.clear(); 66 oled.noBlink(); 67 oled.noCursor(); 68 oled.home(); 69 oled.print("Wait a minutes."); 70 71 //Ultrasonic Sensor 72 pinMode(Trig,OUTPUT); 73 pinMode(Echo,INPUT); 74 75 //LED 76 pinMode(led,OUTPUT); 77 78 //relay 79 pinMode(relay, OUTPUT); 80} 81 82void loop() { 83 84 //Check temperature, humidity, and soil humidity once a minute. 85 if(counter == 1){ 86 ckeckBME280(); 87 checkWater(); 88 checkMoisture(); 89 } 90 91 delay(10000);//10sec 92 counter++; 93 94 if(counter >= 6){ 95 counter = 0; 96 } 97 98 checkWater(); 99} 100 101//BME280 102void ckeckBME280(){ 103 // Get data from BME280 sensor 104 float t = bme.readTemperature(); // C 105 float h = bme.readHumidity(); // % 106 float p = bme.readPressure() / 100.0F; // hPa 107 Serial.print("Temperature:"); 108 Serial.println(t); 109 Serial.print("Humidity:"); 110 Serial.println(h); 111 Serial.print("Pressure:"); 112 Serial.println(p); 113 Serial.println(); 114 115 oled.setCursor(0, 0); 116 oled.print(String(t)+"C "); 117 oled.print(String(h)+"% "); 118} 119 120//Ultrasonic Sensor 121void checkWater(){ 122 //Check the water level in the bucket. 123 digitalWrite(Trig,LOW); 124 delayMicroseconds(1); 125 digitalWrite(Trig,HIGH); 126 delayMicroseconds(11); 127 digitalWrite(Trig,LOW); 128 Duration = pulseIn(Echo,HIGH); 129 130 if (Duration>0) { 131 Distance = Duration/2; 132 Distance = Distance*340*100/1000000; // ultrasonic speed is 340m/s = 34000cm/s = 0.034cm/us 133 Serial.print(Distance); 134 Serial.println(" cm"); 135 136 if(Distance > 12){ 137 digitalWrite(led, HIGH); 138 }else{ 139 digitalWrite(led, LOW); 140 } 141 142 } 143} 144 145//moisture sensor 146void checkMoisture(){ 147 //Measure soil humidity 148 int moisture = analogRead(A0); 149 Serial.print("Moisture Sensor Value:"); 150 Serial.println(moisture); 151 oled.setCursor(0, 1); 152 oled.print("Moisture: " + String(moisture) + " "); 153 154 if(moisture <= 300){ 155 water_count++; 156 if(water_count == 5){//To wait for the water to go through the pot. 157 watering(); 158 water_count = 0; 159 } 160 } 161} 162 163 164//Watering 165void watering(){ 166 digitalWrite(relay, HIGH); 167 delay(2000); 168 digitalWrite(relay, LOW); 169 delay(8000); 170 completeWatering(); 171 counter++; 172} 173 174//Music 175void completeWatering(){ 176 //Let them know that watering is complete. 177 oled.setCursor(0, 0); 178 oled.print(" Thank you! "); 179 oled.setCursor(0, 1); 180 oled.print(" (^ O ^)/ "); 181 182 tone(PIN,DO,BEAT) ; // C 183 delay(BEAT) ; 184 tone(PIN,RE,BEAT) ; // D 185 delay(BEAT) ; 186 tone(PIN,MI,1200) ; // E 187 delay(BEAT) ; 188 delay(BEAT) ; 189 delay(BEAT) ; 190 tone(PIN,RE,BEAT) ; // D 191 delay(BEAT) ; 192 tone(PIN,DO,BEAT) ; // C 193 delay(BEAT) ; 194 delay(BEAT) ; 195 tone(PIN,DO,BEAT) ; // C 196 delay(BEAT) ; 197 tone(PIN,RE,BEAT) ; // D 198 delay(BEAT) ; 199 tone(PIN,MI,BEAT) ; // E 200 delay(BEAT) ; 201 tone(PIN,RE,BEAT) ; // D 202 delay(BEAT) ; 203 tone(PIN,DO,BEAT) ; // C 204 delay(BEAT) ; 205 tone(PIN,RE,1200) ; // D 206 delay(BEAT) ; 207 delay(BEAT) ; 208 delay(4400); 209 210 counter++; 211 oled.setCursor(0, 0); 212 oled.print(" "); 213 oled.setCursor(0, 1); 214 oled.print(" "); 215 ckeckBME280(); 216 int moisture = analogRead(A0); 217 oled.setCursor(0, 1); 218 oled.print("Moisture: " + String(moisture) + " "); 219} 220
AutoWatering.ino
c_cpp
It says a lot, but I'm just measuring the soil humidity and watering it. It's so easy!
1#include <Wire.h> 2//BME280 3#include <Adafruit_BME280.h> 4//OLED 5#include 6 <I2CLiquidCrystal.h> 7 8//------------------------------ 9 10//BME280 11#define 12 I2C_SCL 5 13#define I2C_SDA 4 14Adafruit_BME280 bme; 15 16//OLED 17#define 18 I2C_ADDR 0x3c 19#define BRIGHT 127 20I2CLiquidCrystal oled(I2C_ADDR, (uint8_t)BRIGHT); 21 22//Music 23#define 24 BEAT 300 25#define PIN 10 26 27#define DO 262 28#define RE 294 29#define MI 30 330 31#define FA 349 32#define SO 392 33#define RA 440 34#define SI 494 35#define 36 HDO 523 37 38//---------------------- 39 40//Timer 41int counter = 0; 42 43//Ultrasonic 44 Sensor 45int Trig = 8; 46int Echo = 9; 47int Duration; 48float Distance; 49 50//LED 51int 52 led = 7; 53 54//misture sensor 55int water_count = 0; 56 57//relay 58int relay 59 = 13; 60 61void setup() { 62 //BME280 63 Serial.begin(9600); 64 Wire.begin(); 65 66 if (!bme.begin(0x76)) { 67 Serial.println("Could not find a valid BME280 68 sensor, check wiring!"); 69 while (1); 70 } 71 delay(100); 72 73 //OLED 74 75 oled.begin(16, 2); 76 oled.display(); 77 oled.clear(); 78 oled.noBlink(); 79 80 oled.noCursor(); 81 oled.home(); 82 oled.print("Wait a minutes."); 83 84 85 //Ultrasonic Sensor 86 pinMode(Trig,OUTPUT); 87 pinMode(Echo,INPUT); 88 89 90 //LED 91 pinMode(led,OUTPUT); 92 93 //relay 94 pinMode(relay, OUTPUT); 95} 96 97void 98 loop() { 99 100 //Check temperature, humidity, and soil humidity once a minute. 101 102 if(counter == 1){ 103 ckeckBME280(); 104 checkWater(); 105 checkMoisture(); 106 107 } 108 109 delay(10000);//10sec 110 counter++; 111 112 if(counter >= 6){ 113 114 counter = 0; 115 } 116 117 checkWater(); 118} 119 120//BME280 121void ckeckBME280(){ 122 123 // Get data from BME280 sensor 124 float t = bme.readTemperature(); // 125 C 126 float h = bme.readHumidity(); // % 127 float p = bme.readPressure() 128 / 100.0F; // hPa 129 Serial.print("Temperature:"); 130 Serial.println(t); 131 132 Serial.print("Humidity:"); 133 Serial.println(h); 134 Serial.print("Pressure:"); 135 136 Serial.println(p); 137 Serial.println(); 138 139 oled.setCursor(0, 0); 140 oled.print(String(t)+"C 141 "); 142 oled.print(String(h)+"% "); 143} 144 145//Ultrasonic Sensor 146void 147 checkWater(){ 148 //Check the water level in the bucket. 149 digitalWrite(Trig,LOW); 150 151 delayMicroseconds(1); 152 digitalWrite(Trig,HIGH); 153 delayMicroseconds(11); 154 155 digitalWrite(Trig,LOW); 156 Duration = pulseIn(Echo,HIGH); 157 158 if (Duration>0) 159 { 160 Distance = Duration/2; 161 Distance = Distance*340*100/1000000; // ultrasonic 162 speed is 340m/s = 34000cm/s = 0.034cm/us 163 Serial.print(Distance); 164 Serial.println(" 165 cm"); 166 167 if(Distance > 12){ 168 digitalWrite(led, HIGH); 169 }else{ 170 171 digitalWrite(led, LOW); 172 } 173 174 } 175} 176 177//moisture sensor 178void 179 checkMoisture(){ 180 //Measure soil humidity 181 int moisture = analogRead(A0); 182 183 Serial.print("Moisture Sensor Value:"); 184 Serial.println(moisture); 185 oled.setCursor(0, 186 1); 187 oled.print("Moisture: " + String(moisture) + " "); 188 189 if(moisture 190 <= 300){ 191 water_count++; 192 if(water_count == 5){//To wait for the water 193 to go through the pot. 194 watering(); 195 water_count = 0; 196 } 197 198 } 199} 200 201 202//Watering 203void watering(){ 204 digitalWrite(relay, HIGH); 205 206 delay(2000); 207 digitalWrite(relay, LOW); 208 delay(8000); 209 completeWatering(); 210 211 counter++; 212} 213 214//Music 215void completeWatering(){ 216 //Let them know 217 that watering is complete. 218 oled.setCursor(0, 0); 219 oled.print(" Thank 220 you! "); 221 oled.setCursor(0, 1); 222 oled.print(" (^ O ^)/ "); 223 224 225 tone(PIN,DO,BEAT) ; // C 226 delay(BEAT) ; 227 tone(PIN,RE,BEAT) 228 ; // D 229 delay(BEAT) ; 230 tone(PIN,MI,1200) ; // E 231 delay(BEAT) 232 ; 233 delay(BEAT) ; 234 delay(BEAT) ; 235 tone(PIN,RE,BEAT) ; // 236 D 237 delay(BEAT) ; 238 tone(PIN,DO,BEAT) ; // C 239 delay(BEAT) 240 ; 241 delay(BEAT) ; 242 tone(PIN,DO,BEAT) ; // C 243 delay(BEAT) 244 ; 245 tone(PIN,RE,BEAT) ; // D 246 delay(BEAT) ; 247 tone(PIN,MI,BEAT) 248 ; // E 249 delay(BEAT) ; 250 tone(PIN,RE,BEAT) ; // D 251 delay(BEAT) 252 ; 253 tone(PIN,DO,BEAT) ; // C 254 delay(BEAT) ; 255 tone(PIN,RE,1200) 256 ; // D 257 delay(BEAT) ; 258 delay(BEAT) ; 259 delay(4400); 260 261 262 counter++; 263 oled.setCursor(0, 0); 264 oled.print(" "); 265 266 oled.setCursor(0, 1); 267 oled.print(" "); 268 ckeckBME280(); 269 270 int moisture = analogRead(A0); 271 oled.setCursor(0, 1); 272 oled.print("Moisture: 273 " + String(moisture) + " "); 274} 275
Downloadable files
Step3
Step3. Temperature, humidity and soil moisture are measured and displayed on the OLED.
Step3

Step2
Step2. An LED tells us when the bucket is empty.
Step2

Step2
Step2. An LED tells us when the bucket is empty.
Step2

Step3
Step3. Temperature, humidity and soil moisture are measured and displayed on the OLED.
Step3

Step1
Step1. Measure the soil moisture and water with a pump if the soil is dry.
Step1

Comments
Only logged in users can leave comments