0% found this document useful (0 votes)
3 views

code

The document contains an Arduino code that interfaces with a rain sensor and a light-dependent resistor (LDR) to control a relay and a motor. It initializes an LCD display to show rain and light values, and activates or deactivates components based on predefined thresholds. The setup includes pin configurations and a loop that continuously reads sensor values and updates the display accordingly.

Uploaded by

daoquocthaikn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

code

The document contains an Arduino code that interfaces with a rain sensor and a light-dependent resistor (LDR) to control a relay and a motor. It initializes an LCD display to show rain and light values, and activates or deactivates components based on predefined thresholds. The setup includes pin configurations and a loop that continuously reads sensor values and updates the display accordingly.

Uploaded by

daoquocthaikn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int rainSensorPin = A0;


const int ldrPin = A1;

const int relayPin = 10;


const int motorIn1 = 5;
const int motorIn2 = 6;
const int ledPin = 11;

int rainThreshold = 500;


int lightThreshold = 600;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // hoặc lcd.begin(20, 4) tùy theo loại LCD bạn đang dùng
lcd.backlight();

pinMode(relayPin, OUTPUT);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(ledPin, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("He thong dang chay");
delay(2000);
lcd.clear();
}

void loop() {
int rainValue = analogRead(rainSensorPin);
int lightValue = analogRead(ldrPin);

lcd.setCursor(0, 0);
lcd.print("Rain: ");
lcd.print(rainValue);

lcd.setCursor(0, 1);
lcd.print("Light: ");
lcd.print(lightValue);

if (rainValue < rainThreshold) {


digitalWrite(relayPin, HIGH);
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
digitalWrite(ledPin, LOW);
}
if (lightValue < lightThreshold) {
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
} else {
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
}

delay(1000);
}

You might also like