stm32f103 having problem reading multiple adc pins. [SOLVED]

Post Reply
JovaS
Posts: 5
Joined: Wed Aug 02, 2017 3:54 pm

stm32f103 having problem reading multiple adc pins. [SOLVED]

Post by JovaS » Wed Aug 02, 2017 4:09 pm

I'm using blue pill and have a problem reading from analog input
Here's the code:

void setup(){
pinMode(PA0, INPUT_ANALOG);
pinMode(PA1, INPUT_ANALOG);
Serial.begin(9600);
}

void loop(){
Serial.print(analogRead(PA0)*3300/4096);
Serial.print(" ");
Serial.println(analogRead(PA1)*3300/4096);
delay (1000);
}

If just one pin is connected to a source all reading is ok. But if I connect a source on both pins all data is a mess.
Looks like a hardware problem to me, since on what ever analog pin I connect a source program start to read odd results.

Any idea what I'm doing wrong.
Last edited by JovaS on Thu Aug 03, 2017 11:23 am, edited 1 time in total.

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: stm32f103 having problem reading multiple adc pins.

Post by Pito » Wed Aug 02, 2017 4:23 pm

Try

Code: Select all

Serial.print((analogRead(PA0)*3300)/4096);
..
Serial.print((analogRead(PA1)*3300)/4096); 
What source feeding ADC inputs do you use?
Can you post the schematics?
Pukao Hats Cleaning Services Ltd.

JovaS
Posts: 5
Joined: Wed Aug 02, 2017 3:54 pm

Re: stm32f103 having problem reading multiple adc pins.

Post by JovaS » Wed Aug 02, 2017 6:39 pm

I'm reading data from 2 max471 current sensors.
sensors reading current from circuits powered with pc power supply.
stm32f103c8t6 board is powerd from usb.
Both sensors and stm32 sharing same ground.

Same circuit works fine with arduino uno and nano. I porting it to stm32 because of 12bit adc.

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: stm32f103 having problem reading multiple adc pins.

Post by Pito » Wed Aug 02, 2017 7:52 pm

It seems to me you are overloading the analog inputs with voltages >3.3V. The excessive current flows into Vdd of the stm32 (via the input pin clamping diode), with single input wired it may still work, with 2 inputs it crashes the mcu.

With atmega @5V it may work but mind the stm32 is 3.3V.

Double check the output voltage levels from the 471 Sensor.
It must be less 3.3V.
Also add a 4k7-10k resistor in series with each adc input.

471 voltage output1 ---> |10k resistor|------> PA0

471 voltage output2 ---> |10k resistor|------> PA1
Pukao Hats Cleaning Services Ltd.

JovaS
Posts: 5
Joined: Wed Aug 02, 2017 3:54 pm

Re: stm32f103 having problem reading multiple adc pins.

Post by JovaS » Wed Aug 02, 2017 8:10 pm

I have checked with multimeter and I have on one input 4mV on another 22mV. And both analogs reading ~22mV. I have tried different combinations of pins, same. Strange, like all analog pins are connected. Checked with another board (from same merchant) and same.
Also I have added delays after every read

Serial.print(analogRead(PA0)*3300/4096);
delay(500);
Serial.print(" ");
Serial.println(analogRead(PA1)*3300/4096);
delay(500);

Didn't help.

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: stm32f103 having problem reading multiple adc pins.

Post by Pito » Wed Aug 02, 2017 8:16 pm

Disconnect the 471 sensors, also their gnds.
Connect PA0 and/or PA1 to stm32's gnd or 3.3V Vcc.
Tell what Serial prints out.

Do you have an resistor connected at 471 output (OUT) against gnd? It is a current output which needs a resistor to create the voltage you want to measure with your ADC.

Draw a schematics pls.
Pukao Hats Cleaning Services Ltd.

JovaS
Posts: 5
Joined: Wed Aug 02, 2017 3:54 pm

Re: stm32f103 having problem reading multiple adc pins.

Post by JovaS » Wed Aug 02, 2017 8:52 pm

Connect PA0 and/or PA1 to stm32's gnd or 3.3V Vcc.

Both connected on bords gnd
0 0
Both connected on bords 3.3
3297 3298
3299 3299
3299 3299
3299 3295
3296 3299

Also all fine when I connect one on gnd and another on vcc.

Max471 is on module with all resistors on board.

:(

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: stm32f103 having problem reading multiple adc pins.

Post by Pito » Wed Aug 02, 2017 9:58 pm

What do you measure (min/max) at the 471 output? (not connected to stm32).
Can you give us the link to the module?
What is the input RS+ voltage of the 471?
Pukao Hats Cleaning Services Ltd.

JovaS
Posts: 5
Joined: Wed Aug 02, 2017 3:54 pm

Re: stm32f103 having problem reading multiple adc pins.

Post by JovaS » Wed Aug 02, 2017 11:59 pm

:D
Good news 471 is broken
3.19v on rs+ 3.19 on rs- that's ok. But I'm not reading charge signal, since I'm using it just for measuring current. I have test it now and It's always 0 on charge signal.
Tested stm32 with two potentiometers on different analogs all working fine.

Thank you a lot for your help. And Big sorry onbothering you. :(

stevestrong
Posts: 1829
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: stm32f103 having problem reading multiple adc pins.

Post by stevestrong » Thu Aug 03, 2017 7:53 am

If it works now, please add to the thread title [SOLVED].

Post Reply