ni-mh battery charger Vin problem

hello, I found a schematic and program for a ni-mh battery charger I tried to alter it to charge 3 batteries

the original code is attached

here is my altered code
I'm using 1,2 typ. 4600mah batteries. First I'm not sure if I altered it correctly and second my 5V power supply isn't powering the micro-controller. I have attached the original schematic's the only difference is I've attached a 3AA battery pack (a battery holder with a on/off switch I have it set to on) I'm getting negative battery current readings I'm new to this so I'm not sure if that's normal

any help would be greatly appreciated, thanks

int batteryCapacity = 13800;     //capacity rating of battery in mAh (was 2500)
float resistance = 10.0;     //measured resistance of the power resistor
int cutoffVoltage = 15000;     //maximum battery voltage (in mV) that should not be exceeded (was1600)
float cutoffTemperatureC = 35;     //maximum battery temperature that should not be exceeded (in degrees C)
//float cutoffTemperatureF = 95;     //maximum battery temperature that should not be exceeded (in degrees F)
long cutoffTime = 46800000;     //maximum charge time of 13 hours that should not be exceeded

int outputPin = 9;     // Output signal wire connected to digital pin 9
int outputValue = 150;     //value of PWM output signal 

int analogPinOne = 0;     //first voltage probe connected to analog pin 1
float valueProbeOne = 0;     //variable to store the value of analogPinOne
float voltageProbeOne = 0;     //calculated voltage at analogPinOne

int analogPinTwo = 1;     //second voltage probe connected to analog pin 2
float valueProbeTwo = 0;     //variable to store the value of analogPinTwo
float voltageProbeTwo = 0;     //calculated voltage at analogPinTwo

int analogPinThree = 2;     //third voltage probe connected to analog pin 2
float valueProbeThree = 0;     //variable to store the value of analogPinThree
float tmp36Voltage = 0;     //calculated voltage at analogPinThree
float temperatureC = 0;     //calculated temperature of probe in degrees C
//float temperatureF = 0;     //calculated temperature of probe in degrees F

float voltageDifference = 0;     //difference in voltage between analogPinOne and analogPinTwo
float batteryVoltage = 0;     //calculated voltage of battery
float current = 0;     //calculated current through the load (in mA)
float targetCurrent = batteryCapacity / 10;     //target output current (in mA) set at C/10 or 1/10 of the battery capacity per hour
float currentError = 0;     //difference between target current and actual current (in mA)



void setup()
{
  Serial.begin(9600);     //  setup serial
  pinMode(outputPin, OUTPUT);     // sets the pin as output
}



