Posts Tagged ‘ Adafruit Logger Shield ’

Arduino Based Voltage Logger

Twice this year I have gone out to my garage and found the car’s battery dead. It’s a C-Max hybrid and when that happens, nothing works. You don’t start a hybrid, you boot it up, the computers control everything – even the door locks. Based on googling, this is a common problem with the 2013 model. Apparently, some times the radio doesn’t shut off when you park the car and the battery is soon dead. I have seen this mode twice, once the radio wouldn’t turn on and once it wouldn’t turn off. The fix is fairly simple, pull and replace the fuse. A power cycle brings the radio back to sanity. Maybe a coincidence, but the software was written by Microsoft.

So now it’s winter, I’m recuperating from surgery and the bicycle is hung up until spring. I needed a COVID lockdown project I could work on indoors. I decided to build something to monitor the state of the car’s low voltage battery. A hybrid has a high voltage battery to drive it’s motors and a low voltage battery to run accessories. There is no alternator as on a normal gasoline powered vehicle, instead the engine turns a generator (one of the two motors reversed by magic) to pump up the 300 volt traction battery. There is a DC-DC converter that steps the high voltage down to charge the conventional 12 volt battery. My project would need a measurement range of 0 to 18 volts, measure periodically and log the results with a time stamp to an SD card. I will leave the device connected for months at a time. It would be better to measure idle current drain but I don’t know how.

Arduino Based Data Logger
Arduino Based Data Logger

I bought an Arduino Logging Shield from Adafruit (product 1141). It plugs onto an UNO, provides an SD card socket and a Real Time Clock based on the NXP PCF8523 chip. I also bought a 128×64 pixel OLED display (product 326). I have used the OLED before in Arduino projects and like the flexibility. The display is small but very readable. The UNO I’m using is a Sparkfun RedBoard. It has a perfectly flat bottom which made mounting easier.

Data Logger Shield Testing
Data Logger Shield Testing

I tested the three main outboard components separately by using demonstration programs included with each respective library. The RTC and OLED worked out of the box but I had a hard time with the SD card. I had missed soldering on a six pin female connector that plugs on to the Arduino ICSP pins. Adafruit steals an SPI connection from that port, which is needed to talk to the SD card. Oh well…

On to software. A sketch was quickly put together that would read the RTC and write a time stamp to the SD card. No problem. But when I added the OLED to the mix the Arduino crashed. Apparently, the OLED library and the SD card library won’t fit together in the meager 2k of RAM on the ATMega328 processor. The SD library has a lot of code to handle a FAT file system. The OLED bit maps each pixel in RAM which by itself is 128 * 64 / 8 = 1024 bytes of memory. No wonder the processor barfed. So lowering my expectations, I hogged out the one inch hole I’d made in the plastic case to 3 inches wide and fitted a more familiar 16×2 Liquid Crystal Display.

I had an I2C adapter for these LCD modules ($3 from All Electronics catalog LCD-SI). Soldered it onto the LCD and connected to the I2C terminals on the Logger Shield. Didn’t work. After an entire afternoon checking wires and downloading different LCD libraries (apparently the All Electronics part has a different pinout than the Adafruit equivalent), I ran the I2C_scanner program in the Wire library examples. Turns out that the All Electronics board has a different I2C address, 0x3F instead of 0x20 which was in the documentation. Once the correct address was set, the LCD worked as advertised. All Electronics’ backpack has one advantage over some of the other vendors, it has a switch transistor on board that can turn off the back light on the LCD module saving some power.

LCD with All Electronics I2C Adapter
LCD with All Electronics I2C Adapter

Operation:

Three buttons are installed, Function, Up, and Down. These let me move around in the settings menus. The switch componants can be seen just above the LCD module. My usual practice is to solder a 0.1 uFd capacitor across the switch, ground one side, then wire a 10k pullup on the Arduino side. I have a function that analog reads the switch state and returns high or low. It is a very effective de-bouncer which is a good thing with these cheap button switches.

On a reset, or applying power, the logger boots up by reading the last log interval and file name from nonvolatile memory. Unless the settings menu is requested, it will enter continuous timed measurement mode. The display will light for two seconds then goes dark and the processor sleeps for the rest of the current log interval.

Typical Voltage Reading
Typical Voltage Reading

This is what the records look like on the SD card. Here the interval is set to thirty minutes. The Comma Separated Value format is easy to import into a spread sheet. It only takes about six mouse clicks to produce a usable graph of the measurements.

