Current Sensing using L298

Hi, I am controlling a DC motor with Arduino and using the L298 as an H-bridge for driving the motor.

The H-bridge I am using is the one in the link below:
http://www.kuuco.com/images/propic/090433_Y14.jpg

I am giving the H-brdige PWM from the Arduino and everything is working as desired. However, I need to control the current of the motor since I need to control the torque of the motor directly. I found a lot of post saying that you need to read the current via the sense pin of the L298. This is done by putting a low value resistor, which in my case I used a 0.5ohm 5w ceramic resistor as was suggested, between the sense pin and ground. Then I fed this to the ADC by connecting a wire between the sense pin and the resistor. I set the reference voltage of the ADC to 1.1V. I had positive results since when I started to try a stop the motor with my hand the values of the ADC, which I was seeing using the Serial Monitor, started to increase in value. However, the values were drifting too much (about 4times as much). I also tried to introduce a passive low pass filter but I was having a delay and now I am using an active 8th order filter (MAX292) but I still dont get the desired results since either I am getting a slow response or otherwise I am getting a noisy signal.

First of all, have anyone ever managed to use such method an have positive results? And how can I improve my readings because I have my doubts on how accurate they are. Or else is there another method which does not involve such complications? Because at first I thought it is a simple task and now I am realizing that I am going too much into electronics and other complicated stuff. Thanks a lot! :slight_smile:

The noise is probably due to interference from the high currents being switched
being imposed on the ground wiring because you only use one ground wire
for everything.

Wire the ground side of the sense resistor directly to the Arduino ground (and remove
all other ground connections between module and Arduino). Wire the motor supply
ground as normal (not via the Arduino).

The wire from the sense resistor to the analog pin ought to be a twisted pair with
the Arduino/sense-resistor ground wire. This reduces EMI pick up and helps
ensure the voltage reaching the Arduino is the actual voltage across the sense
resistor. Power the Arduino separately and using a different wire to the one joining
the Arduino to L298 module.

The normal way to sense current is to use an opamp right at the shunt resistor to
boost the voltage to sensible levels at source. The boosted voltage is much less
sensitive to interference.

I think if you had an oscilloscope it would be obvious why the readings are fluctuating. You stated you are using PWM to control the driver. Is PWM a steady state signal ? What do you think the current waveform looks like when you put a scope on the same resistor you are measuring with an analog input. Why don't you sample that point at 40,000 samples per second and then plot it in Excel and you will see the current waveform tracks the PWM waveform but lags it. If you DO NOT use PWM and simply turn the motor on and off using the direction and En , the voltage at the point you are measuring would be much more stable but could still have some fluctuation. I can sample at 40,000 samples per second using the analogBinLogger sketch because I have an SD shield to store the Bin files to. I also have a scope so I don't need the analog samples, but what I am saying is if I do exaactly what you are doing and put a scope on that point while collecting samples at 40,000 samples per second, the plot would look like the current waveform on the scope , tracking the PWM . I can't think of any explanation that would justify a steady state signal on the current sense pin of the motor path to ground during PWM operation. Can you ?

@MarkT: I will try this..so I dont need any low pass filter then (between the sense pin and the ADC)? And its better if I introduce a non-inverting amplifier to amplify the signal? By how much gain ideally?

@raschemmel: I used an oscilloscope and saw what I was getting on the sense pin was very noisy. I'm using the Arduino Nano and if I'm not mistaken its sampling rate is 10kHz.

Ok, then I guess that's your limit.
I would use a Low Pass RC FILTER and a non-inverting op amp amplifier as well.

Would you opt for a passive L.P. RC filter or an active one? Because when I used a 3 stage passive filter the signal was improving but I was getting a delay in the response. Then I used the MAX292 an 8th order filter IC and signal really improved when it came to noise but the response was really delayed. I am also implementing filtering through software by averaging 5 points. I think its a give and take situation were you must find the optimum point between a clean signal vs response time. I appreciate any other ideas. Thanks

I think if you eliminate the filters and just average the analog (ten readings), the accuracy will probably be ok and there shouldn't be much a delay. (1 mS for ten samples).

The analogRead sample speed is a function of the Pre-Scaler for the Timer used for the ADC CLOCK.
The program I told you about changes that frequency making it possible to sample at 40,000 samples per second:
http://forum.arduino.cc/index.php?topic=228549.0

You cannot really control both the current and the voltage. You control one or the other, and the other one is what it is.

However, this does not stop you from controlling the voltage some of the time, and the current at other times.

Actually , I think michinyon hit on the solution (intentionally or unintentionally ) because it is a simple matter to insert a Series Pass Transistor or Mosfet to control the motor power voltage going to the L293 and based on the read current, increase or decrease the drive current (transistor ) or drive voltage (Mosfet) of the series pass element.
There is nothing exotic about that. It is OLD SCHOOL simple linear regular design...

raschemmel:
Actually , I think michinyon hit on the solution (intentionally or unintentionally ) because it is a simple matter to insert a Series Pass Transistor or Mosfet to control the motor power voltage going to the L293 and based on the read current, increase or decrease the drive current (transistor ) or drive voltage (Mosfet) of the series pass element.
There is nothing exotic about that. It is OLD SCHOOL simple linear regular design...

That's probably going to generate even more heat than an L293, if that's possible!

How else can you control the motor voltage programatically ?
I'm open to suggestions.