Introduction
This experiment shows how to monitor the voltage of a battery using an Arduino and display the level on the Serial Monitor.
Components Needed
- Arduino
- Battery (e.g., 9V)
- Voltage Divider Resistor
Circuit Setup
- Use a voltage divider circuit to scale down the battery voltage to a safe range for the Arduino analog input.
Code for Battery Level Monitoring
Upload the following code to your Arduino to monitor the battery level:
const int analogPin = A0; // Pin connected to the voltage divider
int batteryLevel;
void setup() {
Serial.begin(9600);
}
void loop() {
batteryLevel = analogRead(analogPin);
float voltage = (batteryLevel / 1023.0) * 5.0; // Convert to voltage
Serial.print("Battery Voltage: ");
Serial.println(voltage);
delay(1000);
}
Explanation
The Arduino reads the voltage from the voltage divider and calculates the battery level by converting the analog value to a voltage.
Troubleshooting
- If the values seem off, check the resistor values in the voltage divider circuit.
- Ensure the battery is connected correctly to the circuit.