Tuesday, 30 January 2018

Troubleshoot your car battery with ATtiny



Last winter I experienced some problems with my car battery. I knew that it was about time to replace it so off I went to buy a new one. This fact reminded me of an old article about a car battery/charging system diagnostics kit I had seen in one of those 1980s electronics magazines.

That magazine’s battery monitor relied on an IC that had several analog comparators arranged in a way similar to the ones in the old LM3914. This made me think about designing a more up to date battery monitor with today’s microcontrollers in mind which could use a low LED/resistor count and a reduced PCB size and possibly fit inside an empty 1oz Tic-Tac box.

I had originally envisioned building this battery monitor around Microchip’s PIC12F675. However, after reading several instructables on microcontrollers I realized that everybody seemed to prefer Arduino. I concluded that it was about time to get my feet wet in this family of microcontrollers.


After reading this instructable, I also realized it was possible to use Arduino and get a controller with the size needed for this project.

I hope you enjoy it as much as I did putting it all together.













Step 1: Theory of Operation


 
 
 
 
 
 
 
 
 
 
 
 
Show All 14 Items

The operation of this battery monitor relies on what the battery as a voltage source "sees" as a load. Using basic electric circuit theory we have:

Vbatt = Vd + (R1 + R2)•I ……………………….(1)

Vadc = R2•I ……………………………………...(2)

Vbatt = Battery voltage
    Vd = Silicon diode forward voltage drop
Vadc = Voltage that goes to ATtiny 13’s Analog to Digital Converter
       I = current
R1 and R2 are the resistors used in the voltage divider.

From (2):

       Vadc
I = ---------
          R2

Substituting in (1)

                                         Vadc
Vbatt = Vd + (R1 + R2) ------------
                                           R2

Therefore:
                                      R2
Vadc = (Vbatt –Vd) ----------------- …………..……(3)
                                ( R1 + R2 )

Which is the formula used to calculate voltage seen by ADC.

Step 2: Schematic



Battery voltage gets into the battery monitor through connector J1. Diode D1 is used to protect the entire circuit against accidental battery polarity reversal in case you decide to test the battery directly across its terminals in the engine compartment. Should you connect the test alligator clips reversed, nothing will happen.
IC2 is a +5V low dropout voltage regulator. Its input voltage range –with a 100mA load- varies between 5.4V up to its absolute maximum of 30V. I admit that for this battery monitor a 78L05 +5V voltage regulator would have been more than enough, however, I had no 78L05s and several LP2950CZA-5.0 kicking around in my junk box.  Capacitors C1 and C2 (22uF and 1uF) are voltage regulator’s input and output filters. If you want to use pin-compatible 78L05 voltage regulator instead, use a 10uF capacitor as C2.
The battery voltage feeds both, the voltage regulator and the ADC input circuit to ATtiny through a voltage divider formed by a 10K resistor and a 3.3K resistor marked as R1 and R2 respectively. This voltage divider is used to reduce battery voltage to a safe input voltage range between 0V and +5V for ATtiny’s analog input. This voltage divider allows a maximum car battery voltage of 20.8V for this input voltage range. In order to smooth out this analog input voltage, a 10uF (C3) is used to reduce noise.
The visual output to the user is shown by means of three LEDs. A Red LED (LED3) shows either too low a voltage or a dangerous overvoltage. A Yellow LED (LED2) shows a moderate low voltage and the green LED (LED1) shows good voltage and safe charging voltage. Each LED comes with its respective current limiter 330 Ohm resistors, R3, R4 and R5. Resistors R6 and R7 at 10K each are pull up resistors for unused inputs.

Step 3: Software



The software was written based on the hints given by Eric-the-car-guy in this excellent video. He didn't mention the voltage at which a battery can be considered bad. However, my own car's service manual stated that while cranking the engine, voltage should not get lower than 9.6V. If it does, then battery should be replaced which seems to be about right for most car batteries.

This sketch is very simple, just a series of consecutive IF instructions to compare voltage read through ADC3 with some predefined values. If you follow the code closely, you’ll see that the values shown in the Theory of Operation slide show in Step 1 are the same values used in the different IF instructions to decide which LED should come on.

