H-bridge current measurment - Page 1
Forum section: electronics / beginners

author: RARvolt
author_rank: Newbie
timestamp: November 01, 2020, 11:53:22 pm
content: I've been looking around the forum for some time and this is my first post, so hello everybody!For my engineering thesis, I am building a two axis inverted pendulum stabilised with reaction wheels (something similiar tothis).I am using there STM32F411 (aka. BlackPill) microcontroller. It controls two 12V DC motors (Pololu #4757) using two H-bridge modules -Cytron MD13S. So I can control two motors with PWM and DIR each.I also wanted to measure the current draw from each motor and since the hall effect current sensors I had on hand had to wide range I decided to build my own current sensing circuit with shunt resistors.I want to measure current (max 5A) in both directions and have it converted into voltage 0-3.3V (for µC's ADC), so when there is no current flowing the ADC will read Vcc/2. I foundthis application notefrom ST and used schematic from section 2.1:I worked out resistors values as follows:$$I_{max}=5A\\V_{DD}=3.3V\\R_{s}=50m\Omega\\V_{s}=0.25V\\P_{Rs}=1.25W\\G_{max}=\frac{V_{DD}}{2I_{max}R_{s}}=6.6\\G=6\\\frac{R_2}{R_1}=G-0.5=5.5\\R_1=2k\Omega\\R_2=11k\Omega\\2R_2=22k\Omega\\f_{LP,o}=\frac{1}{2{\pi}R_{LP}C_{LP}}\\R_{LP}=2k\Omega\\C_{LP}=100nF\\f_{LP,o}\approx800Hz$$I then created following schematic (x2 for two motors):I got the PCBs from JLCPCB, soldered them, tested voltages in various places, inserted programmed microprocessor and found out that when the motor is spinning in one direction everything is fine, I'm getting reasonable readings, but when motor is spinning in other direction readings are just maxed out to the end of the scale.Example:Motor spinning left - ~1.7V -OKMotor not spinning - 1.65V -OKMotor spinning right - 0V -BADIf I reverse the polarity of the connections I am getting the same result just reversed.Shunt resistor is connected in series with the motor between A and B connectors on the MD13S module.I am attaching full schematic below.What am I missing here?

author: David Hess
author_rank: Super Contributor
timestamp: November 02, 2020, 04:09:13 am
content: I do not think this is your problem but the original circuit has a flaw. The feedback capacitor across R2 needs to be balanced with the same time constant on the non-inverting side so half value capacitors across the 2xR2 resistors. Without this, the AC common mode rejection will be compromised. It may be better to just leave the feedback capacitor out or use the least possible capacitance and rely on CLP to limit bandwidth.Where is the difference amplifier connected? If the source side of the transistors, then it will only see current in one direction.

author: S. Petrukhin
author_rank: Super Contributor
timestamp: November 02, 2020, 06:33:42 am
content: Quote from: RARvolt on November 01, 2020, 11:53:22 pmWhy is it bad? This is how it should be. Vcc/2 is the midpoint, the starting point. An increase in the voltage from it means an increase in the current in direct rotation. The voltage drop from it is an increase in the current in the reverse rotation.You need to make the value obtained from the ADC signed and subtract Vcc/2 from it. Then positive values will indicate the forward value current, negative values will indicate the reverse rotation current, 0-stop.If this is inconvenient, you can enter a flag for the direction of rotation.Code:varReversDirect : boolean;Current : integer;beginReversDirect:=false;Current:=ADC-Vcc/2;if Current<0 the beginReversDirect:=true;Currenr:=-Current;end;end;Also, both lower arms of the bridge must be connected to the current sensor resistor, not one of them.

author: RARvolt
author_rank: Newbie
timestamp: November 02, 2020, 11:18:06 am
content: Quote from: S. Petrukhin on November 02, 2020, 06:33:42 amI have radings from ADC reversed, so 0 means Vcc and 4096 means 0VAny rotation in reverse direction results in reading going straight to zero.The motor is powered from separate 12V PSU. I have grounds connected in single point.I tried powering the load from 3.3V and I had good readings in both directions.When I powered the load from 5V, I measured around 0.5V on non-inverting input of the TLV2316 (pin 5).When I reversed the polarity of the supply (swapped VPP and GND on schematic below), I got 4V.Quote from: David Hess on November 02, 2020, 04:09:13 amBelow is rough schematic of how shunt resistor is connected. Currently I am not powering motor with PWM, just straight DC from PSU.

