As I mentioned in my last post about solar panel setup, there still a few more issues with my sensors. One of them is temperature drift. When the weather is cool like early in the morning, the DC current sensors read one value and in the evening when the temperature is hot, they read a slightly different value. The difference is quite small but get magnified when doing the calculations. For example, in the night around 7 pm when there is no solar power, the sensor would read 2.5V which after all the transformations turns out to be 0 amps of current. All good. Then in the morning say around 5 am when there is still no sun, the sensor would read 2.52V. Not much of a difference, but it get magnified as I will soon show you.

Lets go through the formulas for determining the current flowing through a wire given that the sensor is reading 2.5V. Assuming the supply (reference) voltage is 5V, the mid point is 2.5V. Since the reading is 2.5V the current flowing must be 0 amps. For the case where the reading is 2.52V, the drift from mid point is 0.02V. Now we divide it by the sensitivity which is 66mV/A or 0.066 V/A, and we get 0.02 / 0.066 = 0.3 amps. So although there is no solar power at 5 am, the sensor was reading 0.3 amps and that is the temperature drift. Multiply the battery voltage with the current and we get power. So at 24V battery voltage the power is 24 * 0.3 = 7W.

As you can see, a small drift of 0.02V in reading is showing up as 7W of power and I did not like it. Look at the graph below. You will notice how the reading from solar panel slowly creeps up as night passes.

To compensate for the temperature drift I did not use any sophisticated look up tables. I simply added an offset in software that would bring the value to 0 in the late evening and another offset for early morning. The linear interpolation between these two offsets is used for all in between values from early morning to late night for the next day. This temperature compensation reduces the error in readings even during day time. The psuedo code looks like this

if (hh < NIGHT_HOUR_END || hh >= NIGHT_HOUR_START) {
  // Get offset that would make the value read 0 when there is no sunlight
  temperatureCorrection = getOffset();
  if (hh == NIGHT_HOUR_END - 1 && mm == 59 && ss < 2) {
    coolOffset = temperatureCorrection;
  if (hh == NIGHT_HOUR_START && mm == 10 && ss < 2) {
    hotOffset = temperatureCorrection;
} else {
  // Use interpolation during the day to compensate for temperature drift
  int minutesOffset = 
      Math.max((hh - NIGHT_HOUR_END - OFFSET_CALC_WAIT_HOURS) * 60 + mm, 0);
  temperatureCorrection = coolOffset + (hotOffset - coolOffset) * minutesOffset

Once I applied the correction, the graph looks more palatable.

There is a similar problem with the current sensor on the battery too. Once the battery is fully charged, the charge controller allows only a trickle charge which is of a known value based on the AH of the battery. However, even when the battery if fully charged, the sensor is detecting that the trickle charge is going down slowly over night because of the temperature drift.

I added similar correction on that sensor too.

There are still a few more fixes that I needed to make to make my whole solar panel setup more stable. So I will continue to give a few more updates on it. Sorry if it bores some of you, but it is a good record keeping for me :).