To create the blinking effect on LEDs whenever voltages are higher than 13.1V, a counter is increased by one on every pass through that portion of the code. Before the code loops back to read ADC3 again, execution is delayed 100 milliseconds. IF instructions turn LEDs on whenever the pass counter is below 6 and turn LEDs off whenever counter is between 6 and 11. Once counter reaches 11, the counter is reset to zero to repeat the cycle.

ADC3 input is used to read the analog voltage coming from the voltage divider. By the way, ADC0 had been the first choice as this project’s analog input, however, when voltage coming from the battery was a bit lower than 8.7V all three LEDs would go out.  This didn’t seem right as I had used a low-dropout voltage regulator. After doing some more research on the web I found that anytime you used a pin as an input  that could also function as hardware Reset, these things could happen. Once ADC3 was selected as analog input, battery voltage could come down close to 6V and red LED would still be on which means the software would still be running. It was then that in order to prevent further problems, unused pins 1 and 5 were pulled up to +5V through 10K resistors.

                         ATtiny 13 code:

// This sketch monitors battery voltage and turns on an LED based on it.
// Processor: ATtiny 13.
// Author: rlarios
// Date: 13.04.03
//

int rLED = 4;                                    //Red LED pin
int yLED = 1;                                   //Yellow LED pin
int gLED = 2;                                   //Green LED pin
int val = 0;                                        //This variable will hold voltage input value
int dlyctr = 0;                                    //This is the delay counter.

void setup(){
pinMode(rLED, OUTPUT);               //Define Red LED output
pinMode(yLED, OUTPUT);              //ditto Yellow LED
pinMode(gLED, OUTPUT);              //ditto Green LED
}

void loop(){
val = analogRead(A3);                      //Read voltage through Analog input 3
   if(val<452){                                     //Is battery voltage below 9.6V?
   digitalWrite(rLED, HIGH);               //Yes, this is bad, turn on Red LED
   digitalWrite(yLED, LOW);               //Turn off Yellow LED
   digitalWrite(gLED, LOW);               //Turn off Green LED
   }
   else
   {
      if(val<594){                                   //Is battery voltage between 9.6V and 12.4V?
      digitalWrite(rLED, LOW);             //This is a low voltage, turn off red LED
      digitalWrite(yLED, HIGH);            //Turn on Yelow LED as a warning.
      digitalWrite(gLED, LOW);            //Turn off Green LED
      }
      else
      {
      if(val<629){                                    //Is battery voltage between 12.4V and 13.1V while idle?
         digitalWrite(rLED, LOW);            //Yes, turn off Red LED
         digitalWrite(yLED, LOW);           //Turn off Yellow LED
         digitalWrite(gLED, HIGH);          //Turn on Green LED to indicate fully charged battery voltage.
         }
         else
         {
         dlyctr = dlyctr + 1;                       //increase delay counter. every pass approx. 100ms
         if(val<751){                                 //Is battery voltage above 13.1V and below 15.5V?
            digitalWrite(rLED, LOW);         //Turn off red LED
            digitalWrite(yLED, LOW);        //Turn off yellow LED
            if(dlyctr<6){
               digitalWrite(gLED, HIGH);    //Pulse green LED on for half a second
               }
               else
               {
               digitalWrite(gLED, LOW);     //Pulse green LED off for half a second to show battery is      charging.
               if(dlyctr>10){
                  dlyctr=0;                            //Reset delay counter
                  }
               }
            }
            else                                         //Battery voltage is above 15.5V.Danger ! Overcharge
            {
            digitalWrite(yLED, LOW);        //Turn off Yellow LED
            digitalWrite(gLED, LOW);        //Turn off Green LED
            if(dlyctr<6){
               digitalWrite(rLED, HIGH);     //Turn on Red LED for half a second.
               }
               else
               {
               digitalWrite(rLED, LOW);      //Turn off Red LED for half a second.
               if(dlyctr>10){
                  dlyctr=0;                            //Reset delay counter
                  }
               }
            }
            delay(100);                             //Stop program 100 milliseconds to help pulse LEDs.
         }
      }
   }
}












Step 4: Programming ATtiny 13






















You'll have to wire Arduino UNO and ATtiny 13 as shown in one of the images in this step. Make sure ATtiny 13 pin 1 is properly oriented. Once wiring is ready, you'll have to load Arduino IDE in your laptop computer and configure it so it can be used as a programmer.

