element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Dc motor problem
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 17 replies
  • Answers 1 answer
  • Subscribers 418 subscribers
  • Views 1892 views
  • Users 0 members are here
Related

Dc motor problem

e14 Contributor
e14 Contributor over 12 years ago

Hi all just started with programming an Arduino UNO - I have no previous programming background!!

 

I started with Evil Genius book and I want to make a table feed for my milling machine, but right now im just experimenting with dc motor control.

 

What I have set up is a H Bridge (HMD-02)  2 buttons and 2 potentiometers and 1 dc motor

What i want it to do is 1 Nothing until I press 1 button then Turn forward at speed set by pot 1  , stop when i let go of button and turn backward at speed set by pot 2 when I press button 2???

 

MY sketch starts the motor running as soon as I compile and I cant find the problem??????

 

Please any help??

Many thanks.

Attachments:
dc_motor1.ino.zip
  • Sign in to reply
  • Cancel
Parents
  • e14 Contributor
    0 e14 Contributor over 12 years ago

    It would be helpful if you posted your Arduino sketch.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Hi I tried to attach it as a file!! Copy and paste did not work!

    What is the best way??

     

    Gerhard

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    /*

    * dc moto with h-bridge and 2 potentiometers and 2 buttons

    * 1 button for forward at speed set by pot 1

    * 2 button for bacward at speed set by pot 2

    */



    int pot1 = 0;          // potentiometer to pin A0

    int pot2 = 1;          // potentiometer to pin A1

    int enable = 11;       // to enable on h bridge

    int motorPin1 = 9;     // to In1 on h bridge

    int motorPin2 = 10;    // to In2 on h bride

    int redBut = 12;       // button to 12 pull-down

    int greenBut =13;      // button to 13 pull-down





    void setup()                    // run once, when the sketch starts

    {

      Serial.begin(9600);           // set up Serial library at 9600 bps

      pinMode(pot1, INPUT);       

      pinMode(pot2,INPUT);

      pinMode(enable, OUTPUT);

      pinMode(motorPin1, OUTPUT);

      pinMode(motorPin2, OUTPUT);

      pinMode(greenBut , INPUT);

      pinMode(redBut , INPUT);

      digitalWrite (motorPin1 , LOW);

      digitalWrite (motorPin2 ,LOW);

      digitalWrite(enable ,LOW);

    }



    int getPot1() {

      int v;

      v = analogRead(pot1);

      v /= 4;

      v = max(v, 90);

      v = min(v, 255);

      return v;

    }





    int getPot2() {

      int w;

      w = analogRead(pot2);

      w /= 4;

      w = max(w, 90);

      w = min(w, 255);

      return w;

    }



    int motorFoward() {

      analogWrite(motorPin1, getPot1());

    digitalWrite(enable,HIGH);

    digitalWrite(motorPin2,LOW);

    }



    int motorBackward() {

      analogWrite(motorPin2, getPot2());

      digitalWrite(enable,HIGH);

      digitalWrite(motorPin1,LOW);

    }



    int allStop(){

      digitalWrite (motorPin1 , LOW);

      digitalWrite(motorPin2 , LOW);

      digitalWrite(enable ,LOW);

    }





    void loop()                     // run over and over again

    {

      if (greenBut = HIGH){

      motorFoward();}


      else if (redBut = HIGH){

      motorBackward();}


      else{

      allStop();}

    }





    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    /*

    * dc moto with h-bridge and 2 potentiometers and 2 buttons

    * 1 button for forward at speed set by pot 1

    * 2 button for bacward at speed set by pot 2

    */



    int pot1 = 0;          // potentiometer to pin A0

    int pot2 = 1;          // potentiometer to pin A1

    int enable = 11;       // to enable on h bridge

    int motorPin1 = 9;     // to In1 on h bridge

    int motorPin2 = 10;    // to In2 on h bride

    int redBut = 12;       // button to 12 pull-down

    int greenBut =13;      // button to 13 pull-down





    void setup()                    // run once, when the sketch starts

    {

      Serial.begin(9600);           // set up Serial library at 9600 bps

      pinMode(pot1, INPUT);       

      pinMode(pot2,INPUT);

      pinMode(enable, OUTPUT);

      pinMode(motorPin1, OUTPUT);

      pinMode(motorPin2, OUTPUT);

      pinMode(greenBut , INPUT);

      pinMode(redBut , INPUT);

      digitalWrite (motorPin1 , LOW);

      digitalWrite (motorPin2 ,LOW);

      digitalWrite(enable ,LOW);

    }



    int getPot1() {

      int v;

      v = analogRead(pot1);

      v /= 4;

      v = max(v, 90);

      v = min(v, 255);

      return v;

    }





    int getPot2() {

      int w;

      w = analogRead(pot2);

      w /= 4;

      w = max(w, 90);

      w = min(w, 255);

      return w;

    }



    int motorFoward() {

      analogWrite(motorPin1, getPot1());

    digitalWrite(enable,HIGH);

    digitalWrite(motorPin2,LOW);

    }



    int motorBackward() {

      analogWrite(motorPin2, getPot2());

      digitalWrite(enable,HIGH);

      digitalWrite(motorPin1,LOW);

    }



    int allStop(){

      digitalWrite (motorPin1 , LOW);

      digitalWrite(motorPin2 , LOW);

      digitalWrite(enable ,LOW);

    }





    void loop()                     // run over and over again

    {

      if (greenBut = HIGH){

      motorFoward();}


      else if (redBut = HIGH){

      motorBackward();}


      else{

      allStop();}

    }





    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Don't have time to look through it at the moment, but to post it just add <code>....</code> within the html editor.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vsluiter
    0 vsluiter over 12 years ago in reply to e14 Contributor

    First: also write to 'Enable' in the setup. You've now only defined it, please make sure you also write a high or low, depending on your driver.

     

    Second:

    if (greenBut = HIGH){ Should be:  

    if (greenBut == HIGH){, so with two 'is equal' signs after each other. Same for the red button. With one '=' it's an assignment, with two ('==') it's a comparison.

    There might be more issues, but this will give you a start.

     

    Another small thing: The max of AnalogRead is 1023. Dividing that by four gives a max of 255. So your statement v=min(v,255) is superfluous.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to vsluiter

    Hi Thanks Victor

     

    1 Sorry I dont understand that part please explain a bit more

     

    2 That I will change and then test again.  PS this worked in that nothing happens at startup - but now the motor does not turn at all - even when buttons are pressed.????

     

    3 I may need to put a limit on the motor speed later so I put the code in and set to the max - then it should be easy to change later.  I have a LOT to learn still.

     

     

    Thanks Gerhard

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vsluiter
    0 vsluiter over 12 years ago in reply to e14 Contributor

    Hi Gerhard,

     

    1) You've now just set enable as output, you haven't said which level it should have. It is now either high or low. Maybe it doesn't matter in your program, but it's good practice to tell what level an output has before going into the rest of your program

     

    2) How have you wired your switches? If you put one leg to VCC, and the other to the input, your program should work, I guess. If you've connected one leg to GND and the other to the input pin, you should make the pin pullup (use internal pull up resistor):


      pinMode(greenBut , INPUT_PULLUP);

       pinMode(redBut , INPUT_PULLUP);

     

    In that second case (one leg to GND), you have to check whether a pin is LOW when it is pressed (if (greenBut == LOW)  )

     

    3) Ah, OK image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to vsluiter

    Hi Victor

     

    Will go and read up on the levels in the setup - I have just got that it is an output and to make it LOW so it doesn't start the motor before the button is pushed.  I need a programmers manual!!!

     

    The buttons are connected one to 5V and one leg to pin with a 10K resistor to ground.

     

    I think I am missing something else as I cant get the motor to turn at all now!!!!

     

    Is there any books just on programming Arduino C??????

     

    Gerhard

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vsluiter
    0 vsluiter over 12 years ago in reply to e14 Contributor

    Hey Gerhard,

     

    I can't see the error right now. Your code was right for the switches you have. You could try turning on&off the on-board LED to see whether you're getting in a part of the code at all (for example in 'Move forward').

     

    Another suggestion would be to add a "delay(100)" at the end or beginning of your loop so you go through all actions at a human-comprehensible rate. Maybe you're just writing the PWM too fast, mbed (other dev kit) has this problem, I'm not sure about Arduino.

     

    Regarding 'Arduino C': there's no Arduino C. It's just C++, but with a few handy functions to handle hardware. I'm afraid I can't guide you here, I know Drew Fustini wrote a book, and there's a plethora of online documentation...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Changed If (greenBut == HIGH);

    to If (digitalRead(greenBut) ==HIGH);

    Now it works

    Thanks for help and sugestions

     

    Gerhard

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • vsluiter
    0 vsluiter over 12 years ago in reply to e14 Contributor

    Hello Gerhard,

     

    Thanks for the feedback. It was so obvious we all overlooked image you have to call the DigitalRead on a pin to get the value.

     

    Greetings,

    Victor

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to vsluiter

    you have to call the DigitalRead on a pin to get the value.

    and unfortunately, there is no type checking to alert you to such a common mistake.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    yes small things easy to miss.

     

    This is why I plan to figure the code in small pieces then add the pieces together.

    1motor controll then add the LCD screen and then set limit switches then figure out how to have different modes!

     

    In a couple of YEARS I'll have a table feed for my mill.!

     

    Gerhard

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube