Hello,
I am building an obstacle avoidance robot using the Arduino UNO board. The robot consists of a pair of dc motors being driven using the L293D H-Bridge which has been functioning just fine for a line tracer robot project. I have added a HiTEC HS-422 servo motor (in parallel with a 100 micro farad decoupling capacitor) for the distance measurement part to be able to rotate the ping sensor to the left (~10 degrees) and right (~170 degrees).
Both DC motors and the servo have their own dedicated power source not to overload the Arduino power output and their grounds are connected to the common ground with the Arduino board (reported as a common mistake).
DC motors and the servo work just fine on their own as the robot goes forward and backward and turns right and left and servo passes the sweep test and moves nicely to any predefined angle. As I try to run the DC motors and the servo together, the servo turns to almost 0 degree and kind of twitches around 0 degree regardless of the predefined angle such as 90 or 170 degrees. I have tried the following scenarios and the following code for wiring the H-Bridge to overcome the problem but had no success:
First scenario:
Controlling the speed of the DC motors with a potentiometer and analogWrite(right_mot_Enable_Pin,Motor_Speed):
L293D-pin-1 connected to Arduino-pin- ~6
L293D-pin-2 connected to Arduino-pin- 8
L293D-pin-3 connected to left motor
L293D-pin-4 connected to common ground
L293D-pin-5 connected to common ground
L293D-pin-6 connected to left motor
L293D-pin-7 connected to Arduino-pin- ~10
L293D-pin-8 connected to + 9 V Battery
L293D-pin-9 connected to Arduino-pin- ~3
L293D-pin-10 connected to Arduino-pin- ~5
L293D-pin-11 connected to right motor
L293D-pin-12 connected to common ground
L293D-pin-13 connected to common ground
L293D-pin-14 connected to right motor
L293D-pin-15 connected to Arduino-pin- ~9
L293D-pin-16 connected to +5 V from Arduino
Servo-signal-pin connected to Arduino-pin- ~11
Second scenario:
No speed control on the DC motors:
L293D-pin-1 connected to +5 V from Arduino
L293D-pin-2 connected to Arduino-pin- ~5
L293D-pin-3 connected to left motor
L293D-pin-4 connected to common ground
L293D-pin-5 connected to common ground
L293D-pin-6 connected to left motor
L293D-pin-7 connected to Arduino-pin- ~6
L293D-pin-8 connected to + 9 V Battery
L293D-pin-9 connected to +5 V from Arduino
L293D-pin-10 connected to Arduino-pin- 8
L293D-pin-11 connected to right motor
L293D-pin-12 connected to common ground
L293D-pin-13 connected to common ground
L293D-pin-14 connected to right motor
L293D-pin-15 connected to Arduino-pin- ~9
L293D-pin-16 connected to +5 V from Arduino
Servo-signal-pin connected to Arduino-pin- 4
#include <Servo.h>
Servo myservo;
int Motor_Speed = 0;
const int left_mot_forward_Pin = 8; // Left Motor connected to digital pin 8
const int left_mot_backward_Pin = 10; // left Motor connected to digital pin 10
const int right_mot_forward_Pin = 9; // right Motor connected to digital pin 9
const int right_mot_backward_Pin = 5; // right Motor connected to digital pin 5
const int left_mot_Enable_Pin = 6; // Left Motor Enable Pin
const int right_mot_Enable_Pin = 3; // Right Motor Enable Pin
const int Pot_Pin = A3; // Pot Pin
void setup()
{
Serial.begin(9600);
Motor_Speed = map(analogRead(Pot_Pin), 0, 1023, 0, 255);
myservo.attach(11); // attaches the servo on pin 11 to the servo object
pinMode(left_mot_forward_Pin, OUTPUT); // Left Motor
pinMode(left_mot_backward_Pin, OUTPUT); // Left Motor
pinMode(right_mot_forward_Pin, OUTPUT); // Right Motor
pinMode(right_mot_backward_Pin, OUTPUT); // Right Motor
}
void loop()
{
myservo.write(90); //Rotate the servo to face the front
moveForward();
}
void moveForward() //This function tells the robot to go forward
{
digitalWrite(right_mot_forward_Pin, HIGH); //Right Motor Forward
digitalWrite(right_mot_backward_Pin, LOW); //Right Motor Backward
analogWrite(right_mot_Enable_Pin,Motor_Speed);
digitalWrite(left_mot_forward_Pin, HIGH); //Left Motor Forward
digitalWrite(left_mot_backward_Pin, LOW); //Left Motor Backward
analogWrite(left_mot_Enable_Pin,Motor_Speed);
}