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.




how could a hall magnetic field sensor be used?

using the hall effect sensor couldn’t be easier, as I’m using a cheaper sensor it is set in a binary configuration, so it is just the case of connecting it to a digital input on the Arduino.

i have set up an experiment to show how it could be used to activate an LED

Arduino Hall Magnetic Sensor Module is a switch that will turn on/off in the presence of a magnetic field.



int led = 13;//LED pin
int sensor = 3; //sensor pin
int val; //numeric variable

void setup()
	pinMode(led, OUTPUT); //set LED pin as output
	pinMode(sensor, INPUT); //set sensor pin as input

void loop()
	val = digitalRead(sensor); //Read the sensor
	if(val == HIGH) //when magnetic field is detected, turn led on
		digitalWrite(Led, HIGH);
		digitalWrite(Led, LOW);


fritzing HMFS

photo HMFS


the next step to this is to attach a set of magnets to a spinning disc and read how many reads per second when put into the serial monitor this can be given a feedback loop to then dictate a set speed. this method is used in encoder motors and some stepper motors.

what is a hall magnetic field sensor?

hall magnetic field sensor or hall effect sensor is a surprisingly common device found in a lot of applications.

the sensor works by having a thin layer of a semiconductor such as indium antimonide, which when placed in proximity to a magnetic source it polarises the semiconductor. this polarisation courses a small voltage difference, a high gain amplifier is used to increase the signal to be read properly.

low-quality high gain amps are most commonly used so a lot of these sensors are viewed ad binary, either on or off. high-quality amps can be used for an analog interface. as of how small these sensors can get and how cheap they are they can be used in quantity to read a digital set of variables.

the most common use cases of these sensors are for motors to calculate its rpm and therefore can be used in tandem with other sensor-motor modules to calculate direction and velocity of the system.