0Pricing
Arduino & IoT Academy · Lezione

Usare attachInterrupt su un pin

Esegua il codice nell'istante in cui cambia un segnale.

Usare attachInterrupt su un pin è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. 🎉

Domande Frequenti

La lezione «Usare attachInterrupt su un pin» è gratuita?

Sì — il testo completo di «Usare attachInterrupt su un pin» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Cosa imparerò in «Usare attachInterrupt su un pin»?

Esegua il codice nell'istante in cui cambia un segnale. Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Arduino & IoT Academy?

Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Usare attachInterrupt su un pin»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?

Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Polling contro interrupt
  2. Usare attachInterrupt su un pin
  3. Scrivere una ISR sicura
  4. Contare gli impulsi di un encoder
← Torna a Arduino & IoT Academy