I won’t get into the details here about configuring Arduino UNO as ISP and where to get ATtiny 13's libraries. After some research I found this excellent video by Chris Stubbs on how to do just that. The video also shows how to wire Arduino UNO and ATtiny 13 together.

After Arduino UNO is configured to function as ISP, and ATtiny 13 libraries have been  loaded, it is time to select our target "board". From Arduino IDE menu bar click on Tools>board> ATtiny 13 @9.6MHz (internal 9.6MHz clock). Next, type in the battery monitor sketch into Arduino IDE and proceed to compile it and upload it into ATtiny 13. The selection of ATtiny 13 with a 9.6MHz clock will cause the blinking rate of LEDs to have a period of about 1.4 seconds. This means, LED will be on 0.7 seconds and off 0.7 seconds which is close to what I originally intended.

We will put our Car Battery monitor together in the following steps.

Step 5: Bill of Materials



The printed circuit board Eagle files are attached to this step. I used the toner transfer method to make the one shown. A couple of traces didn't come out right so I tinned them for better appearance and to ensure continuity.

Make sure the resistors you are going to use for the voltage divider, R1 (10K) and R2 (3.3K), are as close as possible to rated values. All the resistors used here are 5% so make sure they are within this tolerance. The resistors I used as R1 and R2 measured 10.001K and 3.245K respectively.


This project is also available as a kit from Club Jameco.


Resistors:

3          10K 1/4W resistor 5%
1          3.3K 1/4W resistor 5%
3          330 Ohm 1/4W resistor 5%

Capacitors:

1          22uF 50V Electrolytic 20%
1          1uF 25V Electrolytic 20%
1          10uF 16V Electrolytic 20%

Semiconductors:

1          1N4004 400PRV 1A
1          Green LED 5mm
1          Yellow LED 5mm
1          Red LED 5mm
1          LP2950CZ-5.0 Low Dropout Voltage regulator +5V               Jameco P/N:266757
1          MCU ATMEL 8-bit ATtiny 13

 Miscellaneous:

1          Socket IC 8 pin
1          Jack DC Power Male 2.1 mm                                                 Jameco P/N:101178
1          Printed Circuit Board.

Tools:

Soldering iron
Solder
Clip-on heatsink
Needle nose pliers
Wire stripper

Additional material:

1          Automobile Fused Power Plugs with cord                              Jameco P/N: 124760
2          Connector Power PL 2 Position straight cable mount            Jameco P/N: 191484
1          ft 22AWG red wire
1          ft 22AWG black wire
1          In-Line Fuse holder for 1-1/4" x 1/4" standard fuse                Jameco P/N: 102868
1          0.5A fuse       1-1/4" x 1/4" standard                                       Jameco P/N: 10410
1          Pair Alligator clips, one red, one black.                                  Jameco P/N: 70991
1          1 oz tic-tac empty box.




Step 6: Assembly


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Show All 7 Items

As usual, solder in first the passive components such as resistors and capacitors. Then, solder in protection diode 1N4004 into its position using a clip-on heat sink on the lead to be soldered to keep the device from overheating. Do the same when soldering in the voltage regulator. Soldering the LEDs has to be done as quickly as possible because you will not be able to use the clip-on heat sink if you want the assembled battery monitor to fit inside an empty 1 oz Tic-Tac box.

Now insert the 8-pin IC socket. Bend at least two opposite leads to keep it in place and make sure no excess solder is used when soldering. If adjacent pins get shorted together, re-heat solder so you can wipe off the excess.

Now the tricky part, you’ll have to be careful to solder in the DC Power Jack Male 2.1 mm connector.  After inserting the power connector, make sure it doesn’t come off while you are soldering it due to oversized holes. If you guys know of a better Eagle library part for this connector, let me know.

Well, this is it, the battery monitor is finally assembled and ready for testing.

Step 7: Test Leads Assembly and Preliminary Testing


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Show All 12 Items

Test leads assembly.

Now we have to put together the test leads by taking 1' of 22AWG red wire and 1' of 22AWG black wire, one connector Power PL2 Position straight cable mount Jameco P/N: 191484 and 1 pair of alligator clips, such as Jameco P/N: 70991, 1 In-Line fuse holder such as Jameco P/N: 102868 and a 500mA fuse that fits inside this In-Line fuse holder.

Strip about 1/8” insulation off the ends on both wire segments. Unscrew the sleeve-like cover off the Power PL2 connector and solder one end of the red wire to the center terminal of Power PL2 connector. Solder one end of the back wire to the remaining outside terminal of PL2 connector.  Make sure these two terminals do not touch each other once cover is back in place. Slide the unsoldered ends of both wires through the sleeve-like cover so it can be screwed back onto PL2 connector.

Solder the black alligator clip to the other end of black wire. Solder In-Line fuse holder to the red wire (cut off excess red wire already soldered to PL2 connector as needed) in such a way that once test leads are finished, both wires have about the same length.  Solder in red alligator clip to the remaining end of in-line fuse holder and test with an Ohm meter to make sure you get an infinite reading. This means, terminals are not touching inside PL2 connector cover.

Automobile fused power plug with cord.

Take Automobile fused power plug with cord Jameco P/N: 124760 and identify the wire at the opposite end that is connected to center terminal of cigarette lighter plug. Solder in this corresponding wire end into central terminal of Power PL 2 connector Jameco P/N: 124760. Solder in the other wire end to the remaining outer terminal of PL 2 connector. Make sure these two terminals are not touching each other before putting cover back on. Test for proper continuity.

Test Battery Monitor Power supply.

Attach the red and black alligator clips of the test probes you just put together to the positive and negative terminals respectively of a 9V battery. For this test, do NOT install ATtiny 13 yet, the following is meant to test onboard voltage regulator. Insert the PL2 connector end of test probes into Battery Monitor Power jack. Using a voltmeter, measure voltage between pins 4 (Gnd) and 8 (Vcc) of DIP IC socket to make sure you have +5V.  If you get this reading this means the regulator is working properly. Disconnect battery from alligator clips.

Battery Monitor preliminary test

Insert ATtiny 13 into its socket making sure its pin 1 is properly oriented. You will need an 8V to 16V variable power supply or the suggested circuit shown in one of the images for this test. Potentiometer in suggested circuit may be either 100K or 10K. Make sure you have a dc Voltmeter to measure the voltage while you perform the test.


Set the voltage on variable power source to 9V and connect the alligator clips to this power source. Red LED should come on, if this is correct, then software was uploaded properly into ATtiny 13. Increase the voltage slowly until you see that red LED goes out and yellow LED comes on. Please note the voltages. In my case, yellow LED came on at 9.78V instead of the expected 9.6V. Continue to increase voltage and note at what voltages the other LEDs go out and come on. If you use the suggested workbench circuit, your batteries’ voltage may dip a little bit when LEDs turn on and off at voltages higher than 13.1V. If your variable power supply is strong enough these dips may not be noticeable.

Well, it seems the 5% resistors may be causing this difference which is about 0.18V or 1.8% deviation from target which is not bad after all. Had we used 1% resistors this deviation would have been much lower. We’ll see how to adjust this in next step.

Step 8: Software Calibration.


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Show All 13 Items

Remember that R1 = 100.001K and R2 = 3.245K? Well, R2 should have been 3.3K and here is where part of the problem is. We had determined that for 9.6V we needed the analog input to read 452 so let’s see what it read:

From formulas derived in slides 4 and 5 in step 1:


                                            R2                                 3.245
Vadc(9.6V) = (Vbat – Vd)------------  =  (9.6 – 0.7)---------------------  =  2.1803 V
                                      R1 + R2                      (10.001 + 3.245)


Plugging this value into digital value formula:

                       2.1803
dVal(9.6V) = --------------- (1023) =  446.09 ≈ 446
                           5

Which is about 6 units below 452, that’s why voltage had to go higher to switch LEDs. For calibration purposes, the value 446 will replace 452 in the code as follows:

