I recently get started to learn basic stuff. I use a nano board and an L298N to explore a little DC motor control. I want to control the PWM and I'm so sure everything is connected correctly. but I got no luck the motor is not running at all. and so far I have done it, I used a MEGA2560 and It's working perfectly! My question is why the nano board is not working PWM on the L298N? I use 4 lithium battery and dc to dc to regulate the voltage at 12V.
UPDATE!! I just used a multimeter to measure the PWM pin of the nano board and I found it's changing!(the voltage is jumping up and down like this) But just wondering why it is not working.
/*
Author : Andrea Lombardo
Site : https://www.lombardoandrea.com
Source : https://github.com/AndreaLombardo/L298N/
Based on Arduino Basic Fade example.
Speed range go from 0 to 255, default is 100.
Use setSpeed(speed) to change.
Sometimes at lower speed motors seems not running.
It's normal, may depends by motor and power supply.
Wiring schema in file "L298N - Schema_with_EN_pin.png"
*/
// Include the library
#include <L298N.h>
// Pin definition
const unsigned int IN1 = 7;
const unsigned int IN2 = 6;
const unsigned int EN = 10;
// Create one motor instance
L298N motor(EN, IN1, IN2);
int speedness = 0;
int speedAmount = 1;
void setup()
{
// Used to display information
Serial.begin(9600);
// Wait for Serial Monitor to be opened
while (!Serial)
{
//do nothing
}
}
void loop()
{
// Apply faded speed to both motors
motor.setSpeed(speedness);
// Tell motor A to go forward (may depend by your wiring)
motor.forward();
// Alternative method:
// motor.run(L298N::FORWARD);
//print the motor status in the serial monitor
printSomeInfo();
// Change the "speedness" for next time through the loop:
speedness = speedness + speedAmount;
// Reverse the direction of the fading at the ends of the fade:
if (speedness <= 0 || speedness >= 255)
{
speedAmount = -speedAmount;
}
// Wait for 30 milliseconds to see the dimming effect
delay(30);
}
/*
Print some informations in Serial Monitor
*/
void printSomeInfo()
{
Serial.print("Motor is moving = ");
Serial.print(motor.isMoving() ? "YES" : "NO");
Serial.print(" at speed = ");
Serial.println(motor.getSpeed());
}
Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.
Have you got gnd of the Nano connected to gnd of the 298?
The En jumper is removed. I connect the pin to a nano pin 10 and got no luck. But it’s working on a mega2560 board. How? I also measured the pin10 and there’s jumping voltage up and down. That’s what its supposed to be. But why the L298 no any action with it. So weird.
I do not know the library but use pin 5 and 6 instead of 6 and 7.
Why? perhaps in1 needs to be a pwm able pin.
On a mega pin 6 and 7 are pwm able pins and with a nano is 7 not a pwm able pin
I actually didn’t use L298 library at very beginning as I tested very basic code . I still got no luck. But anyway I’ll keep trying tomorrow. Hopefully I could get it done. Thank you for your reply.
Hi,
Try this code, it compiles but is untested.
Look in the IDE serial monitor at 9600buad and see what it displays.
Your motor should stop, spin one way, stop, spin the otherway, etc etc.
// Pin definition
const unsigned int IN1 = 7;
const unsigned int IN2 = 6;
const unsigned int EN = 10;
void setup()
{
// Used to display information
Serial.begin(9600);
Serial.println(" Motor Test Code ");
pinMode(EN, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
}
void loop()
{
digitalWrite(EN, LOW);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
Serial.println("Motor Stop");
delay(1000);
digitalWrite(EN, HIGH);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
Serial.println("Motor spins one way");
delay(1000);
digitalWrite(EN, LOW);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
Serial.println("Motor Stop");
delay(1000);
digitalWrite(EN, HIGH);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
Serial.println("Motor spins other way");
delay(1000);
}
the program from tom is for full trottle and with it you can check the basic functionality. After this you can expand the program with pwm but make sure you use analogwrite then