ESP32 - Actuator with Feedback

In a previous tutorial, we have learned about the linear actuator without feedback. In this tutorial, we are going to learn about the linear actuator with feedback (also called the feedback linear actuator). The feedback from the linear actuator provides the information to identify the position of its stroke, and then control the position. In detail, we are going to learn:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×12V Linear Actuator with Feedback
1×L298N Motor Driver Module
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to Feedback Linear Actuator

A feedback linear actuator is a linear actuator that has the feedback signal that allows to identify its position and control it. The feedback is a potentiometer that outputs the voltage value in proportion to the stroke's position.

Feedback Linear Actuator Pinout

A Feedback Linear Actuator has 5 wires:

  • Actuator Positive wire: This wire is used to control the linear actuator by using high voltage (12V, 24V, 48V...).
  • Actuator Positive wire: This wire is used to control the linear actuator by using high voltage (12V, 24V, 48V...).
  • 5V wire: this wire is used for the feedback potentiometer. Connect this wire to 5V or 3.3V
  • GND wire: this wire is used for the feedback potentiometer. Connect this wire to GND
  • Potentiometer wire: (also called feedback wire, or output wire) this wire outputs the voltage value in proportion to the stroke's position.
Feedback Linear Actuator Pinout

How It Works

If we provide high voltage to the positive and negative wires, the stroke of the actuator will be extended or retracted. In detail, If we connect:

  • 12V (12V, 24V, 48V...) and GND to the positive wire and negative wire, respectively: the linear actuator full-speed extends until it reaches the limit.
  • 12V (12V, 24V, 48V...) and GND to the negative wire and positive wire, respectively: the linear actuator full-speed retracts until it reaches the limit.
  • While extending or retracting, if we stop power to the actuator (GND to both positive and negative wire), the actuator stops extending/retracting

※ NOTE THAT:

  • The voltage value for controlling the actuator depends on the specification of the actuator. Read the datasheet or manual to know the corresponding voltage value.
  • The actuator can keep the position even when stopping powering while carrying a load.

The voltage value in the potentiometer wire is proportional to the position of stroke on the actuator. By measuring this voltage, we can know the stroke's position.

Wiring Diagram

Please remove all three jumpers on the L298N module before wiring.

ESP32 Linear Actuator L298N Driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

How to control extend/retract a linear actuator

See ESP32 - Actuator tutorial

How to find the position of the linear actuator

The below shows how to identify the position of stroke on a linear actuator.

Calibration

  • Identify the length of the actuator's stroke (in millimeter) by measuring (using a ruler) or reading the datasheet
  • Identify the output values when the linear actuator is fully extended and fully retracted by running the below code
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-actuator-with-feedback */ // the code for getting the feedback when the actuator fully extended and retracted #define ENA_PIN 27 // The ESP32 pin GPIO27 connected to the EN1 pin L298N #define IN1_PIN 26 // The ESP32 pin GPIO26 connected to the IN1 pin L298N #define IN2_PIN 25 // The ESP32 pin GPIO25 connected to the IN2 pin L298N #define POTENTIOMETER_PIN 36 // The ESP32 pin GPIO36 (ADC0) connected to the potentiometer of the actuator void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // initialize digital pins as outputs. pinMode(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); digitalWrite(ENA_PIN, HIGH); } void loop() { // extend the actuator digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); delay(20000); // wait for actuator fully extends. It will stop extending automatically when reaching the limit // read the analog in value: int POTENTIOMETER_MAX = analogRead(POTENTIOMETER_PIN); Serial.print("POTENTIOMETER_MAX = "); Serial.println(POTENTIOMETER_MAX); // retracts the actuator digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH); delay(20000); // wait for actuator fully extends. It will stop retracting automatically when reaching the limit int POTENTIOMETER_MIN = analogRead(POTENTIOMETER_PIN); Serial.print("POTENTIOMETER_MIN = "); Serial.println(POTENTIOMETER_MIN); }
  • You will see the log on Serial Monitor as below example
COM6
Send
POTENTIOMETER_MAX = 987 POTENTIOMETER_MIN = 13
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Write down these values
  • If the min/max values are swapped, swap IN1_PIN and IN2_PIN
  • Update three value on the below code
  • ESP32 code that calculate the position of the actuator

    /* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-actuator-with-feedback */ #define ENA_PIN 27 // The ESP32 pin GPIO27 connected to the EN1 pin L298N #define IN1_PIN 26 // The ESP32 pin GPIO26 connected to the IN1 pin L298N #define IN2_PIN 25 // The ESP32 pin GPIO25 connected to the IN2 pin L298N #define POTENTIOMETER_PIN 36 // The ESP32 pin GPIO36 (ADC0) connected to the potentiometer of the actuator #define STROKE_LENGTH 102 // PLEASE UPDATE THIS VALUE (in millimeter) #define POTENTIOMETER_MAX 2340 // PLEASE UPDATE THIS VALUE #define POTENTIOMETER_MIN 13 // PLEASE UPDATE THIS VALUE void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // initialize digital pins as outputs. pinMode(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); digitalWrite(ENA_PIN, HIGH); } void loop() { // extend the actuator digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); int potentiometer_value = analogRead(POTENTIOMETER_PIN); int stroke_pos = map(potentiometer_value, POTENTIOMETER_MIN, POTENTIOMETER_MAX, 0, STROKE_LENGTH); Serial.print("The stroke's position = "); Serial.print(stroke_pos); Serial.println(" mm"); }
    • Update the three calibrated values to the code
    • Upload the code to ESP32
    • See the result on Serial Monitor
    COM6
    Send
    The stroke's position = 2 mm The stroke's position = 35 mm The stroke's position = 43 mm The stroke's position = 60 mm The stroke's position = 68 mm The stroke's position = 79 mm The stroke's position = 83 mm The stroke's position = 96 mm The stroke's position = 100 mm
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

    How to control a linear actuator to a specific position

    /* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-actuator-with-feedback */ #define ENA_PIN 27 // The ESP32 pin GPIO27 connected to the EN1 pin L298N #define IN1_PIN 26 // The ESP32 pin GPIO26 connected to the IN1 pin L298N #define IN2_PIN 25 // The ESP32 pin GPIO25 connected to the IN2 pin L298N #define POTENTIOMETER_PIN 36 // The ESP32 pin GPIO36 (ADC0) connected to the potentiometer of the actuator #define STROKE_LENGTH 102 // PLEASE UPDATE THIS VALUE (in millimeter) #define POTENTIOMETER_MAX 2340 // PLEASE UPDATE THIS VALUE #define POTENTIOMETER_MIN 13 // PLEASE UPDATE THIS VALUE #define TOLERANCE 5 // in millimeter int targetPosition_mm = 50; // in millimeter void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // initialize digital pins as outputs. pinMode(ENA_PIN, OUTPUT); pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); digitalWrite(ENA_PIN, HIGH); } void loop() { int potentiometer_value = analogRead(POTENTIOMETER_PIN); int stroke_pos = map(potentiometer_value, POTENTIOMETER_MIN, POTENTIOMETER_MAX, 0, STROKE_LENGTH); Serial.print("The stroke's position = "); Serial.print(stroke_pos); Serial.println(" mm"); if (stroke_pos < (targetPosition_mm - TOLERANCE)) ACTUATOR_extend(); else if (stroke_pos > (targetPosition_mm + TOLERANCE)) ACTUATOR_retract(); else ACTUATOR_stop(); } void ACTUATOR_extend() { digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW); } void ACTUATOR_retract() { digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH); } void ACTUATOR_stop() { digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW); }

    ※ NOTE THAT:

    This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a potentiometer. The ESP32 ADC is good for projects that do NOT need high accuracy. However, for projects that need precise measurements, please note:

    • The ESP32 ADC is not perfectly accurate and might need calibration for correct results. Each ESP32 board can be a bit different, so you need to calibrate the ADC for each individual board.
    • Calibration can be difficult, especially for beginners, and might not always give the exact results you want.

    For projects that need high precision, consider using an external ADC (e.g ADS1115) with the ESP32 or using an Arduino, which has a more reliable ADC. If you still want to calibrate the ESP32 ADC, refer to ESP32 ADC Calibration Driver

    Video Tutorial

    Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

    Learn More

    ※ OUR MESSAGES