Go Down

Topic: Control the direction of DC gear motor (Read 4558 times) previous topic - next topic

acapwans

Hello,

I have 12v DC gear motor and how can I control the direction of the motor by using keyboard? I want to know whether it is possible to change the direction of motor rotation. Btw I'm trying to build pan and tilt camera platform

MarkT

You have two sub problems:

Using the keyboard to control something (anything).
Driving a motor bi-directionally.

The second requires a suitable H-bridge driver for your motor - what are its
specifications?

The keyboard part depends on what you mean by keyboard (keypad on Arduino,
keyboard of computer attached to Arduino, ?)
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

acapwans

I'm using 12v, 100rpm dc gear motor and motor driver from dfrobot http://www.dfrobot.com/index.php?route=product/product&product_id=69

The main concern is that is it possible to control dc motor such as servo motor,
Btw, I'm new to this programming and not so good with electronic things

MarkT

You haven't said enough(*) about the motor - what is its stall current? If its more than
2A think again, the driver won't handle that.

* You motor could be any size/power given the information provided...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

acapwans

By using the code from dfrobot, I managed to rotates both motor using the driver. It seems okey to me.  For the next part, I want to know how to turn the motor into bidirectional.

And after some readings, maybe I will use thumbstick to control the motor

acapwans

After some further research I realized that my driver cannot support 2 bidirectional dc motor, so I assume I have to find another driver right.

JimboZA

#6
Apr 14, 2015, 10:56 am Last Edit: Apr 14, 2015, 11:06 am by JimboZA
After some further research I realized that my driver cannot support 2 bidirectional dc motor, so I assume I have to find another driver right.
According to the link, it's a 298 which is a chip that certainly can support two motors birectionally.

edit.... but I haven't looked to see if the shield makes that functionality available so you may be right. Would be a waste if they didn't.....
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

MorganS

From the schematic, it looks like it's capable of driving 2 motors in either direction simultaneously. There's two LEDs near each motor output, which will show you which direction it's driving.
"The problem is in the code you didn't post."

acapwans

Well that's a good news. Maybe I have to upload simple skecth to know if the motor can rotate both direction

acapwans

okay, so I managed to control the both DC motor in CW and CCW according to the keyboard input. As reference to anyone, this is the my sketch
Code: [Select]
//Motor A - Pin definitions
#define E1  5      //Speed control
#define M1  4         //Direction


//Motor B - Pin definitions
#define E2  6      //Speed control
#define M2  7         //Direction


// Alias for move function
#define MOTOR_A 1
#define MOTOR_B 2
#define CW   0         //Clockwise
#define CCW  1       //Counter-clockwise

byte command;        //Load serial command

void setup()
{
  pinMode(E1, OUTPUT);
  pinMode(M1, OUTPUT);
 

  pinMode(E2, OUTPUT);
  pinMode(M2, OUTPUT);
 
 
  Serial.begin(9600); //Open serial at 9600bps

}

void loop()
{
  if (Serial.available() > 0)
    {
    command = Serial.read();                          //Read serial
    switch(command)                                       //Select command
      {
      case '8':
      case 'w':
      case 'W':
        move(MOTOR_A, 255, CCW);              //Motor A, full speed, CCW
       
        break;

      case '4':
      case 'a':
      case 'A':
         move(MOTOR_B, 255, CW);                //Motor B, full speed, CW
        break;

      case '2':
      case 's':
      case 'S':
        move(MOTOR_A, 255, CW);                //Motor A, full speed, CW
        break;

      case '6':
      case 'd':
      case 'D':
        move(MOTOR_B, 255, CCW);             //Motor B, full speed, CCW
        break;
     
      case '5':
      case 'p':
      case 'P':
        move(MOTOR_A, 0, CW);                    //Motor A, full speed, CW
        move(MOTOR_B, 0, CCW);                 //Motor B, full speed, CCW
        break;
      }
    command = Serial.read();                          //To clear Serial Buffer
    command = 0;
    }
}

void move(int motor, int speed, int direction){
//Move the chosen motor at the chosen speed and direction
//speed: 0 is off, and 255 is full speed

  boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

  if(direction == 1)
  {
    inPin1 = HIGH;
    inPin2 = LOW;
  }

  if(motor == 1)
  {
    digitalWrite(M1, inPin1);
    analogWrite(E1, speed);
  }
  else
  {
    digitalWrite(M2, inPin1);
    analogWrite(E2, speed);
  }
}


the original skecth is from http://garagelab.com/profiles/blogs/wasd-controlled-car-commands-by-serial, then I have to modified it according to my motor shield and desired.

Go Up