Hi all. New to Arduino, hope I chose the correct category.
I've seen this issue on the forums, and tried solutions that seem applicable, but to no avail.
The problem is that the 12v motor connected using an L298N driver spins only when the Arduino is connected to a PC using USB.
Solutions I have read and tried:
- ensure 5v power supplied is actually 5v. Voltage meter says yes.
- try a USB charging adapter instead of PC-connected USB. In this configuration, motor does not spin.
- Insert code to blink LED-BUILTIN to see if code is actually running. LED_BUILTIN does blink in all configurations; USB-to-PC, USB-to-USBCharger, 9V battery connected to Elegoo 9V-5V power converter (voltage meter confirms Arduino 5V pin is getting 5V).
I also saw one post recommending hooking Ardunio ground PIN to ground on L298N. Didn't make sense to me but I tried. Result was the motor runs as if directly connected to 12v. IE code does not control the motor. (Hoping I didn't burn anything out).
What am I missing here?
#define enA 9
#define in1 7
#define in2 6
#define increment 1
#define interval 1000
#define lowspeed 205
#define highspeed 255
int direction = 0;
int isincrease = true;
int speed = lowspeed;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
// Set initial rotation direction
direction = 0;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA,0); //start with no spin
}
void loop() {
// Start Spinning
if (isincrease == true) {
speed += increment;
}
else {
speed -= increment;
}
if (speed <= lowspeed) {
speed = lowspeed; //reset to lowspeed
Serial.println("-----Low Speed-----");
analogWrite(enA,speed);
delay(interval * 3);
Serial.println("--Change Direction--");
if (direction == 0) {
direction = 1; //change direction
digitalWrite(in2,LOW); //other way
digitalWrite(in1,HIGH);
}
else {
direction = 0; //change direction
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH); //other way
}
isincrease = true;
}
if (speed >= highspeed) {
speed = highspeed; //reset to lowspeed
isincrease = false;
Serial.println("-----High Speed-----");
analogWrite(enA,speed);
delay(interval * 3);
}
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
Serial.println(speed);
analogWrite(enA,speed);
delay(interval);
}
