Pages: 1 2 [3] 4 5   Go Down
Author Topic: TIP-120 bi-direction motor control  (Read 4316 times)
0 Members and 1 Guest are viewing this topic.
Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31452
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No it dosn't make me feel better, it is what I think. What are you supposed to think about a more expensive complex circuit that performs worse than a cheaper one, and has the potential of damaging itself?
We all have the duty to make things as safe as possible and this arrangement simply is not fault tolerant.
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Changed the wiring on this to your suggestion, everything works perfectly.

Quick question;

Code:
switch (inByte) {
    case 'q':   
      digitalWrite(2, HIGH);
      break;
    case 'w':   
      digitalWrite(2, LOW);
      break;

This works great, but is there a way to have it so that you can press a letter and
activate 2 pins simultaneously?

I know this does not work, but something in the neighborhood of:

Code:
switch (inByte) {
    case 't':   
      digitalWrite[color=red](2,3, HIGH)[/color];
      break;
    case 'y':   
      digitalWrite[color=red](2,3, LOW)[/color];
      break;

Thanks!
Logged

He who dares, Wins.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
switch (inByte) {
    case 'q':    
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      break;
    case 'w':    
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      break;

Not exactly at the same time, but pretty close. If that's not enough, look at port manipulation - http://www.arduino.cc/en/Reference/PortManipulation.
« Last Edit: December 31, 2011, 03:03:34 pm by dxw00d » Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm, it doesn't give me an error and says it compiled fine,
but this does nothing what I try to get both relays open / shut
at the same time although the lights on the Arduino are flashing
so a signal is getting through.

Code:
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pins:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.  
    // The switch statement expects single number values for each case;
    // in this exmaple, though, you're using single quotes to tell
    // the controller to get the ASCII value for the character.  For
    // example 'a' = 97, 'b' = 98, and so forth:

    switch (inByte) {
    case 'q':    
      digitalWrite(2, HIGH);
      break;
    case 'w':    
      digitalWrite(2, LOW);
      break;
    case 'e':    
      digitalWrite(3, HIGH);
      break;
    case 'r':    
      digitalWrite(3, LOW);
      break;
    case 'a':    
      digitalWrite(4, HIGH);
      break;
    case 's':    
      digitalWrite(4, LOW);
      break;
    case 'd':    
      digitalWrite(5, HIGH);
      break;
    case 'f':    
      digitalWrite(5, LOW);
      break;
   case 't':    
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      break;
    case 'y':    
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      break;
    case 'g':    
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      break;
    case 'h':    
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      break;

    default:
      // turn all the outputs off:
      for (int thisPin = 2; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    }
  }
}
« Last Edit: December 31, 2011, 03:34:23 pm by EnigmaCypher7 » Logged

He who dares, Wins.

Offline Offline
God Member
*****
Karma: 0
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would it not be more direct to write a single 0 byte to the port in this code snip?
Code:
      for (int thisPin = 2; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
Akin to 'PORTD = 0x00'
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Not exactly at the same time, but pretty close. If that's not enough, look at port manipulation - http://www.arduino.cc/en/Reference/PortManipulation.

Actually really great to know, I will keep this bookmarked.

Near the same time would be acceptable for this project but I can't get this to work:

Code:
case 't':   
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      break;
    case 'y':   
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      break;
    case 'g':   
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      break;
    case 'h':   
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      break;

as for:

Code:
for (int thisPin = 2; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
      }

Not worried about that yet, I am going to get the basic functions working
then do a bit of house cleaning.
Logged

He who dares, Wins.

Offline Offline
God Member
*****
Karma: 0
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A toggle switch won't be much help. For this project I need to be able to control a motor from a remote location, to do this I am simply going to use a keyboard button press through RDP as my trigger. I know with RDP there will be some latency but this really isn't going to be a problem, but also with this latency I think a "momentary button" feel from a keyboard key press would be a much better idea.

I like the idea of
Quote
if(letter == 'F' || pushButton == true) { // do your stuff
although I am sure the coding of this is not quits as simple. It would be nice to know the
Syntax.

Something in the neighborhood of:
Code:
Braking via relays will require a third relay that will open inserting a braking resistor in one of the motor legs. Wire the directional select relays with power coming into the NO and ground to the NC, the common from each will be the motor legs. Wired in this fashion you are in braking mode if no directions are selected and if both directions are selected. If you wire power/ground into the commons and the motor on the contacts you will short your motor supply when no relays active or when both are active. The braking relay can be in either motor leg, wired common to a direction relay and the NC to the motor, parallel a suitable resistor across the common to NC so that when Brake is active there is a resistor to dissipate the brake current. Braking in this fashion can also serve as a soft start mechanism.
switch (inByte) {
\\Controls motor polarity (+) (Transistor 1)
 case 'a':   
      digitalWrite(2, HIGH); \\Motor start forward
      break;
 case 'b':   
      digitalWrite(2, LOW); \\Motor stop forward
      break;
\\Controls motor polarity (-) (Transistor 2)
 case 'c':   
      digitalWrite(3, HIGH); \\Motor start reverse
      break;
 case 'd':   
      digitalWrite(3, LOW); \\Motor stop reverse
      break;

(sorry for all of the above comments, my code doesn't include them, but thought it would make the idea easier to follow)

But where to insert the IF statement?

Also still need to make sure that these 2 transistors are not going to both be powered trying
to turn the motor in opposite directions of each other and causing a short or other problems.

Quote
Doesn't matter, forward, stop, reverse is always going to put less strain on a motor than forward, reverse.
Still like this idea but don't know where to implement it in the above code either.

Thanks.
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is this posted here?!?! Bah...
Logged

He who dares, Wins.

Offline Offline
God Member
*****
Karma: 0
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a braking wiring diagram. Coding is simple send the result from an XOR NOT to the brake pin eg ForwardPin !^ ReversePin.

* Motor Control.xps (136.59 KB - downloaded 6 times.)
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem was not the breaking / freespinning of a motor. I rewired all of the relays that control all of the
motors similar to something that Grumpy_Mike had said.


However that current problem was that I wanted to send a signal to 2 other relays through TIP-120 resistors in order to make
2 belted tracks turn at the same time (think tank treads). I was wondering if this could be done in the Arduino, as I am sure
it can. Instead I just told the Vb project I wrote to send BOTH the Q and the A at the same time. This is accomplishing the
task just as I wanted just not with the Arduino. I am still curious how this might be done with the Arduino.

=)
Logged

He who dares, Wins.

Offline Offline
God Member
*****
Karma: 0
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I posted a motor control routine to this forum earlier, I used a joystick, however the motor section could easily be adapted to use fixed rate speeds.
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to use a pair of these to drive a motor.
How are you going to wire it up? I am not sure you can do it with only two transistors.

I know I am a little late on this - but it is possible to use only two transistors to control a DC motor's direction; it's called a half-bridge circuit (and your power supply must be dual-ended). Back in the day, they used such a circuit on the Milton Bradley Big Trak toy - because it was cheaper to build (transistors weren't super expensive at the time, they were just cutting corners to increase profitability, most likely). The toy ran on four D-cell's, with the pack "split" in the middle, that mid-point being "ground" for the motor driver circuitry, leaving +/- 3 VDC for the motors. The transistors switched this to change the motor's direction (the controller used on the toy ran on its own separate 9 volt battery).

This did have a side effect, though: If you constantly made turns in one particular direction (say, a lot of turns in the clockwise direction), you would drain one side of the pack faster than the other, which would lead to the pack trying to self-charge and stabilize, but ultimately causing the batteries to wear out very quickly (which must've made Duracell very happy).

This issue (and the need for a dual-ended supply - plus the cost difference being moot today anyhow) is why you don't generally see the setup used anymore...

smiley
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31452
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Near the same time would be acceptable for this project but I can't get this to work:
So what happens?
Put some print statements in to see if you are getting into the correct case statement. The code as posted looks fine.   
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, I guess that wasn't very elaborate. What happens is; the first part of the operation will run but he 2nd part never does.

As in:

Code:
case 't':   
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      break;

The 2nd pin fires, but not the 3rd.  smiley-fat

Oh and after some assembly time, I finally got to drive this thing around Monday night, it works perfectly! Thank you all for all of the help,
I feel like I have learned a few things too. =D
Logged

He who dares, Wins.

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, its time to take this code a step farther. Everything in the following code works perfectly for me. I am currently controlling 2
motors with the option to add another 2 motors, hence the 2x (Res) entries. I would like to add 2 servos into the mix, one on
Pin 5 and one on Pin 6.

Code:
#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_1KHZ);  // create motor #3, 1KHz pwm (Res)
AF_DCMotor motor4(4, MOTOR12_1KHZ);  // create motor #4, 1KHz pwm (Res)

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();

    int speed; // Local variable

    //Motor 1 - Full Speed

      switch (inByte) {
    case 'w':   
      motor1.setSpeed(225);
      motor1.run(FORWARD);     // Motor Runs Forward
      break;

    case 'k':   
      motor1.run(RELEASE);     // Motor Stops
      break;

    case 's':   
      motor1.setSpeed(225);
      motor1.run(BACKWARD);    // Motor Runs Backwards
      break;

      //Motor 2 - Full Speed

    case 'e':   
      motor2.setSpeed(225);
      motor2.run(FORWARD);     // Motor Runs Forward
      break;

    case 'l':   
      motor2.run(RELEASE);     // Motor Stops
      break;

    case 'd':   
      motor2.setSpeed(225);
      motor2.run(BACKWARD);    // Motor Runs Backwards
      break;

    default:
      // turn all the connections off:
      for (int thisPin = 2; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    }
  }
}

I know I have to add:

Code:
#include <Servo.h>
 
Servo myservo1;  // create servo object to control servo1
Servo myservo2;  // create servo object to control servo2

But as to where and not interrupt the motor code I am not sure. Do you add it to this instance or add it to a following instance?
Btw I am calling everything between the "}" instances.

Can I add the
Code:
#include <Servo.h>
after the
Code:
#include <AFMotor.h>
and the
Code:
Servo myservo1;  // create servo object to control servo1
Servo myservo2;  // create servo object to control servo2
after the
Code:
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_1KHZ);  // create motor #3, 1KHz pwm (Res)
AF_DCMotor motor4(4, MOTOR12_1KHZ);  // create motor #4, 1KHz pwm (Res)

and just add entries like

Code:
case 'T':   
      myservo1.run(FORWARD);    // Servo Runs Forward
      break;

after

 
Code:
switch (inByte) {
    case 'w':   
      motor1.setSpeed(225);
      motor1.run(FORWARD);     // Motor Runs Forward
      break;

Thank you for your help in Advance.  smiley-draw
-Ec7
Logged

He who dares, Wins.

Pages: 1 2 [3] 4 5   Go Up
Jump to: