0Pricing
Arduino & IoT Academy · Lesson

Count Pulses from an Encoder

Tally fast edges without missing any.

Count Pulses from an Encoder is a free Arduino & IoT Academy lesson on CoddyKit — lesson 4 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.

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

Frequently asked questions

Is the “Count Pulses from an Encoder” lesson free?

Yes — the full text of “Count Pulses from an Encoder” 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 “Count Pulses from an Encoder”?

Tally fast edges without missing any. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Count Pulses from an Encoder” 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