นับพัลส์จากเอ็นโคเดอร์
นับขอบสัญญาณความเร็วสูงโดยไม่พลาดแม้แต่ครั้งเดียว
นับพัลส์จากเอ็นโคเดอร์ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 🎉
คำถามที่พบบ่อย
บทเรียน “นับพัลส์จากเอ็นโคเดอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “นับพัลส์จากเอ็นโคเดอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “นับพัลส์จากเอ็นโคเดอร์”
นับขอบสัญญาณความเร็วสูงโดยไม่พลาดแม้แต่ครั้งเดียว คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “นับพัลส์จากเอ็นโคเดอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตรวจสอบสถานะเทียบกับอินเทอร์รัปต์
- ใช้ attachInterrupt กับขา
- เขียน ISR ที่ปลอดภัย
- นับพัลส์จากเอ็นโคเดอร์