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:
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!