0% found this document useful (0 votes)
7 views3 pages

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)
7 views3 pages

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

#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() {
[Link](9600);
[Link](16, 2); // hoặc [Link](20, 4) tùy theo loại LCD bạn đang dùng
[Link]();

pinMode(relayPin, OUTPUT);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(ledPin, OUTPUT);
[Link](0, 0);
[Link]("He thong dang chay");
delay(2000);
[Link]();
}

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

[Link](0, 0);
[Link]("Rain: ");
[Link](rainValue);

[Link](0, 1);
[Link]("Light: ");
[Link](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