void loop()
{
    
  analogWrite(outputPin, outputValue);  //Write output value to output pin

  Serial.print("Output: ");     //display output values for monitoring with a computer
  Serial.println(outputValue); 

  valueProbeOne = analogRead(analogPinOne);    // read the input value at probe one
  voltageProbeOne = (valueProbeOne*5000)/1023;     //calculate voltage at probe one in milliVolts
  Serial.print("Voltage Probe One (mV): ");     //display voltage at probe one
  Serial.println(voltageProbeOne);  
  
  valueProbeTwo = analogRead(analogPinTwo);    // read the input value at probe two
  voltageProbeTwo = (valueProbeTwo*5000)/1023;     //calculate voltage at probe two in milliVolts
  Serial.print("Voltage Probe Two (mV): ");     //display voltage at probe two
  Serial.println(voltageProbeTwo);  
  
  batteryVoltage = 15000 - voltageProbeTwo;     //calculate battery voltage (was 500)
  Serial.print("Battery Voltage (mV): ");     //display battery voltage
  Serial.println(batteryVoltage); 

  current = (voltageProbeTwo - voltageProbeOne) / resistance;     //calculate charge current
  Serial.print("Target Current (mA): ");     //display target current 
  Serial.println(targetCurrent);  
  Serial.print("Battery Current (mA): ");     //display actual current
  Serial.println(current);  
      
  currentError = targetCurrent - current;     //difference between target current and measured current
  Serial.print("Current Error  (mA): ");     //display current error 
  Serial.println(currentError);     

  valueProbeThree = analogRead(analogPinThree);    // read the input value at probe three  
  tmp36Voltage = valueProbeThree * 5.0;     // converting that reading to voltage
  tmp36Voltage /= 1024.0; 
 
  temperatureC = (tmp36Voltage - 0.5) * 100 ;     //converting from 10 mv per degree wit 500 mV offset to degrees ((voltage - 500mV) times 100)
  Serial.print("Temperature (degrees C) ");     //display the temperature in degrees C
  Serial.println(temperatureC); 
 
 /*
  temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;     //convert to Fahrenheit
  Serial.print("Temperature (degrees F) ");
  Serial.println(temperatureF); 
 */
 
  Serial.println();     //extra spaces to make debugging data easier to read
  Serial.println();  



  if(abs(currentError) > 10)     //if output error is large enough, (adjust output was 10)
   {
    outputValue = outputValue + currentError / 10;

    if(outputValue < 1)    //output can never go below 0
     {
      outputValue = 0;
     }

    if(outputValue > 254)     //output can never go above 255
     {
      outputValue = 255;
     }
    
    analogWrite(outputPin, outputValue);     //write the new output value
   }
 
 
  if(temperatureC > cutoffTemperatureC)     //stop charging if the battery temperature exceeds the safety threshold
   {
    outputValue = 0;
    Serial.print("Max Temperature Exceeded");
   }
   
  /*
  if(temperatureF > cutoffTemperatureF)     //stop charging if the battery temperature exceeds the safety threshold
   {
    outputValue = 0;
   }
   */
   
   if(batteryVoltage > cutoffVoltage)     //stop charging if the battery voltage exceeds the safety threshold
   {
    outputValue = 0;
    Serial.print("Max Voltage Exceeded");
   }  
 
   if(millis() > cutoffTime)     //stop charging if the charge time threshold
   {
    outputValue = 0;
    Serial.print("Max Charge Time Exceeded");
   }  

   delay(10000);     //delay 10 seconds before next iteration
}

Arduino_Controlled_Battery_Charger_Code.ino (5.39 KB)

Sorry everyone I always forget to check the format and size of my attachments here is a link with both the schematic and diagram https://www.allaboutcircuits.com/projects/create-an-arduino-controlled-battery-charger/

Hi.
OP's circuit


Tom... :slight_smile:

I'm sorry I'm unclear of your meaning. Are you saying I need to add an operational amplifier?

Sorry noob here. lol

Hello Raptor,

TomGeorge has posted your schematic on this site, for the benefit of others, so they don't have to download it from an external site.

He was referring to you as the OP (= Original Poster), and not suggesting that you needed an op-amp.

oh, thanks. I'm still having problems with the Vin I took an old USB controller and made it to a female 5V USB cord I'm only getting 4.56V is that the problem?

raptor:
oh, thanks. I'm still having problems with the Vin I took an old USB controller and made it to a female 5V USB cord I'm only getting 4.56V is that the problem?

Possibly.

At full change the voltge of an NiMh is circa 1.5V, possibly a bit more, so you may not have enough volts avaialble for the charger to work properly.

I just tried a 12V DC 2A and it's still not powering the micro-controller. :frowning:

raptor:
I just tried a 12V DC 2A and it's still not powering the micro-controller. :frowning:

What design changes to the circuit did you make ?

Hi,
Where did you connect the 12V 2A?

Can you post a picture of your project so we can see you layout?

Thanks.. Tom.. :slight_smile:

Hi,
Your analog pins are A1 and A2, not 1 and 2.

If A1 is battery voltage and A2 is from the temperature sensor, why have you got the difference between them representing CURRENT.

You have no circuitry to measure current.

Can you tell us your electronics, programming, Arduino, hardware experience?

If you are new to programming and arduino, then copying code like this will not help you.

You need to learn how to program so you can solve the problems you have.

Tom... :slight_smile:

raptor:
I'm using 1,2 typ. 4600mah batteries.

When you connect the three cells in series, the capacity stays the same.
so the following line in your code is wrong:

int batteryCapacity = 13800;     //capacity rating of battery in mAh (was 2500)

The code tries to set the current to C/10, which is 1.38A. (above the limit of a standard USB port).
It is impossible to get this current From a 5V supply, when the load is 3 cells and a 10Ω resistor.
The current is likely to be in the region of 40 - 80mA, depending on the state of charge of the cells.

I had never come across 4600mAh AA cells before.
When I looked into them, I found this investigation into the [real capacity of 4600mAh NiMH cells](http://lygte-info.dk/review/batteries2012/Flash AA 4600mAh (Yellow-blue) UK.html).

@srnet:
I've made no changes to the circuitry when I applied the 12V 2A power supply I put the neg. and pos. in the same place as in the diagram.

@Tom:
I have a lot of experience with soldering but very little experience programming, I'm trying to teach myself. I know I won't learn anything by copy and pasting but this project is for a friend and he needs it soon (his chameleons are very unhappy lol)
I have A0 connected to the the middle pin of the IRF510

@john: you're right I'm getting reading of -49 to 49ish
thanks for the information

raptor:
I found a schematic and program for a ni-mh battery charger I tried to alter it to charge 3 batteries

The 'designer' of that circuit seems to think that feeding the input of the 5V regulator on a UNO with 5V is enough for it to operate properly.

The original circuit design is really bad -- it is very clear that the author doesn't understand electronics.

Note the 10 Megohm gate resistor and cap on the (not logic level) MOSFET!

Please look for another design, and post here if you want us to evaluate it.

well I have the 12V 2A as the main power supply and attached the Vin the a 7805 with a 100uf cap connected to the 12V and the input of the 7805, a 10uf cap to the output of the 7805. both caps have a common ground.
the link light is lit but dim the on and TX are fine but my readings are weird.

int batteryCapacity = 2500;     //capacity rating of battery in mAh (was 2500)
float resistance = 10.0;     //measured resistance of the power resistor
int cutoffVoltage = 15000;     //maximum battery voltage (in mV) that should not be exceeded (was1600)
float cutoffTemperatureC = 35;     //maximum battery temperature that should not be exceeded (in degrees C)
//float cutoffTemperatureF = 95;     //maximum battery temperature that should not be exceeded (in degrees F)
long cutoffTime = 46800000;     //maximum charge time of 13 hours that should not be exceeded

int outputPin = 9;     // Output signal wire connected to digital pin 9
int outputValue = 150;     //value of PWM output signal 

int analogPinOne = 0;     //first voltage probe connected to analog pin 1
float valueProbeOne = 0;     //variable to store the value of analogPinOne
float voltageProbeOne = 0;     //calculated voltage at analogPinOne

int analogPinTwo = 1;     //second voltage probe connected to analog pin 2
float valueProbeTwo = 0;     //variable to store the value of analogPinTwo
float voltageProbeTwo = 0;     //calculated voltage at analogPinTwo

int analogPinThree = 2;     //third voltage probe connected to analog pin 2
float valueProbeThree = 0;     //variable to store the value of analogPinThree
float tmp36Voltage = 0;     //calculated voltage at analogPinThree
float temperatureC = 0;     //calculated temperature of probe in degrees C
//float temperatureF = 0;     //calculated temperature of probe in degrees F

float voltageDifference = 0;     //difference in voltage between analogPinOne and analogPinTwo
float batteryVoltage = 0;     //calculated voltage of battery
float current = 0;     //calculated current through the load (in mA)
float targetCurrent = batteryCapacity / 10;     //target output current (in mA) set at C/10 or 1/10 of the battery capacity per hour
float currentError = 0;     //difference between target current and actual current (in mA)



void setup()
{
  Serial.begin(9600);     //  setup serial
  pinMode(outputPin, OUTPUT);     // sets the pin as output
}



void loop()
{
    
  analogWrite(outputPin, outputValue);  //Write output value to output pin

  Serial.print("Output: ");     //display output values for monitoring with a computer
  Serial.println(outputValue); 

  valueProbeOne = analogRead(analogPinOne);    // read the input value at probe one
  voltageProbeOne = (valueProbeOne*5000)/1023;     //calculate voltage at probe one in milliVolts
  Serial.print("Voltage Probe One (mV): ");     //display voltage at probe one
  Serial.println(voltageProbeOne);  
  
  valueProbeTwo = analogRead(analogPinTwo);    // read the input value at probe two
  voltageProbeTwo = (valueProbeTwo*5000)/1023;     //calculate voltage at probe two in milliVolts
  Serial.print("Voltage Probe Two (mV): ");     //display voltage at probe two
  Serial.println(voltageProbeTwo);  
  
  batteryVoltage = 14000 - voltageProbeTwo;     //calculate battery voltage (was 500)
  Serial.print("Battery Voltage (mV): ");     //display battery voltage
  Serial.println(batteryVoltage); 

  current = (voltageProbeTwo - voltageProbeOne) / resistance;     //calculate charge current
  Serial.print("Target Current (mA): ");     //display target current 
  Serial.println(targetCurrent);  
  Serial.print("Battery Current (mA): ");     //display actual current
  Serial.println(current);  
      
  currentError = targetCurrent - current;     //difference between target current and measured current
  Serial.print("Current Error  (mA): ");     //display current error 
  Serial.println(currentError);     

  valueProbeThree = analogRead(analogPinThree);    // read the input value at probe three  
  tmp36Voltage = valueProbeThree * 5.0;     // converting that reading to voltage
  tmp36Voltage /= 1024.0; 
 
  temperatureC = (tmp36Voltage - 0.5) * 100 ;     //converting from 10 mv per degree wit 500 mV offset to degrees ((voltage - 500mV) times 100)
  Serial.print("Temperature (degrees C) ");     //display the temperature in degrees C
  Serial.println(temperatureC); 
 
 /*
  temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;     //convert to Fahrenheit
  Serial.print("Temperature (degrees F) ");
  Serial.println(temperatureF); 
 */
 
  Serial.println();     //extra spaces to make debugging data easier to read
  Serial.println();  



  if(abs(currentError) > 10)     //if output error is large enough, (adjust output was 10)
   {
    outputValue = outputValue + currentError / 10;

    if(outputValue < 1)    //output can never go below 0
     {
      outputValue = 0;
     }

    if(outputValue > 254)     //output can never go above 255
     {
      outputValue = 255;
     }
    
    analogWrite(outputPin, outputValue);     //write the new output value
   }
 
 
  if(temperatureC > cutoffTemperatureC)     //stop charging if the battery temperature exceeds the safety threshold
   {
    outputValue = 0;
    Serial.print("Max Temperature Exceeded");
   }
   
  /*
  if(temperatureF > cutoffTemperatureF)     //stop charging if the battery temperature exceeds the safety threshold
   {
    outputValue = 0;
   }
   */
   
   if(batteryVoltage > cutoffVoltage)     //stop charging if the battery voltage exceeds the safety threshold
   {
    outputValue = 0;
    Serial.print("Max Voltage Exceeded");
   }  
 
   if(millis() > cutoffTime)     //stop charging if the charge time threshold
   {
    outputValue = 0;
    Serial.print("Max Charge Time Exceeded");
   }  

   delay(10000);     //delay 10 seconds before next iteration
}

I think jremington is right and I might just scrap it.

It should be a workable circuit but the cutoff voltage should be equal to the total battery pack voltage + about 5-8% or so.
I see a value of 15000 so 15 volts. Is this correct for your 3 batteries in series? Then your main supply should be more than 15 volts.
The gate resistor should be a lower value, say 10k ohm and there should be a resistor from gate to ground of about 100k ohm.
Remember that power mosfet transistors are sensitive and can be destroyed by static and careless handling. The original circuit puts the gate in a perilous position in that regard- the fet could be damaged already.
If your supply needs to be more that 12 volts there should be a current limiting/voltage dropping resistor between the supply and the arduino Vin. Use ohm's law to calculate the value. Desired voltage drop divided by the expected arduino input current in amps equals the value in ohms. It may have to be a fairly hefty resistor like 1 watt or more.
Mark

It should be a workable circuit

Just barely workable, with the indicated, non-logic-level MOSFET.

thank you so much mark I'm going to try that out soon, but I'm a noob. when you say "gate" do you mean the emitter, collector, or base?

raptor:
thank you so much mark I'm going to try that out soon, but I'm a noob. when you say "gate" do you mean the emitter, collector, or base?

Gate was the correct term.

MOSFETS (Metal Oxide Semiconductor Field Effect Transistors) have gate, drain and source connections.
Bipolar Transistors have base, emitter and collector.

Incidently if you get the circuit or code foe the charger wrong, NiMh batteries get hot very quickly if charge is not stopped when full charge is reached.