0

I have a PWM analog port that is outputting a frequency, is it possible to do an analogRead on the same pin and get a correct value? If so, will reading that port disrupt the freq output on the pin? This is being done on a Teensy 3.2 device. My goal is to output the freq visually on an OLED. Tnx.

  • Just store the PWM value in a variable every time you call analogWrite. Your oled-display routine can then just read that variable. – Gerben Aug 29 '18 at 15:48
1

You want the voltage that would be read by a voltmeter? If you know what value you set for PWM, you know the voltage that is generated.

But if you are curious, you can connect the PWM output to ADC pin and read for example a 100 values and calculate the average. You will see that at the same resulution of PWM and ADC, you get as average of ADC values the same value as you set for PWM.

This is for M0:

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  analogWriteResolution(10);

  for (int i = 0; i < 1024; i += 10) {
    analogWrite(7, i);
    delay(100);
    long sum = 0;
    int n = 0;
    unsigned long start_time = millis();
    while (millis() - start_time < 100) {
      sum += analogRead(A1);
      n++;
    }
    Serial.print(i);
    Serial.print(' ');
    Serial.println(sum / n);
  }
}

void loop()
{
}
1

No, it's not possible.

PWM is not actually analog - it's digital, but switching on and off at a high speed.

If the pin were capable of reading an analog value at the same time as outputting a PWM waveform (which I highly doubt...) all you would get is alternating 0 and 1023 values (or whatever range the Teensy reads in).

0

You can create a parallel branch from PWM pin with a diode. And put a 0.1uF capacitor and parallel 1k resistor after the diode and connect analog pin between diode and resistor to read pwm avarage output voltage. But becareful it will give 0,6V below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.