This is a simple Moisture Tester that I made using an Arduino MEGA, a 10 Element Bar Graph LED Array (Part Number: HDSP-4832), and some Galvanized Nails as sensor probes.
Essentially, the Arduino measures the resistance between the nails and sends that data to the code which determines how many LED's to light up. The higher the number, the more LED's light up. Simple!
Here's a picture of the circuit.
And a screenshot of the Serial Monitor output from the Arduino Code:
And here's the code:
------------------------------
// Arduino Simple Moisture Tester
// By Paul D'Intino
// Feb 11 2010
//initialize values
int moistureSensor = 0;
int moisture_val;
int lightSwitch1 = 35;
int lightSwitch2 = 37;
int lightSwitch3 = 39;
int lightSwitch4 = 41;
int lightSwitch5 = 43;
int lightSwitch6 = 45;
int lightSwitch7 = 47;
int lightSwitch8 = 49;
int lightSwitch9 = 51;
int lightSwitch10 = 53;
void setup() {
Serial.begin(9600); //open serial port
//setup Digital pins as Outputs
pinMode (lightSwitch1, OUTPUT);
pinMode (lightSwitch2, OUTPUT);
pinMode (lightSwitch3, OUTPUT);
pinMode (lightSwitch4, OUTPUT);
pinMode (lightSwitch5, OUTPUT);
pinMode (lightSwitch6, OUTPUT);
pinMode (lightSwitch7, OUTPUT);
pinMode (lightSwitch8, OUTPUT);
pinMode (lightSwitch9, OUTPUT);
pinMode (lightSwitch10, OUTPUT);
//set all the LEDS to OFF on startup
digitalWrite (lightSwitch1, LOW);
digitalWrite (lightSwitch2, LOW);
digitalWrite (lightSwitch3, LOW);
digitalWrite (lightSwitch4, LOW);
digitalWrite (lightSwitch5, LOW);
digitalWrite (lightSwitch6, LOW);
digitalWrite (lightSwitch7, LOW);
digitalWrite (lightSwitch8, LOW);
digitalWrite (lightSwitch9, LOW);
digitalWrite (lightSwitch10, LOW);
}
void loop() {
moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes
Serial.print("moisture sensor reads "); //output the value from the sensors
Serial.println( moisture_val );
//Step 1 - setup the maximum value with all LEDS on
if (moisture_val >= 1000 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, HIGH);
digitalWrite(lightSwitch5, HIGH);
digitalWrite(lightSwitch6, HIGH);
digitalWrite(lightSwitch7, HIGH);
digitalWrite(lightSwitch8, HIGH);
digitalWrite(lightSwitch9, HIGH);
digitalWrite(lightSwitch10, HIGH);
delay(10);
}
//Step 2
else if (moisture_val >= 800 && moisture_val <= 900 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, HIGH);
digitalWrite(lightSwitch5, HIGH);
digitalWrite(lightSwitch6, HIGH);
digitalWrite(lightSwitch7, HIGH);
digitalWrite(lightSwitch8, HIGH);
digitalWrite(lightSwitch9, HIGH);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step 3
else if (moisture_val >= 700 && moisture_val <= 800 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, HIGH);
digitalWrite(lightSwitch5, HIGH);
digitalWrite(lightSwitch6, HIGH);
digitalWrite(lightSwitch7, HIGH);
digitalWrite(lightSwitch8, HIGH);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step 4
else if (moisture_val >= 600 && moisture_val <= 700 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, HIGH);
digitalWrite(lightSwitch5, HIGH);
digitalWrite(lightSwitch6, HIGH);
digitalWrite(lightSwitch7, HIGH);
digitalWrite(lightSwitch8, LOW);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step 5
else if (moisture_val >= 500 && moisture_val <= 600 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, HIGH);
digitalWrite(lightSwitch5, HIGH);
digitalWrite(lightSwitch6, HIGH);
digitalWrite(lightSwitch7, LOW);
digitalWrite(lightSwitch8, LOW);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step 6
else if (moisture_val >= 400 && moisture_val <= 500 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, HIGH);
digitalWrite(lightSwitch5, HIGH);
digitalWrite(lightSwitch6, LOW);
digitalWrite(lightSwitch7, LOW);
digitalWrite(lightSwitch8, LOW);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step 7
else if (moisture_val >= 300 && moisture_val <= 400 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, HIGH);
digitalWrite(lightSwitch5, LOW);
digitalWrite(lightSwitch6, LOW);
digitalWrite(lightSwitch7, LOW);
digitalWrite(lightSwitch8, LOW);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step 8
else if (moisture_val >= 200 && moisture_val <= 300 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, HIGH);
digitalWrite(lightSwitch4, LOW);
digitalWrite(lightSwitch5, LOW);
digitalWrite(lightSwitch6, LOW);
digitalWrite(lightSwitch7, LOW);
digitalWrite(lightSwitch8, LOW);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step 9
else if (moisture_val >= 100 && moisture_val <= 200 )
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, HIGH);
digitalWrite(lightSwitch3, LOW);
digitalWrite(lightSwitch4, LOW);
digitalWrite(lightSwitch5, LOW);
digitalWrite(lightSwitch6, LOW);
digitalWrite(lightSwitch7, LOW);
digitalWrite(lightSwitch8, LOW);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
//Step10 - setup the minimum value with all LEDS off but one Red LED.
else
{
digitalWrite(lightSwitch1, HIGH);
digitalWrite(lightSwitch2, LOW);
digitalWrite(lightSwitch3, LOW);
digitalWrite(lightSwitch4, LOW);
digitalWrite(lightSwitch5, LOW);
digitalWrite(lightSwitch6, LOW);
digitalWrite(lightSwitch7, LOW);
digitalWrite(lightSwitch8, LOW);
digitalWrite(lightSwitch9, LOW);
digitalWrite(lightSwitch10, LOW);
delay(10);
}
}
6 comments:
Use an array to store the led pins and the code will be more compact, readable and maintenable.
In shortening the code, you might consider changing the loop to the following... it's a little more work each cycle, but easier to read/understand.
void loop() {
moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes
Serial.print("moisture sensor reads "); //output the value from the sensors
Serial.println( moisture_val );
int thisState;
digitalWrite(lightSwitch1, HIGH);
thisState = (moisture_val >= 100) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 200) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 300) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 400) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 500) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 600) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 700) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 800) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
thisState = (moisture_val >= 1000) ? HIGH: LOW;
digitalWrite(lightSwitch2, thisState);
delay(10);
}
Thanks for the feedback guys! Yeah the code was very dirty, just some 'copy and paste' for the afternoon =)
Thank you very much! This I will use my greenhouse project.
Sincerely Anders from Sweden
Hello Anders, Glad I could help =)
Hi, i want to use this project in my academic study. And i need the results as % . So, i have to calibrate my sensor. I do not have enough code knowledge and practice. How can i write these codes.. Can anybody help me? mail: hasanyilmaz_86@hotmail.com
Post a Comment