Skip to content

6.1 Power Board

Dorothy Ma edited this page Jan 18, 2026 · 5 revisions

Interrupt Pin for Water Detection

Objective: Implement an interrupt that is called by the water detection sensor to shut down the power board.

  1. To use any pin on the Teensy 4.0 as an interrupt, use attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
  • digitalPinToInterrupt converts the desired pin to the interrupt number recognized by the attachInterrupt function.
  • ISR is the interrupt service routine to be called. Should take 0 parameters and return nothing.
  • mode is the trigger type of the signal. Modes include RISING, FALLING, LOW, and CHANGE for detecting rising signal, falling signal, low signal, or any change in signal.
  • Link to documentation here.
  1. For the rising edge signal of our Water Detection sensor, set up the pinMode to be pinMode(pin, INPUT_PULLDOWN)
  • All digital pins have optional pullup, pulldown, or keeper resistors. PULLDOWN will keep the pin logic at a LOW signal when not driven by external circuitry. Prevents the signal from floating.
  • Our sensor sends a RISING signal, which means the default of the pin should be at LOW logic, such that the interrupt will be called when the sensor triggers a change from LOW to HIGH.
  • Link to pullup, pulldown, and keeper resistors for digital pins here.
  1. Setting the priority of interrupts with NVIC_SET_PRIORITY(iqrn, priority)
  • All pins for Teensy 4.X use the iqrn: IRQ_GPIO6789.
  • There are 16 levels of interrupt, from 0-255, where 0 is the highest priority. The priorities are sorted in intervals of 16, meaning 0-15 will all have the same priority and 16-31 will have the same priority.
  • The default priority is 128.
  • Source: attachInterrupt Priority

Notes:

  • delay() uses interrupts to work, so they will not work if called in the ISR. Use instead delayMicroseconds().
  • To turn off an interrupt, use detachInterrupt(digitalPinToInterrupt(pin)).

Sample Code

Arduino built-in LED will flash at interval of 1000 ms. Setup Pin #11 to detect for a rising edge signal, upon which, the flashing will change to every 200 ms.

#include <Arduino.h>
const byte water_pin = 11;
int pwm;
int delaytime = 1000;


// put function declarations here:
void interrupt_fcn()
{
  Serial.print("HELLO");
  digitalWrite(LED_BUILTIN, HIGH);
  delaytime = 200;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LED_BUILTIN, HIGH);
  pinMode(water_pin, INPUT_PULLDOWN);
  attachInterrupt(digitalPinToInterrupt(water_pin), interrupt_fcn, RISING);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED_BUILTIN, HIGH);
  delay(delaytime);
  digitalWrite(LED_BUILTIN, LOW);
  delay(delaytime);
  pwm = digitalRead(water_pin);
  Serial.print(pwm);
}

Clone this wiki locally