The line in the code:

   if(val<452){                                     //Is battery voltage below 9.6V?

will become:


   if(val<446){                                     //Is battery voltage below 9.6V?


We will do the same for the other voltages as well, new digital value for 12.4V:

dVal(12.4V) = 586

So this line:

      if(val<594){                                   //Is battery voltage between 9.6V and 12.4V?

Will become:

      if(val<586){                                   //Is battery voltage between 9.6V and 12.4V?

dVal(13.1V) = 621

This line:

      if(val<629){                                    //Is battery voltage between 12.4V and 13.1V while idle?

Will become:

      if(val<621){                                    //Is battery voltage between 12.4V and 13.1V while idle?

dVal(15.5V) = 742

And this line:

         if(val<751){                                 //Is battery voltage above 13.1V and below 15.5V?

Will become:

         if(val<742){                                 //Is battery voltage above 13.1V and below 15.5V?


After we’ve done these changes in the code, we’ll have to recompile and upload this modified program to ATtiny 13 as we did on step 4. Once ATtiny 13 is ready, put it back into the battery monitor board and re-run voltage tests we did in step 7 to make sure that we have indeed improved the performance of the code.

The 9.6V transition occurred right at 9.65V, an excellent improvement over our previous attempt. The difference is only 0.5% at this voltage level which is in compliance with the accuracy of most DC Voltmeters. The 12.4V transition occurred at 12.44V with a difference of 0.3%!!

Higher voltage transitions are more difficult to test due to the blinking effect. Based on our results so far those transitions should be OK. Keep in mind that this step was necessary only because we used 5% resistors. Remember, the values shown here for R1 and R2 were the ones I got on the two resistors I used. If you decide to use 5% resistors, use whatever values you obtain in the readings from your own 5% resistors. Besides, based on what I've read so far, I believe most Arduino buffs will love to tweak their code the same way we did here. If for any reason you need to change voltage levels in the future, you now know how this can be done. This procedure is not practical for production purposes, though. This is for educational purposes, only.

This step completes calibration and now we are ready for some real life testing.





Step 9: Final Testing.


We are now ready to troubleshoot our car with our battery monitor built around ATtiny 13. The first test we are going to do is to check the health of our battery in the engine compartment.

                                                          ****   Warning ****

This project is for educational purposes only. Whenever working in the engine compartment always remove from yourself jewelry or any object that could be trapped by moving parts, pulleys, belts, etc. while engine is running. Failure to do so may result in severe injury or death.  If you do not have experience working in the engine compartment, DO NOT perform these tests.  You can test your battery through the cigarette lighter receptacle, instead.

Car Batteries can put out a tremendous amount of electrical current which can be dangerous if not handled correctly.  Do not short circuit battery terminals in any way to check for “charge”. In addition to potential dangerous currents, electrical transients may damage on board computers and/or other sensitive electronic components that control the engine operation. If no damage occurs, important engine operating data stored in non-volatile memory may be erased or get corrupted.

Engine Compartment

First, make sure your car battery monitor is already inside an empty Tic-Tac box. Use the test probes with alligator clips and insert the connector into the power Jack of battery monitor. With engine off, pop the hood and hook up the red alligator clip to the positive terminal of battery and then the black alligator clip to the negative terminal. At this point if battery is OK, green LED should stay fully on. Have somebody start the engine while you watch the battery monitor. While cranking only yellow LED should come on and once engine starts only green LED will come on blinking indicating that battery is getting charged. If this is the case, your battery is in good working condition. If red LED comes on briefly while cranking this may be an indication that battery may be low on charge or near the end of its life.



Cigarette Lighter receptacle

With engine off, plug in the automobile power connector into the cigarette lighter receptacle and the PL2 connector end into the battery monitor. At this point, no LEDs will be on as there is no voltage available at this receptacle. Turn the key to the ACC position. If everything is fine, green LED should come on. Start the engine, you will notice that green LED will go out and no other LED would stay on while engine is cranking. Once engine starts, green LED should come on blinking. Blinking green LED indicates that battery is charging. To test battery charging while engine is idle with most loads on, turn on A/C, radio, windshield wipers, headlights, etc.  Green LED should continue blinking. IF LED stops blinking and stays fully on this could possibly indicate an alternator that is not putting out enough power to maintain charging voltage. Have a mechanic take a look at your charging system.

To test whether alternator is not overcharging, run the engine to at least 2,000 rpm with most loads on. Green LED should continue to blink as before. In the event red LED starts blinking this indicates a dangerous overvoltage is being fed into the battery which could possibly be caused by a faulty voltage regulator.

No comments:

Post a Comment