Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Simple digital input button on teensy 3.6

  1. #1
    Member educa's Avatar
    Join Date
    Jan 2017
    Location
    Belgium
    Posts
    21

    Simple digital input button on teensy 3.6

    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

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    4,903
    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

  3. #3
    Member educa's Avatar
    Join Date
    Jan 2017
    Location
    Belgium
    Posts
    21
    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...

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    4,903
    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...

  5. #5
    Senior Member
    Join Date
    Dec 2014
    Posts
    253
    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.

  6. #6
    Member educa's Avatar
    Join Date
    Jan 2017
    Location
    Belgium
    Posts
    21
    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_timin...rvalTimer.html) ?

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,827
    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.

  8. #8
    Senior Member
    Join Date
    Mar 2013
    Posts
    651
    Depending on the button you may have to debounce the pin, either with code or Hardware or a combination of both.

  9. #9
    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.

  10. #10
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    442
    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);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •