Notifications
Clear all

Control motor direction on Cytron MD10 motoro controller UNO shield

15 Posts
5 Users
2 Likes
3,151 Views
salp
 salp
(@salp)
Member
Joined: 2 years ago
Posts: 21
Topic starter  

I am learning to control DC motors with the Cytron motor controller model MD10 (that is a UNO shield).  The speed control works as expected, however I cannot get the motor to change direction.

The code below sets (PIN 2 to HIGH if PIN A0  > 512) and (PIN 2 to LOW if PIN A0  < 512)

According to the manual I have it set PWM = PIN3,  Direction = PIN2.

I'm not 100% sure I'm setting the PIN2 correctly, because when I read PIN2 it always reads 0.

 

Thanks

Sal

 #include "CytronMotorDriver.h"

#define CTL_PIN 3
#define DIR_PIN 2
#define POT_PIN A0

// Configure the motor driver.
CytronMD motor(PWM_DIR, CTL_PIN, DIR_PIN);

// Motor control variables
int ctrlVal = 0;
int dirVal = 0;

int potValCurrent = 0;
int potValOld = 0;

void setup() {
  Serial.begin(115200);
  pinMode(DIR_PIN, OUTPUT);
}

void loop() {
    potValCurrent = analogRead(POT_PIN);

  if(potValCurrent > (potValOld+2) || potValCurrent < (potValOld-2)){
    Serial.print("Pot value = ");
    Serial.println(potValCurrent);
    
    if(potValCurrent > 512){
      Serial.println("increase");
      digitalWrite(DIR_PIN, HIGH);
      ctrlVal = map(potValCurrent, 513, 1023, 0, 255 );
    } 
    if(potValCurrent < 512){
      Serial.println("decrease");
      digitalWrite(DIR_PIN, LOW);
      ctrlVal = map(potValCurrent, 0, 512, 255, 0 );
    }
    
    motor.setSpeed(ctrlVal);
    potValOld = potValCurrent;

    Serial.print(" Dir Pin = ");
    Serial.print(digitalRead(DIR_PIN));
    Serial.print("    |   Motor speed = ");
    Serial.println(ctrlVal);
  }

}

   
Quote
Topic Tags
Melbul
(@melbul)
Member
Joined: 3 years ago
Posts: 43
 

Hi Sal,

looking at the CytronMotorDriver.h library example for PWM_DIR control of one motor it uses:

PWM=Pin3 DIR=Pin4

take a look at that example sketch?

I use an MDD10A board on my robot and have no issues from using the example sketches included in the library 🙂

Regards,

Mel.


   
ReplyQuote
salp
 salp
(@salp)
Member
Joined: 2 years ago
Posts: 21
Topic starter  

I changed the code to point to PIN4 and also set the jumper for PIN 4 on the Cytron card.

Now the print statements in the console show the PIN state changing properly, but the motor still turns in one direction.

Is it fair to say that if I put a voltage meter on the Cytron output posts I should see the voltage change between positive and negative.

 


   
ReplyQuote
salp
 salp
(@salp)
Member
Joined: 2 years ago
Posts: 21
Topic starter  

I put the voltmeter across the DC motor leads and it shows the voltage ramping up and down, but the polarity stays positive.

Thanks

Sal


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@salp 

I looked at the PWM_DIR example sketch for the CytronMotorDriver library and it implements positive rotation as positive speed and counter-rotation as negative speed. Why are you trying to set the direction pin directly ?

Try changing your sketch to use

ctrlVal = (potValCurrent-512)/2;

no matter what value potValCurrent has. Try that and see what happens, it should tend to zero speed in the middle of the pot range and rotate faster and faster as you approach each end of the pot range but with the direction changing as you cross the centre of the pot.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@salp  Hi Sal,

Just looking at your code, and the first thing that come to mind is that if your are getting a 0 ( low ) on any digital pin when you have written a digital high, it may be because you do not have a connection to that pin..The Arduino Uno will not can the state if there isn't a connection to that pin, so you'll always get a zero.

Try connecting a Led thru a 220 ohm to that pin, after disconnecting any other connections to the same pin) and see if it lites up the LED while monitoring the pin using digitalRead(), then disconnect the LED, and read it again.. you'll see it remains a 0.. at least on mine it does... 

Hope this helps

