cancel
Showing results for 
Search instead for 
Did you mean: 

When PWM output through TIM2 channel 1 on STM32G061G6U6 is enabled, the data register of ADC1 sometimes stores high values (~4095) even if there is no signal being fed to it.

PIvan.9
Associate II

Here is the configuration I use:

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_TIM2_Init();
  
  // configure PWM
  __HAL_TIM_SET_AUTORELOAD(&htim2, 1000);
  __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1001);
  
  // start PWM
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  
  // configure ADC
  HAL_ADC_MspInit(&hadc1);
  // start ADC in interrupt mode
  HAL_ADC_Start_IT(&hadc1);
 
  while (1)
  {
  // some code
  }
 
// callback for ADC interrupts
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  // some code
}

When lines 11 and 14 are not commented out (i.e. compare register is configured and PWM is enabled), the ADC1 sometimes reads high values with no signal being fed to it (ADC input pins are pulled down, so there is no floating voltage). When lines 11 and 14 are commented out, there is no such issue. Also, by changing the TIM2 PWM channel to any other one, the issue dissapers. This was tested on 2 MCUs.

Does anyone have any ideas why TIM2 channel 1 PWM might interfere with ADC readings?

13 REPLIES 13

Which pins exactly, both PWM and ADC?

What is the ADC readout periodicity, what is the sampling time, how exactly is reference set, etc.?

JW

PIvan.9
Associate II

PA0 for PWM output and PA2/PA3 for ADC input.

LCE
Principal

And what about external hardware:

  • Are the PWM / ADC pins (or pcb traces) close to each other?
  • How are these IOs connected externally?
  • Especially the ADC inputs, any low impedance driver connected, or at least an RC low pass close to the pins?
  • What are the frequencies of the PWM and the ADC sampling rate?

PIvan.9
Associate II

ADC is connected to an active mode phototransistor circuit (Vout in the image):

0693W00000HrhtIQAR.pngPWM and ADC traces are close to each other only at some points and mostly due to the MCU package. I initially thought about crosstalk between them, but 10kOhm (Re) pull-downs on ADC pins should have helped with it. There is no RC filter and a phototransistor signal is not expected to be noisy anyway.

PWM frequency is 50kHz, but, here is the interesting part, the duty cycle is set to 100%, so there should have been no swithching between HIGH and LOW in my tests.

The ADC sampling frequency is 1.6MHz, but that's not taking into account the interrupt code.

If the PWM is inactive (better check pins on scope), it's probably no hardware problem.

On the other hand, depending on the phototransistor, it seems very bold to connect it directly to the STM's ADC input.

To finally check if the problem is related to external circuitry, I would bridge the pull-down R with 0 Ohm, or even connect the ADC input to GND very close to the pin.

Better remove the phototransistor before doing that!

PIvan.9
Associate II

I've checked the PWM output pin, it is truly at 100% duty cycle. I've also checked that the duty cycle can be reduced to other values. It is possible, so PWM works properly. However, it doesn't change the situation, ADC still detects max values from time to time.

I've also removed phototransistors on both ADC pins and replaced Re with jumper wires. The result is the same. However, it's not possible to connect ADC inputs to ground really close to the pins due to the layout. The distance to Re is ~2cm.

It would be nice, if someone could check this issue with their own PCBs, although I understand that chances for this are pretty low.

PIvan.9
Associate II

As a last test, I've commented out

// HAL_ADC_MspInit(&hadc1);

command, so no ADC pins should be configured and ADC can't receive any data from anywhere, however the issue is still there.

Philippe Cherbonnel
ST Employee

Hello,

Can you show the content of MX_ADC1_Init() and HAL_ADC_MspInit() ?

By the way, it should not be needed to call HAL_ADC_MspInit() because already called by HAL_ADC_Init().

> It would be nice, if someone could check this issue with their own PCBs,

Conversely, you can try to do it on a "known good" board such as Nucleo or EVAL.

JW