Skip to main content
BEN OTHMEN BADR
Associate II
February 9, 2017
Question

Problem with ADC values

  • February 9, 2017
  • 30 replies
  • 6555 views
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

    This topic has been closed for replies.

    30 replies

    Nesrine M_O
    Associate
    February 9, 2017
    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-

    BEN OTHMEN BADR
    Associate II
    February 9, 2017
    Posted on February 09, 2017 at 13:22

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

    T J
    Senior III
    February 9, 2017
    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.

    BEN OTHMEN BADR
    Associate II
    February 9, 2017
    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.

     

    T J
    Senior III
    February 9, 2017
    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...

    raptorhal2
    Lead
    February 10, 2017
    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

    BEN OTHMEN BADR
    Associate II
    February 11, 2017
    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.

    raptorhal2
    Lead
    February 11, 2017
    Posted on February 11, 2017 at 20:39

    PA5 is dedicated to an on board function. Try using the PA4/PA3 combination.

    Cheers, Hal

    BEN OTHMEN BADR
    Associate II
    February 14, 2017
    Posted on February 14, 2017 at 15:13

    Hi Braid,

    Yes I tried this configuration also, without any positive result, I tried also to use LQFP-48 STM32F072 chip, still fail to get real values from ADC.

    Regards

    AvaTar
    Senior III
    February 14, 2017
    Posted on February 14, 2017 at 15:28

    Even with buffer, the DAC output is relatively 'weak'. Your ADC sampling time setting is not visible, but try longer one's.

    Example of values :

    DAC : ADC

    1760 : 1693

    1761 : 1694

    1762 : 1696

    1763 : 1697

    1764 : 1696

    1765 : 1697

    Do you honestly expect the ADC to follow LSB steps of the DAC output ?

    I think you are a quite a bit optimistic here. Try bigger steps.

    Max
    ST Employee
    February 16, 2017
    Posted on February 16, 2017 at 05:11

    Hello

    ,

    Did you try to perform an ADC calibration to see if this improve the results?

    /* Run the ADC calibration in single-ended mode */ if (HAL_ADCEx_Calibration_Start(&hdac, ADC_SINGLE_ENDED) != HAL_OK) { /* Calibration Error */ Error_Handler(); }

    world04
    Visitor II
    February 16, 2017
    Posted on February 16, 2017 at 23:18

    If I am not mistaken, the calibrate of the ADC is not available in the F4. It's also a good idea to take a look into the available errata of the used MCU. They are often good for surprises.

    Max
    ST Employee
    February 17, 2017
    Posted on February 17, 2017 at 04:19

    Sorry

    Kaltofen.Jupp

    I was not clear enough,

    At this point I was not talking about the F4 case described by

    mao.rongwei

    for which I already provided an explanation, and improvement can be obtained by following the recommendations in

    mao.rongweiwww.st.com/content/ccc/resource/technical/document/application_note/a0/71/3e/e4/8f/b6/40/e6/DM000508pdf/files/DM000508pdf/jcr:content/translations/en.DM000508pdf

    as explainedin the

    http://www.st.com/content/ccc/resource/technical/document/errata_sheet/0a/98/58/84/86/b6/47/a2/DM000375pdf/files/DM000375pdf/jcr:content/translations/en.DM000375pdf

    .

    I was talking about

    Ben_Othmen.Badreddin

    case, the original subject of this thread, i.e. discrepancy between DAC and ADC measured on the STM32F3 discovery board.

    The SAR ADC embedded in the STM32F3 series do have a calibration feature, and I think it should be used in this specific case ifa fewLSB of precision is expected.

    Additionally, the STM32F3 Discovery board does implement a LC filter between VDD and VDDA/Vref, so noise on the supply should not impact the ADC tosuch an extend.

    Anyway, the supply to check for noise on this board is VDDA, not VDD.

    By default Vref is connected to VDDA on this board by R31 = 0R, but Vref is usually another MCU pin to check for noise.

    I do believethe precision specification of the ADC and DAC from the datasheet should be achievable on this board in the condition described (i.e

    PA4 and PA5 connected together, very simple code)

    .

    regards,

    Max

    Max
    ST Employee
    February 17, 2017
    Posted on February 17, 2017 at 10:25

    Yes, the STM32F413 line has been corrected.

    This is the most recent member of the F4 series.

    world04
    Visitor II
    April 13, 2017
    Posted on April 13, 2017 at 22:42

    i have done some experiences with the F4 Line and will write something about it, even this not matches the exact thread topic, but maybe helpful related by the discussed issue.

    The ADC of the F4 line are not able to justify or calibrate to get more stable values. The source of the issue is not the MPU or the ADC who is build-in  itself as more the design of the Discovery-board and the reference voltage generation on them.

    I had rewired the mpu's pins of the DAC and ADC to an external temperature compensated high-stable reference voltage circuit and checked the values and the values are quite more stable and does'nt differs more that 10 at 12bit's resolution. It's possible to use two adc's and couple them to use them as a tandem. This requires a differential signaling - thats would be helpfully if the signal source that should converted is connected by a longer line or difficult environment.

    The F7 Nucleo board is more versatile and uses jumpers to disconnect the references. I will do some test soon.