H-Bridge battery voltage measurement problem

Hi guys,

I'm making a robot and I'm having a problem with his drive.
I'm using a H-Bridge which is getting the power from a 9V battery to drive the motor. Since i want the robot to be able to determine how far he traveled I need to make a dependency on the battery voltage. Because when the battery discharges the motor rotates slower at the same pwm rate.

And now to the problem,
i need to be able to measure the voltage during the operation of the robot. I'm using a voltage divider circuit to split the battery voltage in half so that it is within the boundaries of 5V. When I measure only the battery, everything works fine, but the problem occurs when I connect it to the H-Bridge. I include the wiring diagram and the code that i use for the H-Bridge

wiring:

sketch:

//this sketch is intended to drive the robot for a given amount of time
//and then measure the battery voltage
//and physically measure distance it has driven
//so I can find out the speed it goes at given battery voltage

#include <bateeRobo.h>  //my library which contains a function to control the H-Bridge

int i = 0, beg, ende, m1 = 6, m2 = 11, aPin = 0, pwm, time;

void setup() {
    Serial.begin(9600);
}

void loop() {
    ++i;
    Serial.println("Enter pwm");
    while(Serial.available()==0);
    pwm = Serial.parseInt();
    
    Serial.println("Enter time (in ms)");
    while(Serial.available()==0);
    time = Serial.parseInt();
    
    Serial.print(i);
    Serial.print(": ");
    calibration(m1, m2, aPin, time, pwm);
}

void calibration(int pin1, int pin2, int measPin, int del, byte pwm) {
    int i;  
    
    //measurement of the battery voltage before the test run
    for (i = 0; i < 10; ++i ) {
      beg += analogRead(measPin);
      delay(10);
    }
    beg = beg / 10;
    
    writeHBridge(pwm ,pin1, pin2);  //set the H-Bridge pwm to 255, FWD
    delay(del);
    writeHBridge(0 ,pin1, pin2);  //stop the motor
    
    //measurement of the battery voltage after the test run
    for (i = 0; i < 10; ++i) {
      ende += analogRead(measPin);
      delay(10);
    }
    ende = ende/10;

    Serial.print(beg);
    Serial.print(" ");
    Serial.print(ende);
    Serial.print(" pwm: ");
    Serial.println(pwm);       
}

when the H-Bridge is powered off "writeHBridge(0 ,pin1, pin2)" I'm not able to measure the 1/2 of the battery voltage, the input in the analogPin 0 gives 1023 all the time, but when i disconnect one wire from the motor it measures correct values.(around 900)

then another interesting thing is when the motor is running at full pwm(255) the battery voltage measurement is also possible.
I assume that there are some current flows that i dont know where they originate at that do affect the measurement on the analog pin 0

i'm looking forward to your replies

That H-bridge circuit is fundamentally wrong in several ways.

  1. The circuit is upside down, you have -ve at the top and +ve at the bottom
  2. You have connected +ve to ground, this is the wrong way.
  3. The MOSFETs are not logic level, yet you are attempting to drive them with 5V, doesn't work.
  4. The bridge is running at 9V so you cannot drive it from 5V anyway as the p-channel
    devices will never turn off.
  5. You have shoot-through - as the H-bridge switches both n-channel and p-channel
    devices will be on together for a short time, causing current spikes.
  6. The MOSFETs you have chosen have far far too much on resistance, 5 and 15 ohms
    respectively - they should be a hundred to a thousand times better than that.
  7. you need current-limiting resistors on the Arduino outputs when driving capacitive
    loads like a MOSFET gate.

Oh man, thank you for the response,

i was lazy and did not draw an accurate picture. The H-Bridge works fine. I've been playing with it for 2 months now without any problem, of course the voltage drop on the MOSFETs is a bit of trouble.. normally the motor runs at 7V so it a 2V drop.
Do you think that using bipolar transistors would be better? I have BC 327s, BC 337s , C945 and A733 transistors at home.
do you think it would solve the problem with the measurement?

the actual circuit looks like this

I have found out that in turned off state the current is able to flow from the source to the drain on the MOSFETS

Hi, can you draw an accurate drawing for us please.
The problem with reading voltage is that when you are driving the motor the pwm is causing the battery voltage to vary, at pwm of 255 you have no modulation, just continuous current, hence the battery volts is not changing.
Placing a .1uF cap between A0 and gnd will provide you with an average battery voltage reading.
Also a 100uF 16V electro capacitor or larger depending on your motor requirements placed across the battery would also help.
What values are the voltage dropping resistors, what are the voltage and current ratings on the motor.
What size is the battery powering the motor, a picture of your project would also help.
I know its a lot for such a simple exercise but we need to know these things to help give you correct advice.

Thanks Tom... :slight_smile:
ps. your last posted circuit did not show in the post.

Batee:
i was lazy and did not draw an accurate picture.

It was a brilliant idea to post an incorrect schematic, when asking for help.

thanks for response, something must have happened to the image hosting site
circuit:

if it is not working then, http://postimg.org/image/cj6luqsz9/

here is the picture of my project, i dont know whether it is useful enough, i dont have a proper camera

in case it doesnt work: http://postimg.org/image/8g2rgyaan/

those voltage divider resistors are both 10K
im using BS170 n-channel and BS250 p-channel mosfets
the voltage on the motor is 5,7 V when the battery is fully charged(9,67 V) and the current it is drawing is 100 mA
here is the datasheet of the motor if needed, its the version 132-0050
http://www.produktinfo.conrad.com/datenblaetter/225000-249999/244040-da-01-de-Getriebemotor_Typ_33G.pdf

that idea with a capacitor before the analogPin 0 is great, howewer i need first to solve the measurement when the pwm is 255

i have measured the voltage divider circuit with a multimeter and it showed on both resistors 4,8 V, and the interesting thing is that the arduino measured 1030 - 1060. isnt its resolution only 1024?

The problem with reading voltage is that when you are driving the motor, the pwm is causing the battery voltage to vary, at pwm of 255 you have no modulation, just continuous current, hence the battery volts is not changing.

Is the reason for your 255 reading, the motor is drawing current that is causing the battery volts to change with each On Off cycle of PWM, at 255 there is no pulsing it is pure DC.
The current through the motor is 100mA, the spec says 380mA max, so with PWM the current is really closer to 380mA peaks, this causes the battery voltage to go up and down as well.
What size battery are you using to drive the motor?
I hope it is not the small 9V on the left of the picture.
Try both capacitor recomendations.
The 255 observation is not a fault, it is a result of battery and load and PWM.

Tom.... :slight_smile:
ps also not having the mosfets turn fully on will not help the situation.

The battery fucuation may depend on the H-bridge.
With output high from the aurdino the BS170 gets 5 volts gate to source. The BS250 gets 4 volts (9V-5V) and booth are conducting.
Perhaps not a short, but the 9 volt are reduced untill the BS250 get less gate - source voltage.

You must use an level shift (2 more BS170) and for best result use 4 outputs from the aurdino to make it possible to add an small pause between conducting the high side and low side.

the H bridge giving you troubles

Pelle

Hi, just looking closely at your circuit diagram, you should have the source of your p-ch fet connected to positive supply, the drain of the p-ch fet connected to the motor and to the drain of the n-ch fet, the source of the n-ch fet connected to gnd.
But even so as Pelleplutt says you will have problems with getting the p-ch fets to switch at all.

Tom.. :slight_smile:

Batee:
thanks for response, something must have happened to the image hosting site
circuit:

Most of the faults are still present on that circuit - they need fixing.

yes i do use that small 9V battery that is present on the picture. its just for the purpose of testing

ok, i have replaced the 10K resistors at the voltage divider and updated the code a bit. i made no change to the circuit.
(every shop is closed today and I dont have good capacitors at home)

now it seems to work, but if it works it still doesn't have to be good.
i have updated the code a bit. I made several measurements, with the arduino. here's the measurement code

void calibration(int pin1, int pin2, int measPin, int del, byte pwm) {
    int i;  
    
    //measurement of the battery voltage before the test run
    beg = analogRead(measPin);
    
    delay(100);
    writeHBridge(pwm ,pin1, pin2);  //set the H-Bridge pwm to 255, FWD
    delay(del);
    
    //measurement during operation
    mid = analogRead(measPin);
    
    delay(100);
    writeHBridge(0 ,pin1, pin2);  //stop the motor
    delay(100);
    
    //measurement of the battery voltage after the test run
    ende = analogRead(measPin);

    Serial.print(beg);
    Serial.print(" ");
    Serial.print(mid);
    Serial.print(" ");
    Serial.print(ende);
    Serial.print(" pwm: ");
    Serial.println(pwm);       
}

here are the results

BEG- measurement at the beginning of the test - motor turned off
MID - measurement during the running of the motor
END - measurement at the end - motor turned off
additional data - differences and their percentages

i tried to put a 0,15uF capacitor before the analog input pin, but t created nonsense then without it i could get pretty accurate results even with different pwm

then I did measure drain-source(DS), gate-source(GS) and gate-drain(GD) voltages using a multimeter both when the motor is running and when its off
results:
ON state

OFF state

i suppose that these values are not very satisfying

next i'm gonna try that level shift, interesting idea. How big should be the pause between low and high sides
i was also thinking about using a different MOSFET or BJT what would you recommend?

If you add drain source at on state for Q1+Q4 and Q2+Q3 it's just over 6 volts. Should be round 9 volts.
Measure current from battery and the current thru the motor.
I guess there is a difference.

Pause could be 1us.
Pelle

The original reason the OP gave for going to all this effort is the following:

Since i want the robot to be able to determine how far he traveled I need to make a dependency on the battery voltage. Because when the battery discharges the motor rotates slower at the same pwm rate.

However, this method simply won't work. In addition to the battery continually discharging, other factors will change the motor rotation rate (like going up a ramp or across a rug). Wheel encoders are a far better solution.

Thank you guys for your responses. I have made deeper research on H-Bridges and MOSFETs and I found out that this my bridge is really a complete fail, however it's accomplishing it's task :D. I'm now busy learning more about H-Bridges and stuff.

I was also thinking about encoders for measuring speed, but until I get my salary I'm a bit limited :D. I chose this way of measuring speed because I have pretty homogeneous conditions to test the robot out. And since that motor has a plenty of torque, I assume that the battery voltage has the most influence on speed.

Thank you guys for helping me get the right direction. I'll post here when I have something reasonable or a question :smiley:

The steeper the learning curve the better the view from the top!

Hi guys,

so here I'm again. After a lot of research I came up with this schematic. It took me this long to understand all of your previous advices you have given, because at first I didn't understand much a I tended not to believe you because of the lack of knowledge :smiley:

I decided to make a motor shield, and here it is.
It has an H-Bridge,
low dropout voltage regulator to power servos,
a current sensing circuit. I want to determine the speed of the motor by measuring the current flowing through it
and a voltage divider on the battery to measure its voltage.

On the software side I'm gonna add the required delay (it's in the datasheet ;)) to turn the MOSFET fully off when changing sense of direction

I made simulation of that circuit and everything seems to be ok with it.
I didn't start building it yet, I wanted you to see it if you had any remarks and further advices.