Hello. I'm trying to build a garbage collecting robot. I'm using two DC motors (300rpm) and two servo motors (4.8-6V).
The first problem is, only one DC motor works, the other doesn't, and the second problem is that the servos do not work either. My first goal is to get the dc motors running.
//L293D
//Motor A
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
//Motor B
const int motorPin3 = 10; // Pin 7 of L293
const int motorPin4 = 9; // Pin 2 of L293
//This will run only one time.
void setup(){
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
//Motor Control - Motor A: motorPin1,motorpin2 & Motor B: motorpin3,motorpin4
//This code will turn Motor A clockwise for 2 sec.
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10000);
//This code will turn Motor A counter-clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10000);
//This code will turn Motor B clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(10000);
//This code will turn Motor B counter-clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(10000);
//And this code will stop motors
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
void loop(){
}
and this is the code for the two servos:
/*
#include <Servo.h>
Servo servoLeft; //Define left servo
Servo servoRight; //Define right servo
*/
void setup() {
servoLeft.attach(2); //Set left servo to pin 10
servoRight.attach(3); //Set right servo to pin 9
}
void loop (){
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servoLeft(pos); // tell servo to go to position in variable 'pos'
delay(5000); // waits 5ms for the servo to reach the position
servoRight(pos);
delay(5000)
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servoLeft(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 5ms for the servo to reach the position
servoRight(pos);
delay(1)
}
}
Well the servo one doesn't even compile because you've commented out some vital parts and you never defined pos at all. And even if it did compile you do realise you have a 5 second delay between each 1 degree move of the servos don't you? So it's going to take about 15 minutes to get from 0 to 180?
"On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins."
I didn't connect the servos to pins 9 and 10, is that the part of the paragraph you were talking about?
TolpuddleSartre:
I assumed that at some point, the OP is going to want to control the speed of the motors, and is going to hit a big wall of disappointment.
Ah right, so nothing to do with the current problems just anticipating possible future problems. Fair enough but for a minute there I thought I was missing something about the existing problem.
But that does raise another question because the PWM for motor speed control should normally be applied to the Enable pins of the L293D (pins 1 and 9) which aren't currently being used.
So jessabu, please confirm that you are actually using a bare L293D (not a motor driver board). And can you provide a circuit diagram showing all the L293D and motor connections, particularly where pins 1 and 9 on the L293D are connected?
BTW if Motor A works then the comments for the Motor pins are wrong. motorPin1 must be connected to pin 15 on the L293D. Pin 14 is one of the outputs to the motor. Which is also why an up to date circuit diagram is important.
//L293D
//Motor A
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
//Motor B
const int motorPin3 = 10; // Pin 7 of L293
const int motorPin4 = 9; // Pin 2 of L293
//This will run only one time.
void setup(){
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
//Motor Control - Motor A: motorPin1,motorpin2 & Motor B: motorpin3,motorpin4
//This code will turn Motor A clockwise for 2 sec.
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10000);
//This code will turn Motor A counter-clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10000);
//This code will turn Motor B clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(10000);
//This code will turn Motor B counter-clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(10000);
//And this code will stop motors
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
void loop(){
}
that is the code that I am using for the DC motors, and yes, I am using a bare L293D chip, not a motor driver board.
Check that the Enable pins on the L293 (1 and 9) are properly connected to 5V.
Since no battery is shown I assume it's powered by USB? It's a really bad idea running motors from the Arduino 5V pin, it can't deliver enough current. Take out the link to L293 pin8 and try it with a separate battery maybe 4 x AA cells +ve connected to pin 8 and -ve to Arduino ground.
And just to be certain...you're saying that either motor will run provided that it's connected to pins 5 and 6? The motor connected to 9 and 10 never works?
And just to be certain...you're saying that either motor will run provided that it's connected to pins 5 and 6? The motor connected to 9 and 10 never works?
yes, I am.
The wheels aren't powered by just USB, I connected a 9V battery to the power rails.