# CAN Battery Monitor



## jackbauer (Jan 12, 2008)

Here is a little project I'm working on. I cannot find a nice robust battery monitor so decided to have a go at my own. Sensor board is based on an Atmega328 and an ADS1115 16 bit ADC in differential mode. Reads voltage from 0-500v and current from -600 to +600A and temperature via a thermistor. Transmits via CAN and bluetooth.

All files on my github:
https://github.com/damienmaguire/EV-CAN-BAttery-Monitor

Software is only bare bones with no checking for valid CAN IDs etc but it does work. Struggling a bit with the amp hour calculations.


----------



## Tomdb (Jan 28, 2013)

The reason the AH calc is wrong.


```
unsigned long msec = millis();
float time = msec / 1000.0;
float ampSeconds = Amps*time;
int ampHours = ampSeconds/3600;
 
  return ampHours;
```
You reset the AH counter every time, thus only updating it with the new value and cumulative. 

You need a new unsigned long for previous time.

Thus you will have something along the line of this:



```
unsigned long msec = millis();

float time = (msec[B]-last[/B]) / 1000.0;
float ampSeconds = [B]ampSeconds +[/B] Amps*time;
int ampHours = ampSeconds/3600;
[B]last = msec;[/B]
  return ampHours;
```
subtracting the current time from the last to get the duration since last measurement, then multiple this by the amp reading and add this to the running total. Since the float for your total ampSeconds should be signed you can even get a negative number.


----------



## kennybobby (Aug 10, 2012)

Good catch Tom.

What projects are you working on these days?


----------



## Tony Bogs (Apr 12, 2014)

I have looked into the topic of keeping an accurate track of charge in and out of the battery pack for my charger project.
Aspects like accuracy and speed of the ADC, bandwidth of the amp sensor, processing speed of the controller.

I am using this NI white paper about sigma-delta ADCs like the ADS1115 as a reference:
http://www.ni.com/white-paper/11342/en/

Although sigma delta converters have built-in integrators, the sampling frequency has to be at least twice the bandwidth of the input signal (Nyquist criterium).

So I think you are going to need a set of hardware integrators for the mega328 if you want to accurately take high frequency components into account. 
Like for instance the 5 kHz PWM base frequency of an IGBT inverter.
A set is needed because integration has to be continuous and HW integrators have to be reset before they reach their maximum operational output voltage. 
Or another option: go for a faster ADC and controller combination.
Bandwidth has to be at least 50kHz for a light vehicle. The ADS1115 has a maximum sampling frequency of 868 Hz.


----------



## Tomdb (Jan 28, 2013)

Working on my own battery monitor actually.

Simple output, pwm to ground, for a fuel gauge driver.
4 12v outputs 
2 12v inputs
2 canbus 
1 serial bus
1 isolated differential input, using a HCPL 7520

Using a teensy 3.1 (3.2) and there are great libraries for the dual 16bit ADCs with averaging in hardware and not just software. Plus this micro should pack some punch when it comes to heavy number crunching....






So this way I can interface with bms slaves (canbased or serial )

If I get my software working properly this should need to be the only "smart" bit in an EV besides the bms slaves.


----------



## john61ct (Feb 25, 2017)

For House bank / fossil fuel vehicle use, integration with this "Smart Alt Regulator" project would be great, relly need to make time to start figuring this stuff out.

http://arduinoalternatorregulator.b...=2017-02-10T17:58:00-08:00&max-results=10&m=1


----------



## Tony Bogs (Apr 12, 2014)

The very simple but accurate US$5 analog Ah integrator for my charger design. 
For maximum accuracy three are needed: one is in calibration mode, next one samples (integrates) and the last one is in hold for ADC conversion followed by a reset.


----------



## jackbauer (Jan 12, 2008)

Very interesting stuff. One reason I went with CAN on this design is to directly drive the gauges on modern instrument clusters like the BMW E90. This way I can drive the fuel gauge , tacho for current , warning lights , temp gauge etc.


----------



## Tony Bogs (Apr 12, 2014)

Of course, in most cars CAN has been the standard for more than 20 years.
I'm very interested to see the ADC anti-aliasing filters but I'm not having any luck opening files from the github zip download. Not with KiCad, gEDA or eagle (6.5).


----------



## jackbauer (Jan 12, 2008)

Sorry I should have mentioned. All files are in designspark pcb format.


----------



## Tony Bogs (Apr 12, 2014)

Ok, that won't work then. But I could take a look at the bluetooth ino with the ADC routines.

The ADS1115 can only convert 1 channel at the time. 
If multiple channels are converted, amp measurement is discontinuous. 
In that case the Ah result can show a substantial incremental deviation if the current is not DC (or very LF).

Maybe it can be solved when a second ADC1115 for amp measurement is added, running in continuous mode. 
I haven't studied the details of continuous mode of the ADS1115.

P.S. Filtering out the higher frequencies (anti-aliasing filter) should be enough though. 
Cut-off (-3dB) at less than 280 Hz (RC filter, three measurements per loop) if the ADS runs at max speed.


----------



## Tomdb (Jan 28, 2013)

Worked some more on my BMS master with a teensy 3.2.
Got the current sensor measurement implemented.






Using Boekels 12s Tesla module with two Tesla BMBs, tracking the current and running a counter (Ampsecond counter)

Chevy Volt Charger is currently controlled by another arduino, I set the voltage and Current via a serial interface and charging commences once the correct pin goes high. This way I can emulate a dumb charger which only gets switched on and off by the BMS master.


----------

