Teensy and PWM
Teensy and PWM
Teensy 3.1/3.2 boards have 12 pins support PWM
PWM Resolution
Teensy 3.1/3.2 Boards based on 32 bit M4 arm cortex board , and the resolution of these boards higher than normal arduino boards (Like Mega and Uno) .
the Teensy PWM resolution can be up to 16 bit and you can set the PWM resolution using this function
analogWriteResolution(value);
value: Resolution value (2 - 16 )
PWM Frequency
in default , the PWM frequency for teensy 3.1/3.2 is 488 Hz and you can change it using this function:
analogWriteFrequency(pin, freq);
pin: PWM pin
freq : Frequency in Hz
Simple Code
simple code here to enable PWM on teensy pins 4 and 5 , and change the frequency for pin# 4
Code : download it from here
Code : download it from here
/*
* PWM teensy 3.1/3.2 simple code
* more information
* https://mb-raw.blogspot.com/2018/02/teensy-and-pwm.html
* written by : Mohannad Rawashdeh
*/
const int PWMpin1=4;//FTM1 timer
const int PWMpin2=5;//FTM0 timer
void setup()
{
pinMode(PWMpin1,OUTPUT);
pinMode(PWMpin2,OUTPUT);
// set the PWM frequency
analogWriteFrequency(PWMpin1,10000);
analogWriteResolution(8); // 0 - 255
analogWrite(PWMpin1,127);
analogWrite(PWMpin2,127);
Serial.begin(115200);
}
void loop()
{
}
![]() |
| output on pin# 5 |
![]() |
| output on pin# 4 |

Resolution Vs frequency
![]() |
| source : https://www.pjrc.com/teensy/td_pulse.html |
so I decided to write a code to match these information , this code automatically calculate your CPU frequency and depend on your resolution you want , the code will choice the ideal PWM frequency for your CPU and resolution setup
the code will get the CPU frequency using this code :
long PWM_CPU_Frequency=F_CPU/1E6;
and you need to set the resolution you want by changing this variable
int PWM_Resolution=8;
the code will get the CPU frequency using this code :
long PWM_CPU_Frequency=F_CPU/1E6;
and you need to set the resolution you want by changing this variable
int PWM_Resolution=8;
the output pin is pin# 4
Code : download it from here
/*
* PWM teensy 3.1/3.2 PWM ideal frequency
* more information
* https://mb-raw.blogspot.com/2018/02/teensy-and-pwm.html
* written by : Mohannad Rawashdeh
*/
const int PWMpin1=4;//FTM1 timer
long PWM_CPU_Frequency=F_CPU/1E6;
int PWM_Resolution=8; //default
int FREQ_pointer=0;
long PWM_Freq;
//unsigned int PWM_Max_Value=2^PWM_Resolution - 1;
unsigned int PWM_Max_Value=pow(2,PWM_Resolution);
// Resolution - CPU freq
long PWM_ideal_freq[5][15]
{
{6000000,3000000,1500000,750000,375000,187500,93750,46875,23437,11718,5859,2929,1464,732,366},
{12000000,6000000,3000000,1500000,750000,375000,187500,93750,46875,23437,11718,5859,2929,1464,732},
{9000000,4500000,2250000,1125000,562500,281250,140625,70312,35156,17578,8789,4394,2197,1098,549},
{12000000,6000000,3000000,1500000,750000,375000,187500,93750,46875,23437,11718,5859,2929,1464,732},
{15000000,7500000,3750000,1875000,937500,468750,234375,117187,58593,29296,14648,7324,3662,1831,915}
};
/* for CPU frequency
* 24MHZ >> 1
* 48MHZ >> 2
* 72MHZ >> 3
* 96MHZ >> 4
* 120MHZ >> 5
*/
void GetFreqValue(long FREQ)
{
switch(FREQ)
{
case 24:
FREQ_pointer=1;
break;
case 48:
FREQ_pointer=2;
break;
case 72:
FREQ_pointer=3;
break;
case 96:
FREQ_pointer=4;
break;
case 120:
FREQ_pointer=5;
break;
}
}
void setup() {
Serial.begin(57600);
GetFreqValue(PWM_CPU_Frequency);
PWM_Freq=PWM_ideal_freq[FREQ_pointer-1][PWM_Resolution-2];
analogWriteFrequency(PWMpin1,PWM_Freq);
analogWriteResolution(PWM_Resolution);
analogWrite(PWMpin1,PWM_Max_Value/2);
}
void loop() {
Print_PWM_Data();
delay(10000);
}
void Print_PWM_Data()
{
Serial.println("***** PWM frequency - resoultion automatic Calibration ****");
Serial.print("CPU Frequency [MHZ] : ");
Serial.println(PWM_CPU_Frequency);
Serial.print("PWM Resolution: ");
Serial.println(PWM_Resolution);
Serial.print("PWM ideal frequency: ");
Serial.println(PWM_Freq);
Serial.print("PWM Max Value: ");
Serial.println(PWM_Max_Value);
Serial.println("Written By: Mohannad Rawashdeh ");
Serial.println("***********************************");
}
here's the result on the oscillscope as well as on Serial monitor
then I changed my CPU frequency to 96 MHz with the same resolution and I got these results







Comments
Post a Comment