Teensy 4.0 pulse output

Status
Not open for further replies.

Deane

Well-known member
I am considering a project in which the processor controls a laser to turn it on for 100 nanoseconds. With the fast speed of this new Teensy 4 I am thinking this could work. Has anyone tried generating pulses this short with it yet? How short a pulse can it output at full speed? Thanks.
 
Various ON and OFF testing yes, this context possibly not :)

Check out :: static inline void delayNanoseconds(uint32_t nsec)

This is available for Teensy 4.0. Supposing a scope is at hand the ON, delayNanoseconds( X ), OFF could be observed to perhaps a usable value of X to get the desired with interrupts disabled.

For ref here is one post about 150 MHz cycling with scope pictures :: Teensy-4-0-Bitbang-FAST

Finer control than Nanoseconds is possible - I posted a delayCycles() { i.e. 1/600MHz } that was good starting near 20 cycles - post #8 on same thread - but had variability of 1-4 cycles depending on the value chosen.
 
Thanks Paul. Looks like this should work well if it can cycle at 150 MHz (6 nanoseconds). 100 nanoseconds should be easy.
Why did you say "this context possibly not"?
I think I will buy one and start experimenting. Where is the best place to get one in the US?
 
Thanks Paul. Looks like this should work well if it can cycle at 150 MHz (6 nanoseconds). 100 nanoseconds should be easy.
Why did you say "this context possibly not"?
I think I will buy one and start experimenting. Where is the best place to get one in the US?

Not Paul - but you are welcome.

The context was : "processor controls a laser … Has anyone tried generating pulses this short with it yet?"
For the note with regard to short laser pulses : "this context possibly not"

As far as Teensy 4.0 purchase they are stocked by : PJRC.com, SparkFun.com, Adafruit.com … and others { digikey, OSHpark, Amazon, there is a thread/page on distributors ... local shops } depending on where you shop and how shipping speed/cost factors in.
 
After giving up on the Teensy 4 for lack of fast AD conversions last January I dragged it out and started playing with it as my lidar timing generator. My notes say I had it creating 100 ns. pulses but when I run the software now all I get is a 7.2 seconds period square wave on pin 13 and no observable pulses on pin 14. What has gone wrong here? Any ideas appreciated.
The code is short:
Code:
/* LED Blink, Teensyduino Tutorial #1
   [URL]http://www.pjrc.com/teensy/tutorial.html[/URL]

   This example code is in the public domain.
*/

// Teensy 2.0 has the LED on pin 11
// Teensy++ 2.0 has the LED on pin 6
// Teensy 3.x / Teensy LC have the LED on pin 13
const int ledPin = 13;
const int fastPin = 14;
const int us = 1;
const int ns = 78;  // 78 gives a 100 ns pulse
// the setup() method runs once, when the sketch starts

void setup() {
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);
  pinMode(fastPin, OUTPUT);
}

// the loop() runs over and over again,
// as long as the board has power

void loop() {
  noInterrupts();
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(500);                  // wait for 500 ms
  digitalWrite(ledPin, LOW);    // set the LED off

  digitalWriteFast(fastPin, HIGH);   // Fast pulse test
  delayNanoseconds(ns);
  digitalWriteFast(fastPin, LOW);

  delay(500);  // Wait and repeat.
}
 
Last edited by a moderator:
Please use code-tags when posting code.

How do you measure this?
I had problems getting my cheap scope to trigger with your code.
I think you just did'nt see the pulses.

The code below works better:
Don't disable the interrupts forever. Esp. not with delay().

If you really want to disable them, use delayMicroseconds():

Code:
const int ledPin = 13;
const int fastPin = 14;
const int ns = 78;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(fastPin, OUTPUT);
  noInterrupts();
}

void loop() {
  for (;;) {
    digitalWriteFast(ledPin, HIGH);
    delayMicroseconds(500 * 1000);
    digitalWriteFast(ledPin, LOW);
    digitalWriteFast(fastPin, HIGH); // Fast pulse test
    delayNanoseconds(ns);
    digitalWriteFast(fastPin, LOW);
    delayMicroseconds(500 * 1000);
  }
}

then, delay() calls yield(). You don't want that.
loop() calls yield(), too.
 
Last edited:
@Frank B Thank you for the updated code. It works. However the code I was using was an offical PRJC demo program so I don't know why the disable Interupts command was in there to start with.
I have a lot to work through to make this function the way I want. If we eliminate the delay at the bottom the code creates 100 nanosecond pules and the off time between them is 167 nanoseconds.
My question now is there any way to speed this up so that I can just a bit so I can send a shorter pulse on a different pin just a few ns. after the 100 ns pulse has ended? I am really not familar with C language for the Teensy 4.0.
 
Not sure.. if this is what you want?
Code:
const int ledPin = 13;
const int fastPin = 14;
const int fasterPin = 15; //added
const int ns = 78;


void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(fastPin, OUTPUT);
  pinMode(fasterPin, OUTPUT);
  noInterrupts();
}


void loop() {
  for (;;) {
    digitalWriteFast(ledPin, HIGH);
    delayMicroseconds(500 * 1000);
    digitalWriteFast(ledPin, LOW);
    digitalWriteFast(fastPin, HIGH); // Fast pulse test
    delayNanoseconds(ns);
    digitalWriteFast(fastPin, LOW);


    //Added:
    digitalWriteFast(fasterPin, HIGH);
    //delayNanoseconds(5); //for fastest operation remove delay
    digitalWriteFast(fasterPin, LOW);
    
    delayMicroseconds(500 * 1000);
  }
}
 
