cancel
Showing results for 
Search instead for 
Did you mean: 

ADC read from INA139 current sensor on STM32F03 - too high readings

AAlex.4
Associate II

Hi guys,

I have an issue reading a current sensor based on INA139 ic on an STM32F03 mcu - the readings I'm getting are too high. I cannot understand what is wrong with my setup. I really hope someone can help.

The current meter based on INA139 ic is outputing about 23.5mV per 1 amp.

0693W000001cN75QAE.png

The above is my first setup and with less that 1A to the load the STM32F03 is reading about 47Amps. When I test the current meter output alone with a multimeter I can see few millivolts indeed. When I test the PIN9 output with a multimeter (nothing connected to it) my multimeter shows about 3.6v. When current sensor is connected to the MCU multimeter shows 1.1V (which were indeed about 47 Amps if INA139 would have been outputing 1.1v, but it is not). And the question is why is this happening?

With my second setup I use a LM358A opamp in between them like in the picture (same design as here http://forembed.com/creating-the-current-sensor-board.html and as the datasheet suggest to use a buffer to improve the accuracy of the readings):

0693W000001cN7oQAE.png

This time (load less that 1A) but MCU readings are about 25Amps, and multimeter readings on pin 9 is 0.6V. But when testing INA139 + LM358 setup alone the output of the opamp is correct (about 23 mV for 1 Amp load).

Note that if I attach to pin 9 a resistor divider 10k 1k and input 12v then the readings on that pin is very close to correct readings.

The code that I'm using is about right:

(some pieces of code)

IO_CONFIG(GPIO_Mode_AN, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)

sampleTime = ADC_SampleTime_601Cycles5

RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz

there is even calibration procedure

Entire code is here https://github.com/betaflight/betaflight/blob/4.0.4/src/main/drivers/adc_stm32f30x.c starting with line 112.

So my question is why STM32F03 can't read rather low values from INA139, and my multimeter is showing such high voltages on the pin itself when INA139 output seems to be correct?

Thank you in advance.

9 REPLIES 9
TDK
Guru

> When I test the PIN9 output with a multimeter (nothing connected to it) my multimeter shows about 3.6v.

So PC1 is at 3.6V, which is above your VDD voltage of 3.3V? With R1 disconnected? Doesn't sound right. If that is the case, you have a power supply or shorted pin error somewhere.

If you feel a post has answered your question, please click "Accept as Solution".
Dvorak.Peter
Senior II

"When I test the PIN9 output with a multimeter (nothing connected to it) my multimeter shows about 3.6v. "

This would suggest a fried input stage on that pin.

Try an alternate pin for your ADC. A normal ADC pin should not be pulling up that much.

I would use your first schematic with 100nF capacitor in parallel to 47kohms.

The second schematic is just not going to work well for number of reasons.

So PC1 is at 3.6V, which is above your VDD voltage of 3.3V?  - right.

With R1 disconnected? - yes.

I'm positive power supply is ok. On the board there is a 3.3v regulator which is converting 3.3v from 5.1v and apart from that this MCU seems to be working fine. It is the brain of a quad which flies ok. Trying to figure out what's wrong with the current readings.

"This would suggest a fried input stage on that pin" - changed the code and made it as a digital pin that drives a led on off every half a second. It works fine.

"Try an alternate pin for your ADC." Tried PC2 - exactly the same behavior. But if I make a resistor divider the reading on both PC1 and PC2 are almost exact.

"I would use your first schematic with 100nF capacitor in parallel to 47kohms." - tried on PC2 and reading are 65 Amps (that's maximum value ADC can read) and multimeter shows those ~3.6 volts.

"The second schematic is just not going to work well for number of reasons." - 4.2.7 from https://www.st.com/resource/en/application_note/cd00211314-how-to-get-the-best-adc-accuracy-in-stm32-microcontrollers-stmicroelectronics.pdf talks about my case here. INA139 output is a current source and has high impedance. So I tried the opamp hoping that will fix my issue but it is not.

The problem is likely related to another STM32F03 mcu pin connected to 5V logic. ( or >3.3V )

Just checked PC2 with a variable resistor divider (5.1 - var 33k - PC2 - 3k9 - GND) and I got this:

PC2 voltage | Value read in code | Calculated value

0.9v | 1300 | 1116

1v | 1400 | 1241

2.07v | 2370 | 2568

3.08v | 3280 | 3822

So with voltage dividers I get some readings. It is strange that for low values the readings are a bit high, but with high values the readings are lower. The read values are a bit high. With 3.08v on the pin it is almost close to the maximum readings. So at 3.1v to 3.3v it always shows me maximum readings.

One other strange thing is that 33k + 3k9 would give me the lowest 0.54V at PC2, but the multimeter is showing 0.87V instead. The multimeter readings are correct, so that means that 33k+3k9 voltage divider cannot pull PC2 down to 0.54V.

I will try to check all pins to see if there is any other that has a voltage more that 3.3v on it.

AAlex.4
Associate II

@Dvorak.Peter​ @TDK​ thanks for you help. I found a solution to my problem. While checking all the pins voltages I found out that I can actually use PA2 for my purpose and it seems to be working fine (now I get low voltages and the reading is under 1A which seems to be right).

Regarding the overvoltage pins here is what I have found:

0693W000001cg9oQAA.png

This is actually original schematics, but I removed the 10k resistor and the 1k LED resistor. Instead of PC1 1k I used 100R resistor, and instead of 12v input I'm using it as current sensor input (there is actually a 5.1v input and a 3.3v regulator on the board that powers the MCU, so why 12v was actually connected to PA2 is a mystery for me. The documentation said only about the LED that shows 12v rail is up). Unfortunately I didn't know that 12v input is directly connected to PA2, so I tried to use PC1 instead.

Anyway, in the picture you can see all the pins with voltages more than 3.3v. PD2 had the higher voltage so I removed the 10k resistor and as a result PB0 voltage dropped to ~0v. But PC1-PC3 & PA1 didn't dropped. I still have no idea why is that, but I'm pretty sure I checked all the pins for voltages higher than 3.3v.

Thankfully I found out that PA2 can be actually used and it works fine for me now.

TDK
Guru

Are you saying that PA2 was connected directly to the 12V rail? And that, after fixing that, the chip, including the PA2 pin, still works? That's surprising to me.

If you feel a post has answered your question, please click "Accept as Solution".

The thing is that this board was designed as part of a stack of boards. The 5V and 12V rails were made available to all the boards in the stack. Documentation says on my board that there is a status led connected to it to show that 12V is up. But it seems that there is more than that (10k 1k resistor divider and direct connection to PA2). Here is a picture:

0693W000001cgt3QAA.png

I don't have the full stack, only the top board, and I never put 12V o it. That's why PA2 is still alive.