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)


    1.  Gather all parts required:


    Close-up picture showing the motor encoder pins

    Arduino Mega 2560

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

    Green Connector
    Green Connector




    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()
    void loop()
    // to print encoder value on the screen Serial.print("Encoder Value="); Serial.println(encoderValue);
    void count()

      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()
    void loop()
    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);
    void count()

      Download Code  


    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.


    Give this project a try for yourself! Get the BOM.


    Learn More About:




    • 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

    • rudigunawan 2017-04-26

      very interesting. but I tried to make the motor clockwise and counter clockwise, but the count keep going up, not going down, can you help me with this problem?

      • RK37 2017-04-27

        Hi rudigunawan, what exactly did you do to try to reverse the direction of the motor’s rotation?

    • This example will definitely not work.  It’s missing the entire section to calculate from a quadrature encoder.  This is a FALSE FAKE PROJECT.

      • RK37 2017-08-05

        The original author of this article is not available, so it’s difficult to provide a solid answer here. However, the article never says anything about a quadrature encoder. It says that the project uses an incremental encoder, which apparently just outputs pulses as the motor is rotating. As far as I can tell the code sets up an interrupt attached to the encoder output, and each time the encoder sends a pulse the encoder value is incremented.

    • Irfan Jaffar 2017-10-05

      hello i want to ask about channel A and B. for pin B we will connect to which port in arduino?

    • Very informative. Can i get the H-bridge schematic and PCB layout ?

      • RK37 2017-12-07

        Unfortunately, we are no longer in contact with the original author of this article, so we’re limited to whatever information is currently in the article.

    • Pietro63 2017-11-08

      Good morning, where i can find schematic and PCB layout (or only schematic) of this H bridge (PWM and DIR) ???? Pls help me… !! Thanks !!

    • Najib Alshaibani 2018-03-18

      Great information smile
      I have Gear box motor with encoder that consist of 8 wires
      2 Blue
      2 green
      2 yellow
      1 Red
      1 black
      I want to connect them with arduion , but i don’t know how to connect them , is there anyone to help who to connect them
      Thank you so much >>

    • sohaibchoudhary 2018-06-07

      excellent work
      can you send me the schematic of H-bridge.i really need this.plz

    • Jake S 2018-09-28

      I know it’s a while since they asked but for all those below asking about the H-Bridge, it was stated what to look for in the Bill Of Materials, search for L298 driver module.