

A DC motor with an encoder is a precision device that combines the functionality of a direct current (DC) motor with a rotary encoder. This combination allows for the precise control of the motor's position, speed, and acceleration. Common applications include robotics, automated machinery, and any system requiring accurate motion control.








| Pin Number | Description | Notes |
|---|---|---|
| 1 | Motor Power (+) | Connect to motor power supply |
| 2 | Motor Power (-) | Connect to ground |
| 3 | Encoder Vcc | Encoder power supply (3.3V/5V) |
| 4 | Encoder GND | Ground for encoder |
| 5 | Encoder Output A | Signal A output |
| 6 | Encoder Output B | Signal B output (optional) |
| 7 | Encoder Index | Index pulse (optional) |
// Define motor and encoder pins
const int motorPin1 = 3; // Motor pin 1
const int motorPin2 = 4; // Motor pin 2
const int encoderPinA = 2; // Encoder output A
// Variables to hold encoder position
volatile long encoderPos = 0;
// Interrupt service routine for encoder A
void encoderA_ISR() {
// Change in encoder position - adjust as necessary based on motor direction
encoderPos++;
}
void setup() {
// Set motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Set encoder pin as input
pinMode(encoderPinA, INPUT_PULLUP);
// Attach interrupt for the encoder pin A
attachInterrupt(digitalPinToInterrupt(encoderPinA), encoderA_ISR, RISING);
// Start the motor
analogWrite(motorPin1, 128); // Set speed (0-255)
digitalWrite(motorPin2, LOW); // Set direction
}
void loop() {
// The main loop of the program
// Use encoderPos to control or monitor the motor position
}
Q: How do I reverse the motor direction? A: Reverse the polarity of the motor connections or control the motor driver's direction pin.
Q: What is the purpose of the encoder index pulse? A: The index pulse is a reference signal used to establish a known position for the motor.
Q: Can I use this motor with a 3.3V system? A: Yes, but ensure the encoder is compatible with 3.3V and use a motor driver that supports 3.3V logic levels.
Remember to always refer to the specific datasheet of your DC motor with encoder for precise specifications and connection details.