1
\$\begingroup\$

I am trying to read the ADC of a STM32F4. The code skeleton was generated by CubeMX, the HAL library is used. The read values are feasible but have periodic noise, as seen here:

ADC Reads from a loop with 10ms delay

The plot was generated by a loop doing single measurements with a 10ms delay. Is noise of such magnitude to be expected or is something wrong? Here is how I init the ADC:

hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv =  ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

The plot was generated with this code:

for (int i=0; i<128; i++)
{               
   HAL_ADC_Start(&hadc1);

   for (int k=0; k<3; k++)
   {
       if (HAL_ADC_PollForConversion(&hadc1, 20) == HAL_OK)
       {
           uint32_t newValInt = HAL_ADC_GetValue(&hadc1);
           CanTxMsgTypeDef msg;
           msg.StdId = 0x500+i;
           msg.IDE = CAN_ID_STD;
           msg.RTR = CAN_RTR_DATA;
           msg.DLC = 8;                         
           *((uint32_t *)(&(msg.Data[4]))) = newValInt;
           can_tx(&msg, 1000);

           HAL_ADC_Stop(&hadc1);
           HAL_Delay(10);

           break;
       }
   }
}

Do you folks have any idea what the cause of that noise could be? Did I configure any (maybe seemingly unrelated) pin false? Is there a problem with the STM? Of course i could do averaging, however, I think the noise indicates some problem.

Edit: The problem also occured without the delay. We found out that the supply voltage was not clean. After applying a circuit-filter, we get almost constant values with sigma~1.7. Thanks anyway!

\$\endgroup\$
4
  • \$\begingroup\$ I think it should be more stable, but that depends on how you feed the signal to the controller and how good the ground connection is. What I'm missing is the sample and hold time - did you set it up long enough for your signal to settle? \$\endgroup\$
    – Arsenal
    Commented Dec 10, 2018 at 16:12
  • \$\begingroup\$ It's hard to know without some information about the pin connections. Is this your own custom board or a commercial one? How is the signal applied to the ADC? Do you have any filtering at all? \$\endgroup\$ Commented Dec 10, 2018 at 17:07
  • 3
    \$\begingroup\$ If HAL_Delay is in ms, and your sample rate is 100 Hz, you could be looking at aliased 60-cycle. \$\endgroup\$ Commented Dec 10, 2018 at 17:09
  • \$\begingroup\$ This is far too broad and incomplete especially as no details of the hardware are given. Getting "quiet" ADC results is a skill and an art. Design the circuit with care, filter the analog power, and route the board with care. Electronically low pass filter the signal being measured if appropriate. During measurement turn off all parts of the chip and circuit that are not needed. Oversample and filter the data again in software to the desired bandwidth of actual meaning... \$\endgroup\$ Commented Dec 10, 2018 at 18:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.