Solar Panel Monitoring Using Arduino With INA219 Sensor
Solar Panel Power Monitoring Arduino Save Data to MicroSD – In the previous tutorial we learned the basics of how to create a data logger using Arduino to save sensor data into micro SD memory precise time data, hours, seconds, minutes, date, month and year.
Now we will learn how to monitor Arduino solar panels in the form of voltage, current and power. Then the data will be stored in micro SD memory for certain purposes.
A. Tools and Materials
In making the Arduino solar panel monitoring tool and making a power logger for it, how many components are needed, including:
- Arduino Uno
- You can also use Nano, pro mini or pro micro.
- Solar Panel (In this case I am using a solar panel with a maximum capacity of 2W)
- INA219 sensor
- Micro SD Module
- RTC DS3231 (You can also use the DS1307)
- Sufficient jumper cable
In making this Arduino solar panel monitoring device, I will share 2 steps to make this learning easier. The 2 steps are:
- Monitoring the Voltage, Current and Power of the Arduino solar panel
- Creating a Data Logger
How to Monitor Solar Panels (Current, Voltage and Power).
To be able to read the power source in the form of current, voltage and power when monitoring the Arduino solar panel, we need a sensor that can detect and read voltage, current and power.
One of the sensors I used in this tutorial is the INA219 current sensor. I have explained in detail about this sensor in the article How to Access the Arduino INA219 Sensor Module Power Voltage Current Sensor.
Arduino solar panel monitoring circuit
Now let’s assemble the INA219 sensor to Arduino. The series are as follows:
Circuit explanation:
In the Arduino solar panel monitoring circuit above, I connected the solar panel directly to the INA219 sensor and the load that was passed was only a shunt resistor belonging to the INA219 module which was 0.1 Ohm 1%. I did that to get the maximum reading value from the Solar Panel.
The INA219 sensor uses the I2C protocol to communicate with Arduino. So, only 2 pins are used, namely SDA and SCL plus 5V and GND Power pins.
I have explained how the INA219 sensor detects voltage, current and power on the previous page, please read again the article How to Access the INA219 Arduino Sensor Module Power Voltage Current Sensor.
B. Solar Panel Monitor Program
To be able to read the current and voltage power values from the INA219 sensor, we need an additional library that makes it easier for us to communicate using the I2C protocol.
The INA2019 Sensor library is available in the Arduino IDE library manager. Please add it to your Arduino IDE by:
Click the Sketch menu
- Include Library
- Manage Libary.
- Please look for Adafruit INA219
- Click install.
After that, please copy the program below and upload it to Arduino. After uploading, please open the serial monitor to get the results of the current and voltage readings.
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219; // The default address is 0x40, if jumpers A0 and A1 are open
//Adafruit_INA219 ina219 (0x41); // if jumper A0 is connected
// Adafruit_INA219 ina219 (0x44); // if jumper A1 is connected
// Adafruit_INA219 ina219 (0x45); // if jumper A0 and A1 is connected
void setup(void)
{
Serial.begin(9600);
if (! ina219.begin()) {
Serial.println("Failed to get INA219 Sensor");
while (1) {
delay(10);
}
}
// By default, the calibration is at (32V, 2A).
// If you want precise current readings, use settings of 32V, 1A
// ina219.setCalibration_32V_1A ();
// If you want more precision, use the 16V, 400mA settings
ina219.setCalibration_16V_400mA();
}
void loop(void)
{
float v_shunt = 0;
float v_bus = 0;
float current = 0;
float v_load = 0;
float power = 0;
v_shunt = ina219.getShuntVoltage_mV();
v_bus = ina219.getBusVoltage_V();
v_load = v_bus + (v_shunt / 1000);
current = ina219.getCurrent_mA();
power = v_load * current;
Serial.print("V: ");
Serial.print(v_load);
Serial.print(" V ");
Serial.print("I: ");
Serial.print(current);
Serial.print(" mA ");
Serial.print("P: ");
Serial.print(power);
Serial.println(" mW");
delay(100);
}
C. How to Make a Data Logger for Solar Panels
Now we will create a data logger to store the Voltage, Current and Electrical Power data obtained from the INA219 Sensor. The basic data logger series can be seen here.
For the whole circuit, we add the above circuit with two more components, namely the Micro SD module and the DS3231 RTC.
Here is the wiring:
After the circuit is installed, now please copy the program code below and upload it to Arduino.
D. Program Code:
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
File myFile;
RTC_DS3231 rtc;
Adafruit_INA219 ina219; // The default address is 0x40, if jumpers A0 and A1 are open
//Adafruit_INA219 ina219 (0x41); // if jumper A0 is connected
// Adafruit_INA219 ina219 (0x44); // if jumper A1 is connected
// Adafruit_INA219 ina219 (0x45); // if jumper A0 and A1 is connected
int chipselect = 10;
int sensorPin = A0;
int dataSensor = 0;
int sec;
int secBefore;
String nameFile = "data_log.txt";
String clockData;
String dateData;
void setup(void)
{
Serial.begin(9600);
if (!SD.begin(chipselect)) {
Serial.println("Micro SD reading failed!");
Serial.println("* Is the connection correct?");
Serial.println("* Or the memory card is not inserted?");
Serial.println("\n");
while (1) {
delay(10);
}
} else {
Serial.println("MicroSD Memory Card readable.");
}
if (! rtc.begin()) {
Serial.print("RTC Failed! ");
Serial.println("s the connection correct?");
Serial.println("\n");
Serial.flush();
abort();
} else {
Serial.println("RTC Ok.");
}
if (! ina219.begin()) {
Serial.println("INA219 Failed");
while (1) {
delay(10);
}
}
// By default, the calibration is at (32V, 2A).
// If you want precise current readings, use settings of 32V, 1A
// ina219.setCalibration_32V_1A ();
// If you want more precision, use the 16V, 400mA settings
ina219.setCalibration_16V_400mA();
}
void loop(void)
{
DateTime now = rtc.now();
dateData = now.timestamp(DateTime::TIMESTAMP_DATE);
clockData = now.timestamp(DateTime::TIMESTAMP_TIME);
sec = now.second();
float v_shunt = 0;
float v_bus = 0;
float current = 0;
float v_load = 0;
float power = 0;
v_shunt = ina219.getShuntVoltage_mV();
v_bus = ina219.getBusVoltage_V();
v_load = v_bus + (v_shunt / 1000);
current = ina219.getCurrent_mA();
power = v_load * current;
String V = String (v_load, 5);
String I = String (current, 5);
String P = String (power, 5);
if (sec != secBefore)
{
if (SD.exists(nameFile)) {
Serial.println("Write data...");
myFile = SD.open(nameFile, FILE_WRITE);
myFile.print(dateData + " " + clockData);
myFile.print(" ");
myFile.print(V);
myFile.print(" ");
myFile.print(I);
myFile.print(" ");
myFile.println(P);
myFile.close();
}
else {
Serial.println("File " + nameFile + " not in memory.");
myFile = SD.open(nameFile, FILE_WRITE);
myFile.close();
}
secBefore = sec;
}
}
Program Description:
There are two parts of the data that will be stored in the micro SD, the first is the time and the second is the solar panel data.
The time section consists of the hour, minute, second, date, month and year. The solar panel data section consists of voltage (V), current (mA) and power (mW).
To retrieve time data using the command:
dataDate = now.timestamp (DateTime :: TIMESTAMP_DATE);
dataJam = now.timestamp (DateTime :: TIMESTAMP_TIME);
To retrieve solar panel data using perinth:
voltage_shunt = ina219.getShuntVoltage_mV ();
voltage_bus = ina219.getBusVoltage_V ();
voltage_load = bus_voltage + (shunt_voltage / 1000);
current = ina219.getCurrent_mA ();
power = voltage_load * current;
The solar panel data is in the form of float or domed values. In order to be able to store flavored values into micro SD, it is preferable to use strings. Otherwise, the stored float value is only the float value with two decimal places.
Meanwhile, if we convert to a string, we can determine how long the value behind the comma you want to store. The more the value after the comma, the more accurate the value will be.
The way to convert float to string is:
String V = String (voltage_load, 5);
String I = String (current, 5);
String P = String (power, 5);
The number 5 in the command above functions to declare the 5 values behind the comma. For example there is a value = 4.54561409, when we use the command:
String data = String (value, 5);
then the result is data = 4.54561
From the program above, the results stored on the micro SD are:
Hopefully this Arduino solar panel monitoring article can help you in making a data logger for solar panels using Arduino.
If there are suggestions and criticisms to be able to build this website for the better, please comment below.
If this article is useful, please share it with other friends using the share button below.