0Pricing
Arduino & IoT Academy · レッスン

エンコーダーのパルスを数える

高速なエッジを取りこぼさずに数えます。

「エンコーダーのパルスを数える」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「エンコーダーのパルスを数える」レッスンは無料ですか?

はい。「エンコーダーのパルスを数える」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「エンコーダーのパルスを数える」で何を学びますか?

高速なエッジを取りこぼさずに数えます。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Arduino & IoT Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「エンコーダーのパルスを数える」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このArduino & IoT Academyレッスンでコードを書いて実行できますか?

はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ポーリングと割り込み
  2. ピンでattachInterruptを使う
  3. 安全なISRを書く
  4. エンコーダーのパルスを数える
← Arduino & IoT Academyに戻る