Go Down

Topic: DIY L298n motor controller - need a little help (Read 3 times) previous topic - next topic

carbon_adam

#15
Apr 15, 2013, 12:02 pm Last Edit: Apr 15, 2013, 12:20 pm by carbon_adam Reason: 1
cleaned up a little -  cant wait to go test it:)

Code: [Select]
#include <PWM.h>

int ENA = 11; //PWM
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 25000; //frequency (in Hz)

void setup ()
{
   InitTimersSafe();

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(ENA, frequency);
  if(success)
  {
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  InitTimersSafe(); //initialize all timers except for 0, to save time keeping functions
  }

}

void test_motor_1 ()
{
 
  for (int i = 0; i < 256; i++)
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite (ENA, i);
    delay (50);
  }

  delay (3000);
  digitalWrite (IN1, LOW);
 
  for (int i = 0; i < 256; i++)
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    analogWrite (ENA, i);
    delay (50);
    Serial.println(i);
  }

  delay (3000);
  digitalWrite (IN2, LOW);
}

void loop()
{
  test_motor_1();
}

carbon_adam

that completely resolves the PWM noise issue it runs at 25k no problem and the l298 seems to handle that well.  I have no heating issues and a fair amount of torque.
Here is a little test sketch I made that runs PWM on pin 9 forwards and backwards

Code: [Select]
// simple move forward and backward one motor on the l298n using pins 12 13 9/ 9 being PWM
#include <PWM.h>
int ENA = 9;
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 20000; //frequency (in Hz)

void setup ()
{
  Serial.begin(9600);
  InitTimersSafe();
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
}

void test_motor_1 ()
{
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite (ENA, i);
    delay (30);
    Serial.println(i);
  }

  delay (100);
  digitalWrite (IN1, LOW);
 
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    analogWrite (ENA, i);
    delay (30);
   Serial.println(i);
  }

  delay (100);
  digitalWrite (IN2, LOW);
}

void loop()
{
  test_motor_1();
}

Go Up