Arduino forum,
I need a PWM with a period of 20ms (50Hz) with a T1 of 0 and a T2 of 450us. But every thing I've read says there are only two periods, 490Hz and 980Hz for pins 5 and 6. I haven't found a way to set the period time and resolution bits?
With other hardware I have written code that toggles the clocks and uses delays to simulate the PWM that I need and it has worked. I wrote the following Nano program to do the same but with the limitations to the delayMicroseconds() command I had to stack two delays back to back to get a 20ms. That worked but check out the scope photos attached to see the fall time problem.
#define Pin 4
void setup()
{
}
void loop()
{
digitalWrite(Pin, HIGH);
delayMicroseconds(450);
digitalWrite(Pin, LOW);
delay(19);
delayMicroseconds(550);
// total delay of 19550us which the datasheet didn't guarantee with delayMiroseconds()
digitalWrite(Pin, LOW);
}
I got the 20ms period alright and the pulse begins to fall at 450us but with the lag in the fall time it doesn't get to half level until about 575us. Do these pins need pull downs? I tried several different ports but all were the same.
Maybe there's a completely different way to do this.
kolaha,
Those are the exact numbers I use in my program with the other hardware but in
it says:
"This function works very accurately in the range 3 microseconds and up to 16383. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times. Larger delay times may actually delay for an extremely brief time."
so I was hesitant to use delayMicroseconds(19546);
Arduino forum,
Thanks to those who pointed out that I forgot to declare "Pin" as an output.
Don't know how much longer I can continue to use the excuse that I'm new to Arduino.
The waveform is now as it should be.
Still concerned about using delayMicroseconds(19546) since Arduino doesn't guarantee values above 16383. I'll use the back to back delays which gave me the correct period.
jerdon
Arduino forum,
Since I need a period of 20ms and the limit of delayMicroseconds is 16383 I always include a delay of 10ms so I never need to exceed the max microseconds.