Mechatronics LAB
Published © Apache-2.0

Interface raindrop sensor to NodeMcu| For Beginner

Interface raindrop sensor to NodeMcu| For Beginner

BeginnerFull instructions provided1 hour6,561
Interface raindrop sensor to NodeMcu| For Beginner

Story

Read more

Schematics

raindrop sensor

Code

Code

Python
import machine
import time

sensor_pin = machine.A0
enable_pin = machine.Pin(13, machine.Pin.OUT)  # Adjust pin as per your setup

def connect_wifi(ssid, password):
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    while not wlan.isconnected():
        pass
    print("WiFi connected")

def read_sensor():
    sensor_value = sensor_pin.read()
    
    # Adjust sensor value range based on calibration
    sensor_value = sensor_value if 150 <= sensor_value <= 440 else 1023
    
    return sensor_value

def main():
    connect_wifi("your_wifi_ssid", "your_wifi_password")
    
    while True:
        sensor_value = read_sensor()
        
        if sensor_value >= 20:
            print("Rain detected")
            enable_pin.on()
        else:
            print("No rain detected")
            enable_pin.off()
        
        time.sleep(1)  # Adjust delay as needed

if __name__ == "__main__":
    main()

Code

Arduino
#include <ESP8266WiFi.h>

const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";

WiFiClient client;

int sensorPin = A0;
int enablePin = D7; // Adjust as per your setup
int sensorValue = 0;

void setup() {
  pinMode(enablePin, OUTPUT);
  Serial.begin(115200);
  delay(10);
  
  WiFi.begin(ssid, password);
  Serial.println();
  Serial.println("Connecting to WiFi...");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
}

void loop() {
  sensorValue = analogRead(sensorPin);
  
  // Adjust sensorValue range based on your sensor calibration
  sensorValue = constrain(sensorValue, 150, 440);
  sensorValue = map(sensorValue, 150, 440, 1023, 0);
  
  if (sensorValue >= 20) {
    Serial.println("Rain detected");
    digitalWrite(enablePin, HIGH);
  } else {
    Serial.println("No rain detected");
    digitalWrite(enablePin, LOW);
  }
  
  delay(1000); // Adjust delay as needed
}

Credits

Mechatronics LAB

Mechatronics LAB

75 projects • 44 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .

Comments