cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with ADC values

BEN OTHMEN BADR
Associate II
Posted on February 09, 2017 at 12:45

Hi,

I have an STM32F3Discovery board, and I tried to use ADC, my problem is that the values are not correct.

the idea is to generate a ramp from the DAC and read values using ADC.

My code is generated using STM32CubeMX, file attached.

Example of values :

DAC : ADC

1760 : 1693

1761 : 1694 1762 : 1696 1763 : 1697 1764 : 1696 1765 : 1697

My main function :
int main(void)
{
 int a;
 char buff[100];
 
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
 MX_DAC_Init();
 MX_USB_DEVICE_Init();
 MX_ADC2_Init();
 HAL_DAC_Start(&hdac,DAC_CHANNEL_1);
 
 while(1){
 for(int i=0;i<4095;i++){
 HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,i);
 HAL_Delay(100);
 if (HAL_ADC_Start(&hadc2) != HAL_OK){Error_Handler();}
 if (HAL_ADC_PollForConversion(&hadc2, 1000) != HAL_OK){Error_Handler();}
 else{a = HAL_ADC_GetValue(&hadc2);}
 if(hUsbDeviceFS.dev_state == 3){
 sprintf(buff,'%4d : %4d\r\n',i,a);
 CDC_Transmit_FS(buff,strlen(buff));
 }
 }
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Thanks for your help

30 REPLIES 30
Nesrine M_O
Lead II
Posted on February 09, 2017 at 13:20

Hi

Ben_Othmen.Badreddin

,

Please try to start from ADC examples under the STM32F3 HAL package:

STM32Cube_FW_F3_V1.7.0\Projects\STM32303C_EVAL\Examples\ADC

-Nesrine-

T J
Lead
Posted on February 09, 2017 at 13:21

there is a slight noise problem when you are reading 12 bit voltages...

you should have a bypass cap on the A/D input to help it.

but how have you stabilized the Reference voltage ?

is it attached to the 3v3 rail ?

how noisy is that rail ?

also, please consider the value of your result..

12 bit is 4096 counts,

3v3 /4096 is 0.0008V per step...

you dont need much noise to move the input pin by 800 uV relative to the reference voltage.

I use the over sampling technique,

where by I get 16 results,

add them up and divide by 16,

this produces much better results.

Posted on February 09, 2017 at 13:22

Thank you for your fast answer, I'll do it now, regards

Posted on February 09, 2017 at 13:51

Hi,

Actually I'm using STM32F3Discovery board, VDD is 3.0 Volt, in addition I'm using DAC to generate ramp, DAC and ADC are respectively PA4 and PA5, the two pins are connected together and they are very close, on the oscilloscope, the signal is clean, so I dont think that's a question of noise, by the way the same conditions and program applied on STM32F4Discovery works very very good.

thank you.

 

Posted on February 09, 2017 at 14:21

Yes,

please check the Voltage rail is stable within 800uV and then also check that you have a bypass capacitor on your A/D input.

this will be the difference on the F4 discovery board.

actually, I had a '303 running here, it also had a noise problem.

I found that if you use internal pull-ups and you toggle an IO pin, you will see some noise on the A/D pin.

I switched to a '072 and '091 and only use external pull-ups, and all the noise went away.

Now I can use the FREE IDE uV5...

Posted on February 09, 2017 at 15:05

Hi,

I have check one more time schematics for the F4Discovery, there is no bypass capacitor for analog inputs (PA4 and PA5)

for the moment I have not any internal poll-up or pull-down resistor, just I have a generated code from STM32CubeMx and I'm using just two pins, one for the DAC out and the other for the ADC in.

I have do a test with a very good stabilized voltage, over the time the ADC generate stable values but with a constant difference with the real continuous signal.

please check my CubeMx project file.

Best regards.

Posted on February 09, 2017 at 15:09

please try to add a DAC to 1k to 0.1uF on the A/D input pin, this should help.

did you look at averaging 16 samples ?

raptorhal2
Lead
Posted on February 10, 2017 at 22:37

Is DAC output buffer enabled? That would provide more power to feed the ADC input resistance/capacitance, but have some output error near max voltage.

What is the ADC channel conversion sampling time? A longer sampling time will produce less error from a weakly powered signal, such as a DAC with no buffer enabled.

Cheers, Hal

Posted on February 11, 2017 at 09:17

Hi Baird,

Yes I'm using output buffer enabled, also I have added an external OPAMP follower, with RC filtering to avoid noise ... the same result.

I have tried to inject a signal from a DDS, the same result.

Now I have ordered F072, maybe it give me better results.

Regards.