Skip to content

855princekumar/2ch-motor-encoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MotorEncoder Library

Arduino & Raspberry Pi Pico Quadrature Encoder Reader

Author: Prince Kumar (TA - Mechatronics)

Repo: GitHub – 2ch-motor-encoder

image image image image image

Features

  • 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
image

Works with the following encoder types:

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

Quick Start

  1. Connect encoder pins:

    • Channel A → GPIO2
    • Channel B → GPIO3
  2. Install library: download ZIP → Arduino IDE → Sketch > Include Library > Add .ZIP Library.

  3. Load example: File > Examples > MotorEncoder > BasicUsage

  4. Open Serial Monitor at 115200 baud.

Usage Example

#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);
}

Parameters

  • 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

Output Data (EncoderData struct)

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

Note: Home Position / Initial Tick Value

By default, the library considers the encoder position as zero on startup.

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.

Applications

  • Robotics
  • Differential drive control
  • AGVs & line-followers
  • Mechatronics labs (teaching tool)

References and Further Reading

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.

License

MIT License – Free to use for projects, teaching, and research.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages