Simple digital input button on teensy 3.6

Status
Not open for further replies.

educa

Active member
Hi and sorry if my question seems very easy.

I just want to connect a couple of buttons to a teensy and be able to do a digitalRead.

But... it is on a teensy 3.6


I searched the website and found some info about the obvious pulling up or down of a signal , but I think most of these are for AVR-based teensy pcbs

Therefore, I'd rather first ask before I possibly break my 3.6

Does 3.6 have internal pullups and do I init them the same way like with the avr based teensy's ? I believe if that would be the case, then I just have to set the pin as input and enable that pullup somehow and then connect my button to that pin and to ground like on regular avrs ?


Kind regards,

Bart
 
Are you asking if you can simply setup a pin to be an input pin with the internal pullup resistor enabled?

Yes like: pinMode(pin, INPUT_PULLUP);

You also have pulldown resistors as well: pinMode(pin, INPUT_PULLDOWN);

Note: I am not sure if the really old style of Arduino of getting a pullup by:
digitalWrite(pin, HIGH);
pinMode(pin, INPUT);

Will give you an input pull up... Have not tried that in a very long time... And doubt that would work
 
Yes Kurt. The very simple reading a button :)

I suppose you can also use attachInterrupt like on a regular arduino? I saw at least that on a teensy 3.6 a huge amount of pins can give an interrupt...
 
Yes you can use attachInterrupt. Just remember the interrupt numbers are different between some arduino boards and Teensy.
If you use the macro/function digitalPinToInterrupt(pin)
As part of it, than should work on both...

Also for buttons, should look at bounce library to take care of some of the issues with buttons, like the signal may change off/on/off/on as you press it...
 
Note that, even with attachInterrupt(), you still need to also use a pull-up (or pull-down) to drive the pin when the button is not actuating the connections to GND (or 3.3V.)
pinMode(..., INPUT_PULLUP / INPUT_PULLDOWN) is the way to go.
 
Great thank you ;)

I actually have 1 more question now that I see to have attention of 2 knowledged persons ;)

In my code, I will write some code to move stepper motors. That is something I can easily do, but at some point there is 1 little piece of code which I need to run automatically in the background.
It sounds worse than it actually is.

In fact what I need to do is for 1 single output pin I want to set the pin high, then continue my main program loop and somehow I would want that pin to be set low aprox 10ms after I set it high without the main loop having to take care of that. I do say aprox since anything between 9.5ms and 10.5ms would already be fine.

I do understand somehow that I might need a timer for that and on arduino avr platform I already managed to do that, but teensy 3.6 is not AVR of course.

Can I easily do that somehow.


Say for example I set pin 35 (A16) as output and I make it a logic HIGH. What would I exactly need then to enable a timer to automatically turn that pin low after a certain amount of time (+- 10ms) ?

Or should I just simply use the intervaltimer (https://www.pjrc.com/teensy/td_timing_IntervalTimer.html) ?
 
Yes myTimer.begin(function, 10000); should work fine.

A simple update to an output will be fast and return to the task at hand and should have microseconds of accuracy.
 
Depending on the button you may have to debounce the pin, either with code or Hardware or a combination of both.
 
There are a number of different ways to do this:
The simplest (hardware) is to set the mode to INPUT_PULLUP and ground that pin via your push button switch. You'll probably need software debouncing with this.
Personally, I would just set the mode to INPUT, use an external 100k and 1 nF cap to GND, connect the switch to Vcc (presumably 3.3V) via 4.7k, and when depressed, the switch should pull the pin high. If you need longer debouncing, increase the 4.7k and/or the 1nF cap.
 
Say for example I set pin 35 (A16) as output and I make it a logic HIGH. What would I exactly need then to enable a timer to automatically turn that pin low after a certain amount of time (+- 10ms) ?

As an alternative to using an IntervalTimer for the delay you can also use TeensyDelay. It simply calls a function in the background after the trigger period expires. Internally it uses a FTM / TPM timer to generate the delay and can handle up to 8 independent delay channels.

Here an example:
Code:
#include <TeensyDelay.h>

void switchOff()
{  
  digitalWriteFast(35,LOW);
}

void setup() 
{
  pinMode(35, OUTPUT);

  TeensyDelay::begin();
  TeensyDelay::addDelayChannel(switchOff);  // setup a delay channel and attach the switchOff function to it
}

void loop()
{
     digitalWriteFast(35,HIGH);
     TeensyDelay::trigger(10000); //call the switchOff() function after 10ms. This call is not blocking

     // do something 
     
     delay(500);
}
 
Status
Not open for further replies.
Back
Top