LouisR


   
ReplyQuote
Melbul
(@melbul)
Member
Joined: 3 years ago
Posts: 43
 

@salp

Did you figure it out?

Change this:

ctrlVal = map(potValCurrent, 0, 512, 255, 0 );

To this:

ctrlVal = map(potValCurrent, 0, 512, -255, 0 );

 I think that should fix your sketch?

 

Mel.

 


   
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@Slap,@melbul 

I made a circuit in TinkerCad simulator to replicate your circuit and software sketch. I left the code pretty much intact except for using Pin D3 ( a PWM pin) to replicate your Cytronics board, See following attached files.. It should work.. anyway, you can play around with this circuit..pretty simple.. I hope you find what the problem is. good luck.

Regards,

LouisR


   
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

OOPs... @Salp, I make a mistake.. the Arduino board doesn't care if there is a connection to a pin or not.. I found a glitch in the TinkerCad Simulator..it wanted to see a connection for some reason, so when the sketch in the simulator is run, the serial monitor kept showing me it was a zero from the pin even though I use the digitalWrite() to make it HIGH..When I connected a load to it ( an led and resistor) it then show a 1.. weird!.. Anyway.. just ignore my comment about the pin connection.. otherwise , the simulator works pretty well, as I prototype all my projects on it before using the bread board and actual components.

good luck solving your problem.. keep us informed..I'd like to know what was wrong.. still learning this C++ coding thing.

regards,

   LouisR

LouisR


   
ReplyQuote
Melbul
(@melbul)
Member
Joined: 3 years ago
Posts: 43
 

@inst-tech @salp

the only modification required to the sketch is the addition of the negative sign in the one line of code above.

the library needs that identifier to reverse the motor.

if you check out the library example I mentioned above and look at the the motor.setspeed commands they will make it fairly obvious.

@salp

sorry, I did not mention that on the original reply. In hindsight I should of pointed you in that direction first!

 

regards to all,

mel.


   
Inst-Tech reacted
ReplyQuote
salp
 salp
(@salp)
Member
Joined: 2 years ago
Posts: 21
Topic starter  

@melbul  the code modification to the map command worked.  I also deleted the write commands to the dir pin

Thanks to everyone, apologies for my tardiness free time is at a premium and it takes me a while to get back to sometimes.

Sal

 


   
ReplyQuote
salp
 salp
(@salp)
Member
Joined: 2 years ago
Posts: 21
Topic starter  

@inst-tech  Thanks for the input, i'm still learning the concept pull-up and pull-down resistors which seem to resolve many of the issues I have with floating values.  I always assumed that if nothing is connected the value was zero, I got a ways to go.

On the positive side I'n now able to control the dc motor and with help from others in this forum I also resolved my issues with reading a wheel encoder (same problem of not using pull-down resistors). I'm making progress in automating my drill press bed.

Thanks to all for the help


   
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@salp Glad you are solving your problems..Good Job!

Now on to the next phase of learning as we go..It's how most of us learn, by making mistakes and then having to research the problem for solutions. 40 years is not nearly enough time for me to have learned it all, but it did give me a good start...lol

Kind regards,

LouisR

LouisR


   
ReplyQuote
(@maxli)
Member
Joined: 3 years ago
Posts: 36
 

@inst-tech

Very useful advice. I think I'll try it


   
Inst-Tech reacted
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 2 years ago
Posts: 554
 

@maxli Indeed, part of the fun of these projects is learning how to problem solve.. in electronics, it is almost a necessity..there are many things that can go wrong, and it just takes time, and experience , to find a possible solution... which I might add, can be many different ways of going about it..that's where the experience comes in, been there, done that, and got the hat and the tee shirt...lol

Using the "black box theory" in troubleshooting is a very helpful tool to know how to use..

Basically, all systems are a "black box",  We divide the system up into smaller "black boxes, connected by inputs and outputs. We really don't care what's in them at this point, we are just interested in inputs, and outputs, and if they are the correct input to a box, and that it has a correct output from the box..We call it system isolation..then once we find the box, that hasn't the correct input or output, we can then look into the box to see what went wrong..Remember hardware and software both have common problems.. In time all hardware will fail, and in time, all software will eventually work!.. Good luck on your projects..

regards,

LouisR

LouisR


   
ReplyQuote