The connection is really strait forward. From arduino pins 6,7,5,4 connect accordingly to inputs IN1, IN2, IN3, IN4 in lm298n. And that's it, it will work. In terms of power supply, there are 2 power inputs in lm298n (5V, VCC), 5V can be connected to arduino, but if your motor requires more power, then you need to connect additional power supply to VCC. It is a really good practice to connect all GND cable together, I mean from additional power supply and from arduino. A code below increase a speed of two motors connected to lm298n and then decrease it.
I started recently my new mobile robotics project, and a picture shows a mobile framework connected to lm298n and arduino.
// Initialize int PWM1 = 6; // PWM Pin Motor 1 int PoM1 = 7; // Polarity Pin Motor 1 int PWM2 = 5; // PWM Pin Motor 2 int PoM2 = 4; // Polarity Pin Motor 2 int ValM1 = 0; // Initial Value for PWM Motor 1 int ValM2 = 0; // Initial Value for PWM Motor 2 int i = 25; // increment // Used to detect acceleration or deceleration boolean goUp = true ; void setup() { pinMode(PWM1, OUTPUT); pinMode(PoM1, OUTPUT); pinMode(PWM2, OUTPUT); pinMode(PoM2, OUTPUT); digitalWrite(PoM1, LOW) ; // Both motor with same polarity digitalWrite(PoM2, LOW) ; analogWrite(PWM1, ValM1); // Stop both motors => ValMx = 0 analogWrite(PWM2, ValM2); Serial.begin(9600); // Used to check value } // Main program void loop() { // give some time to the motor to adapt to new value delay (500) ; if ((ValM1 < 250) && goUp) // First phase of acceleration { ValM1 = ValM1 + i ; // increase PWM value => Acceleration ValM2 = ValM2 + i ; } else { goUp = false ; // Acceleration completed ValM1 = ValM1 - i ; // Decrease PWM => deceleration ValM2 = ValM2 - i ; // My motor made fanzy noise below 70 if (ValM1 < 75) { // One below 75, I set to 0 = STOP ValM1 = 0 ; ValM2 = 0 ; goUp = true ; // deceleration completed } } // If PWM values are OK, I send to motor controller if ((ValM1 > 75) && (ValM1 < 255)) { analogWrite(PWM1, ValM1); analogWrite(PWM2, ValM2); } Serial.print(ValM1); // Debug. Print Value Motor 1 Serial.print("\t"); // Print tab Serial.println(ValM2); // Print Value Motor 2 to Serial } // End.
No comments:
Post a Comment