Pages: [1]   Go Down
Author Topic: Do not know how to run forward the dc motors.  (Read 1375 times)
0 Members and 1 Guest are viewing this topic.
universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am buying the motors shield this: http://www.dfrobot.com/wiki/index.php?title=Arduino_Motor_Shield_(L298N)_(SKU:DRI0009)


and plugged in to arduino uno, to control dc motors like in this robot kit: http://www.famosastudio.com/robotics/2wd-miniq-robot-kit-plus



I am using PWM mode.
And the programming code following from dfrobot:
Code:
//Arduino PWM Speed Control:
int E1 = 5; 
int M1 = 4;
int E2 = 6;                     
int M2 = 7;                       
 
void setup()
{
    pinMode(M1, OUTPUT);   
    pinMode(M2, OUTPUT);
}
 
void loop()
{
  int value;
  for(value = 0 ; value <= 255; value+=5)
  {
    digitalWrite(M1,HIGH);   
    digitalWrite(M2, HIGH);       
    analogWrite(E1, value);   //PWM Speed Control
    analogWrite(E2, value);   //PWM Speed Control
    delay(30);
  } 
}

the motor works rotating backward with this code, but how to run forward?
Please help! I heard this shield can have bidirectional rotation  smiley-mr-green
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you read the dfrobot page you attempted to link to? There is a table about half way down that shows what the pins are used for in each mode. Some pins are described as direction pins. Setting a direction pin (M1/M2 in PWM mode) HIGH sets that motor to backward.
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you read the dfrobot page you attempted to link to? There is a table about half way down that shows what the pins are used for in each mode. Some pins are described as direction pins. Setting a direction pin (M1/M2 in PWM mode) HIGH sets that motor to backward.

Oh yeah I read that mode.
But how to sets the motor to forward?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But how to sets the motor to forward?

