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

Thread: Teensy 2.0 ++ attachInterrupt issue

  1. #1
    Junior Member
    Join Date
    Nov 2016
    Location
    Germany
    Posts
    8

    Teensy 2.0 ++ attachInterrupt issue

    Hello everybody,

    I'm building a simple weather station based on the sparkfun sen 08942 hardware. --> Datasheet
    My circuit consists out of a Teensy 2 ++ and the aneometer directly wired. (the other ones I don't need in the moment)

    Now in order to log this properly I use the
    Code:
    attachInterrupt(0, functionanme, FALLING);
    function. It works. However the interrupt is not triggered correctly. More precisely it gets triggered three times while the switch actually only closes once.

    I use the simplest code for testing:

    Code:
    volatile int aneoRevs = 0;
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      
      pinMode(1, INPUT_PULLUP);
      attachInterrupt(1, aneometer, FALLING);
    }
    
    void aneometer() {
       aneoRevs++;
       Serial.println(aneoRevs);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    }
    As you see the aneometer is connected to the D1 pin on the ++.

    The strange thing is, that once I switch from the teensy to an Arduino Uno, the issue is gone and he's counting the revs correctly.

    I guess this has to do with the Teensy 2 ++ hardware, but I don't know what it is. Can someone may point me out what I have to change in my circuit to get it running properly?

    Thanks in advance.
    Last edited by binux; 04-04-2017 at 06:39 PM.

  2. #2
    Junior Member
    Join Date
    Sep 2013
    Location
    Alps, Switzerland, (middleof)
    Posts
    18
    Reed switches in anemometers tend to bounce. You can debounce in software, but i prefer a simple Schmitt-Trigger. Then instead of fiddling around with Interrupts i also prefer to count frequency with the sensational library from Paul https://www.pjrc.com/teensy/td_libs_FreqMeasure.html

  3. #3
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    295
    The reason why the code works on UNO and not on Teensy could be that Serial.println(aneoRevs) in the interrupt handler will take only a few microseconds using the USB Serial on Teensy, but on UNO using 9600 baud, it will take around 5 or 6 milliseconds using the UART serial port. This extra time is probably enough for the contact bounce to stop before the interrupt handler returns. So the UNO only sees one interrupt per turn, but the faster Serial output, and therefore faster interrupt handler, allows the Teensy to count several contact bounces.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,474
    Normally the Bounce library should be used. Interrupts are terribly frustrating with mechanical chatter. The faster the processor, the more able it is to respond to the mechanical chatter. Teensy is much, much faster than Arduino Uno.

    Use the Bounce library. It really works well.

    Or if you really want to use the interrupt, you can build hardware to deal with the chatter, or at code to measure the microseconds since the last interrupt and ignore interrupts that occur too quickly.

Posting Permissions

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