Arduino Controlling the Direction and Speed of a Brushed Motor with an (L293) H-Bridge

Arduino Controlling the Direction and Speed of a Brushed Motor with an H-Bridge



What are we trying to do with arduino?


  1. You want to regulate the direction and speed of a brushed motor with an H-Bridge. This extends the functionality controlling both motor direction and speed through commands from the serial port.


Solution


Connect a brushed motor to the output pins of the H-Bridge as shown in Figure
Controlling the Direction and Speed of a Brushed Motor with an H-Bridge Arduino

Controlling the Direction and Speed of a Brushed Motor with an H-Bridge





Connecting a brushed motor using analogWrite for speed control


  1. This sketch uses commands from the Serial Monitor to control the speed and direction
  2. of the motor. Sending 0 will stop the motor, and the digits 1 through 9 will control the
  3. speed. Sending “+” and “-” will set the motor direction:

/*
* Brushed_H_Bridge sketch
* commands from serial port control motor speed and direction
* digits '0' through '9' are valid where '0' is off, '9' is max speed
* + or - set the direction
*/
const int enPin = 5; // H-Bridge enable pin
 Controlling the Direction and Speed of a Brushed Motor with an H-Bridge
const int in1Pin = 7; // H-Bridge input pins
const int in2Pin = 4;
void setup(){
Serial.begin(9600);pinMode(in1Pin, OUTPUT);pinMode(in2Pin, OUTPUT);Serial.println("Speed (0-9) or + - to set direction");
}
void loop(){
if ( Serial.available()) {char ch = Serial.read();
if(isDigit(ch)) // is ch a number?{
int speed = map(ch, '0', '9', 0, 255);analogWrite(enPin, speed);Serial.println(speed);
}else if (ch == '+'){
Serial.println("CW");digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,HIGH);
}else if (ch == '-'){
Serial.println("CCW");digitalWrite(in1Pin,HIGH);digitalWrite(in2Pin,LOW);
}else{
Serial.print("Unexpected character ");Serial.println(ch);
}}
}

Discussion


  1. This formula is comparable  in that motor direction is controlled by the amounton the IN1 and IN2 pins. however additionally, speed is controlled by the analogWrite valueon the IN pin . 
  2. Writing a worth of zero can stop the motor; writing 255 can run the motor at full speed. The motor speed can vary in proportion to values among this range.




Using Sensors to Control the Direction and Speed ofBrushed Motors (L293 H-Bridge)

Problem


  1. You want to control the direction and speed of brushed motors with feedback from sensors.
  2. For example, you want two photo sensors to control motor speed and direction to cause a robot to move toward a beam of light.

Arduino Controlling the Direction and Speed of a Brushed Motor with an H-Bridge


Solution


  1. This Solution uses similar motor connections to those shown in Figure , but with the addition of two light-dependent resistors, as shown in Figure
  2. Figure . Two motors controlled using sensors
  3. The sketch monitors the light level on the sensors and drives the motors to steer toward the sensor detecting the brighter light level:

/*
* Brushed_H_Bridge_Direction sketch
* uses photo sensors to control motor direction
* robot moves in the direction of a light
*/
int leftPins[] = {5,7,4}; // on pin for PWM, two pins for motor direction
int rightPins[] = {6,3,2};
8.11 Using Sensors to Control the Direction and Speed of Brushed Motors (L293 H-Bridge) | 311
const int MIN_PWM = 64; // this can range from 0 to MAX_PWM;
const int MAX_PWM = 128; // this can range from around 50 to 255;
const int leftSensorPin = 0; // analog pins with sensors
const int rightSensorPin = 1;
int sensorThreshold = 0; // must have this much light on a sensor to move
void setup(){
for(int i=1; i < 3; i++){
pinMode(leftPins[i], OUTPUT);pinMode(rightPins[i], OUTPUT);
}
}
void loop(){
int leftVal = analogRead(leftSensorPin);int rightVal = analogRead(rightSensorPin);if(sensorThreshold == 0){ // have the sensors been calibrated ?// if not, calibrate sensors to something above the ambient averagesensorThreshold = ((leftVal + rightVal) / 2) + 100 ;}if( leftVal > sensorThreshold || rightVal > sensorThreshold){// if there is adequate light to move ahead
setSpeed(rightPins, map(rightVal,0,1023, MIN_PWM, MAX_PWM));setSpeed(leftPins, map(leftVal ,0,1023, MIN_PWM, MAX_PWM));
}
}

void setSpeed(int pins[], int speed )
{
if(speed < 0)
{
digitalWrite(pins[1],HIGH);
digitalWrite(pins[2],LOW);
speed = -speed;
}
else
{
digitalWrite(pins[1],LOW);
digitalWrite(pins[2],HIGH);
}
analogWrite(pins[0], speed);

}

Discussion



This sketch controls the speed of two motors in response to the amount of light detected by two photocells. The photocells are arranged so that an increase in light on one side will increase the speed of the motor on the other side. This causes the robot to turn toward the side with the brighter light. Light shining equally on both cells makes the robot move forward in a straight line. Insufficient light causes the robot to stop.

Light is sensed through analog inputs 0 and 1 using analogRead. Whenthe program starts, the ambient light is measured and this threshold is used to determine the minimum light level needed to move the robot. A margin of 100 is added to the average level of the two sensors so the robot won’t move for small changes in ambient light level. Light level as measured with analogRead is converted into a PWM value using the map function. Set MIN_PWM to the approximate value that enables your robot to move

(low values will not provide sufficient torque; find this through trial and error with yourrobot).