Really? It's a direction pin. It can be set HIGH or LOW. HIGH sets the motor direction to backwards. Try the alternative option.
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When I set to LOW, the motor is disabled, not moving forward.
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the two motors moving forward with this code:
Code:
int value;
  for(value = 0 ; value <= 255; value+=5)
  {
    digitalWrite(M1,LOW);   
    digitalWrite(M2,LOW);       
    analogWrite(E1, value);   //PWM Speed Control
    analogWrite(E2, value);   //PWM Speed Control
    delay(30);

but when I change the code with

Code:
int value;
  for(value = 0 ; value <= 255; value+=5)
  {
    digitalWrite(M1,LOW);   
    digitalWrite(M2, HIGH);       
    analogWrite(E1, value);   //PWM Speed Control
    analogWrite(E2, value);   //PWM Speed Control
    delay(30);
in order to move one motor backward and the other motor forward
it can not work my setting M1 LOW and M2 HIGH, the motor stay still, what happen?
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Won't get much help from the page you listed... Can you give us a link to a page that actually has the information about the shield?
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the best page for arduino motor shield, let me know if there ia more necessary:
 http://www.dfrobot.com/wiki/index.php/Arduino_Motor_Shield_(L298N)_(SKU:DRI0009)

 http://www.shieldlist.org/dfrobot/2a-motor
« Last Edit: October 31, 2012, 09:54:50 am by creativen » Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You left the closing parenthesis out of the link again.

http://www.dfrobot.com/wiki/index.php/Arduino_Motor_Shield_%28L298N%29_%28SKU:DRI0009%29
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But according to this http://www.e-shore.com.my/homepage/all-projects/193-ir-remote-control-mobile-robot.html
it should be able to set the M1 or M2 to low, but it doesn't work if I set one or both of them.

What is wrong here?
Is it maybe my motor shield error/broken?
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay - do you have the motor shield set for PLL or PWM? The function of the pins changes depending on how you have the board configured.

It would appear that the jumper by the double screw terminal block needs to be set properly.

In PWM mode pin 4 is motor 2 direction and Pin 5 is motor 2 PWM speed control, Pin 7 is Motor 1 Direction and pin 6 is Motor 2 PWM speed control.

Also - Yu don't give a complee listing - do you have you output pins defined properly?

Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay - do you have the motor shield set for PLL or PWM? The function of the pins changes depending on how you have the board configured.

It would appear that the jumper by the double screw terminal block needs to be set properly.

In PWM mode pin 4 is motor 2 direction and Pin 5 is motor 2 PWM speed control, Pin 7 is Motor 1 Direction and pin 6 is Motor 2 PWM speed control.

Also - Yu don't give a complee listing - do you have you output pins defined properly?



The motor shield set to PWM. well I have made sure the jumper at the PWM mode.
I would like to correct your statement to:
In PWM mode pin 4 is motor 2 direction and Pin 5 is motor 2 PWM speed control, Pin 7 is Motor 1 Direction and pin 6 is Motor 1 PWM speed control.
Well, I have define the output pins properly like this:
Code:
//=======================================================================
//  2 wheels Robot Platform Template
//  www.e-shore.com.my
//=======================================================================
 
//motor control pin
const int E1=5;    //motor 1 (right) enable pin
const int M1=4;    //motor 1 (right) direction pin
const int E2=6;    //motor 2 (left) enable pin
const int M2=7;    //motor 2 (left) direction pin
 
//  setup function
//=======================================================================
void setup()
{
  //configure all motor control pin as output
  pinMode(E1,OUTPUT);
  pinMode(M1,OUTPUT);
  pinMode(E2,OUTPUT);
  pinMode(M2,OUTPUT);
 
  //disable both motor by default
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
 
}
 
//  loop function
//=======================================================================
void loop()
{
  //right wheel forward at 70 PWM speed
  digitalWrite(M1,LOW);
  analogWrite(E1,40);
 
  //left wheel forward at 70 PWM speed
  digitalWrite(M2,HIGH);
  analogWrite(E2,40);
 
  //delay for 300ms
  delay(300);
 
  //right wheel stop
  digitalWrite(E1,LOW);
 
  //left wheel stop
  digitalWrite(E2,LOW);
 
  //program halt
  while(1); 
}
I got the code from http://www.e-shore.com.my/homepage/all-projects/134-2-wheels-robot-platform-setup.html
I also try this code, but can not work:
Code:
//=======================================================================
//  IR Remote Control Mobile Robot
//  www.e-shore.com.my
//=======================================================================
 
#include <IRremote.h>
#include <IRremoteInt.h>
 
//motor control pin
const int E1=5;    //motor 1 (right) enable pin
const int M1=4;    //motor 1 (right) direction pin
const int E2=6;    //motor 2 (left) enable pin
const int M2=7;    //motor 2 (left) direction pin
 
//IR Receiver Module Pin and variable
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
 
//  setup function
//=======================================================================
void setup()
{
//configure all motor control pin as output
pinMode(E1,OUTPUT);
pinMode(M1,OUTPUT);
pinMode(E2,OUTPUT);
pinMode(M2,OUTPUT);
 
//disable both motor by default
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
 
//start IR receiver
irrecv.enableIRIn();
}
 
//  loop function
//=======================================================================
void loop()
{
//IR signal received
if(irrecv.decode(&results))
{
//forward (VOL+)
if(results.value==0xFD807F)
{
motor(40,40);
}
//reverse (VOL-)
else if(results.value==0xFD906F)
{
motor(-40,-40);
}
//rotate left (PREVIOUS)
else if(results.value==0xFD20DF)
{
motor(-30,30);
}
//rotate right (NEXT)
else if(results.value==0xFD609F)
{
motor(30,-30);
}
//forward left (POWER)
else if(results.value==0xFD00FF)
{
motor(10,40);
}
//forward right (FUNC/STOP)
else if(results.value==0xFD40BF)
{
motor(40,10);
}
//reverse left (DOWN)
else if(results.value==0xFD10EF)
{
motor(-10,-40);
}
//reverse right (UP)
else if(results.value==0xFD50AF)
{
motor(-40,-10);
}
 
//receive the next value
irrecv.resume();
 
//short delay waiting for repeating IR signal
// (prevent it to stop if no signal received)
delay(150);
}
//no IR signal received
else
{
//right wheel stop
digitalWrite(E1,LOW);
 
//left wheel stop
digitalWrite(E2,LOW);
}
}
 
//  extra function
//=======================================================================
 
//function to control the motor
void motor(int left, int right)
{
//limit the max speed
if(left>255)left=255;
else if(left<-255)left=-255;
if(right>255)right=255;
else if(right<-255)right=-255;
 
//left wheel forward
if(left>0)
{
//left wheel direction forward
digitalWrite(M2,HIGH);
//left wheel speed
analogWrite(E2,left);
}
//left wheel reverse
else if(left<0)
{
//left wheel direction reverse
digitalWrite(M2,LOW);
//left wheel speed
analogWrite(E2,-left);
}
//left wheel stop
else
{
//left wheel stop
digitalWrite(E2,LOW);
}
 
//right wheel forward
if(right>0)
{
//right wheel direction forward
digitalWrite(M1,LOW);
analogWrite(E1,right);
}
//right wheel reverse
else if(right<0)
{
//right wheel direction reverse
digitalWrite(M1,HIGH);
analogWrite(E1,-right);
}
//right wheel stop
else
{
//right wheel stop
digitalWrite(E1,LOW);
}
}

I think there must be problem with LOW mode of M1 or/and M2.

What do you suggest?
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

any help?  smiley-eek-blue
Logged

Pages: [1]   Go Up
Jump to: