Back to Parent

// Moisture sensor and LED light
int moisture_pin = A5; 
int led_pin = D4;

void setup() {
    pinMode(led_pin, OUTPUT); // Set the LED pin as output
    pinMode(moisture_pin, INPUT);  // Set the moisture sensor pin as input
}

void loop() {
    // Reading the moisture level
    int moisture_analog = analogRead(moisture_pin); 

     // Calculate moisture percentage
    float moisture_percentage = (moisture_analog / 4095.00) * 100;

    // Turn on the LED if moisture is 50% or more
    if (moisture_percentage >= 50) {
        digitalWrite(led_pin, HIGH);
    } else {
        digitalWrite(led_pin, LOW);
    }

    // Delay for 2 seconds before the next loop
    delay(2000);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0