Hello everyone i have just started to use arduino. I would like to read clock pulse from PWM pin with pulseIn command for per second. For example, when I assign the value 255 to the pwm pin, I want to read the value 255 with the pulseIn command in serial port screen. I would be very grateful if you could share a sample code.
...the PWM goes HIGH.
Permanently.
When I assign the value 255 to the pwm pin, I think it takes the high value 255 times per second. I didn't understand how it is permanently high.
I think you're confusing duty cycle with frequency.
On the AVR, analogWrite (pin, 255) is equivalent to digitalWrite (pin, HIGH).
Easy enough:
const unsigned long TIMEOUT = 10000; // 10 millisecond (100 Hz)
const byte PWMPin = 3;
const byte InputPin = 4;
int readPWM(int pin)
{
unsigned long highDuration = pulseIn(pin, HIGH, TIMEOUT);
unsigned long lowDuration = pulseIn(pin, LOW, TIMEOUT);
// Serial.print(highDuration);
// Serial.print(", ");
// Serial.print(lowDuration);
// Serial.print(" -> ");
if (highDuration == 0 || lowDuration == 0)
return digitalRead(pin) == HIGH ? 255 : 0;
return (highDuration * 255) / (highDuration + lowDuration);
}
void setup()
{
Serial.begin(115200);
delay(200);
pinMode(InputPin, INPUT);
for (int i = 0; i < 256; i++)
{
analogWrite(PWMPin, i);
delay(1);
Serial.print(i);
Serial.print(" -> ");
Serial.println(readPWM(InputPin));
}
}
void loop() {}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.