Friday, September 3, 2021

0000 0000 0111 0001

L298N motor control module

A colleague of mine challenged me to come up with a "rocking device" capable of moving a fairly large cylindrical object (not a baby...) backwards and forwards every few minutes.

The coding part is fairly simple:

#include <LowPower.h>

// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;

byte mspeed = 255;
int mtime = 1600;
int wtime = 5000;

void setup() {
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}

void loop() {

  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, mspeed);
  delay(mtime);

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

  delay(wtime);

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);

  analogWrite(enA, mspeed);
  delay(mtime);

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

  delay(wtime);

}


I decided to repurpose a "broken" UNO by replacing the fried ATMega328 with a new ATMega88 as per this blog post and video.

When it comes to translating mA control pulses from such a micro-controller it becomes necessary to design either some amplification/protection circuit, or simply use a purpose built module based around the L298N IC. From the datasheet:

The L298 is an integrated monolithic circuit. It is a high voltage, high current dual full-bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC and stepping motors.

I happened to have a module based around this IC which looked perfect for this application (thanks buckets).

My first attempt using a 12V motor was a bust as it was rotating too fast (not geared). Even when I used a geared motor, there were issues with overcoming friction to get the motor in motion.

I solved this by "blipping" the throttle a little bit before the motion starts, in order to get the rotation happening. This in code is a simple staggered start with the motor given almost the full juice for 20ms and then two thirds of that again for 10ms before the final controlled amount (e.g. 25/255 steps).

The final code didn't need this because the gearing was more forgiving.

Now that the prototype is working fine, I'm just waiting on a full sized motor (probably a salvaged windscreen wiper) to complete the challenge. Nice project!




No comments:

Post a Comment