Devices & Components
Arduino Uno Rev3
Grove - 4-Channel SPDT Relay
4-CHANNEL RELAY CONTROLLER FOR I2C
Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
Gravity: Analog Capacitive Soil Moisture Sensor- Corrosion Resistant
Hardware & Tools
Multitool, Screwdriver
Multitool, Screwdriver
Software & Tools
Arduino IDE
Project description
Code
Code
c_cpp
1const int relayPin = 2; 2const int sensorPin = A0; 3float sensorValue = 0; 4 5void setup() { 6// put your setup code here, to run once: 7Serial.begin(9600); 8//begins communication with serial monitor 9pinMode(relayPin, OUTPUT); 10//sets pin 2 to output 11pinMode(sensorPin, INPUT); 12//sets pin A0 to input 13digitalWrite(relayPin, HIGH); 14//turns on relay, in turn turning on pump 15delay(500); 16//runs pump for 1/2 second 17} 18 19void loop() { 20// put your main code here, to run repeatedly: 21Serial.print("MOISTURE LEVEL: "); 22//prints "MOISTURE LEVEL: " 23sensorValue = analogRead(sensorPin); 24//sets "sensorValue" to input value from A0 25Serial.print(sensorValue); 26//prints moisture level 27if(sensorValue>500) 28{ 29digitalWrite(relayPin, HIGH); 30//if moisture level is above 750, turns off pump 31} 32else 33{ 34digitalWrite(relayPin, LOW); 35//if moisture level is not above 750, turns on pump 36} 37Serial.println(); 38//prints new line for spacing 39delay(1000); 40//waits 1 second before re-checking moisture level 41}
Code
c_cpp
1const int relayPin = 2; 2const int sensorPin = A0; 3float sensorValue = 0; 4 5void setup() { 6// put your setup code here, to run once: 7Serial.begin(9600); 8//begins communication with serial monitor 9pinMode(relayPin, OUTPUT); 10//sets pin 2 to output 11pinMode(sensorPin, INPUT); 12//sets pin A0 to input 13digitalWrite(relayPin, HIGH); 14//turns on relay, in turn turning on pump 15delay(500); 16//runs pump for 1/2 second 17} 18 19void loop() { 20// put your main code here, to run repeatedly: 21Serial.print("MOISTURE LEVEL: "); 22//prints "MOISTURE LEVEL: " 23sensorValue = analogRead(sensorPin); 24//sets "sensorValue" to input value from A0 25Serial.print(sensorValue); 26//prints moisture level 27if(sensorValue>500) 28{ 29digitalWrite(relayPin, HIGH); 30//if moisture level is above 750, turns off pump 31} 32else 33{ 34digitalWrite(relayPin, LOW); 35//if moisture level is not above 750, turns on pump 36} 37Serial.println(); 38//prints new line for spacing 39delay(1000); 40//waits 1 second before re-checking moisture level 41}
Downloadable files
WayinTop
https://github.com/WayinTop/Automatic-Plant-Watering-System-Tutorial/blob/master/4%20Channel%20Relay%20Plant%20Watering%20System%20and%20code/Plant%20Watering%20System%20Tutorial-English.pdf
Comments
Only logged in users can leave comments