Simple 2-way Motor Control for the Arduino

361,585

128

67

Intro: Simple 2-way Motor Control for the Arduino

WARNING: This is not a safe way to control a motor. Each I/O pin can only handle 40 mA of current. I would recommend using a H-Bridge or something along those lines.

When I say simple, I don't mean use a speed control. But control the motor directly without any external circuitry. Here's how:


How this came about:

I recently was working on an instructable about charlieplexing with an arduino. And I was wondering if the same principle would work with motors to some extent. So I came up with the idea that if you used a motor instead of an led you could have 2-way control of it & if you used 2 PWM (Pulse Width Modulation) ports you could have 2-way variable speed control for a motor with no external hardware!! So I decided to post my findings. Have fun!If you have any questions Please ask them.

I am not responsible for any thing bad that happens to you or to your arduino!

Step 1: The Stuff You Need:

Parts:
- Arduino
- Small DC motor
- Wire for motor

Tools:
- Computer with Arduino IDE installed
- A-B USB cord

Step 2: Connecting the Motor

Connect your DC motor to your arduino.

- Connect one wire from the motor to pin 5 on you arduino
- Connect the other wire from your motor to pin 6 on your arduino

The hardware setup for this is pretty simple.

Step 3: Coding the Arduino

Here's some basic steps to program your arduino.

1) Download the source code from below
2) Open the file in the Arduino IDE
3) Press the "Upload to I/O Board" button
4) Once the program is uploaded it will start running


I tried to add a good amount of comments to the code, but if you have any questions, please ask them.

Step 4: How This Works & More Ideas

How This Works:
When you make one pin HIGH & another LOW, the pin that is HIGH has a positive voltage & the pin that is LOW works like a ground(-).

Note: The rotation direction maybe different than below.

Pin 5:Pin 6:Rotates: HIGH LOW CCW LOW HIGH CW PWM: 127 LOW CCW 1/2 Speed
CCW = Counter Clockwise
CW = Clockwise

Ideas:
Add a second motor. Sorry I don't have any code for this, But there is a schematic posted below.

Share

    Recommendations

    • Audio Contest 2018

      Audio Contest 2018
    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Optics Contest

      Optics Contest

    67 Discussions

    0
    None
    JosuaR1

    1 year ago

    This is not a good way of controlling motors. First, a simple way to do this is to use an NPN transmitter, however you don't get direction control, however you do get speed control if you also use a capacitor. If you also want direction control, I recomend using a motor driver such as the L293D since it is also quite easy to use and can control 2 motors at a time.

    0
    None
    Sriram.1

    1 year ago

    pls send us the code

    0
    None
    SebastiánV17

    2 years ago

    WTF!!!!!!

    Inductive loads like relays or motors NEVER should be connected directly to arduino. For inductive loads you need a transistor and a flywheel diode.

    0
    None
    matthiasl2

    3 years ago

    BEGINNERS READ: Do NOT try this out, there is no transistor, no protecting diode in this circuit. Your chance to ruin your MCU is very high with this. Author: Please delete this pages.

    1 reply
    0
    None
    Bidoofmatthiasl2

    Reply 2 years ago

    Is there any way to do this without ruining the MCU?

    0
    None
    rajesh_ghosh

    2 years ago

    The warning you have put should be more bold and bright. This shall surely fry the MCU in long run. Better would be to remove the article or post a better one with safety precautions to the MCU.

    0
    None
    ggv2

    3 years ago on Introduction

    This is not a stepper motor, right? Can Darligton pair be used instead of H-bridge?

    0
    None
    fritte89

    3 years ago on Introduction

    Okay, first off, I'm really not trying to be mean or anything. That said, could you please take this instructable down for the sake of very beginners? I believe it would be really frustrating and for some people even spirit crushing to fry their first arduino. This is a really bad idea in so many ways.The ultimate hardware killer.

    It would be very kind if you would either take it down or change it a little bit and make it better :) (This will help: http://embedded-lab.com/blog/wp-content/uploads/20...

    0
    None
    StevenV2

    3 years ago on Introduction

    Saying that this is not the safest way to control a motor is putting it mildly. I would recommend NOT to do this. The 40 mA is a story in itself (in short: it's less!), but even smaller motors will ultimately damage the I/O pins. Even if it seems to work perfectly. If you don't want to damage your Arduino, use an H-bridge.

    0
    None
    dsilva33danimalik

    Reply 4 years ago on Introduction

    The motor is not running because the motor is using to much current from the arduino. You can damage your arduino because the pins can short out. I don't recomend this method for you.

    0
    None
    kpnd.dangdsilva33

    Reply 4 years ago on Introduction

    the out current is about 50mA, it is not enough to load the motor. I have a tips that you can use 2 pin mode as an output, and those connect to one pole of the motor, so it can double the current :D

    0
    None
    StevenV2kpnd.dang

    Reply 3 years ago on Introduction

    @kpnd.dang -

    NEVER do this when you're using digitalWrite and esp. analogWrite to
    control the pins! Between two Writes the connected pins are at a different level
    and the pins are short-circuited.

    0
    None
    FrancescoP3kpnd.dang

    Reply 3 years ago on Introduction

    yes maybe it works but I would recommend to drive it using transistors and protection diodes, because even if you double the current for the motor, when the motor stop it will generate too much current in reverse on your IC(whatever is) pin, frying it =D

    check this schem =D

    http://www.thebox.myzen.co.uk/Workshop/Motors_1_files/shapeimage_2.png

    Hi computergeek,

    could you explain what the rotateright/rotateleft values indicate or used for (are those timers or voltages ?)

    0
    None
    JishnuM

    3 years ago on Introduction

    the code saves as a tmp file on my computer for some reason how do i fix it or can i just get a picture of the code

    1 reply
    0
    None

    Just go to the property of the file and make one necessary change................................i.e., change the extension from .tmp to .pde and open it in IDE........................it worked for me in the same way!!!!!!!!!!

    I tried and it worked for me............................I used Arduino UNO and a DC Motor...........................

    0
    None
    MaxA3

    3 years ago on Introduction

    Hi,

    Is there an one way version of this code?

    Btw this two way one didnt work. for some reason my motor wouldnt run eventhough it is a 3V 0,13A motor and I know it works fine when I attach it to my ground and 5v pins.