I just recently started working with electronics and Arduino. I was able to connect the Arduino to an L298N.
I was just trying to run a 5V DC motor (which spins if I connect its pin to 5V and GND on Arduino) with the L298N.
I am using the following code and I connected the pins 9,8,7 to ENA, IN1, and IN2. When I power the L298N with 5V, and check the voltage across OUT1 and OUT2, I notice it moving between 0 and 5V as desired. But the moment I connect it to the DC motor, the voltage reading goes to 0 and the motor does not spin. It still spins if I connect it to the Arduino 5V and Gnd, but not the L298N.
I also tried to connect a 12V electromagnet instead of the motor and applied 12 V power to the L298N. But same results.
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
void setup() {
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
void loop() {
directionControl();
delay(1000);
}
// This function lets you control spinning direction of motors
void directionControl() {
// Set motors to maximum speed
// For PWM maximum possible values are 0 to 255
analogWrite(enA, 255);
// Turn on motor A & B
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(2000);
// Turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}