Contare gli impulsi di un encoder
Conteggi i fronti rapidi senza perderne nessuno.
Contare gli impulsi di un encoder è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 4 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.
A Wheel of Pulses
A rotary encoder sends a quick stream of electrical pulses as it spins. Count those pulses and you know how far it has turned.
Too Fast to Poll
Spin an encoder quickly and pulses fly by in microseconds. A normal loop reading the pin would miss many of them.
Interrupts to the Rescue
This is the perfect job for an interrupt. The hardware catches every edge for you, so no pulse slips through unnoticed. ⚡
A Volatile Counter
Make a volatile counter the ISR can increment. Marking it volatile keeps the loop reading the true, up-to-date total.
volatile long pulseCount = 0;Attach the Interrupt
In setup, point attachInterrupt at the encoder pin and fire on RISING so each pulse calls your counting handler.
attachInterrupt(digitalPinToInterrupt(2), countPulse, RISING);A Tiny ISR
The whole handler does one thing: add one to the counter. Short and fast, exactly as a good ISR should be.
void countPulse() {
pulseCount++;
}Read Without Tearing
To copy the count in the loop, briefly pause interrupts so the ISR cannot change it mid-read and leave you a torn value.
noInterrupts();
long c = pulseCount;
interrupts();Use the Count
Now the loop can print the total, compute speed, or react, all while the ISR keeps tallying new pulses in the background.
Serial.println(c);From Pulses to Rotation
Each encoder has a fixed pulses-per-turn. Divide your count by that number to learn how many full revolutions happened.
Measuring Speed
Count pulses over a known time window in the loop, and you can turn that rate into speed or RPM for a motor or wheel.
Reset When Needed
To start a fresh measurement, set the counter back to zero, again pausing interrupts so you do not lose a pulse arriving at that instant.
noInterrupts();
pulseCount = 0;
interrupts();Quick Check
Why do we briefly pause interrupts when reading the pulse counter in the loop?
Recap
You built a pulse counter: a volatile total, a tiny ISR, and safe reads in the loop. Now you can measure rotation and speed reliably. 🎉
Domande Frequenti
La lezione «Contare gli impulsi di un encoder» è gratuita?
Sì — il testo completo di «Contare gli impulsi di un encoder» è 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 «Contare gli impulsi di un encoder»?
Conteggi i fronti rapidi senza perderne nessuno. 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 4 di 4.
Quanto tempo richiede la lezione «Contare gli impulsi di un encoder»?
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
- Polling contro interrupt
- Usare attachInterrupt su un pin
- Scrivere una ISR sicura
- Contare gli impulsi di un encoder