Strange situation with motor shield on Uno

The Motor section of the forum says “moving big things” and “using high currents” so i’m posting here because it’s just a basic setup of an Uno and motor shield (from DFRobot)

It’s possible however it is a hardware situation because the code seems fine, although i might be missing something that is affecting the actual running of the program.

The setup is as the usual 2WD vehicle as “tricycle” (3rd small wheel for stability).

Turning would be pivoting around the middle of both the (independent) axles by running one motor forward and the other in reverse.

I am using a WiiChuck as the controller reading the joystick axes (axises?), ‘joy-Y’ for FWD/REV and ‘joy-X’ for LEFT/RIGHT.

What is baffling me is that running the motors on one axis is fine, but on the other, it hardly budges - the motor is in effect on a very low ratio gearing which means it really only moves when being driven at top speed (ie. max PWM).

On the shield, the LED lights for each of the pins, M1-enable, M1-direction, M2-enable & M2-direction indicate the same - brightly lit when the vehicle does move (in one axis), and dimly lit when it doesn’t move (or is straining).

If you confirm that the code below is fine, what could be happening on the shield that makes one axis work, and the other not.

I have even disabled the setting of the PWM speed to the joystick position and just declared it as one value; instead of (pseudo-code) PWMspeed = [i]analog[/i]read(joystick_position); it’s simply PWMspeed = fixed_var;, that fixed variable (constant, i suppose !) being 250.

I even flipped the polarity on one motor but it only flipped the axes and it still failed on the other one.

Would appreciate any advice on what steps to take for trouble-shooting this situation.

The Code:

// This motor shield use Pin 6,5,7,4 to control the motor
// Simply connect your motors to M1+,M1-,M2+,M2-
// Through serial monitor, type 'a','s', 'w','d','x' 
//                           to control the motor
// www.dfrobot.com  // Last modified on 24/12/2009
/*
 Digital 4	Motor 1 Direction control
 Digital 5	Motor 1 PWM control      
 Digital 6	Motor 2 PWM control      
 Digital 7	Motor 2 Direction control
 */
byte EN1 = 6; 
byte EN2 = 5; 
byte IN1 = 7;
byte IN2 = 4;  

byte maxPWMspd = 250;
#include "motorStuff.h"

const int ledPin = 3;

#include <Wire.h>
#include "nunchuck_funcs.h"
int loop_cnt=0;
byte accx,accy,accz,joyx,joyy;


void setup() {
  for(int i=4;i<=7;i++)  //For Arduino Motor Shield
    pinMode(i, OUTPUT);  //set pin 4,5,6,7 to output mode
  pinMode(ledPin, OUTPUT);

  nunchuck_setpowerpins();
  nunchuck_init(); // send the initilization handshake
}

void loop() {

    if( loop_cnt > 10 ) {   // ORIG.EG : every 100 msecs get new data
        loop_cnt = 0;
        nunchuck_get_data();
    //    accx = nunchuck_accelx();        accy = nunchuck_accely();        DON'T NEED THIS YET
        joyx = nunchuck_joyx();          joyy = nunchuck_joyy();
    }          // 134 - 141                     // 128 - 135              // middle position
    loop_cnt++;
    delay(1);
    
    if (joyy > 145)  // FWD
    {
      FWD(joyy);
    }
    else
    {
      if (joyy < 115) // REV
      {
        REV(255-joyy);
      }
      else STOP();
    }
    
    if (joyx > 150)  // RIGHT
    {
      RG(joyx);
    }
    else
    {  if (joyx < 120) // LEFT
      {
        LF(255-joyx);
      }
      else STOP();
    }

}

and the include for the “motorStuff.h”

void Motor1(int pwm, boolean reverse)
{ analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
  if(reverse)  {
    digitalWrite(IN1,HIGH);   
  } else {
    digitalWrite(IN1,LOW);   
  }
} 