More questions

What is the purpose of the line?
for (;;)
Wouldn't the loop run forever without this for statement because it is already inside the "loop()" command?

Also, I have a question about a command I have never seen a description of:
Someone told me that I could increase the speed of an Arduino port write by using this: PIND = PIND | 0b00000100; // Toggle the D2 port
It increased the speed of the write command by 31 times in my Nano! How does this work?
Does the Teensy 4.0 have a similar command? The Nano operates at 8MHz and the Teensy at 600 MHz so there is already a great boost in speed.
 
Last edited:
The for (;;) is a loop (inside the loop())
It is there to prevent that additional things are calld (not visible in your program - teensyduino adds them, invisible)
SO...no...it runs !faster! with for(;;)


Edit: Err, not really. As long as you wait 500ms there - it will wait 500ms .. :)


Teensy uses an ARM processor. Most of those AVR "tricks" do not work, because the internal registers are totally different. But Teensyduino has better tricks.
You already use it - it is "digitalWriteFast".

Maybe is would help to know what you are trying to do?
Can you explain your needs and what you are trying to archieve?

You already have the 100ns pulse you wanted in your first post.
 
Last edited:
Thanks for informing me about the tricks in getting max speed from the Teensy 4.
I would be happy to explain the goals. I am trying to build a specialized LIDAR system with a range up to 1km.
So the timing would look something like this (see attachment).
What I don't know is if the built in A/D is fast enough to do this. I had little luck getting it to work fast last year.
 

Attachments

  • LIDAR Timing Diagram.jpg
    LIDAR Timing Diagram.jpg
    111.1 KB · Views: 69
My question now is there any way to speed this up so that I can just a bit so I can send a shorter pulse on a different pin just a few ns.

This is pushing the limits of my cheap equipment....
Cheap scope with cheap probes.

pic_26_1.png
Is that short enough?
 
Last edited:
Ok, the 2nd puls (code from Post #8) might be little too fast-.. maybe try it.
If it does not work, we'll see how to make it a little bit longer.

Edit: There is a gap between both pulses. Seeing Post#12, that gap is not allowed?
In that case we have to go down to the register level .. like on AVR, to set two pins simultanously.
 
Last edited:
Hi Frank, your attachment shows "Invalid Attachment specified. If you followed a valid link, please notify the administrator"...
Paul
 
@Pauls: Thank you - fixed, I hope?
@Deane: If you want the whole procedure exactly as shown in your picture, it gets a little more complicated.. I don't see that you can program that.. This is not meant in a derogatory way
You should look for help, then. It will take some time to do it exactly as shown. I *think* the teensy can do all that, but the timing is tight - esp. with the A/D


I measured again, my scope says the second pulse is 8.4 nanoseconds.

Edit: Have not seen at the first look, that it would need a 10MHz A/D. So, no the teensy can't do that.
 
Last edited:
I was thinking the A/D could do it because the processor is 600 MHz and as of last January no one seemed to know the speed of the A/D.
Maybe more info has come out since then. The manual for the ARM chip is over 1000 pages and I couldn't find it in there. Where did you get the 10 MHz number for it?

So I will start looking into triggering an external A/D. Ideally it will have some built in memory so that it can just keep running and take 20 readings, one every 100 ns.
Any ideas on an A/D that can do that?
 
Attachment is fixed. I can see your scope screen.
Here is what I see with the code of msg #8 and measured on pin 15 [fasterPin]:

SDS00027.png

The long pulse measures 97.2 ns.

Paul
 
Thank you Pauls
The AD is 1MSPS.
That was known back in january.
2021-01-19 18_45_54-Start.png

I've seen to late that you'd need 10MSPS.That is too much, I don't believe that you can overclock it that much.
 
Last edited:
10MSPS ADC needs a separate (presumably parallel) ADC chip and you need a datapath for it too, this is high speed acquisition.
FPGA's are often used once you get to these kinds of speeds as the hardware needed can be directly configured.
 
Hi Deane, just thinking out loud here. How does the analog signal that you want to sample, look like? I assume it's pulse-like because that is what the laser sends out. In that case, could you use a high-speed comparator for the analog signal [like a 1-bit ADC]? That comparator will output a digital signal. And that digital signal can be fed into a not-too-expensive logic analyzer for timing measurements.

Paul
 
PaulS, no that wouldn't work as I need to know the amount of signal coming back. That is essential and important data. It would not be either on or off.
I have a 20 Mega samples per second A/D Maxim 1425 but there are probably others that are better. Yes, this is high speed data acquisition. An FPGA would be nice
but that would require learning an entirely new set of software and hardware tools. I have seen some advertised as easy to learn such as the Alchitry.
I was hopeful that the 600 MHz Teensy would be fast enough. Shouldn't this work with an external A/D?
 
If you want to integrate pulses, use an integrator before sampling. Then you need a much lower sample rate.
The pulse time can be captured with a comparator, the pulse energy by differencing the integrated values before and after
the pulse. The integrator needs to be reset after each pulse too.
 
@MarkT : Are you suggesting to integrate the analog output of the APD and sample it at a slower rate, like maybe once every 200 ns (5 MHz rate)? I suppose that could work but the internal A/D has a 1 MHz sample rate max. so I would still need an external A/D.
 
Status
Not open for further replies.
Back
Top