DEV Community

Cover image for Arduino - Actuator
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at arduinogetstarted.com

Arduino - Actuator

A linear actuator is just a motor that pushes or pulls a rod in a straight line instead of spinning. Arduino can't drive one directly because its pins are low voltage and low current, so you need a driver board in between. This tutorial covers controlling a 12V linear actuator with a BTS7960 driver: extending, retracting, stopping, and varying speed with PWM.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino - Actuator

What you need

Wiring

Arduino - Actuator wiring diagram

The linear actuator has two wires, positive (red) and negative (black), which connect to the BTS7960's B+ and B- screw terminals. The driver's RPWM and LPWM pins go to two PWM pins on the Arduino, and R_EN and L_EN go to two more Arduino pins. VCC connects to the Arduino's 5V pin for logic power, and GND is shared between both boards. The 12V for the actuator itself comes through a DC power jack into the driver, not through the Arduino. Since the actuator can tug on its wires as it moves, it helps to mount a screw terminal shield on the Arduino so each wire is clamped down instead of relying on friction-fit jumpers.

How it works

Feeding the actuator 12V one way makes it extend at full speed, and reversing the polarity makes it retract, both until they hit a mechanical limit. Grounding both wires stops the motion. The BTS7960 is built from two half-bridge chips that together form an H-bridge, which is exactly what you need to swap polarity under program control. R_EN and L_EN simply turn the two halves on or off. RPWM and LPWM each take a PWM signal: pulsing RPWM extends the actuator, pulsing LPWM retracts it, and the duty cycle sets the speed, since a lower average voltage through PWM moves the actuator slower than a fixed 12V ever could.

Code

The sketch first sets R_EN and L_EN HIGH to enable the driver, then uses analogWrite() on RPWM to extend the actuator at full speed, writes 0 to stop it, switches to analogWrite() on LPWM to retract, and stops again. Keeping one of RPWM or LPWM at 0 while PWM-ing the other is what selects direction. To change speed, you just lower the PWM value passed to whichever pin is active, anywhere from 0 (stopped) to 255 (full speed). To brake instead of coast, set both RPWM and LPWM to 0; to coast, drop R_EN and L_EN LOW instead.

arduino-actuator.ino

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-actuator
 */

// constants won't change
const int RPWM_PIN = 9;  // the Arduino pin connected to the RPWM pin of BTS7960 (extend)
const int LPWM_PIN = 10; // the Arduino pin connected to the LPWM pin of BTS7960 (retract)
const int R_EN_PIN = 12; // the Arduino pin connected to the R_EN pin of BTS7960
const int L_EN_PIN = 11; // the Arduino pin connected to the L_EN pin of BTS7960

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pins as outputs.
  pinMode(RPWM_PIN, OUTPUT);
  pinMode(LPWM_PIN, OUTPUT);
  pinMode(R_EN_PIN, OUTPUT);
  pinMode(L_EN_PIN, OUTPUT);

  // no power to the actuator yet, then turn on the driver
  analogWrite(RPWM_PIN, 0);
  analogWrite(LPWM_PIN, 0);
  digitalWrite(R_EN_PIN, HIGH);
  digitalWrite(L_EN_PIN, HIGH);
}

// the loop function runs over and over again forever
void loop() {
  // extend the actuator at maximum speed
  analogWrite(LPWM_PIN, 0);
  analogWrite(RPWM_PIN, 255);

  delay(20000); // actuator will stop extending automatically when reaching the limit

  // retract the actuator at maximum speed
  analogWrite(RPWM_PIN, 0);
  analogWrite(LPWM_PIN, 255);

  delay(20000); // actuator will stop retracting automatically when reaching the limit
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://arduinogetstarted.com/tutorials/arduino-actuator

Top comments (0)