PWM

Im trying to get the frequency from the arduino so that i can determine the motor speed, one way i read about is using pulsein function to measure the time of the pulse as Frequency =1/time. I want to know if the duty cycle is always 50% and how will it affect pulse if it changes

I want to know if the duty cycle is always 50% and how will it affect pulse if it changes

The duty cycle is always 50% if you always use analogWrite(somePin, 127);.

If you use some other value as the 2nd argument, the duty cycle will change. Before you can make use of the pin’s on time to calculate frequency, you must know the duty cycle.

If the pwm is at 255 its on 100% duty cycle
191 -75% duty cycle
127- 50% duty cycle
64- 25% duty cycle
what if i want a pwm of 150, 230 or 100. How do i do that ?

what if i want a pwm of 150, 230 or 100. How do i do that ?

analogWrite(somePin, 150);
analogWrite(somePin, 230);
analogWrite(somePin, 100);

PaulS:

analogWrite(somePin, 150);

analogWrite(somePin, 230);
analogWrite(somePin, 100);

Sorry i actually meant how do i get the duty cycle for those

A duty cycle of 230%? Do you even know what duty cycle means?

Sorry i actually meant how do i get the duty cycle for those

The duty cycle is the %-age of time the pin is on, in any given cycle. Since 0 means 0% and 255 means 100%, the duty cycle is simply the analogWrite() value divided by 255 then multiplied by 100, using floating point arithmetic.

PaulS:
The duty cycle is the %-age of time the pin is on, in any given cycle. Since 0 means 0% and 255 means 100%, the duty cycle is simply the analogWrite() value divided by 255 then multiplied by 100, using floating point arithmetic.

Thank youu much appreciated :slight_smile: