Adding Servos to Arduino mega 2560 r3

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");
}

Wheather distance is less than 20 or not there is a servo.write(110) at the end of loop. That servo will be permanently at position 110.

1 Like

Your pictures are useless, the best thing is to post an annotated schematic showing how you have wired it. This will tell more than several pages of ramblings or a list. I have no idea of what servos you have or what there power requirements etc are. The best place for your L298Ns is in the trash bin. Purchase some with MOSFET outputs.

From what I can tell I would not expect your arduino to operate very long if at all. Your battery is rated at 3.7V. The L298 will have a voltage drop of about 2.8V. Hmmm that only leaves 0.9V for the motor, wonder why it does not work. If you have a multimeter measure the voltages, if not get an inexpensive one, they can be purchased for less then $10.00 US.

Consider the previous advice from post #2. Also post links to "Technical information" on the parts you used, not with the same as but qualifier. Without the needed information all we can do is take a guess. You will probably give up in a week or two if it does not operate. Spend the time doing the schematic, it will expand your knowledge a lot and probably will answer several of your questions. There are many schematic capture programs available online as demos, free will offering or free.

Servos usually run on 5v. You are only supplying tops 4.2 v. That might be enough to make it jitter. The sg90 needs at least 4.8 volts and shouldn't get more than around 5.2v. Try powering it with a modified phone charger or a solid 5v supply and see what happens.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.