Skip to content

Instantly share code, notes, and snippets.

@rambo
Created March 27, 2014 19:08
Show Gist options
  • Save rambo/9815572 to your computer and use it in GitHub Desktop.
Save rambo/9815572 to your computer and use it in GitHub Desktop.
Arduino code for soil humidity and LDR (light measurement)
// http://forum.arduino.cc/index.php/topic,37975.0.html
#define moisture_input A0
#define divider_top 7
#define divider_bottom 8
int SoilMoisture(){
int reading;
// set driver pins to outputs
pinMode(divider_top,OUTPUT);
pinMode(divider_bottom,OUTPUT);
// drive a current through the divider in one direction
digitalWrite(divider_top,HIGH);
digitalWrite(divider_bottom,LOW);
// wait a moment for capacitance effects to settle
delay(1000);
// take a reading
reading=analogRead(moisture_input);
// reverse the current
digitalWrite(divider_top,LOW);
digitalWrite(divider_bottom,HIGH);
// give as much time in 'revers'e as in 'forward'
delay(1000);
// stop the current
digitalWrite(divider_bottom,LOW);
pinMode(divider_top,INPUT);
pinMode(divider_bottom,INPUT);
return reading;
}
void setup() {
Serial.begin(115200);
pinMode(4,OUTPUT);
Serial.println("Booted");
}
void loop() {
int light = analogRead(1);
//Serial.println(SoilMoisture(), DEC);
Serial.println(light, DEC);
if (light > 300)
{
digitalWrite(4,HIGH);
}
else
{
digitalWrite(4,LOW);
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment