Arexx 04 – motor control

In the little freetime I found the last days, I read a first book about the Arduino in german called “Arduino Praxiseinstieg by Bruehlmann” .

Then I analysed the motor control function of the motor contol ic L293D. It’s pretty simple. There are some enable inputs on the ic witch are constantly on plus and for that always enabled. For every motor there is a so called H-Bridge, so that every pin of the motor has one half bridge, which means one switch to + and one switch to ground. Two motors, four pins. If the processor pin is low the ground switch is activ. If the pin is high the +switch is active, that results to:

  • logic P5 on == right motor forward
  • logic P6 on == right motor backward
  • logic P10 on == left forward
  • logic P9 on == left backward

Don’t be confused that P5 is not on the ┬ÁC pin5 but on pin 10 and so on …

You can control the logic P5 in two ways, that means go right forward

  1. continous on : digitalWrite( 5, HIGH)
  2. controlled : analogWrite( 5, 200)

The number “200” can have a range of 0..255 resulting in a voltage of 0..100% on the motors.

Here is my first test program, because I was to lazy to write a lot I made some simple functions, OK the repetition of delay and motorstop could be another function .. :

/*
Name project: Motor-Test
Author: didi 4august2012
Company: dmemos.de
Discription:
Some tests how to control the motors
*/

void setup()
{
//Setup: Initalization
//pin 10 left foreward
pinMode(10, OUTPUT);
//pin 9 left backward
pinMode(9, OUTPUT);
//pin 5 right foreward
pinMode(5, OUTPUT);
//pin 6 right backward
pinMode(6, OUTPUT);

//Then set the start value of the signals to zero:
analogWrite(9, 0);
analogWrite(10, 0);
analogWrite(5, 0);
analogWrite(6, 0);

//Startup delay:
delay(2000);

}

void loop()
{
rightForward( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

leftForward( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

rightBackward( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

leftBackward( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

forward( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

backward( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

rightFast( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

leftFast( 220 );
delay( 2000 );
motorStop();
delay( 2000 );

}

void motorSet( byte lf, byte lb, byte rf, byte rb)
{ analogWrite(9, lf);
analogWrite(10,lb);
analogWrite(5, rf);
analogWrite(6, rb);
}

void motorStop( )
{ motorSet( 0,0,0,0);
}

void leftForward( byte spd ) // move left forward speed 0..255
{ motorSet( spd, 0, 0, 0 ); }

void leftBackward( byte spd ) // move left backward speed 0..255
{ motorSet( 0, spd, 0, 0 ); }

void rightForward( byte spd ) // move right forward speed 0..255
{ motorSet( 0, 0, spd, 0 ); }

void rightBackward( byte spd ) // move right backward speed 0..255
{ motorSet( 0, 0, 0, spd ); }

void forward( byte spd ) // move forward speed 0..255
{ motorSet( spd, 0, spd, 0 ); }

void backward( byte spd ) // move backward speed 0..255
{ motorSet( 0, spd, 0, spd ); }

void leftFast( byte spd ) // left forward, right back > fast turn
{ motorSet(spd,0,0,spd); }

void rightFast( byte spd ) // left back, right forward > fast turn
{ motorSet( 0,spd,spd,0); }

Troubleshooting : I needed half an hour to recognize, that the reason for the not working, that means sometimes hanging motors, is simply the weak supply via USB, with battery it worked without problems.


Leave a Reply

Your email address will not be published. Required fields are marked *

*

2 × two =

Top