jbh@junkbox-2:~/Desktop$ cat /media/jbh/LOGGER1/G.CSV
2020/12/06 20:34:42,09.1
2020/12/06 21:04:00,09.1
2020/12/06 21:34:00,09.1
2020/12/06 22:04:00,09.1
2020/12/06 22:34:00,09.1

It turned out that programming read and save voltage readings was the easy part – about 65 lines in the sketch. More fun was doing setup menus. I wanted:

  1. to set the Real Time Clock
  2. to set the logging interval
    (5 sec, 10 sec, 30 sec, 1 min, 5 min, 10 min, 30 min)
  3. to set the log file name
    restricted to letter.CSV where letter is A-Z to make things easier
  4. to list a directory of the SD card and to browse any file

This forms the rest of the sketch, about 800 lines (so far). There were serious memory problems getting it all running. I have in fact ordered an Adafruit Metro M0 which has much more RAM, but with moving some code into functions it is working OK now on the RedBoard. (update: The sketch will require minor changes to compile on the M0).

Setup mode is triggered by holding the Function button down on reset. Arduino will not check the buttons while sleeping, pressing the reset button wakes everything up. In general, a short press of the Function button selects things, a long press exits the task. Up and Down buttons move through the menu items.

Holding Function On A Reset
Holding Function On A Reset

At this point, releasing the Function button displays the setup menu categories. Pressing the Up or Down button will move through the list. Pressing Function will enter the option currently in the display. There are four choices:

Settings Menu Options
Settings Menu Options

SD Directory:

This section allows scrolling through the SD card directory and optionally examining saved records in a selected file.

The Up/Down buttons scroll through the directory of files on the SD card. File names and sizes are shown:

Showing Files On The SD Card
Showing Files On The SD Card

A short press of Function will open the file in the display and begin displaying records, the most recent shown first. This screen is displayed on entry for two seconds:

Opting To View An SD Card File
Opting To View An SD Card File

Followed immediately by the contents of the last file record. The information on the top line is: record number, first letter of file name, and the voltage measured. The second line shows the date and time the measurement was recorded. I couldn’t fit in the year.

Viewing One File Record
Viewing One File Record

A Function long press exits the file browser. Another long press exits the directory option and returns to the main menu. You can select another menu category or do a third long press to exit Setup altogether and begin timed measuring.

New Logfile Name:

Twenty six files are allowed, A.CSV through Z.CSV. Only the first letter changes which simplified programming. A short press on Function while “New Logfile Name” is displayed enters the change code, displaying the currently selected file. Pressing Up or Down will scroll through all names possible. If a named file is already in the SD card directory, the second line will show “Exists”. Short pressing Function at that point will select the file and append future readings.

Existing SD Card File
Existing SD Card File

If the displayed name is not currently on the SD card, the second line will show “Available”. Short pressing Function will then create the file.

File Name Does Not Exist Yet
File Name Does Not Exist Yet

In either case a long press of Function will exit the change code, returning to the main Setup menu.

Set Log Interval:

Seven different logging intervals can be selected.

  1. 5 seconds
  2. 10 seconds
  3. 30 seconds
  4. 1 minute
  5. 5 minutes
  6. 10 minutes
  7. 30 minutes
Scrolling Through Log Intervals
Scrolling Through Log Intervals

Up and Down buttons will scroll through the seven choices, a short press on Function will select one of the values and automatically return to the main Setup menu. A long press will return without changing the previous setting.

Intervals are implemented in hardware. A selection is sent to the RTC chip which generates a pulse when the interval has expired. That pulse on the Square Wave output is hard wired to Arduino digital pin 3 which triggers an interrupt to wake up the processor. Selecting different intervals obviously has an effect on file size. Examples: 30 Minute interval – 37.4 kilobytes in a month, 5 Second interval – 13.5 megabytes in a month. My sixteen gigabyte SD card could easily store 5 second measurements for three years. Might be tough loading that into a spread sheet.

Set Clock:

This should not have to be done often. Code in the Adafruit RTC library detects a virgin clock (no battery backup installed for some time) and sets the RTC chip to the compile time of the sketch – which in turn should be traceable to the clock time on the computer hosting the IDE. There is a way to calibrate the PCF8523 but it is not implemented in this sketch. The only time I see changing the clock is if I move to a different time zone.

Short pressing the Function button while “Set Clock” is displayed produces this display:

Time Set Display
Time Set Display


There is a cursor which can be moved back and forth to the digit that needs changing by short presses on the Function button. It is under the ones digit of seconds in the above photo. Use the Up and Down buttons to make needed changes, then long press Function to finalize and return to the Setup menu.

At this point I have not implemented the hard wire connection to the car battery. It will require some sort of protection circuit and scale the voltage down to the 0-5 volts needed at the ATMega328 analog read pin (A3). I also have to find a way to hide the logger in the back of the car. Thus this is a work in progress.

Input Circuit:

Cars are notorious for large transients on the battery buss. I’ve seen quoted figures from +100 to -75 volts, the worst is seen when the alternator is actively charging and the connection to the battery is suddenly lost. So I spent some time working up a circuit to interface the battery to expensive electronics. It was suggested I use an off the shelf cigarette lighter USB adapter and I did some experiments with one. The problem is the buck converter drops out at about 8.5 volts and also if it stops bucking, you get 12 volts on the output. Also requires a third wire to pick off the battery voltage being measured.

Auto USB Adapter Parted Out
Auto USB Adapter Parted Out

On my experimental breadboard I replaced the auto USB adapter with a good old 7805 regulator. This produced the expected five volts with the input as low as 6.5 volts, so 7805 it is. This is the schematic of the input circuit:

Input and Calibration Circuit
Input and Calibration Circuit

D1 protects against negative transients, R1 and C1 soak up positive transients. I will add an 18 volt zener diode in the future when I find one. D2 and C3 clamp the measurement voltage to 5 volts.

These input components were glued into the box top, ugly but it works. Calibration components were added on the Logger Shield breadboard area.

Components for Input
Components for Input

Testing:

Did much testing today with a variable power supply to see how the logger behaved under brownout conditions. It ramped down good at first, but any input voltage below about 9 did not register. The processor held up down to about 6.5 volts though you could not read the LCD. At 6.0 volts everything stopped.

With analogReference(DEFAULT) 5 volts
With analogReference(DEFAULT) 5 volts

Thinking it over I realized that the 5 volt regulator was dropping out. That caused the analog reference used for voltage measurements to dip in sync with the dropping input so no wonder it was showing the same voltage every time. The fix was obvious, lower the analog reference voltage to a point where it would stay steady and recalibrate. Only one change to the sketch, adding analogReference(INTERNAL) in setup produced this run:

Test 17.0 Volts to 5.5 Volts
Test 17.0 Volts to 5.5 Volts

Which is pretty much what I hoped the device would do. The real fix for this dropout problem will be replacing the five volt regulator with an Adafruit Buck-Boost converter. I thought I had one of those but I can’t find it.

Installation:

I found a pair of copper clips like those on a small battery charger and attached them to the logger power cable. The problematic C-Max battery is in the rear of the car in the area where the spare tire would be if it had one. Rear mounted batterys are usual on a hybrid, which has no 12 volt starter motor and therefore a super low resistance connection is not necessary. But storing the logger where it could access the battery directly might trigger my wife if she saw it, so I picked a location where she would never go. Under the hood.

A C-Max has a pair of heavy metal terminals near the engine. These are designated points where jumper cables or a battery charger can connect to feed or drain power from the low voltage battery. There is a fuse/relay box inches away with about a 6″x10″ flat removable lid. I removed the lid and glued on a chunk of sheet metal cut from an old tape drive enclosure. A big magnet salvaged from a defunct hard drive bolted to the bottom of the logger completed the mounting. It is now stuck on the fuse box lid, happily logging the battery voltage every 30 seconds.

It’s hard to see what’s going on in this photo, but you can see the logger atop the gray metal plate. There is a zip lock baggie protecting the unit from soon to come road salt. Left and below center you can see the positive jumper terminal with the logger wire clipped on. About eight inches up the positive wire, inside the baggie, is an in-line 3 amp fuse holder. The logger negative lead can be seen at the lower left corner clipped to the chassis ground terminal that forms the other half of the jumper cable connection.

Logger Installed
Logger Installed

First Data Run:

The Logger has been in the C-Max for 8 days over Christmas. The interval was set to 30 seconds, the log file is 600K. During that time we made two short three-five mile trips. Here is the complete capture:

First Data From Logger
First Data From Logger

It’s about what I expected from a properly functioning charge system and I don’t think it shows any major phantom drain. This is not a precision instrument but it was calibrated at 12.0 volts against my Simpson 360, so should be close. The important thing is the trend over several days, we typically only go out twice a week.

Following is a blow up of the first peak, on 12/21 You can see it charging at about 15 volts, dropping to 12.5 while waiting in the parking lot of the grocery store.

A Trip to Casey's Then Home
A Trip to Casey’s Then Home

Buck-Boost Converter:

I thought a better power converter than the 7805 would solve the low voltage range problem. The Adafruit Buck-Boost (#2190) I tried works only to 12 volts. Searching the net for a converter that would work from 2 to 20 volts input did not turn up anything. I finally found and ordered a converter from Pololu (#S10V4F5) which is spec’ed to work to 18 volts. Close enough. It is tiny, even smaller than the 7805 it replaced.

Pololu Buck Boost Converter
Pololu Buck Boost Converter

The BBC is a mixed blessing. Switching regulators trade current for voltage, basically tries to transfer power, not voltage or current specifically. The good news is input current went down at 14 volts from 30 Ma sleeping to 10 Ma. The bad news is at the low end, current went up as high as 250 Ma as the boost kicked in to maintain 5 volts. I could not get a good measurement below 3.5 volts as the power supply I have for testing would go into current limiting.

Testing revealed that the low end of the measurement range was severely affected by the series resistor installed to help protect the circuit from transients. The 22 ohm series resistor in fact went up in smoke. I did the math figuring a drop across that protection resistor of one volt would be acceptable, at 300 Ma that comes out to a value of 3.3 ohms. The 22 ohm resistor was replaced by four 10 ohm resistors in parallel, giving a comfortable margin. At the same time the input protection diode was change to a Schottky type which measured half the voltage drop of the 1N4004. These changes can be seen in the above photo. Voltage drop across the protection components does affect calibration significantly at the low end.

Here is the schematic of the final input circuit:

Input Power Conditioning
Input Power Conditioning

January 2021

Logger has been in the car from December 31 to January 31. There have been no voltage fades. Did have some trouble with Libre Calc manipulating the chart, there are 88,600 thirty second measurements in a 2.2 meg file.

Log for January 2021
Log for January 2021

February 2021

Logger data for February shows no significant voltage drop. Even though the first two weeks were mostly single digit temperatures.

February Logger Data
February Logger Data

April 2021

This is data from April (through May 5th when I remembered to retrieve the SD card). Sometime during the last week of April, the C-Max radio locked up and would not turn on. So that’s happened twice, and once it locked in the On state and wouldn’t turn off. The graph shows a slight voltage droop that week a couple of tenths. I reset the radio by pulling and replacing the fuse on May 1.

April Battery Data Readout
April Battery Data Readout

Future:

I’m considering the project done for now and putting it back in the car. I am however experimenting with an Adafruit Metro M0 I bought when I thought there was no hope of the sketch working in an UNO’s 2K of RAM. Most of the code compiles but the M0 does not have EEPROM so changes are in order. There is enough memory on the Metro to add code for an ESP32 coprocessor so an email voltage alert might be possible. Would probably be a whole new build to make room. Maybe next winter.

While this device is intended to monitor a 12 volt battery in my car, it could be modified easily to measure and log anything that can be scaled to the 0-5 volt range of the Arduino (0-1.1 volts with the current analogReference(INTERNAL)). It would require changing the input scaling resistors and implementing a separate power supply. I can do this by adding jacks to the enclosure. Adding code would be a problem If I keep the ATMega328 processor. It’s now using pretty much every last byte of memory.

Two other projects come to mind, three years ago I started building a temperature controller for a modified toaster oven. The objective was to do surface mount PC board soldering, also to retemper hardened steel tool cutters. Both of these applications require specific heat and cool cycles and the logger will be ideal for graphing the temperature profile. Another back burner project I have is monitoring water level in the house sump pump. I have constructed a capacitance based level sensor and could use the logger to watch levels change as the pump cycles, sometimes every 15 seconds in the spring rains.

Revision History:

  • Dec 8 2020 Initial software release Beta V07.2 dropbox link
  • Dec 14 2020 Added information on the protection and calibration circuit
    Changed analog reference. Updated dropbox file link
  • Dec 15 2020 changed prot ckt R2 to 33k to make cal less touchy. Better voltage graph.
  • Dec 31 2020 Rebuilt input circuit with Pololu Buck-Boost converter