void Motor2(int pwm, boolean reverse)
{ analogWrite(EN2,pwm);
  if(reverse)  {
    digitalWrite(IN2,LOW);  // flipped logic vs. Motor1 because physical motor "mounted backwards".
  } else  {
    digitalWrite(IN2,HIGH);   
  }
} 


void FWD(int pwmspeed)  {
        Motor1(pwmspeed,true);
        Motor2(pwmspeed,true);
}

void REV(int pwmspeed)  {
        Motor1(pwmspeed,false);
        Motor2(pwmspeed,false);
}

void LF(int pwmspeed)  {
        Motor1(pwmspeed,false);
        Motor2(pwmspeed,true);
}

void RG(int pwmspeed)  {
        Motor1(pwmspeed,true);
        Motor2(pwmspeed,false);
}

void STOP()  {
        Motor1(0,false);
        Motor2(0,false);
}

the WiiChuck code should be straight-forward - and irrelevant as to reading of the position for the speed - but for completion’s sake, i’ll add it here;

/*
 * This library is from the Bionic Arduino course : 
 * 2007-11 Tod E. Kurt, http://todbot.com/blog/
 *
 * The Wii Nunchuck reading code originally from Windmeadow Labs
 *   http://www.windmeadow.com/node/42
 */

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif


static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);
    delay(100);  // wait for things to stabilize        
}

// initialize the I2C system, join the I2C bus,
static void nunchuck_init()
{ 
    Wire.begin();                // join i2c bus as master
    Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
    Wire.write((uint8_t)0x40);// sends memory address
    Wire.write((uint8_t)0x00);// sends sent a zero.  
#else
    Wire.send((uint8_t)0x40);// sends memory address
    Wire.send((uint8_t)0x00);// sends sent a zero.  
#endif
    Wire.endTransmission();// stop transmitting
}

// Send a request for data to the nunchuck
// was "send_zero()"
static void nunchuck_send_request()
{
    Wire.beginTransmission(0x52);// transmit to device 0x52
#if (ARDUINO >= 100)
    Wire.write((uint8_t)0x00);// sends one byte
#else
    Wire.send((uint8_t)0x00);// sends one byte
#endif
    Wire.endTransmission();// stop transmitting
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
static char nunchuk_decode_byte (char x)
{
    x = (x ^ 0x17) + 0x17;
    return x;
}

// Receive data back from the nunchuck, 
static int nunchuck_get_data()
{
    int cnt=0;
    Wire.requestFrom (0x52, 6);// request data from nunchuck
    while (Wire.available ()) {
        // receive byte as an integer
#if (ARDUINO >= 100)
        nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.read() );
#else
        nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.receive() );
#endif
        cnt++;
    }
    nunchuck_send_request();  // send request for next data payload
    // If we recieved the 6 bytes, then go print them
    if (cnt >= 5) {
        return 1;   // success
    }
    return 0; //failure
}


// returns zbutton state: 1=pressed, 0=notpressed
static int nunchuck_zbutton()
{
    return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1;  // voodoo
}

// returns zbutton state: 1=pressed, 0=notpressed
static int nunchuck_cbutton()
{
    return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1;  // voodoo
}

// returns value of x-axis joystick
static int nunchuck_joyx()
{
    return nunchuck_buf[0]; 
}

// returns value of y-axis joystick
static int nunchuck_joyy()
{
    return nunchuck_buf[1];
}

There should be an example sketch, that comes with the library you are using for the shield.

Why don't you get your device working with the unmodified example sketch first and then slowly modify the example sketch according to your needs.

Hi, Can you post a link to your motor driver shield please?

As @boylesg has asked, have you looked at the examples section of the IDE and used oe of the supplied codes to check your shield?

Thanks..Tom.. :)

Hi, Where did you get the library? Tom... :o

Hi, How have you got your JP3 jumper configured? What is your motors power supply? What are your motors?

Tom... :)

Hi guys, that “motorStuff.h” basically is the example code from DFRobot.
it’s not really a library, just myself saving the code separately and then ‘including’ it.