author: S. Petrukhin
author_rank: Super Contributor
timestamp: November 02, 2020, 12:06:30 pm
content: Quote from: RARvolt on November 02, 2020, 11:18:06 amWait, I didn't understand your problem right away. I thought you were taking a measurement on the shunt to the engine. To measure the DC motor current in any direction, you can measure it up to the H-bridge, then you will not have negative values, you will not need to make the zero point with an offset of half Vcc. Whichever way the H-bridge turns on the motor, it will always draw current from the power supply in one direction. And the lack of signal in your experiment when changing Gnd and Vpp, I think, is due to the fact that the OP and shunt lands were connected.Note the division of land in figure 8.

author: RARvolt
author_rank: Newbie
timestamp: November 02, 2020, 12:48:24 pm
content: I kinda want to read negative values, because I am going to implement a regulator that takes into account actual current of the motor to calculate the force applied to the reaction wheel.Actually - when I know the direction of the motor from the encoder I can just invert the reading - right? In that case, I need to connect GND of the driver through the sense resistor and I could get full 12 bit reading in both directions.There will be some offset caused by the current draw from electronics inside the driver, but I can zero it out.I need to remove R10, R11, R15, and R18 from the circuit. Then I will add a second 2k resistor on top of R16 to get 1k (Gain=12). Do I need the 2k R12 then? Should I short it or leave it?

author: voltsandjolts
author_rank: Unknown
timestamp: November 02, 2020, 01:48:06 pm
content: You could put your sense resistor at the bottom end of the H-bridge to sense current in both directions.Sense voltage is always positive but you know which way the bridge is going.Edit: Ahh, I see, maybe that is what you are doing in the above post.

author: RARvolt
author_rank: Newbie
timestamp: November 02, 2020, 02:07:33 pm
content: Quote from: voltsandjolts on November 02, 2020, 01:48:06 pmI originally connected sense resistor like below, but now it is in the configuration you showed above.I am still wondering how I should wire the circuit to get bidirectional current measurement such that$$I_{max}=Vcc\\I_0=Vcc/2\\-I_{max}=0V\\Vcc=3.3V$$

author: Jeroen3
author_rank: Super Contributor
timestamp: November 02, 2020, 02:51:23 pm
content: If you are going with the approach in series with the motor, save yourself the trouble and use a high side current amplifier capable of the 12V common mode. Eg: INA240.Make sure you don't have (negative) transients during the transition between fet and diode. (diodes are not infinitely fast and the motor is an inductor)For motor current control in series with the motor is the best place, since it can also measure the braking current. But it is the most complicated shunt placement due to common mode change and transients. Since the voltage at the shunt changes from Gnd to +12V each PWM cycle.

author: S. Petrukhin
author_rank: Super Contributor
timestamp: November 02, 2020, 04:08:39 pm
content: Quote from: RARvolt on November 02, 2020, 02:07:33 pmWhy do you need to measure a negative current value? You know the direction, you just need to measure the current-the voltage drop on the h-bridge shunt to gnd. So you will have the full ADC range of 4096 steps to measure the current, not half of it.

author: David Hess
author_rank: Super Contributor
timestamp: November 03, 2020, 01:43:07 am
content: Quote from: S. Petrukhin on November 02, 2020, 04:08:39 pmBipolar measurement of the current allows knowing the braking direction. That cannot happen in this case but other circuit configurations would allow it.

author: mawyatt
author_rank: Super Contributor
timestamp: November 03, 2020, 01:50:24 am
content: Here's a link to a Hall Effect current sensor developed for measuring Stepper Motor Sine and Cosine coil currents, might be useful.https://www.photomacrography.net/forum/viewtopic.php?f=25&t=39689&hilit=Motor+Current+SensorBest,