0Pricing
Arduino & IoT Academy · Leçon

attachInterrupt sur une broche

Exécutez du code dès qu’un signal change.

attachInterrupt sur une broche est une leçon Arduino & IoT Academy gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Arduino & IoT Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Arduino & IoT Academy comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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

Questions Fréquemment Posées

La leçon « attachInterrupt sur une broche » est-elle gratuite ?

Oui — le texte complet de « attachInterrupt sur une broche » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Arduino & IoT Academy, passe à CoddyKit PRO. Le cours Arduino & IoT Academy comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « attachInterrupt sur une broche » ?

Exécutez du code dès qu’un signal change. Tu pratiques Arduino & IoT Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Arduino & IoT Academy ?

Aucune expérience préalable n'est requise. Arduino & IoT Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.

Combien de temps prend la leçon « attachInterrupt sur une broche » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Arduino & IoT Academy ?

Oui. Chaque leçon Arduino & IoT Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Scrutation ou interruptions
  2. attachInterrupt sur une broche
  3. Écrire une ISR sûre
  4. Compter les impulsions d’un encodeur
← Retour à Arduino & IoT Academy