this is the shield;
DFRobot Arduino Motor Shield (L293) 1A

and this is the motor (x 2) ;
fischertechnik mini-motor 9V, 0.65A

i have tried running one motor at a time and that works for each direction at “maxPWMspeed” - halfSpeed just isn’t enough to drive the motor - i can’t really tell from the datasheet what is the motor power supply coming from through the barrel jack (which is where my 9V battery is plugged in) - but it should be the 9V because it’s definitely more than the 5V via USB when uploading and brief-testing.

the “curiousity” is running two motors in one axis, ie. M1-clockwise(CW) and M2-counter-clockwise(CCW) or M1-CCW and M2-CW works.

but when it’s on the other axis, ie. M1-CW and M2-CW or M1-CCW and M2-CCW - neither work.

as i mentioned, when i tried flipping the wiring on one motor, it just means that M1-CW and M2-CW (or M1-CCW and M2-CCW) now works - but then the other axis fails !! :confused:

that’s what’s baffling.

the shield has no jumper - i think the 2A version has though.

Hi, Okay so the motor shield powers the motor fro the Vin input, which is the DC socket.

9V battery? ? Is it one of these? Then you are lucky it is running one motor. You need a supply that is 2A to run two of those motors.

If you have a DMM measure the Battery voltage when you try and run one motor then two.

It would be easier to put "motorStuff" as a function the code, rather than another bit of code to download at the moment.

Tom.... :)

TomGeorge: Hi, Okay so the motor shield powers the motor fro the Vin input, which is the DC socket.

9V battery? ?

Then you are lucky it is running one motor. You need a supply that is 2A to run two of those motors.

i did also use one of those but i also have a fischertecnik "Akkupak" - it's basically 7 x rechargeable AAs (so 7x1.2=8.4V and 1500mAh) - but it can't really be a power issue if it works on one axis, but not on the other ? (well, it is in the fact that 9V isn't really sufficient to drive PWM values below max level)

TomGeorge: If you have a DMM measure the Battery voltage when you try and run one motor then two.

is DMM a digital multi-meter ?

i have a LED volt-display, but that's rigged up to the Arduino power rail and it does show fluctuation below 5V - i can try plugging that to the battery - that would be in parallel to barrel jack to the Arduino, right ?

TomGeorge: It would be easier to put "motorStuff" as a function the code, rather than another bit of code to download at the moment.

does that actually make a difference to the compiler ?

actually, the motorStuff is already a function calling a function - i guess i should be able to re-write it myself to simplify things - but that wouldn't affect power issues, right ?

i am now thinking that it's just a characteristic of the shield - and that opposing "IN-n" pins draw more current than when they are in-sync (ie. if both are "HIGH" vs. if one is HIGH and the other LOW) - because that's the key difference between the two axes.

is that supposition an electronic possibility ?

Hi,

Change this;

  for(int i=4;i<=7;i++)//For Arduino Motor Shield
    pinMode(i, OUTPUT);  //set pin 4,5,6,7 to output mode

to;

  for(int i=4;i<=7;i++)
  {//For Arduino Motor Shield
    pinMode(i, OUTPUT);  //set pin 4,5,6,7 to output mode
  }

Add the { and }.

DMM , yes Digital Volt Meter. If you don't have one, it is worth buying a cheap one, it should be able to read DCVolts, OHMS, Diode Test at least.

Check your battery voltage as see what is happening.

Tom... :)

thanks, have added the curly braces.

i do have a cheap analog multimeter (well, multitester...) but it's easier to use the LED voltmeter since it's on the vehicle and can just display the readings as it goes along.

from the battery it was showing about 9.21V and dropped to 9.10V motors running - and no change when it does NOT move - i noticed the power cutting off actually - i thought it was a weak battery from earlier but even now at fully charged, there are moments the power just cuts out.

some sort of short-circuit ?

i assume the L293 chip has some power-cut protection ?

i will now be taking the shield off and having a closer look.