what is a encoder motor

encoder pic


a rotary encoder/encoder motor/shaft encoder are electro-mechanical devices that converts the angular position or motion of a shaft or axle to an analog or digital signal.

encoder motor

within the encoder are a set of hall effect sensors directly pointing at a magnetic timing disc within the armature that is attached to the shaft that is then connected to the gearbox. when the motor is given power the two sensors start reading the number of passes per second (the reason for two sensors is for reliability, accuracy, and redundancy). the encoder then translates and gives the mean value of the two sensors as an output. it is then the job of the controller (the Arduino board) to relate the two and incorporate the gear ratio and therefore can give the rpm of the output shaft.

I have found an experiment of someone using an encoder motor to give values using a void loop to give rpm:encoder motor diagram

//The sample code for driving one way motor encoder
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 3;//B pin -> the digital pin 3
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction 
void setup()
  Serial.begin(57600);//Initialize the serial port
  EncoderInit();//Initialize the module
void loop()
  duration = 0;
void EncoderInit()
  Direction = true;//default -> Forward  
  attachInterrupt(0, wheelSpeed, CHANGE);
void wheelSpeed()
  int Lstate = digitalRead(encoder0pinA);
  if((encoder0PinALast == LOW) && Lstate==HIGH)
    int val = digitalRead(encoder0pinB);
    if(val == LOW && Direction)
      Direction = false; //Reverse
    else if(val == HIGH && !Direction)
      Direction = true;  //Forward
  encoder0PinALast = Lstate;
  if(!Direction)  duration++;
  else  duration--;

below I have set up a theoretical experiment of two motors moving an object 1m and are using each others encoder values to make sure the motors both drive at the same rate:





The experiment above, in the video, was to find out how the encoders work with a spinning disc with magnetics attached and how to convert it into readable information. We first of found the correct wiring diagram for the encoder motor and then proceeded to connect and code the two individual motors to count and compare with each other to calculate the RPM and rotation direction of the motor.  The experiment shows how well one of these devices can perform and adding two only improves the reliability and flexibility of the module.

Feedback Loops

A “Closed Loop” system can use the feedback signal to adjust the speed and direction of the motor to achieve the desired result. In the case of an RC servo motor, the feedback is in the form of a potentiometer (pot) connected to the output shaft of the motor. The output of the pot is proportional to the position of the servo shaft.

New Doc 2018-02-19_1


As it turns out, there’s a  mechanism built into all Arduinos that is ideal for monitoring these of real-time events. This mechanism is called an Interrupt. An Interrupt’s job is to make sure that the processor responds quickly to important events. When a certain signal is detected, an Interrupt interrupts whatever the processor is doing, and executes some code designed to react to whatever external stimulus is being fed to the Arduino. Once that code has wrapped up, the processor goes back to whatever it was originally doing as if nothing happened.

What is good about this is that it structures your system to react quickly and efficiently to important events that aren’t easy to anticipate in software. Best of all, it frees up your processor for doing other stuff while it’s waiting for an event to show.

PID Libary

A PID controller calculates an ‘error’ value as the difference between a measured [Input] and the desired setpoint. The controller attempts to minimize the error by adjusting an Output. So, you tell the PID what to measure (the “Input”,) Where you want that measurement to be (the “Setpoint”,) and the variable to adjust that can make that happen (the “Output”.) The PID then adjusts the output trying to make the input equal the setpoint

experiment to make the motor act in conjunction with the rpm.





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s