Untitled
unknown
plain_text
10 months ago
2.9 kB
17
Indexable
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address if needed
// Define pins for sensor, LEDs, and buzzer
int sensorPin = A0; // Analog pin for the pH sensor (simulating temperature here)
int greenLedPin = 2; // Green LED (Neutral)
int yellowLedPin = 3; // Yellow LED (Low pH)
int redLedPin = 4; // Red LED (High pH)
int buzzerPin = 5; // Buzzer pin
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Initializing...");
delay(2000); // Display initial message for 2 seconds
// Set LED and buzzer pins as outputs
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
// Read the sensor value (simulating pH with temperature)
int sensorValue = analogRead(sensorPin);
// Convert the sensor value to voltage (for example, TMP36, adjust as needed)
float voltage = sensorValue * (5.0 / 1023.0); // Assuming 5V and 10-bit ADC
float pHValue = (voltage - 0.5) * 100; // Example for TMP36, adjust for your sensor
// Display the pH value on the LCD (first line)
lcd.clear();
lcd.setCursor(0, 0); // Move to the first line
lcd.print("pH: ");
lcd.print(pHValue);
// Logic for LEDs and buzzer based on pH value
if (pHValue >= 7.00 && pHValue <= 7.80) { // Neutral range
digitalWrite(greenLedPin, HIGH); // Green LED (Neutral)
digitalWrite(yellowLedPin, LOW); // Turn off Yellow LED
digitalWrite(redLedPin, LOW); // Turn off Red LED
digitalWrite(buzzerPin, LOW); // No buzzer
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Status: Good");
}
else if (pHValue < 7.00) { // Below the neutral range
digitalWrite(greenLedPin, LOW); // Turn off Green LED
digitalWrite(yellowLedPin, HIGH); // Yellow LED (Low pH)
digitalWrite(redLedPin, LOW); // Turn off Red LED
digitalWrite(buzzerPin, LOW); // No buzzer
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Status: Bad");
}
else if (pHValue > 7.80) { // Above the neutral range
digitalWrite(greenLedPin, LOW); // Turn off Green LED
digitalWrite(yellowLedPin, LOW); // Turn off Yellow LED
digitalWrite(redLedPin, HIGH); // Red LED (High pH)
digitalWrite(buzzerPin, LOW); // No buzzer
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Status: Bad");
}
// Extreme pH values (0-2 or 12-14)
if ((pHValue >= 0 && pHValue <= 2) || (pHValue >= 12 && pHValue <= 14)) {
digitalWrite(buzzerPin, HIGH); // Turn on buzzer for extreme values
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Status: Bad");
}
delay(1000); // Wait for 1 second before checking again
}
Editor is loading...
Leave a Comment