// Pin definitions
int mosfetPin = 6; // MOSFET Gate control pin
int ledRed = 9; // Red LED (low voltage indicator)
int ledYellow = 10; // Yellow LED (medium voltage indicator)
int ledGreen = 11; // Green LED (high voltage indicator)
int ledBlue = 12; // Blue LED (load indication)
int analogPin = A0; // Analog pin to read battery voltage
int batteryVoltage = 0; // Variable to store battery voltage
float voltageReading = 0; // Variable to store converted voltage
// Voltage thresholds for the LEDs (adjusted for 1.5V AA battery)
float lowVoltage = 1.0; // Low battery threshold in Volts
float mediumVoltage = 1.2; // Medium battery threshold in Volts
float highVoltage = 1.3; // High battery threshold in Volts
void setup() {
// Set pin modes
pinMode(mosfetPin, OUTPUT); // Set MOSFET control pin as output
pinMode(ledRed, OUTPUT); // Set Red LED as output
pinMode(ledYellow, OUTPUT); // Set Yellow LED as output
pinMode(ledGreen, OUTPUT); // Set Green LED as output
pinMode(ledBlue, OUTPUT); // Set Blue LED as output
Serial.begin(9600); // Initialize serial monitor for debugging
}
void loop() {
// Read the battery voltage from the analog pin
int sensorValue = analogRead(analogPin);
voltageReading = sensorValue * (5.0 / 1023.0); // Convert to voltage (Arduino uses 5V reference)
// Print the battery voltage to the serial monitor for debugging
Serial.print("Battery Voltage: ");
Serial.println(voltageReading);
// Control LEDs based on battery voltage
if (voltageReading < lowVoltage) {
// Low voltage condition, turn off MOSFET, indicate with Red LED
digitalWrite(mosfetPin, LOW); // MOSFET OFF (no load)
digitalWrite(ledBlue, LOW); // Turn off Blue LED (no load)
turnOffLEDs();
digitalWrite(ledRed, HIGH); // Turn on Red LED (low voltage)
}
else if (voltageReading >= lowVoltage && voltageReading < mediumVoltage) {
// Medium voltage condition, turn on MOSFET, indicate with Yellow LED
digitalWrite(mosfetPin, HIGH); // MOSFET ON (apply load)
digitalWrite(ledBlue, HIGH); // Turn on Blue LED (load condition)
turnOffLEDs();
digitalWrite(ledYellow, HIGH); // Turn on Yellow LED (medium voltage)
}
else if (voltageReading >= mediumVoltage && voltageReading < highVoltage) {
// High voltage condition, turn on MOSFET, indicate with Green LED
digitalWrite(mosfetPin, HIGH); // MOSFET ON (apply load)
digitalWrite(ledBlue, HIGH); // Turn on Blue LED (load condition)
turnOffLEDs();
digitalWrite(ledYellow, HIGH); // Turn on Yellow LED (medium voltage)
}
else {
// Full charge condition, turn off MOSFET, indicate with Green LED
digitalWrite(mosfetPin, LOW); // MOSFET OFF (no load)
digitalWrite(ledBlue, LOW); // Turn off Blue LED (no load)
turnOffLEDs();
digitalWrite(ledGreen, HIGH); // Turn on Green LED (high voltage)
}
// Small delay before the next reading
delay(500);
}
// Function to turn off all LEDs
void turnOffLEDs() {
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
}