Controlling a motor with an Arduino is relatively easy. In addition to simply spinning the motor, you can control the position of the motor shaft if the motor has a rotary encoder.

Faraday's Law states that:

Any change in the magnetic environment of a coil of wire will cause a voltage (emf) to be “induced” in the coil.

This explains how generators are able to produce voltage. This is mechanical energy to electrical energy conversion. Motors operate in reverse of generators; they convert electrical energy to mechanical energy. In motors,  current is fed into the armature winding which creates a magnetic field that interacts with the magnetic field created by permanent magnets in the stator. The interaction between two magnetic fields causes the armature to rotate.

There are many different types of motors, including:

  • Direct Current (DC) motors (the one that I'll be using in this tutorial).
  • Alternating Current (AC) motors.
  • Brushless DC (BLDC) motors.
  • Brushless AC (BLAC) motors.
A rotary encoder is a device that converts the angular position or motion of a shaft or an axle to an analog or digital code. There are two types of rotatory encoders:
  • Incremental encoders. An incremental rotary encoder outputs pulses only while a motor is rotating. To determine the shaft position using an incremental encoder, you must know the starting position and the use external circuitry to count the number of output pulses.
  • Absolute encoders. An absolute rotary encoder outputs a digital code corresponding to the rotation angle. There is no need to count pulses to know the position of the motor shaft. You need only read the digital output of the encoder.
In this project, we are using an incremental rotary encoder. The figure below shows a typical rotary encoder.


 

Magnetic Encoder in motor used in this tutorial

An H-Bridge is an electronic circuit that enables a microcontroller, such as an Arduino, to control the movement of a motor. A microcontroller cannot be connected directly to a DC motor because the microcontroller cannot supply the high current required by the motor. The H-bridge contains the high-current switches needed for motor control. These switches are controlled by signals from the Arduino.

An H-bridge allows a motor to be moved forwards or backwards. The direction is determined by the sequence of control signals from the Arduino.

Motor speed is determined by setting the duty cycle of the control signals. This type of speed control is called pulse-width modulation. The motor will run at full speed when the duty cycle is 100%. When the duty cycle is 0%, however, the motor will stop and will no longer move.

For this tutorial, we have designed and built an H-bridge that uses the following components:

  • TIP 142 and TIP 147 transistors
  • Diodes
  • Green Connectors
  • Optocouplers

A simplified diagram of the H-Bridge is shown:


    A fabricated H-bridge module is used for this tutorial. You can find a lot of schematics for H-bridge circuits in the web, but if you don't want to build you own, you can use a L298 IC.

    Equipment required

    The following equipment was used for this experiment
    • Lipo Battery 11.1V 
    • Arduino ATmega 2560
    • H-Bridge (self-fabricated or IC L298) 
    • motor with encoder
    • jumpers (as required)
    • Green Connector (aka L-connectors)
    • 6 pin connector (for motor specifically)
    • Wheel (optional for visible motion of motor shaft)


    Steps:

    1.  Gather all parts required:



    Motor
    Motor

    Close-up picture showing the motor encoder pins

    Arduino
    Arduino Mega 2560

    6-pin Connector for motor
    6-pin Connector for motor

    Green Connector
    Green Connector

    Battery
    Battery

    H-Bridge
    H-Bridge

    Jumpers
    Jumpers

    3pin Connector for H-Bridge
    3-pin Connector for H-Bridge

    Wheel (optional)
    Wheel (optional)

    2.  Connect the jumper to the battery and then power up the H-bridge with the battery:



    3. Connect the 3-pin connector to the H-Bridge module then connect the H-Bridge module to Arduino following this connection:

    • Arduino 13 --> PWM
    • Arduino 12 --> Forward
    • Arduino 11 --> Reverse 


    5.  Now connect the 6-Pin connector to the motor and connect then connect the motor to the H-bridge module.


    Don't forget to connect the motor power pins to the H-Bridge power pins. The motor encoder pins labeled  Hall VCC and Hall GND should be connected to the Arduino 5V and GND pins. Finally, connect motor encoder output channel CH.A to Arduino pin number 21. The connections are shown below:


    A closer look of the Arduino pin connections:


    6.  Now connect the Arduino to your computer or laptop for programming:


    Code for Checking Encoder Manually

                        int encoderValue=0;
    
    void count(void); // code for counting the increasing values of encoder ticks void setup()
    
    {
    
    Serial.begin(9600);
    
    pinMode(21,INPUT);
    
    attachInterrupt(2,count,FALLING);
    
    encoderValue=0;
    
    }
    
    void loop()
    
    {
    
    Serial.print("Starting\n");
    
    // to print encoder value on the screen Serial.print("Encoder Value="); Serial.println(encoderValue);
    
    }
    
    void count()
    
    {
    
    encoderValue++;
    
    }
                      

      Download Code  


    Code for Running Motor till Encoder Specific Counts

                        //defining Pins names for the code
    
    int pwm=13; int forward=12; int reverse=11;
    
    
    int encoderValue=0; void count(void); void setup()
    
    {
    
    Serial.begin(9600);
    
    pinMode(21,INPUT);
    
    attachInterrupt(2,count,FALLING);
    
    encoderValue=0;
    
    }
    
    void loop()
    
    {
    
    digitalWrite(pwm,1);
    
    digitalWrite(forward,0);
    
    analogWrite(reverse,140);
    
    Serial.print("Starting\n");
    
    delay(3000); Serial.print("Encoder Value="); Serial.println(encoderValue); while(1)
    
    {
     
    
    
    // to print encoder value on the screen Serial.print("Encoder Value="); Serial.println(encoderValue); //Setting value of encoder
    
    //defining the while statement condition if(encoderValue<5000)
    
    break; // loop will break as soon as encoder value reaches 5000 or above digitalWrite(forward,1);
    
    digitalWrite(reverse,0);
    
    analogWrite(pwm,150);
    
    }
    
    digitalWrite(forward,1);
    
    digitalWrite(reverse,1);
    
    analogWrite(pwm,255);
    
    }
    
    void count()
    
    {
    
    encoderValue++;
    
    }
                      

      Download Code  


    Conclusion

    Encoders are devices that can be attached to any rotary or linear device to track the motion and orientation of the device. Here we used an absolute rotary encoder to both control the motor speed and shaft position.


    Videos



    Tags:

     

    Comments

    5 Comments


    • PUSSYCATlovesGURGEL 2016-03-11

      OK ALL FRIENDS!  we need now is : arduino control a 3 phase ac motor , high power 10 HP ac motor, 96Volts , this is for a EV project , batery bank is 8pcs x 12Volts . thanks very much!

    • Uwhilpe 2016-11-01

      Great article!

    • Peter V 2016-11-19

      I can’t see where is the rotary encoder plugged in. Can you please add this info to the article?

      • tim yb 2016-11-22

        Hi Peter,

        The photo shows how the rotary encoder is attached to the motor. The details of the mechanical interface would depend on the particular encoder and motor being used.

    • shahgs 2016-12-04

      Very informative.
      One question, can you please provide the link where can I buy motor with encoder. Tried few things on internet, but could not find (some are available, but they are back ordered and would take time. so if I get proper link or product number, I order proper item. Thanks