0Pricing
Arduino & IoT Academy · Lesson

attachInterrupt on a Pin

Run code the instant a signal changes.

attachInterrupt on a Pin is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Wiring Code to a Pin

The attachInterrupt function tells your board: watch this pin, and the moment its signal changes, run my function right away.

Only Special Pins

Not every pin supports interrupts. On an Uno only pins 2 and 3 do, so plug your signal into one of those.

Translate Pin to Number

Wrap the pin in digitalPinToInterrupt so the right interrupt number is used. It keeps your code portable across boards.

digitalPinToInterrupt(2);

Three Pieces

You give attachInterrupt three things: which pin to watch, the function to run, and the kind of change that should trigger it.

attachInterrupt(digitalPinToInterrupt(2), myISR, RISING);

The Handler Function

The function you name is your handler, also called an ISR. The board calls it automatically; you never call it yourself.

void myISR() {
  flag = true;
}

RISING Edge

Pass RISING to fire when the pin goes from LOW to HIGH, like the moment a signal first switches on. 📈

FALLING Edge

Pass FALLING to fire when the pin drops from HIGH to LOW. A pull-up button often triggers here, since pressing pulls the pin down.

CHANGE Mode

Pass CHANGE to fire on either edge, both up and down. It is handy when you care about every transition of the signal.

Set It Up in setup()

Call attachInterrupt once inside setup, right after you set the pin mode. From then on the watching happens on its own.

pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), myISR, FALLING);

The Loop Stays Light

With the interrupt attached, your loop no longer polls that pin. The handler runs only when the chosen edge actually occurs.

Turning It Off

If you ever need to stop watching, detachInterrupt removes the link. Most projects attach once and never detach.

detachInterrupt(digitalPinToInterrupt(2));

Quick Check

Which trigger mode fires only when a pin goes from LOW up to HIGH?

Recap

You learned to call attachInterrupt with a pin, a handler, and a mode like RISING. Now hardware runs your code the instant a signal changes. 🎉

Frequently asked questions

Is the “attachInterrupt on a Pin” lesson free?

Yes — the full text of “attachInterrupt on a Pin” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “attachInterrupt on a Pin”?

Run code the instant a signal changes. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “attachInterrupt on a Pin” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Polling vs Interrupts
  2. attachInterrupt on a Pin
  3. Writing a Safe ISR
  4. Count Pulses from an Encoder
← Back to Arduino & IoT Academy