Controlling Motor Using Arduino

Introduction: Controlling Motor Using Arduino

Controlling motors using Arduino can be tricky.

Firstly, motors require more current than what Arduino's output pins can supply. Also motors can generate their own current through a process called induction, which can damage you circuit, unless you don't plan for it.

However, motors make it possible to move physical things, making your projects more existing, so they are worth the complications!

Motor can also be controlled using a H-Bridge but for this tutorial, I'll be using Transistors and Diodes.

Things required -

  1. MOSFET Transistor (or similar). Purchase link -
  2. 10 Kilo Ohm Resistor. Purchase link -
  3. Diode 1N4007 (or similar). Purchase link -
  4. Motor. Purchase link -
  5. 9V Battery. Purchase link -
  6. Switch. Purchase link -
  7. Battery Snap. Purchase link -
  8. Arduino board (Obviously). Purchase link -

Obviously, you can skip the Switch if you don't want the motor to start and stop on press of button. But be sure to make elaborate arrangements on the circuit if you are skipping that.

Step 1: Theory Part

Moving things take a lot of energy. Motors typically require more current than what Arduino can provide. To start moving when it has a heavy load, the motor will draw as much current as it can. The Arduino boards can only prove 40 milliamps (mA) from its digital pins, which is much less than what most motors require to work.

Thus to make this motor work, we will be drawing power from a external power source, ie from a 9V battery. Input of current from the 9V battery will be guarded by the Arduino.

Transistors are components that allow you to control high current and high voltage power sources from the low current output of the Arduino. There are many different kinds, but they work on the same principle. You can think of transistors as digital switches. When you provide voltage to one of the transistor's pins, called the gate, it closes the circuit between the other two pins, called the source and drain. This way, you can turn higher current/voltage motor on and off with your Arduino.

Polarized 1N4007 Diode is a special component used to make the current flow only in one direction. If a motor is manually rotated backwards, it generates electricity, which can damage the circuit. Thus using this diode, we can solve the problem.

Step 2: The Circuit and Programming Part

Make the circuit look exactly the same as given in the image above. Make sure that the metal plate of transistor is towards the right. Also make sure that the end of Diode 1N4007 with a strip is towards the right.

Do not attempt this experiment if you are unclear with the circuit. Leave a comment for clarification. Incorrect circuit can damage your board.

The code -

  1. const int switchPin = 2; // Pin to which your switch is connected

  2. const int motorPin = 9; // Pin to which your motor is connected

  3. int switchState = 0;

  4. // Remember that 0 = LOW and 1 = HIGH. If switchState is 0, then

  5. // it means that the button is pressed else, if it is 1, then it means
  6. // that the button is pressed
  7. void setup() {

  8. pinMode(motorPin, OUTPUT);

  9. // Telling Arduino board to only give output to motor. It means no input will be taken.

  10. pinMode(switchPin, INPUT);

  11. // Telling Arduino to only take input from the switch

  12. }

  13. void loop() {

  14. switchState = digitalRead(switchPin);

  15. // Get the state of the switch, ie, find out if the switch is pressed or not

  16. if(switchState == HIGH){

  17. digitalWrite(motorPin, HIGH);

  18. // Motor will run if the switch is pressed

  19. } else {

  20. digitalWrite(motorPin, LOW);

  21. //Motor will stop if the switch is not pressed
  22. }

  23. }

Comments should explain most of the code but if you are still unclear about something, then kindly leave a comment below for clarification.

Be sure to change the switchPin and motorPin's address if you have placed it on some other pin on Arduino board.

I am using Arduino UNO R3 but this code should run on probably all the Arduino boards.

You can also download the code and upload it as sketch immediately.

Be the First to Share


    • Knitting and Crochet Speed Challenge

      Knitting and Crochet Speed Challenge
    • Anything Goes Contest 2021

      Anything Goes Contest 2021
    • Raspberry Pi Contest

      Raspberry Pi Contest



    4 years ago

    I have used IRF9540N mosfet but it is not working?!

    What would the circuitry look like if you did not want to use a switch and just control it using the program? Please Reply

    That's so cool! You'll have to show us some of the uses you found for your arduino controlled motor!