Overview
Rotary encoders measure motor rotation using quadrature signals (channels A and B). The ESP32 reads these signals via interrupts to count encoder ticks and calculate velocity.Quadrature Encoding
Complete Implementation
encoder_reader.h
encoder_reader.cpp
Usage Example: Single Encoder
single_encoder_example.ino
Four-Wheel Implementation
four_wheel_encoders.ino
Forward Kinematics from Encoders
Calculate robot velocity from wheel encoders:mecanum_odometry.ino
Troubleshooting
Count not incrementing
Count not incrementing
Solutions:
- Check wiring (A to GPIO 34, B to GPIO 35, GND, VCC)
- Verify encoder power (5V or 3.3V depending on encoder)
- Test pins individually:
- Manually rotate motor and observe output
Count only goes one direction
Count only goes one direction
Cause: Channel B not connected or wrong pinSolutions:
- Verify pin_b is correct input-only pin (34-39)
- Check Channel B wiring
- Swap A and B channels if reversed
Erratic count / noise
Erratic count / noise
Solutions:
- Add hardware debouncing (0.1µF capacitor across A and B)
- Use pull-up resistors (10kΩ)
- Shorten encoder wires
- Keep encoder wires away from motor power wires
Velocity always zero
Velocity always zero
Solutions:
- Ensure
updateVelocity()called regularly (every 50ms) - Check COUNTS_PER_REV matches your encoder
- Verify motor is actually spinning
- Add debug prints:
Velocity fluctuates wildly
Velocity fluctuates wildly
Cause: Update rate too fast or too slowSolutions:
- Use fixed 20-50 Hz update rate (50ms)
- Add low-pass filter:
- Average over multiple readings
Pin Considerations (ESP32)
Next Steps
PID Controller
Use encoder velocity for PID feedback
Motor Control
Complete closed-loop motor control
Serial Communication
Send encoder data to ROS2
Hardware Reference
JGA25-370 encoder specifications