Devices & Components
SparkFun Soil Moisture Sensor (with Screw Terminals)
Male/Female Jumper Wires
Buzzer
9V battery (generic)
Jumper wires (generic)
Resistor 220 ohm
Mini Breadboard
10 mm Heat Shrink
9V Battery Clip
Hardware & Tools
Laser cutter (generic)
Hot glue gun (generic)
Software & Tools
PlatformIO IDE
Project description
Code
Soil Moisture Sensor code
csharp
This was made with Platform IO, and therefore the user-declared functions are put first.
1int greenLight = 0; //Defining pins 2int yellowLight = 1; 3int redLight = 2; 4int piezoBuzzer = 3; 5int maximumMoistureLevel; //The max moisture level and current moisture levels will be needed for percentage calculations 6int currentMoistureLevel;//Like so: current/max*100 = Moisture level as a percentage 7 8void moistureDetection(){ //Create a function for all the long code, to keep the loop free 9 if(currentMoistureLevel/maximumMoistureLevel <= 0.1){ //If the moisture is below 10% 10 digitalWrite(greenLight, LOW); 11 digitalWrite(yellowLight, LOW); 12 digitalWrite(redLight, HIGH); //Switch on red light, and sound the buzzer 13 tone(piezoBuzzer, 5000, 500); 14 delay(2000); 15 }else if (currentMoistureLevel/maximumMoistureLevel <= 0.3 && currentMoistureLevel/maximumMoistureLevel > 0.1) 16 {//if the moisture level is in between 10 and 30% 17 digitalWrite(greenLight, LOW); 18 digitalWrite(yellowLight, LOW); 19 digitalWrite(redLight, HIGH); //Switch red light on, but don't sound the buzzer 20 }else if (currentMoistureLevel/maximumMoistureLevel <= 0.6 && currentMoistureLevel/maximumMoistureLevel > 0.3) 21 {//if the moisture level is in between 30 and 60% 22 digitalWrite(greenLight, LOW); 23 digitalWrite(yellowLight, HIGH);//Just switch yellow light on 24 digitalWrite(redLight, LOW); 25 } else //Otherwise the moisture level is above 60%, and therefore it's good enough 26 { 27 digitalWrite(greenLight, HIGH);//Switch green light on 28 digitalWrite(yellowLight, LOW); 29 digitalWrite(redLight, LOW); 30 } 31} 32 33void setup() { 34 for (int i = 0; i < 4; i++)//Use a for loop, to not have to initiate all the pins by hand 35 { 36 pinMode(i, OUTPUT); 37 } 38 pinMode (A0, INPUT); //A0 is the pin used for the Soil Moisture Sensor 39 maximumMoistureLevel = analogRead(A0); 40 tone(piezoBuzzer, 5000, 500); 41 delay(200); 42 tone(piezoBuzzer, 6000, 500);//Make a sound to show that the program has been initiated. 43 delay(600); 44} 45 46void loop() { 47 currentMoistureLevel = analogRead(A0); //Keep renewing the readings for the current moisture level 48 moistureDetection(); 49 delay(100); //Short delay to not overload the program 50 Serial.println(currentMoistureLevel);//Just so you can see the moisture level as a reading between 0-1023 51 52 53}
Soil Moisture Sensor code
csharp
This was made with Platform IO, and therefore the user-declared functions are put first.
1int greenLight = 0; //Defining pins 2int yellowLight = 1; 3int redLight = 2; 4int piezoBuzzer = 3; 5int maximumMoistureLevel; //The max moisture level and current moisture levels will be needed for percentage calculations 6int currentMoistureLevel;//Like so: current/max*100 = Moisture level as a percentage 7 8void moistureDetection(){ //Create a function for all the long code, to keep the loop free 9 if(currentMoistureLevel/maximumMoistureLevel <= 0.1){ //If the moisture is below 10% 10 digitalWrite(greenLight, LOW); 11 digitalWrite(yellowLight, LOW); 12 digitalWrite(redLight, HIGH); //Switch on red light, and sound the buzzer 13 tone(piezoBuzzer, 5000, 500); 14 delay(2000); 15 }else if (currentMoistureLevel/maximumMoistureLevel <= 0.3 && currentMoistureLevel/maximumMoistureLevel > 0.1) 16 {//if the moisture level is in between 10 and 30% 17 digitalWrite(greenLight, LOW); 18 digitalWrite(yellowLight, LOW); 19 digitalWrite(redLight, HIGH); //Switch red light on, but don't sound the buzzer 20 }else if (currentMoistureLevel/maximumMoistureLevel <= 0.6 && currentMoistureLevel/maximumMoistureLevel > 0.3) 21 {//if the moisture level is in between 30 and 60% 22 digitalWrite(greenLight, LOW); 23 digitalWrite(yellowLight, HIGH);//Just switch yellow light on 24 digitalWrite(redLight, LOW); 25 } else //Otherwise the moisture level is above 60%, and therefore it's good enough 26 { 27 digitalWrite(greenLight, HIGH);//Switch green light on 28 digitalWrite(yellowLight, LOW); 29 digitalWrite(redLight, LOW); 30 } 31} 32 33void setup() { 34 for (int i = 0; i < 4; i++)//Use a for loop, to not have to initiate all the pins by hand 35 { 36 pinMode(i, OUTPUT); 37 } 38 pinMode (A0, INPUT); //A0 is the pin used for the Soil Moisture Sensor 39 maximumMoistureLevel = analogRead(A0); 40 tone(piezoBuzzer, 5000, 500); 41 delay(200); 42 tone(piezoBuzzer, 6000, 500);//Make a sound to show that the program has been initiated. 43 delay(600); 44} 45 46void loop() { 47 currentMoistureLevel = analogRead(A0); //Keep renewing the readings for the current moisture level 48 moistureDetection(); 49 delay(100); //Short delay to not overload the program 50 Serial.println(currentMoistureLevel);//Just so you can see the moisture level as a reading between 0-1023 51 52 53}
Downloadable files
Wiring Diagram
This is the wiring diagram for the circuitry, made with Fritzing, which by the way now costs $12 AUD :(
Wiring Diagram

Wiring Diagram
This is the wiring diagram for the circuitry, made with Fritzing, which by the way now costs $12 AUD :(
Wiring Diagram

Documentation
tinkerCadmodel
this is the casing model, made on tinker cad. I don't suggest 3D printing
tinkerCadmodel
Step By Step Instruction on how to make the casing.
Illustrated instruction manual, for building the casing.
Step By Step Instruction on how to make the casing.
Laser-Cutting File - Adobe Illustrator
Laser-Cutting File - Adobe Illustrator
Step By Step Instruction on how to make the casing.
Illustrated instruction manual, for building the casing.
Step By Step Instruction on how to make the casing.
tinkerCadmodel
this is the casing model, made on tinker cad. I don't suggest 3D printing
tinkerCadmodel
Comments
Only logged in users can leave comments