0

How to get a pwm pin value that is in pinMode OUTPUT?

How to get a digital OUTPUT PIN value:

#define pin 3

void setup()
{
  pinMode(pin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
    delay(1000);
    digitalWrite(pin, HIGH);
    Serial.println(bitRead(PORTD,pin));
    delay(1000);
    digitalWrite(pin, LOW);
    Serial.println(bitRead(PORTD,pin));
}

enter image description here

For testing reasons, I want to get PWM output pin value.

Incorrect sketch:

#define pin 3

void setup()
{
  pinMode(pin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
    delay(1000);
    analogWrite(pin, 100);
    Serial.println(pulseIn(pin, HIGH)); // value of the PWM output pin
}

This sketch was created for example only. I don't know PWM value because PWM value will be generating dynamically. Could you explain how to get a PWM output pin value, please?

4
  • 2
    You can't. There is no single "value" for it, it's constantly changing. You could average it through a low-pass filter and feed it back through an analog pin. Or you could connect it to an input pin and measure pulse width and frequency, but you can't do it on the pin itself. Not with normal Arduino tools. Besides, you know the value already. It's "100", since that's what you set it to. What are you actually trying to achieve?
    – Majenko
    Commented Dec 17, 2018 at 10:32
  • BTW: the first sketch reads value in POTD register, not the value on the actual pin. It might be for example shorted and PIND register would read it correctly. It's possible to read the actual value (by reading PIND) but as Majenko wrote, it's changing, so you'd have to read it at constant rate or measure the low and high time. Funny, the pulseIn should be actually working as it must be using PIND internally
    – KIIV
    Commented Dec 17, 2018 at 10:41
  • 1
    Maybe you would be able to get the value back by reading the corresponding Output Compare Registers of the used timer (I think, this is used for the generation, though I didn't check).
    – chrisl
    Commented Dec 17, 2018 at 10:51
  • store the value to a variable. that is the right way. but if your task is to read the generated pwm on analog pin, then arduino.stackexchange.com/questions/55698/…
    – Juraj
    Commented Dec 17, 2018 at 11:55

2 Answers 2

1

According to the source code implementation of analogWrite function (Arduino analogWrite source code explanation).

You can use OCRnx register for reading PWM value.

1
  • That would be OCR2B for pin 3.
    – Gerben
    Commented Dec 17, 2018 at 16:07
1

I want to get PWM output pin value.

The pin will either be 0 or 1.

analogWrite(pin, 100);

It will be 1 for 100/256 (39%) of the time, and 0 the rest of the time.


Why do you want to do this?

2
  • 1
    Because I'm seeking a solution for solving this question without creating a new variable for store the PWM value.
    – Kvartu
    Commented Dec 17, 2018 at 10:58
  • As mentioned in another answer you could interrogate the timer's register to see what value the analogWrite put there, but why bother doing this obscure thing (which would change for every output pin) when you can save the value in one byte in a variable? Also, that is not the "output pin value" but the duty cycle of the PWM output.
    – Nick Gammon
    Commented Dec 17, 2018 at 20:50

Your Answer

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

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