Arduino & Raspberry Pi Pico Quadrature Encoder Reader
Author: Prince Kumar (TA - Mechatronics)
Repo: GitHub – 2ch-motor-encoder
- Read direction (FWD/REV)
- Get shaft RPM & shaft angle
- Compute output RPM after gearbox
- Calculate wheel speed in m/s and km/h
- Auto-detect gear ratio if motor-rated RPM is given
- Supports active-high & active-low encoders
| Encoder Type | Signal Type | Recommended Placement | Notes |
|---|---|---|---|
| Mechanical Switch | Digital (active-high/low) | Near wheel shaft | Good for low-speed experiments; debounce may be required |
| Optical / Photoelectric Slot | Digital (active-high/low) | Motor shaft or wheel | Fast response, suitable for robotics projects |
| Magnetic Hall-Effect | Digital (active-high/low) | Close to rotating magnet | Durable, works in dusty/dirty environments |
| Incremental Rotary | Digital quadrature | Motor shaft | Provides high resolution angle and speed |
-
Connect encoder pins:
- Channel A → GPIO2
- Channel B → GPIO3
-
Install library: download ZIP → Arduino IDE → Sketch > Include Library > Add .ZIP Library.
-
Load example: File > Examples > MotorEncoder > BasicUsage
-
Open Serial Monitor at 115200 baud.
#include <MotorEncoder.h>
MotorEncoder encoder;
void setup() {
Serial.begin(115200);
encoder.begin(2, 3, 600, 0, 100.0, 300, false, true);
}
void loop() {
EncoderData data = encoder.read();
Serial.println(data.shaftRPM);
}- pulsesPerRev: PPR from datasheet (0 if unknown)
- gearRatio: if 0, auto-calculated when motorRatedRPM is given
- wheelDiameter_mm: wheel outer diameter
- motorRatedRPM: vendor RPM (after gearbox)
- invertDirection: true if gearbox inverts motion
- activeHigh: true for standard encoders, false for switch-based
| Field | Unit | Description |
|---|---|---|
| tickCount | ticks | Raw pulse count |
| directionForward | bool | Rotation direction |
| shaftRPM | RPM | Motor shaft speed |
| shaftAngleDeg | ° | Shaft angular position |
| outputRPM | RPM | Gearbox output speed |
| wheelSpeed_mps | m/s | Linear speed |
| wheelSpeed_kmph | km/h | Linear speed |
| wheelAngleDeg | ° | Wheel angular position |
If you want to start from a custom position instead of zero (for example, to set a known home/reference point), you can use the following snippet:
#include <MotorEncoder.h>
MotorEncoder encoder;
void setup() {
Serial.begin(115200);
encoder.begin(2, 3, 600, 0, 100.0, 300, false, true);
// Set a custom initial tick count (e.g., home position)
encoder.setInitialTicks(100); // starts counting from 100 ticks instead of 0
}
void loop() {
EncoderData data = encoder.read();
Serial.print("Ticks: "); Serial.println(data.tickCount);
}This allows to define a reference position, which can be useful in robotics, line-followers, or practical lab setups where the motor/wheel does not always start from zero.
- Robotics
- Differential drive control
- AGVs & line-followers
- Mechatronics labs (teaching tool)
- Quadrature Encoder Basics – Video 1 (YouTube)
- Type of Encoder as Absolute or Incremental – Video 2 (YouTube)
- Arduino Official Interrupt Reference
- Quadrature Encoder Wikipedia Article
- Pico Arduino Core Documentation
- PJRC Encoder Library (for comparison)
These links give background on how encoders work, how interrupts are handled on Arduino-style boards, and how the common Encoder library implements similar functionality.
MIT License – Free to use for projects, teaching, and research.