PWM on Teensy 4.0

fdu

Member
Dear ,

I have a problem with PWM on Teensy 4.0 . I would like use PWM on pin 19 with

analogwrite(... ) command but don't work .

I think one library disturb PWM .

In my software i use this :

#include "Watchdog_t4.h"
#include <SPI.h>
#include <Ethernet.h>
// #include <CircularBuffer.h>
#include "TeensyTimerTool.h"
#include <ModbusRtu.h>
#include <ModbusMaster.h>
#include "EEPROM.h"
#include <TeensyDMX.h>
#include <elapsedMillis.h>
#include "arduinoFFT.h"

Have you a idea ?

Thanks
 
It's impossible to say what's wrong without knowing what your program is doing and how you're using other pins. The program below shows that PWM on pin 19 does work on T4.0. Please try to put together a small program that shows the problem.

Forum Rule: Always post complete source code & details to reproduce any issue!

Code:
#include <FreqCount.h>

// FreqCount is hard-coded to measure on pin 9 for T4.x
// Jumper pin 19 (PWM output) to pin 9 (FreqCount input)

#define PWM_OUTPUT_PIN 19

void setup() {
  Serial.begin(9600);
  while (!Serial) {}
  
  analogWriteFrequency(PWM_OUTPUT_PIN, 5000);  // 5 kHz test signal
  analogWrite(PWM_OUTPUT_PIN, 128);
  
  FreqCount.begin(1000000);  // 1-sec count (T4.x count time is microseconds)
}

void loop() {
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);
  }
}
 
Thank you for the answer .

I know the pwm in pin 19 with a little program works.

I misstated the problem.

My question is is there a known problem between one of the libraries and the registers that drive the PWM on pin 19.

I am looking in the datasheet of the processor the registers concerned.

Thanks
 
I know the pwm in pin 19 with a little program works. My question is is there a known problem between one of the libraries and the registers that drive the PWM on pin 19.

There can be conflicts between different libraries if they are using the same pins or configuring the same timers. We can't tell from what you've posted whether that is the case. For example, if you were somehow using the same timer via TeensyTimerTool that is being used to generate the PWM, then either the TeensyTimer or the PWM would probably not do what you want.
 
Thank you for the answer .

I know the pwm in pin 19 with a little program works.

I misstated the problem.

My question is is there a known problem between one of the libraries and the registers that drive the PWM on pin 19.

I am looking in the datasheet of the processor the registers concerned.

Thanks

Well pin 19 (along with pin 18) is on the main I2C bus. It could be one of your libraries or the startup code is causing the I2C bus to be initialized. Or if you have the Teensy mounted in an external board setup, often times the I2C pins have default pull-up resistors installed, and that may impact PWM performance.
 
My question is is there a known problem between one of the libraries and the registers that drive the PWM on pin 19.

Quick answer would be no, or at least not known to me.

But some of these libraries, like ModbusRtu, do use specific pins depending on their config. Maybe start looking there?

I can also say the problem is very unlikely to be from SPI, Ethernet or EEPROM, as these libraries are very widely used without any such problems reported (and I'm also pretty familiar with the code inside those libraries).


I am looking in the datasheet of the processor the registers concerned.

Wouldn't just deleting portions of your program (in another experimental copy of course) which uses each library be a lot easier than diving into thousands of pages of terse hardware register documentation?

Also, no need to include elapsedMillis.h. On all Teensy boards it's always built in. It was created for Teensy and the library is just a copy of Teensy's code which was made available for non-Teensy boards.
 
Back
Top