Built-in project · Arduino Uno R3

Linear actuator out and back on an Arduino Uno

A 12 V linear actuator on an L298N from pins 8 and 9 runs out for 12 s and back for 12 s. Its own limit switches stop it at each end. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $60. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Driving a DC motor in both directions with an H-bridge
  • Two logic pins set the direction of the current
  • Sharing ground between the Arduino and a 12 V supply
  • Limit switches that stop a motor without a sensor
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 4

How it works

An Arduino cannot power a linear actuator, so an L298N motor driver sits between them. The Uno only sends two small signals, pin 8 to IN1 and pin 9 to IN2. The driver turns them into 12 V across the actuator on OUT1 and OUT2.

The sketch pushes the actuator out for 12 seconds, stops for 1 second, pulls it back for 12 seconds, stops again, and repeats. Swapping which input is HIGH reverses the current, so the rod moves the other way.

The sketch does not know where the rod is. The actuator's own limit switch stops it at the end of its stroke, and 12 seconds is long enough to reach it.

Parts list

4 parts, plus the jumper wires. They retail for about $60; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Arduino Uno R3 pin GNDGround
Motor driver module, L298N pin IN2Arduino Uno R3 pin 9
Motor driver module, L298N pin IN1Arduino Uno R3 pin 8
Linear actuator, 12 V pin M+Motor driver module, L298N pin OUT1
Linear actuator, 12 V pin M-Motor driver module, L298N pin OUT2
Motor driver module, L298N pin 12V12 V
Motor driver module, L298N pin GNDGround

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A 12 V linear actuator on an L298N, all the way out and back.
//
//   L298N IN1 -> pin 8    IN2 -> pin 9    (ENA jumper on)
//   L298N OUT1 -> actuator M+    OUT2 -> actuator M-
//   L298N 12V -> 12 V,  GND -> GND (shared with the Uno)
//
// The 100 mm stroke takes about 8 s at 12.7 mm/s, and the L298N keeps some of
// the 12 V, so it is slower here. The sketch drives each way for 12 s anyway:
// the actuator's own limit switch stops it at the end, and the other way
// still runs at once. No position sensor needed.

const int IN1 = 8;
const int IN2 = 9;

void drive(int a, int b, const char *what) {
  digitalWrite(IN1, a);
  digitalWrite(IN2, b);
  Serial.println(what);
}

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: linear actuator, out and in");
}

void loop() {
  drive(HIGH, LOW, "out");
  delay(12000);
  drive(LOW, LOW, "stop");
  delay(1000);
  drive(LOW, HIGH, "in");
  delay(12000);
  drive(LOW, LOW, "stop");
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

drive sketch.ino

Sets IN1 and IN2 to the two values it is given and prints a label. HIGH and LOW means out, LOW and HIGH means in, and LOW and LOW means stop. Having one function for this keeps loop short and easy to read.

void drive(int a, int b, const char *what) {
  digitalWrite(IN1, a);
  digitalWrite(IN2, b);
  Serial.println(what);
}

loop sketch.ino

Runs the cycle forever: out for 12000 ms, stop for a second, in for 12000 ms, stop for a second. The stops let the motor settle before it reverses, which is easier on the driver and the actuator.

void loop() {
  drive(HIGH, LOW, "out");
  delay(12000);
  drive(LOW, LOW, "stop");
  delay(1000);
  drive(LOW, HIGH, "in");
  delay(12000);
  drive(LOW, LOW, "stop");
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Nothing moves This project
    Check that the 12 V supply is on the L298N 12V pin and that its GND is joined to the Uno GND.
  • The actuator goes the wrong way This project
    Swap the two actuator wires on OUT1 and OUT2, or swap the pins 8 and 9 in the code.
  • It stops short of the full stroke This project
    The L298N drops some voltage, so the actuator is slower than its rating. The 12 second delay in loop is there to allow for this.
  • The motors do not run or run weakly. Motor driver module
    Check the power jumpers and supply. Motor voltage goes on the +12V terminal, and ground must join the Arduino's ground. The driver drops about 2 V, so use a supply above the motor voltage.
  • Speed control with analogWrite does nothing. Motor driver module
    Remove the ENA and ENB jumpers and connect those pins to PWM pins. With the jumpers on, the channel is always at full speed.
  • The actuator pushes the wrong way. Linear actuator
    Reversing the polarity reverses the rod direction. Use an H-bridge or two relays to swap the wires from code. Do not feed it from a pin.
  • The supply sags, or the actuator is slow and the driver heats up. Linear actuator
    It draws a large current, often 1 A or more, and much more when pushing a load. Use a 12 V supply that can supply it, and a driver rated well above that current.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.