Arduino - 线性执行器(支撑或滑杆)
Arduino - 执行器
In this tutorial, we are going to learn:
在本教程中,我们将学习:
- How linear actuator works
线性执行器的工作原理 - How to make linear actuator extend or retract.
如何使线性执行器伸展或缩回。 - How to control a linear actuator using L298N driver and Arduino.
如何使用 L298N 驱动器和 Arduino 控制线性执行器。 - How to control the speed of a linear actuator.
如何控制线性执行器的速度。
Arduino OLED Display Tutorial
Arduino OLED显示教程
This tutorial is for linear actuator without feedback. If you want to learn about linear actuator with feedback, see this Arduino - Actuator with Feedback tutorial.
本教程适用于没有反馈的线性执行器。如果您想了解带反馈的线性执行器,请参阅此 Arduino - 带反馈的执行器教程。
Hardware Required 所需硬件
1 | × | Arduino UNO or Genuino UNO Arduino UNO 或 Genuino UNO | |
---|---|---|---|
1 | × | USB 2.0 cable type A/B USB 2.0 电缆 A/B 型 | |
1 | × | Linear Actuator 线性执行器 | |
1 | × | L298N Motor Driver Module L298N 电机驱动模块 | |
1 | × | 12V Power Adapter 12V电源适配器 | |
1 | × | DC Power Jack 直流电源插孔 | |
1 | × | (Optional) 9V Power Adapter for Arduino (可选)用于Arduino的9V电源适配器 | |
1 | × | (Optional) Transparent Acrylic Enclosure For Arduino Uno (可选)Arduino Uno透明亚克力外壳 |
About Linear Actuator 关于线性执行器
Linear Actuator Pinout 线性执行器引脚排列
Linear Actuator has two wires:
线性执行器有两根电线:
- Positive wire: usually red
正极线:通常为红色 - Negative wire: usually black
负极线:通常为黑色
How It Works 它是如何工作的
When you buy a linear actuator, you need to know what voltage linear actuator works. Let’s take a 12V linear actuator as an example.
当您购买线性执行器时,您需要知道什么电压线性执行器工作。我们以12V线性执行器为例。
When you power the 12V linear actuator by a 12V power source:
当您通过 12V 电源为 12V 线性执行器供电时:
- 12V and GND to the positive wire and negative wire, respectively: the linear actuator full-speed extends until it reaches the limit.
正极线和负极线分别为 12V 和 GND:线性执行器全速延伸直至达到极限。 - 12V and GND to the negative wire and positive wire, respectively: the linear actuator full-speed retracts until it reaches the limit.
负极线和正极线分别为12V和GND:线性执行器全速缩回,直到达到极限。
While extending or retracting, if we stop power to the actuator (GND to both positive and negative wire), the actuator stops extending/retracting
在伸展或缩回时,如果我们停止对执行器的电源(正极和负极线的 GND),执行器将停止伸展/缩回
※ NOTE THAT: ※ 注意事项:
For DC motor, servo motor, and stepper motor without gearing, when carrying a load, if we stop powering, they cannot keep the position. Unlike these motors, the actuator can keep the position even when stopping powering while carrying a load.
对于直流电机、伺服电机和步进电机,在承载负载时,如果我们停止供电,它们就无法保持位置。与这些电机不同,执行器即使在承载负载时停止供电也能保持位置。
If we provide power to linear actuators below 12V, the linear actuator still extends/retracts but not at maximum speed. It means if we change the voltage of the power supply, we can change the speed of the linear actuator. However, this method is not used in practice because of the difficulty in controlling the voltage of the power source. Instead, we fix the voltage of the power source and control the speed of the linear actuator via a PWM signal. The more duty cycle the PWM is, the higher speed the linear actuator extends/retracts.
如果我们为低于 12V 的线性执行器供电,线性执行器仍会伸展/缩回,但不会以最大速度伸展/缩回。这意味着如果我们改变电源的电压,我们可以改变线性执行器的速度。但是,由于难以控制电源的电压,因此在实践中没有使用这种方法。取而代之的是,我们固定电源的电压,并通过PWM信号控制线性执行器的速度。PWM的占空比越大,线性执行器伸展/缩回的速度就越高。
How to control a linear actuator using Arduino 如何使用Arduino控制线性执行器
Controlling a linear actuator includes:
控制线性执行器包括:
- Extends the linear actuator at maximum speed.
以最大速度伸展线性执行器。 - Retracts the linear actuator at maximum speed.
以最大速度缩回线性执行器。 - (optional) controls the extending/retracting speed
(可选)控制伸缩速度
Arduino can generate the signal to control the linear actuator. However, this signal has low voltage and current, We cannot use it to control the linear actuator. We need to use a hardware driver in between Arduino and linear actuator. The driver does two works:
Arduino可以生成信号来控制线性执行器。但是,该信号具有低电压和低电流,我们不能用它来控制线性执行器。我们需要在Arduino和线性执行器之间使用硬件驱动程序。驱动程序执行两项工作:
- Amplify the control signal from Arduino (current and voltage)
放大来自Arduino的控制信号(电流和电压) - Receive the another control signal from Arduino to swap pole of power supply → for direction control.
接收来自Arduino的另一个控制信号,以交换电源→的极点进行方向控制。
※ NOTE THAT: ※ 注意事项:
- This tutorial can be applied to all linear actuators. 12V linear actuator is just an example.
本教程可应用于所有线性执行器。12V线性执行器只是一个例子。 - When you controls 5V linear actuator, although Arduino pin outputs 5V (the same as linear actuator voltage), you still needs a driver in between Arduino and linear actuator because the Arduino pin does not provide enough the current for linear actuator.
当您控制 5V 线性执行器时,尽管 Arduino 引脚输出 5V(与线性执行器电压相同),但您仍然需要在 Arduino 和线性执行器之间有一个驱动器,因为 Arduino 引脚无法为线性执行器提供足够的电流。
There are many kinds of the chip, modules (e.g. L293D, L298N) can be used as linear actuator drivers. In this tutorial, we will use the L298N driver.
芯片种类繁多,模块(如L293D、L298N)可用作线性执行器驱动器。在本教程中,我们将使用 L298N 驱动程序。
※ NOTE THAT: ※ 注意事项:
You can also use relays as a driver. However it requires 4 relays to control a single linear actuator (both extend/retract)
您还可以将继电器用作驱动程序。但是,它需要 4 个继电器来控制单个线性执行器(均为伸展/缩回)
About L298N Driver 关于L298N Driver
L298N Driver can be used to control linear actuator, DC motor and stepper motor. In this tutorial, we learn how to use it to control the linear actuator.
L298N驱动器可用于控制直线执行器、直流电机和步进电机。在本教程中,我们将学习如何使用它来控制线性执行器。
L298N Driver Pinout L298N 驱动器引脚排列
L298N Driver has two channels, called channel A and channel B. Therefore, L298N Driver can control two linear actuator independently at the same time. Let’s assum that the linear actuator A is connected to channel A, the linear actuator B is connected to channel B. L298N Driver has 13 pins:
L298N 驱动器有两个通道,称为通道 A 和通道 B。因此,L298N驱动器可以同时独立控制两个线性执行器。假设线性执行器 A 连接到通道 A,线性执行器 B 连接到通道 B。 L298N 驱动器有 13 个引脚:
The common pins for both channels: 两个通道的通用引脚:
- VCC pin: supplies power for the linear actuator. It can be anywhere between 5 to 35V.
VCC引脚:为线性执行器供电。它可以在 5 到 35V 之间。 - GND pin: is a common ground pin, needs to be connected to GND (0V).
GND引脚:是一个公共接地引脚,需要连接到GND(0V)。 - 5V pin: supplies power for L298N module. It can be supplied by 5V from Arduino.
5V引脚:为L298N模块供电。它可以由Arduino的5V供电。
Channel A pins: 通道 A 引脚:
- ENA pins: are used to control the speed of the linear actuator A. Removing the jumper and connecting this pin to PWM input will let us control the extending/retracting speed of the linear actuator A.
ENA引脚:用于控制线性执行器A的速度,移除跳线并将此引脚连接到PWM输入,将使我们能够控制线性执行器A的伸缩速度。 - IN1 & IN2 pins: are used to control the moving direction of a linear actuator. When one of them is HIGH and the other is LOW, the a linear actuator will extend or retract. If both the inputs are either HIGH or LOW the linear actuator will stop.
IN1 和 IN2 引脚:用于控制线性执行器的移动方向。当其中一个为高电平,另一个为低电平时,线性执行器将伸展或缩回。如果两个输入均为高电平或低电平,则线性执行器将停止。 - OUT1 & OUT2 pins: are connected to a linear actuator A.
OUT1 和 OUT2 引脚:连接到线性执行器 A。
Channel B pins: 通道 B 引脚:
- ENB pins: are used to control the speed of the linear actuator B. Removing the jumper and connecting this pin to PWM input will let us control the extending/retracting speed of the linear actuator B.
ENB引脚:用于控制线性执行器B的速度,移除跳线并将此引脚连接到PWM输入,可以让我们控制线性执行器B的伸缩速度。 - IN3 & IN4 pins: are used to control the moving direction of a linear actuator. When one of them is HIGH and the other is LOW, the a linear actuator will extend or retract. If both the inputs are either HIGH or LOW the linear actuator will stop.
IN3 和 IN4 引脚:用于控制线性执行器的移动方向。当其中一个为高电平,另一个为低电平时,线性执行器将伸展或缩回。如果两个输入均为高电平或低电平,则线性执行器将停止。 - OUT3 & OUT4 pins: are connected to a linear actuator.
OUT3 和 OUT4 引脚:连接到线性执行器。
As described above, the L298N driver has two input powers:
如上所述,L298N 驱动器具有两种输入功率:
- One for linear actuator (VCC and GND pins): from 5 to 35V.
一个用于线性执行器(VCC 和 GND 引脚):从 5V 到 35V。 - One for the L298N module’s internal operation (5V and GND pins): from 5 to 7V.
一个用于 L298N 模块的内部操作(5V 和 GND 引脚):从 5V 到 7V。
The L298N driver also has three jumpers for advanced uses or other purposes. For the sake of simplicity, please remove all jumpers from the L298N driver.
L298N 驱动器还具有三个跳线,用于高级用途或其他用途。为简单起见,请从 L298N 驱动器上移除所有跳线。
We can control two linear actuators independently at the same time by using an Arduino and an L298N Driver. To control each linear actuator, we need only three pins from Arduino.
我们可以使用Arduino和L298N驱动器同时独立控制两个线性执行器。为了控制每个线性执行器,我们只需要Arduino的三个引脚。
※ NOTE THAT: ※ 注意事项:
The rest of this tutorial controls a linear actuator using channel A. Controlling the other linear actuator is similar.
本教程的其余部分使用通道 A 控制线性执行器,控制其他线性执行器与此类似。
How To Control Linear Actuator 如何控制线性执行器
We will learn how to control Linear Actuator using L298N driver
我们将学习如何使用 L298N 驱动器控制线性执行器
Wiring Diagram 接线图
Please remove all three jumpers on the L298N module before wiring.
接线前请拆下 L298N 模块上的所有三个跳线。
How To Make Linear Actuator Expand/Retract 如何使线性执行器展开/缩回
The moving direction of a Linear Actuator can be controlled by applying a logic HIGH/LOW to IN1 and IN2 pins. The below table illustrates how to control the direction in both channels.
线性执行器的移动方向可以通过对 IN1 和 IN2 引脚施加逻辑 HIGH/LOW 来控制。下表说明了如何控制两个通道中的方向。
IN1 pin IN1 引脚 | IN2 pin IN2 引脚 | Direction 方向 |
---|---|---|
LOW | LOW | Linear Actuator A stops 线性执行器 A 停止 |
HIGH | HIGH | Linear Actuator A stops 线性执行器 A 停止 |
HIGH | LOW | Linear Actuator A extends 线性执行器 A 扩展 |
LOW | HIGH | Linear Actuator A retracts 线性执行器 A 缩回 |
- Extends Linear Actuator A
扩展线性执行器 A
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW);
Retracts Linear Actuator A
缩回线性执行器 A
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH);
※ NOTE THAT: ※ 注意事项:
The moving direction is inverted if the OUT1 & OUT2 pin connects to two pins of the linear actuator in an inverse way. If so, it just needs to swap between OUT1 & OUT2 pin or change the control signal on IN1 and IN2 pin in the code.
如果 OUT1 和 OUT2 引脚以相反的方式连接到线性执行器的两个引脚,则移动方向是反转的。如果是这样,它只需要在 OUT1 和 OUT2 引脚之间切换,或者在代码中更改 IN1 和 IN2 引脚上的控制信号。
How To Stop Linear Actuator from Extending or Retracting 如何阻止线性执行器伸展或缩回
The linear actuator automatically stops extending/retracting when it reaches the limit. We can also programmably stop it from extending/retracting while it has not reached the limit.
当线性执行器达到极限时,它会自动停止伸展/缩回。我们还可以通过编程方式阻止它在未达到极限时伸展/缩回。
There are two ways to stop linear actuator
有两种方法可以停止线性执行器
- Controls the speed to 0
将速度控制为 0
analogWrite(ENA_PIN, 0);
- Controls IN1 IN2 pins to the same value (LOW or HIGH)
将 IN1 IN2 引脚控制为相同值(LOW 或 HIGH)
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
Or
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH);
How To Control the Speed of Linear Actuator via L298N Driver 如何通过L298N驱动器控制线性执行器的速度
It is simple to control the speed of the linear actuator. Instead of controlling ENA pin to HIGH, We generate a PWM signal to the ENA pin. We can do this by:
控制线性执行器的速度很简单。我们不是将ENA引脚控制为高电平,而是向ENA引脚生成PWM信号。我们可以通过以下方式做到这一点:
- Connect an Arduino pin to ENA of L298N
将Arduino引脚连接到L298N的ENA - Generate PWM signal to ENA pin by using analogWrite() function. L298N Driver amplify PWM signal to linear actuator
使用 analogWrite() 函数生成 PWM 信号到 ENA 引脚。L298N 驱动器将 PWM 信号放大到线性执行器
analogWrite(ENA_PIN, speed); // speed is a value from 0 to 255
The speed is a value between 0 and 255. If the speed is 0, the linear actuator stops. If the speed is 255, the linear actuator extends/retracts at maximum speed.
速度是介于 0 和 255 之间的值。如果速度为 0,则线性执行器停止。如果速度为 255,则线性执行器以最大速度伸展/缩回。
Arduino Example Code Arduino示例代码
The below code does:
以下代码执行:
- Extend actuator at maximum speed
以最大速度扩展执行器 - Stop the linear actuator 停止线性执行器
- Retract actuator at maximum speed
以最大速度缩回执行器 - Stop the linear actuator 停止线性执行器
/** 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 ENA_PIN = 9; // the Arduino pin connected to the EN1 pin L298N
const int IN1_PIN = 6; // the Arduino pin connected to the IN1 pin L298N
const int IN2_PIN = 5; // the Arduino pin connected to the IN2 pin L298N// the setup function runs once when you press reset or power the board
void setup() {// initialize digital pins as outputs.pinMode(ENA_PIN, OUTPUT);pinMode(IN1_PIN, OUTPUT);pinMode(IN2_PIN, OUTPUT);digitalWrite(ENA_PIN, HIGH);
}// the loop function runs over and over again forever
void loop() {// extend the actuatordigitalWrite(IN1_PIN, HIGH);digitalWrite(IN2_PIN, LOW);delay(20000); // actuator will stop extending automatically when reaching the limit// retracts the actuatordigitalWrite(IN1_PIN, LOW);digitalWrite(IN2_PIN, HIGH);delay(20000); // actuator will stop retracting automatically when reaching the limit
}
Quick Steps 快速步骤
- Remove all three jumpers on the L298N module.
卸下 L298N 模块上的所有三个跳线。 - Copy the above code and open with Arduino IDE
复制上面的代码并使用Arduino IDE打开 - Click Upload button on Arduino IDE to upload code to Arduino
单击Arduino IDE上的“上传”按钮,将代码上传到Arduino - You will see: 您将看到:
- Linear actuator extends and then stops when reaching the limit
线性执行器伸展,然后在达到极限时停止 - Linear actuator keeps the position a mount of time
线性执行器使位置保持时间的安装 - Linear actuator retracts and then stops when reaching the limit
线性执行器缩回,然后在达到极限时停止 - Linear actuator keeps the position a mount of time
线性执行器使位置保持时间的安装 - The above process is run repeatedly.
上述过程重复运行。
- Linear actuator extends and then stops when reaching the limit