Hi,

I started with the Teensy 4.0 (a few days ago) to build a quick and dirty camera chip tester. Therefore I am running a LED at ~2 MHz with PWM and check for intensity changes in the image output. Unfortunately, I do see some strange behaviour with my setup while I believe the Teensy is working absolutely fine. I hope, someone can answer some questions and has some hints for me.
Here it comes:

(1) I am using pin 15 at the T4 for PWM. This is my (slightly thinned) code:

Code:
#define DEBUG

const int led =  15;
const int internalLed =  13;

// intensity control via serial
char serialBuffer[16];
uint8_t serialBufferIdx = 0;
bool serialComplete = false;
char delimiter[] = "\r\n";
uint32_t timer = 0;

void CheckSerial(void);

void setup()   
{                
  pinMode(led, OUTPUT);
  pinMode(internalLed, OUTPUT);
  
  analogWriteFrequency(led, 2343750);
  analogWriteResolution(6);


  Serial.begin(9600);
}

void loop()                     
{

  CheckSerial(); // checks the serial and evaluates the "cmds"

}

// Serial input: number + '\r\n', i.e. "20\r\n"
void CheckSerial()
{

  while (Serial.available())
  {
    timer = millis(); // set for timeout
    serialBuffer[serialBufferIdx] = Serial.read();
    serialBufferIdx++;

    if (serialBufferIdx > 2) // check for \r\n
    {
      serialComplete = serialBuffer[serialBufferIdx-2] == '\r' && serialBuffer[serialBufferIdx-1] == '\n';
      if (serialComplete) break;
    }
  }
  
  if (serialComplete)
  {
    char* ptr = strtok(serialBuffer, delimiter);
    int number = atoi(ptr);

    if (number == 0)
    {
      pinMode(led, INPUT);
      #ifdef DEBUG
      Serial.println("off");
      #endif
    }
    else if (number == 63)
    {
      pinMode(led, OUTPUT);
      digitalWrite(led, HIGH);
      
      #ifdef DEBUG
      Serial.println("on");
      #endif
    }
    else if (number > 0 && number < 63)
    {
      pinMode(led, OUTPUT);
      analogWrite(led, number);
      #ifdef DEBUG
      Serial.print("new Intensity: ");
      Serial.print(number);
      Serial.println();
      #endif
    }
    else
    {
      #ifdef DEBUG
      Serial.println("error");
      #endif
    }

    // reset buffer
    while (serialBufferIdx > 0)
      serialBuffer[serialBufferIdx--] = ' ';
    serialBufferIdx = 0;
    serialComplete = false;
    
  }

  // [... time out timer routine...]
}
Does this perform a PWM with 64 steps @ ~2 MHz at pin 15? Unfortunetly I do not have an oscilloscope, but least my logic analyzer claims that I have the correct frequency at the pin.

(2) Connecting resistor + led from pin 15 to GND ( PIN15 -> RESISTOR -> LED -> GND) all seems to be fine. The LED collects ca. 15 mA and I see changes from off-state to on-state as expected. Since this is too much light for my application, I reduce the current by using a larger resistor. Now I see, that the LED goes off much earlier, i.e. at value 20/63 [analogWrite(led,20)]. Can anyone explain me why this could be the case? Less current should mean less light, but no light at all?

(3) Due to the issue (2) I am using a HEX buffer (74LS07) as a LED driver. It can sink up to 40 mA. Here is the schematics from the datasheet (link to the datasheet).

Click image for larger version. 

Name:	74LS07_Schematics.png 
Views:	8 
Size:	36.2 KB 
ID:	20709

Connecting Pin15 with its input and connecting the led: VCC -> RESISTOR -> LED -> OUTPUT let the LED operate. But now I observe a very strange behaviour.
(a) The non-inverting 74LS07 seems to invert the PWM, 0 := max light and 63 := no light
(b) Increasing the resistor value leads to a loss of contrast. I mean, the max intensity is less (as expected), but it does not turn off anymore.
Do I need a pull-up resistor at pin 15 of the Teensy for this?

(4) What is the correct way to handle the timer's border values (0 and 63)? I did not look too long into the datasheet, but I believe this is not pure off and on, is it? As you can see in my code, I turn it on or define the pin as an input, respectively. Is it ok or is there any better/more elegant solution?


I know, the problem is not a pure programmer's issue, but hopefully I can get certain about the Teensy stuff.
Let me know, if you have any further questions or if I shall provide any further information.

Best wishes from Germany,
Marius