Voltage at PWM output pin jittering MEGA2560

Hi Guys,
I have a throttle which outputs a signal between 0.8V and 3.6V to the arduino. Right now all the arduino does is read that signal on an analog input pin convert it back to a voltage and then write it on a PWM pin. When I connect the serial to read the values they are stable and as expected, however the PWM pin somehow jitters within +-0.1V although the variable written to it is stable. What could this be due to?

On top of this, I have a voltage divider, an op amp, and a LCD touch screen connected to the MEGA2560 but they all work well on their side.

Thank you for your help!

What is connected to the PWM output? Are you using any lowpass filter?

Yes, with a RC lowpass filter it should be pretty stable.

For example, here is analogWrite in steps of 0,25,50, 75,100,125, 150, 175, 200, 225, 250
with a 10K/4.7uF filter (or 4.7K, 10uF, I forget which)

Yes it is connected to a low pass RC filter (3.3kOhm and 22uF). However, even when I measure the voltage with a multimeter directly from the pin, the output is erratic, giving out different PWMs, as if the arduino itself isn't focusing on a value. However, as mentioned before, the Serial prints a stable, constant value for the PWM output pin.

I see only two options:

  1. the power supply itself is not stable
  2. something is messing with the timer

is the PWM stable if you just output a constant value and have nothing else in the code and nothing but the RC flter connected to the arduino?

Ok so isolating the circuit didn't change anything to the problem. However, when setting it to a constant, more precisely using analogWrite at 255, in the setup part of the code, the output is stable on a multimeter but remains at 1.91V instead of 5. When putting the analogWrite in the loop part of the code the instability is still here and stays around 3 +/-0.1V on the multimeter.

EDIT: I actually just changed from output pin 9 to output pin 8 (Analog output without a wave drawn on it), and now the output voltage is super stable. However, as soon as I plug the throttle to the arduino analog input, the voltage is capped at 2.5-2.6V whereas when it is not connected to the arduino it reaches its full range from 0.8 to 3.6V

EDIT: I actually just changed from output pin 9 to output pin 8 (Analog output without a wave drawn on it), and now the output voltage is super stable. However, as soon as I plug the throttle to the arduino analog input, the voltage is capped at 2.5-2.6V whereas when it is not connected to the arduino it reaches its full range from 0.8 to 3.6V

Pin 8 is not a pwm pin, and analogWrite defaults to digitalWrite(). From wiring_analog.c

case NOT_ON_TIMER:
			default:
				if (val < 128) {
					digitalWrite(pin, LOW);
				} else {
					digitalWrite(pin, HIGH);
				}

cattledog:
Pin 8 is not a pwm pin, and analogWrite defaults to digitalWrite(). From wiring_analog.c

How is it possible that it outputs different voltages readable by a multimeter?

How is it possible that it outputs different voltages readable by a multimeter?

You now have two anomalies relating to reading values from a pin which should be at 5v.

The analogWrite >128 on a non timer pin, and the analogWrite(timer_pin,255) both default to digitalWrite(pin,HIGH).

Can you read 5v and 0v on a normal digital output simply using digitalWrite HIGH and LOW?

Can you post a sketch of the low pass filter, and where you are taking the measurements?

When testing digital pins output with a voltmeter I obtained 4.74V on high and 0V on low. However, pin 8 from which the output is sent responds well to the analogWrite and gives different voltages according to the 0 to 1023 range.

Here is a schematic of my circuit:

The 2.3V are measured accordingly to the measurement icon. However, when disconnecting the throttle from pin A2 and measuring between the throttle output and the GND, the full range from 0 to 1023V is obtained.

I made an error in that pin 8 on a Mega is PWM from timer4. I was thinking it was pin 8 on a UNO which is not PWM.

Are you saying that the problem is with the throttle output, and the pwm echo out of the throttle output is correct?

The 2.3V are measured accordingly to the measurement icon. However, when disconnecting the throttle from pin A2 and measuring between the throttle output and the GND, the full range from 0 to 1023V is obtained.

When connected to A2 the throttle output does not go as high as expected.

When the throttle output is disconnected from A2 you can measure full range. How do you measure 0-1023 when it is disconnected from A2?

Try to read the throttle at A2 with two sequential analogRead() calls. Discard the first, and only use the second.

There is possibly an issue with the ADC sample capacitor charge time given the impedance of your source.

cattledog:
Are you saying that the problem is with the throttle output, and the pwm echo out of the throttle output is correct?

Yes, the PWM output from the Arduino and the low pass filter are working fine. The problem is with the throttle output signal from the actual throttle, going into the pin A2 of the arduino; but only when it is connected with the Arduino.

cattledog:
When connected to A2 the throttle output does not go as high as expected.

Exactly, once it is connected the throttle doesn't give full range anymore and reaches a maximum of 2.3V. Also, it increases normally until 2.3V and then caps there, meaning that after a certain amount of twist, once it reaches 2.3V, it just stops increasing.

cattledog:
When the throttle output is disconnected from A2 you can measure full range. How do you measure 0-1023 when it is disconnected from A2?

Yes, I can, by using a solderless breadboard and connecting the throttle output on the same line as a wire going to A2 in the arduino as well as an extra wire with an open end. I then use this open end and the ground of the Arduino to measure the voltage with a multimeter.
Hence, doing this allowed me to observe that it is only when it is connected to A2 that the throttle doesn't reach full range anymore.

cattledog:
Try to read the throttle at A2 with two sequential analogRead() calls. Discard the first, and only use the second.

Thank you I will try this tomorrow and come back to you with the results!