Hello,
I have been trying to run a DC motor that is connected to an Arduino Uno board via an L298N motor driver. The code is verified and uploaded successfully but the motor does not work. I selected the correct COM port and the AVR board type using the tools option of the Arduino IDE. Here is the code:
// Motor A connections
int motorPin1 = 6; // Pin 6 on Arduino Uno
int motorPin2 = 7; // Pin 7 on Arduino Uno
int enablePin = 5; // Pin 5 on Arduino Uno
void setup() {
// Set the motor control pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop() {
// Move the motor forward
motorForward();
delay(3000); // Wait for 3 seconds
// Stop the motor
motorStop();
delay(1000); // Wait for 1 second
// Move the motor backward
motorBackward();
delay(3000); // Wait for 3 seconds
// Stop the motor
motorStop();
delay(1000); // Wait for 1 second
}
void motorForward() {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 255); // Set maximum speed
}
void motorBackward() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
analogWrite(enablePin, 255); // Set maximum speed
}
void motorStop() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 0); // Stop motor
}
I know the components are not faulty because the motor was running when I had directly connected it to a 6V power source. The motor controller is recently bought. And the Arduino board (though old) works because its power light is on & the LEDs next to terminal 13 blink every time I click on "upload" on the Arduino software. I am not sure what is causing the issue.
On the Uno R3 (and R4) pins 3, 5, 6, 9, 10 and 11 support PWM capability. On these pins, digitalWrite will set the pin to HIGH or LOW (on or off) as with any other digital pin, but in addition, analogWrite can also be used to set the PWM duty cycle. This allows e.g. the brightness of a LED or the speed of a motor to be adjusted in increments between 0 and 255, as opposed to just on or off. Of course, analogWrite can only be used on pins that support PWM mode. It will not work on digital only pins such as 2,4,7,8 for example.
In this case, the Op is correctly using analogWrite on a PWM capable pin (5) to set the duty cycle of PWM signal to control the speed of the motor. As per comments in their code, the value of 255 sets the duty cycle to 100% which would will run the motor at full speed.
pragya11, you mentioned that the motor runs from your 6V battery. However, this does not necessarily mean that the battery has sufficient power to run all of the components including motor, motor driver board and Uno simultaneously?
With only 6V (or less) on the L298's + power pin, it's 5V regulator may not produce enough voltage for it's logic circuits. Remove the power jumper and connect Arduino's 5V pin to the L298's 5V pin.
NOTE: After the L298 drops 1.5 (or more ) volts, there may not be enough to run the motor.
runaway_pancake connected the 5V to Arduino's 5V!
hammy I used six 1.5V batteries and they worked. The Arduio-to-PC cable was an additional problem - now fixed.
JCA34F the one I have does say anything outside the motor case but I tried a few things so I know a 9V can drive it.
BitSeeker thanks for that. I am using the PWM pins. 9V was sufficient.
I am able to power up the motor (switch it on/off) and control its speed. My motor is attached to a mini, toy-type conveyor belt and I want it to run at super slow speed (using it for an in-lab experiment setup). However, I cannot go below the speed of 50 (with/without the conveyor belt on). But I do hear a constant beep like noise as if the motor is trying to start but for some reason it cannot physically rotate.I will have to use a different type of motor. But for now, I am good with my Arduino setup.
Now I know its not an arduino issue but if anyone can give me any lead on motors that have low RPM capibilities, that'll be great. Otherwise, thankyou for your feedback so far