Trying to run two DC motors whose ends are connected to 2 wheels, with the L298N motor driver, but dont have an external power supply, so I connected a 12V AC to DC adapter to the barrel plug of my Arduino Mega 2560 R3, and connected 2 wires from the 5V and GND pins of the arduino, to the 5V and GND of the L298N.
Diagram of the setup and code is attached as below.
What am I doing wrong? I don't really have alot of microcontroller experience and this is the start of a university project.
//motor without speed control test
int motor1pin1 = 3;
int motor1pin2 = 4;
int motor2pin1 = 5;
int motor2pin2 = 6;
void setup() {
// put your setup code here, to run once:
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW); //rotate in 1 direction -a
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW); //rotate in 1 direction -b
delay(1000);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH); //rotate in direction opposite to a
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH); //rotate in direction opposite to b
delay(1000);
}
Thank you for your reply.
May I know why using an external power supply to the motor controller, and passing 12V to the mega and connecting the 5V pins on the mega and motor controller together not the same thing?
First I did not say to connect a 12V supply the the motor driver board, that would be way too much voltage for the motors.
It is different because the 5V output of the Arduino cannot supply enough Current to drive the motors. So you need a separate power supply that can supply enough current for the motors.
The driver board will decrease the voltage to the motors by up to 2V. A 6V, 2A supply will be OK but the motors may run a little slower.
Most people use a 2S Li-ion battery which is perfect.
See next post. Striking this post. If you supply power to the L298N from the 12vdc power supply, and you insert the "regulator" jumper, you can supply power to the Arduino from the L298N's 5v output (you are using that pin as an input).
I already deleted my post because I assumed all these motors had max 6V, but it seems some can take 3V to 12V. We do not know what motors @kaulsay has.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Do NOT use the Arduino 5V output to power motors. That can damage the Arduino, or cause it to malfunction.
That is the nominal voltage range, and it is OK to exceed that, especially if you use PWM to control the motor speed.
Apply 12V to the driver motor input. Keep in mind that up to about 4V is wasted as heat in the ancient, very inefficient L298 driver, so the full 12V is never applied to the motors.
Wouldn't it work to put 12V on the module's input, use the module's 7805 to get 5V, then waste another 2V through the L298N, and deliver 3V to the motors at the bottom end of their 3-6V range?
The motors are not powered from the module's 5V regulator. If they were, that regulator would burn out almost as quickly as the Arduino's regulator would.
The L298 will drop around 2V, exposing those 6V motors to around 10V.
It quite clear from your photo how you have it connected the schematic was not necessary.
However as I already pointed out it is wrong.
Did you review the tutorial?