Hi,
I'm new to Arduino and couldn't find a way to create a 100kHz PWM. I checked many places and saw many codes to create it, however many of them were not full code, they were partial. I didn't understand how to manipulate them to create a 100 kHz pwm. Can you share your full code with me if you have a solution?
Thank You
The Timer1 library should work for that.
Try Timer1.initialize(10); //100Kz
#include <TimerOne.h>
//UNO only
void setup()
{
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
Timer1.initialize(10); // Frequency, 10us = 100khz
Timer1.pwm(9,512); // 50% DC on pin 9
//Timer1.pwm(10,255); // 25% DC on pin 10
}
void loop()
{
}
larryd:
The Timer1 library should work for that.Try Timer1.initialize(10); //100Kz
#include <TimerOne.h>
//UNO only
void setup()
{
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
Timer1.initialize(10); // Frequency, 10us = 100khz
Timer1.pwm(9,512); // 50% DC on pin 9
//Timer1.pwm(10,255); // 25% DC on pin 10
}
void loop()
{
}
Thank you for your help. I needed two 100kHz pens at the same time but one of them should be inverted. I set two different timers to fast pwm and solved the problem mostly. However, the cycles don't match perfectly. I played with the cycles but one's crest doesn't match with other's trough. For matching, them I thought to initialise them at the same time but couldn't find a way to it. Even though I initialise them one right after the other one, it causes a stress in matching due to high frequency. How can I initialise two fast pwm, which are set in two different timers, exactly at the same time? Thank you
You need to set the counts to values that differ by the number of clocks it takes to write one of them. By
the time the second write happens the first timer has advanced to match it.
I needed two 100kHz pins at the same time but one of them should be inverted. I set two different timers to fast pwm and solved the problem mostly.
You should be able to use one 16 bit timer (Timer1 on a UNO) in a mode which uses ICR1 as top (to get the frequency at 100 KHz) and have the two hardware outputs available for the matching output.
Why are you using two timers?