Continue to Site

# Reading constant battery voltage and using the obtained value in sensor formula

#### anon7548

##### Newbie
Hi, i am using 3.7~4.2V lithium battery. I am using internal voltage reference to read constant battery voltage as we know that the battery voltage level depletes overtime. The problem is that my sensor (mini solar panel) reads max value under little light and does not go beyond that level no matter how much light falls onto it in the later stage. I need my logic to be <<< if read sensor voltage less than 3V detect night and do something whereas if voltage level is above 3V detect day and go to sleep. The entire project is ready i just need to figure out this.

Code:
void loop() {
printVolts();
//REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
analogReference( INTERNAL);
DIDR0 = 0;
// Detect end-of-conversion
val = val * 5.7; //Multiply by the inverse of the divider
Serial.println("val:     ");
Serial.println(val);
}

Last edited by a moderator:

#### betwixt

##### Super Moderator
Staff member
The unloaded PV voltage tends to flatten out at around 0.7V per cell (there may be many cells in series in a single panel) even under low light conditions. As you have noticed, as the light level increases, the voltage rises, hits a limit then goes no higher. It's an electrical phenomenon and nothing in software can avoid it.

The solution is to load the PV so the cells have to 'push' harder and show a better light to voltage relationship. You can do it by wiring a resistor across the panel or if you are trying to conserve the power, use another MCU pin to periodically switch a load on, measure the voltage then switch the load off again.

The light level to output power relationship of PV panels is complicated and the reason why maximum power point tracking (MPPT) is used on larger systems.

Brian.

#### KlausST

##### Super Moderator
Staff member
Hi,

I don't understand "... read constant battery voltage...". What does "constant" mean?

You talk about "battery voltage" and "sensor". But in your code I see you only measure "A1" ... whatever A1 is...
I don't see how you can measure two things here (battery and sensor).

How is your variable "val" declared. First you use it as integer, but then you use it a float.

Please give complete informations and draw a flow chart about the problem.

Btw: if you go to sleep, how do you decide to wake up?
Some informatiin about microcontroller type and compiler could be useful, also schematic and some clear comments in your code.
You could put a comment what "val" means or you could use a more descriptive variable name, like "SolarVolt" or "BattVolt".

Klaus

Edit: Corrected "A0" to "A1"

Last edited:

#### anon7548

##### Newbie
Hi,

I don't understand "... read constant battery voltage...". What does "constant" mean?

You talk about "battery voltage" and "sensor". But in your code I see you only measure "A0" ... whatever A0 is...
I don't see how you can measure two things here (battery and sensor).

How is your variable "val" declared. First you use it as integer, but then you use it a float.

Please give complete informations and draw a flow chart about the problem.

Btw: if you go to sleep, how do you decide to wake up?
Some informatiin about microcontroller type and compiler could be useful, also schematic and some clear comments in your code.
You could put a comment what "val" means or you could use a more descriptive variable name, like "SolarVolt" or "BattVolt".

Klaus
Okay, by reading constant battery voltage, i mean, the voltage level should remain constant (same/unchanged)irrespective of what the voltage level is at the battery and yes i am able to read same voltage level.

Code:
float val;
float voltage;
int led = 8;

void setup(){
Serial.begin(9600);
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (led, INPUT);

}
void loop() {
printVolts();
//REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
analogReference( INTERNAL);
DIDR0 = 0;
// Detect end-of-conversion
val = val * 5.7; //Multiply by the inverse of the divider
Serial.println("val:     ");
Serial.println(val);
delay(1000);
}
void printVolts()
{
voltage =  (sensorValue/ val) * 1024.;

delay(1000);
Serial.println(                    "voltage:  ");
Serial.print(voltage);

}

check this, val is the value obtained from battery or battery voltage (A0) and (A1) is reading solar pin

Last edited by a moderator:

#### anon7548

##### Newbie
The unloaded PV voltage tends to flatten out at around 0.7V per cell (there may be many cells in series in a single panel) even under low light conditions. As you have noticed, as the light level increases, the voltage rises, hits a limit then goes no higher. It's an electrical phenomenon and nothing in software can avoid it.

The solution is to load the PV so the cells have to 'push' harder and show a better light to voltage relationship. You can do it by wiring a resistor across the panel or if you are trying to conserve the power, use another MCU pin to periodically switch a load on, measure the voltage then switch the load off again.

The light level to output power relationship of PV panels is complicated and the reason why maximum power point tracking (MPPT) is used on larger systems.

Brian.
I tried using resistor also but unfortunately, i could not find much differences, below i have attached schematic

#### Attachments

• solar charge.png
162.2 KB · Views: 23

#### betwixt

##### Super Moderator
Staff member
Try a load of about half the PV rating, 45/4 = 22.5mA so the load resistor should be 5/.0225 = 222 Ohms. Use a standard 220 Ohm value and see what the PV voltage on A1 measures under different light conditions.

A word of caution: The PV might produce more than 5V unloaded and in bright light, without a full specification it's hard to tell. It may be rated to produce 5V at 45mA load, in which case it might be more than 5V unloaded and therefore could damage the MCU. For safety, wire a schottky diode from the positive side of the PV to the 5V line on the Arduino board. It will have no effect on the reading but will ensure the PV voltage cant go much higher than 5V and overload the A1 input.

Brian.

#### anon7548

##### Newbie
Try a load of about half the PV rating, 45/4 = 22.5mA so the load resistor should be 5/.0225 = 222 Ohms. Use a standard 220 Ohm value and see what the PV voltage on A1 measures under different light conditions.

A word of caution: The PV might produce more than 5V unloaded and in bright light, without a full specification it's hard to tell. It may be rated to produce 5V at 45mA load, in which case it might be more than 5V unloaded and therefore could damage the MCU. For safety, wire a schottky diode from the positive side of the PV to the 5V line on the Arduino board. It will have no effect on the reading but will ensure the PV voltage cant go much higher than 5V and overload the A1 input.

Brian.
Yes, i have used schottky diode for protection