Set MAX_PWM to a value (up to 255) to determine the fastest speed you want the robot to move. Motor speed is controlled in the set-speed function. Two pins are used to control the direction for each motor, with another pin to control speed. The pin numbers are held in the left-pins and right-pins arrays. The first pin in each array is the speed pin; the other two pins are for direction. 

An alternative to the L293 is the Toshiba FB6612FNG. This can be used in any of the recipes showing the L293D. Figure 8-13 shows the wiring for the FB6612 as used on the Pololu breakout board (SparkFun ROB-09402).


 Using Sensors to Control the Direction and Speed of Brushed Motors (L293 H-Bridge) 

  1. You can reduce the number of pins needed by adding additional hardware to control the direction pins. This is done by using only one pin per motor for direction, with a transistor or logic gate to invert the level on the other H-Bridge input. 
  2. You can find circuit diagrams for this in the Arduino wiki, but if you want something already wired up, you can use an H-Bridge shield such as the Freeduino motor control shield (NKC
  3. Electronics ARD-0015) or the Ardumoto from SparkFun (DEV-09213). 
  4. These shields plug directly into Arduino and only require connections to the motor power supply and windings.

Here is the sketch revised for the Ardumoto shield:
/** Brushed_H_Bridge_Direction sketch for Ardumotor shield* uses photo sensors to control motor direction* robot moves in the direction of a light*/int leftPins[] = {10,12}; // one pin for PWM, one pin for motor directionint rightPins[] = {11,13};const int MIN_PWM = 64; // this can range from 0 to MAX_PWM;const int MAX_PWM = 128; // this can range from around 50 to 255;const int leftSensorPin = 0; // analog pins with sensorsconst int rightSensorPin = 1;int sensorThreshold = 0; // must have this much light on a sensor to movevoid setup()
{pinMode(leftPins[1], OUTPUT);pinMode(rightPins[1], OUTPUT);}
void loop()
{int leftVal = analogRead(leftSensorPin);int rightVal = analogRead(rightSensorPin);if(sensorThreshold == 0){ // have the sensors been calibrated ?// if not, calibrate sensors to something above the ambient averagesensorThreshold = ((leftVal + rightVal) / 2) + 100 ;}if( leftVal > sensorThreshold || rightVal > sensorThreshold){// if there is adequate light to move aheadsetSpeed(rightPins, map(rightVal,0,1023, MIN_PWM, MAX_PWM));setSpeed(leftPins, map(leftVal, 0,1023, MIN_PWM, MAX_PWM));}}void setSpeed(int pins[], int speed ){if(speed < 0){digitalWrite(pins[1],HIGH);speed = -speed;}else{digitalWrite(pins[1],LOW);}analogWrite(pins[0], speed);}
The loop function is identical to the preceding sketch. setSpeed has less code because
hardware on the shield allows a single pin to control motor direction.
The pin assignments for the Freeduino shield are as follows:
int leftPins[] = {10,13}; // PWM, Direction
int rightPins[] = {9,12}; // PWM, Direction
Here is the same functionality implemented using the Adafruit Motor Shield; see Figure. This uses a library named AFMotor that can be downloaded from the Adafruit website.

Figure. Using the Adafruit Motor Shield
Arduino adafruit motor shield
adafruit motor shield

The Adafruit shield supports four connections for motor windings; the sketch that
follows has the motors connected to connectors 3 and 4:

/*
* Brushed_H_Bridge_Direction sketch for Adafruit Motor shield
* uses photo sensors to control motor direction
* robot moves in the direction of a light
*/
#include "AFMotor.h" // adafruit motor shield library
 Using Sensors to Control the Direction and Speed of Brushed Motors (L293 H-Bridge)
AF_DCMotor leftMotor(3, MOTOR12_1KHZ); // motor #3, 1 KHz pwm uses pin 5
AF_DCMotor rightMotor(4, MOTOR12_1KHZ); // motor #4, 1 KHz pwm uses pin 6
const int MIN_PWM = 64; // this can range from 0 to MAX_PWM;
const int MAX_PWM = 128; // this can range from around 50 to 255;
const int leftSensorPin = 0; // analog pins with sensors
const int rightSensorPin = 1;
int sensorThreshold = 0; // must be more light than this on sensors to move
void setup(){}
void loop(){
int leftVal = analogRead(leftSensorPin);int rightVal = analogRead(rightSensorPin);
if(sensorThreshold == 0){ // have the sensors been calibrated ?
// if not, calibrate sensors to something above the ambient averagesensorThreshold = ((leftVal + rightVal) / 2) + 100 ;
}if( leftVal > sensorThreshold || rightVal > sensorThreshold){
// if there is adequate light to move aheadsetSpeed(rightMotor, map(rightVal,0,1023, MIN_PWM, MAX_PWM));setSpeed(leftMotor, map(leftVal ,0,1023, MIN_PWM, MAX_PWM));
}
}
void setSpeed(AF_DCMotor &motor, int speed ){
if(speed < 0){
motor.run(BACKWARD);speed = -speed;
}else{
motor.run(FORWARD);
}
motor.setSpeed(speed);
}
If you have a different shield than the ones mentioned above, you will need to refer to
the data sheet and make sure the values in the sketch match the pins used for PWM
and direction.

Check this link too 



Blog by :- JAMCrux

Arduino Controlling the Direction and Speed of a Brushed Motor with an (L293) H-Bridge Arduino Controlling the Direction and Speed of a Brushed Motor with an (L293) H-Bridge Reviewed by Accrux on January 17, 2017 Rating: 5
Accrux. Powered by Blogger.