This circuit is designed to interface an ESP8266 NodeMCU microcontroller with several peripheral devices, including an I2C LCD 16x2 screen, a SIM900A GSM module, and an HC-SR04 ultrasonic sensor. The ESP8266 NodeMCU is responsible for controlling the peripherals, gathering sensor data, and displaying information on the LCD screen. The SIM900A module allows for GSM communication capabilities, and the HC-SR04 sensor is used to measure distances, which can be useful in applications such as water level monitoring in a dam.
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial mySerial(D6, D7);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int FloatSensor = D5;
int buttonState = 1;
const int trigPin1 = D3;
const int echoPin1 = D4;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000;
long duration1;
int distance1;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Wire.begin(D2, D1);
pinMode(FloatSensor, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" WELCOME TO");
lcd.setCursor(0, 1);
lcd.print(" OUR PROJECTS");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" SMART");
lcd.setCursor(0, 1);
lcd.print(" DAM MONITORING");
delay(3000);
lcd.clear();
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}
void loop() {
ultrasonic();
if (buttonState == HIGH) {
Serial.println("WATER LEVEL -HIGH");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" HIGH ALERTS");
lcd.setCursor(0, 1);
lcd.print("WATER LEVEL-");
lcd.setCursor(13, 1);
lcd.print(distance1);
} else {
Serial.println("WATER LEVEL - LOW");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" LOW ALERTS");
lcd.setCursor(0, 1);
lcd.print("WATER LEVEL-");
lcd.setCursor(13, 1);
lcd.print(distance1);
}
if (distance1 < 10) {
// Add action when water level is below threshold
} else {
// Add action when water level is above threshold
}
currentMillis = millis();
if (currentMillis - startMillis >= period) {
startMillis = currentMillis;
}
}
void ultrasonic() {
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.207 / 2;
Serial.println(distance1);
buttonState = digitalRead(FloatSensor);
delay(100);
}
This code initializes and uses the connected components to monitor water levels and display alerts on the LCD screen. The ultrasonic
function is responsible for triggering the HC-SR04 sensor and calculating the distance based on the time it takes for the ultrasonic pulse to return. The main loop checks the water level and updates the LCD display accordingly.