Hello All.
Right now, I am currently working on a project that requires the use of several motors.
Using a Mega 2560 r3 board and a L298N Motor driver board, I want to control two 12 V DC motors and 3 SG90 servo motors. As of now, I have managed to get the l298N to work with the two DC motors as shown in this wiring diagram #1.
(the image has a different board, I removed the second motor for clarity, and the power supply is 12 V, but it still works)
Now, I want to be able to add 3 Sg90 servo motors to this setup with an ultrasonic sensor. While the ultrasonic works, the I have been having some problems getting the servos to work. Since I need many servos, I did not think the Mega could provide that kind of current and voltage, so I hooked up another external power supply purely to handle the servos. It looks something like diagram #2.
(I only showed one servo, but the other two would be wired up the same way)
However, no matter what I do, the servos will not respond. When I plug even one servo in, it may stutter or start up for a moment, but not enough to consider my test code a success. Even a simple servo.write(90) yields no result. Is there any way to successfully add more servos to this circuit?
This is my first post, but I hope I've made my situation clear.
I've included my current test code below, the point where the servo is being sent instructions is under the comment //Servo Logic in the main loop function
//Libraries
#include <Servo.h>
//motor controller pins
#define ENA 43
#define IN1 45
#define IN2 47
#define IN3 49
#define IN4 51
#define ENB 53
#define carSpeed 300
#define carSpeed2 300
int rightDistance = 0, leftDistance = 0;
//Ultrasonic Variables
const int trigPin = 13;
const int echoPin = 12;
long duration;
int distance, resultDistance;
//Servo Logic
Servo servoRed;
#define servoPinOne 7
Servo servoGreen;
#define servoPinTwo 6
Servo servoBlue;
#define servoPinThree 5
void setup() {
Serial.begin(9600);
//Servo Setup
servoRed.attach(servoPinOne);
servoGreen.attach(servoPinTwo);
servoBlue.attach(servoPinThree);
//Motor Setup
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
stop();
//Ultrasonic Setup
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
void loop() {
//Ultrasonic Logic
digitalWrite(trigPin, LOW);
delayMicroseconds(1000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
Serial.print(distance);
//Servo Logic
if(distance <= 20){
forward();
servoRed.write(90);
Serial.print("ServoRed");
}else{
stop();
}
servoRed.write(110);
}
void stop() {
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
Serial.println("Stop!");
}
void forward(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("Forward");
}
