直流电机简介及利用L298N控制

Arduino-直流电机

Arduino - 直流电机 |Arduino教程 — Arduino - DC Motor | Arduino Tutorial (arduinogetstarted.com)

In this tutorial, we are going to learn:
在本教程中,我们将学习:

  • How DC motor works 直流电机的工作原理
  • How to control the speed and direction of DC motor.
    如何控制直流电机的速度和方向。
  • How to control a DC motor using L298N driver.
    如何使用 L298N 驱动器控制直流电机。
  • How to control two DC motors using L298N driver.
    如何使用 L298N 驱动器控制两个直流电机。

About DC Motor 关于直流电机

DC Motor Pinout 直流电机引脚排列

DC Motor has two wires:
直流电机有两根电线:

  • Positive wire: usually red
    正极线:通常为红色
  • Negative wire: usually black
    负极线:通常为黑色

DC Motor Pinout

How It Works 它是如何工作的

When you buy a DC motor, you need to know what voltage DC motor work. Let’s take a 12V DC motor as an example.
购买直流电机时,您需要知道直流电机的工作电压是多少。我们以12V直流电机为例。

When you power the 12V DC motor by a 12V power source:
当您通过 12V 电源为 12V 直流电机供电时:

  • 12V and GND to the positive wire and negative wire, respectively: the DC motor rotates at maximum speed in the clockwise direction
    正极线和负极线分别为12V和GND:直流电机顺时针方向以最大速度旋转
  • 12V and GND to the negative wire and positive wire, respectively: the DC motor rotates at maximum speed in the anti-clockwise direction
    负极线和正极线分别为12V和GND:直流电机逆时针方向以最大速度旋转

As described above, when the power pole is swapped between two wires of the DC motor, the rotating direction is reversed. This method is used to control the direction of the DC motor. Of course, not by changing manually but by programming.
如上所述,当电线杆在直流电机的两根电线之间交换时,旋转方向反转。这种方法用于控制直流电机的方向。当然,不是通过手动更改,而是通过编程。

If we provide power to DC motors below 12V, the motor still rotates but not at maximum speed. It means if we change the voltage of the power supply, we can change the speed of the DC motor. 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 DC motor via a PWM signal. The more duty cycle the PWM is, the higher speed the DC motor rotates.
如果我们为低于 12V 的直流电机供电,电机仍会旋转,但不会以最大速度旋转。这意味着如果我们改变电源的电压,我们可以改变直流电机的速度。但是,由于难以控制电源的电压,因此在实践中没有使用这种方法。取而代之的是,我们通过PWM信号固定电源的电压并控制直流电机的速度。PWM的占空比越大,直流电机的旋转速度就越高。

How to control DC motor

How to control DC motor using Arduino 如何使用Arduino控制直流电机

Controlling DC motor includes two factors: speed and direction. Arduino can generate the PWM signal. However, this PWM signal has low voltage and current, We cannot use it to control the DC motor. We need to use a hardware driver in between Arduino and DC motor. The driver does two works:
控制直流电机包括两个因素:速度和方向。Arduino可以生成PWM信号。但是,这种PWM信号具有低电压和低电流,我们不能用它来控制直流电机。我们需要在Arduino和DC电机之间使用硬件驱动程序。驱动程序执行两项工作:

  • Amplify the PWM signal from Arduino (current and voltage) → for speed control
    放大来自Arduino(电流和电压)→的PWM信号,用于速度控制
  • Receive the control signal from Arduino to swap pole of power supply → for direction control.
    接收来自Arduino的控制信号,交换电源→的极点进行方向控制。

Arduino control DC motor

※ NOTE THAT: ※ 注意事项:

  • This tutorial can be applied to all DC motors. 12V DC motor is just an example.
    本教程可应用于所有直流电机。12V直流电机只是一个例子。
  • When you controls 5V DC motor, although Arduino pin outputs 5V (the same as DC motor voltage), you still needs a driver in between Arduino and DC motor because the Arduino pin does not provide enough the current for DC motor.
    当您控制 5V 直流电机时,虽然 Arduino 引脚输出 5V(与直流电机电压相同),但您仍然需要在 Arduino 和 DC 电机之间有一个驱动器,因为 Arduino 引脚无法为直流电机提供足够的电流。

There are many kinds of the chip, modules (e.g. L293D, L298N) can be used as DC motor drivers. In this tutorial, we will use the L298N driver.
芯片种类繁多,模块(如L293D、L298N)可用作直流电机驱动器。在本教程中,我们将使用 L298N 驱动程序。

About L298N Driver 关于L298N Driver

L298N Driver can be used to control DC motor and stepper motor. In this tutorial, we learn how to use it to control the DC motor.
L298N驱动器可用于控制直流电机和步进电机。在本教程中,我们将学习如何使用它来控制直流电机。

L298N Driver Pinout

L298N Driver Pinout L298N 驱动器引脚排列

L298N Driver Pinout

L298N Driver can control two DC motor independently at the same time, called motor A and motor B. L298N Driver has 13 pins:
L298N驱动器可以同时独立控制两个直流电机,称为电机A和电机B.L298N驱动器有13个引脚:

The common pins for both motors: 两种电机的通用引脚:
  • VCC pin: supplies power for the motor. 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供电。
Motor A pins (Channel A): 电机 A 引脚(通道 A):
  • ENA pins: are used to control the speed of Motor A. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor A.
    ENA引脚:用于控制电机A的速度,移除跳线并将此引脚连接到PWM输入,可以让我们控制电机A的速度。
  • IN1 & IN2 pins: are used to control the spinning direction of Motor A. When one of them is HIGH and the other is LOW, the Motor A will spin. If both the inputs are either HIGH or LOW the Motor A will stop.
    IN1 和 IN2 引脚:用于控制电机 A 的旋转方向。当其中一个为高电平而另一个为低电平时,电机 A 将旋转。如果两个输入均为高电平或低电平,则电机 A 将停止。
  • OUT1 & OUT2 pins: are connected to Motor A.
    OUT1 和 OUT2 引脚:连接到电机 A。
Motor B pins (Channel B): 电机 B 引脚(通道 B):
  • ENB pins: are used to control the speed of Motor B. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor B.
    ENB引脚:用于控制电机B的速度,移除跳线并将此引脚连接到PWM输入,可以让我们控制电机B的速度。
  • IN3 & IN4 pins: are used to control the spinning direction of Motor B. When one of them is HIGH and the other is LOW, Motor B will spin. If both the inputs are either HIGH or LOW the Motor B will stop.
    IN3 和 IN4 引脚:用于控制电机 B 的旋转方向。当其中一个为高电平而另一个为低电平时,电机 B 将旋转。如果两个输入均为高电平或低电平,则电机 B 将停止。
  • OUT3 & OUT4 pins: are connected to Motor B.
    OUT3 和 OUT4 引脚:连接到电机 B。

As described above, the L298N driver has two input powers:
如上所述,L298N 驱动器具有两种输入功率:

  • One for DC motor (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 DC motors independently at the same time by using an Arduino and an L298N Driver. To control each motor, we need only three pins from Arduino.
我们可以使用Arduino和L298N驱动器同时独立控制两个直流电机。为了控制每个电机,我们只需要Arduino的三个引脚。

※ NOTE THAT: ※ 注意事项:

The rest of this tutorial controls a DC motor using channel A. Controlling the other DC motor is similar.
本教程的其余部分使用通道 A 控制直流电机,控制另一台直流电机类似。

How To Control the Speed of DC Motor via L298N Driver 如何通过L298N驱动器控制直流电机的速度

It is simple to control the speed of the DC motor by generating a PWM signal to the ENA pin of L298N. We can do this by:
通过向 L298N 的 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 DC motor
    使用 analogWrite() 函数生成 PWM 信号到 ENA 引脚。L298N 驱动器将 PWM 信号放大至直流电机
analogWrite(ENA_PIN, speed); // speed is a value from 0 to 255 speed 是介于 0 到 255 之间的值

The speed is a value between 0 and 255. If the speed is 0, the motor stops. If the speed is 255, the motor spins at maximum speed.
速度是介于 0 和 255 之间的值。如果速度为 0,则电机停止。如果速度为 255,则电机以最大速度旋转。

How To Control the Direction of DC Motor via L298N Driver 如何通过L298N驱动器控制直流电机的方向

The spinning direction of a motor 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 方向
LOWLOWMotor A stops 电机 A 停止
HIGHHIGHMotor A stops 电机 A 停止
HIGHLOWMotor A spins Clockwise 电机 A 顺时针旋转
LOWHIGHMotor A spins Anti-Clockwise 电机 A 逆时针旋转
  • Control motor A spins clockwise
    控制电机 A 顺时针旋转
digitalWrite(IN1_PIN, HIGH);  
digitalWrite(IN2_PIN, LOW);  
  • Control motor A spins anti-clockwise
    控制电机 A 逆时针旋转
digitalWrite(IN1_PIN, LOW); 
digitalWrite(IN2_PIN, HIGH);  

※ NOTE THAT: ※ 注意事项:

The spinning direction is inverted if the OUT1 & OUT2 pin connects to two pins of the DC motor 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 DC Motor Spinning 如何停止直流电机旋转

There are two ways to stop DC motor
有两种方法可以停止直流电机

  • Controls the speed to 0
analogWrite(ENA_PIN, 0); 
  • Controls IN1 IN2 pins to the same value (LOW or HIGH)
digitalWrite(IN1_PIN, LOW);  
digitalWrite(IN2_PIN, LOW);  

  • Or
digitalWrite(IN1_PIN, HIGH);  
digitalWrite(IN2_PIN, HIGH); 

How to control a DC motor using L298N driver. 如何使用 L298N 驱动器控制直流电机。

Wiring Diagram 接线图

Please remove all three jumpers on the L298N module before wiring.
接线前请拆下 L298N 模块上的所有三个跳线。

Arduino DC Motor L298N Driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片

Arduino Code Arduino代码

The below code does:
以下代码执行:

  • Speed up DC motor 加速直流电机
  • Change the direction 改变方向
  • Speed down DC motor 减速直流电机
  • Stop the motor 停止电机
/*

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

// 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);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise
  digitalWrite(IN2_PIN, LOW);  // control motor A spins clockwise

  for (int speed = 0; speed <= 255; speed++) {
    analogWrite(ENA_PIN, speed); // control the speed
    delay(10);
  }

  delay(1000); // rotate at maximum speed 1 seconds in in clockwise direction

  // change direction
  digitalWrite(IN1_PIN, LOW);   // control motor A spins anti-clockwise
  digitalWrite(IN2_PIN, HIGH);  // control motor A spins anti-clockwise

  delay(1000); // rotate at maximum speed 1 seconds in in anti-clockwise direction

  for (int speed = 255; speed >= 0; speed--) {
    analogWrite(ENA_PIN, speed); // control the speed
    delay(10);
  }

  delay(1000); // stop motor 1 second
}
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: 您将看到:

    • DC motor is speeded up and then rotates at the maximum speed 1 second
      直流电机加速,然后以最大速度旋转 1 秒
    • DC motor’s direction is changed
      直流电机的方向改变
    • DC motor rotates at the maximum speed of 1 second in the reverse direction
      直流电机以 1 秒的最大速度反向旋转
    • DC motor is speeded down
      直流电机降速
    • DC motor stop 1 second
      直流电机停止 1 秒
    • The above process is run repeatedly.
      上述过程重复运行。
  • ※ NOTE THAT: ※ 注意事项:

    In this tutorial, we learn how to control the DC motor speed that is relative to the maximum speed. In order to control the absolute speed (round per second), we need to use a PID controller and an encoder. Controlling the absolute speed of the DC motor will be presented in another tutorial.
    在本教程中,我们将学习如何控制相对于最大速度的直流电机速度。为了控制绝对速度(每秒回合数),我们需要使用PID控制器和编码器。控制直流电机的绝对速度将在另一个教程中介绍。

How to Control two DC Motors using L298N Driver 如何使用 L298N 驱动程序控制直流电机

以下代码可使直流电机无限旋转,并在触动两个限位开关中的一个时改变方向
在这里插入图片描述

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

#include <ezButton.h>

#define DIRECTION_CCW -1
#define DIRECTION_CW 1

#define ENA_PIN 7  // The Arduino pin connected to the EN1 pin L298N
#define IN1_PIN 6  // The Arduino pin connected to the IN1 pin L298N
#define IN2_PIN 5  // The Arduino pin connected to the IN2 pin L298N

ezButton limitSwitch_1(A0);  // create ezButton object that attach to pin A0
ezButton limitSwitch_2(A1);  // create ezButton object that attach to pin A1

int direction = DIRECTION_CW;
int prev_direction = DIRECTION_CW;

void setup() {
  Serial.begin(9600);

  limitSwitch_1.setDebounceTime(50);  // set debounce time to 50 milliseconds
  limitSwitch_2.setDebounceTime(50);  // set debounce time to 50 milliseconds

  // initialize digital pins as outputs.
  pinMode(ENA_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);

  digitalWrite(ENA_PIN, HIGH);  // max speed
  digitalWrite(IN1_PIN, HIGH);  // control motor A spins clockwise
  digitalWrite(IN2_PIN, LOW);   // control motor A spins clockwise
}

void loop() {
  limitSwitch_1.loop();  // MUST call the loop() function first
  limitSwitch_2.loop();  // MUST call the loop() function first

  if (limitSwitch_1.isPressed()) {
    direction *= -1;  // change direction
    Serial.println(F("The limit switch 1: TOUCHED"));
  }

  if (limitSwitch_2.isPressed()) {
    direction *= -1;  // change direction
    Serial.println(F("The limit switch 2: TOUCHED"));
  }

  if (prev_direction != direction) {
    Serial.print(F("The direction -> "));
    if (direction == DIRECTION_CW) {
      Serial.println(F("CLOCKWISE"));
      digitalWrite(IN1_PIN, HIGH);  // control motor A spins clockwise
      digitalWrite(IN2_PIN, LOW);   // control motor A spins clockwise
    } else {
      Serial.println(F("ANTI-CLOCKWISE"));
      digitalWrite(IN1_PIN, LOW);   // control motor A spins anti-clockwise
      digitalWrite(IN2_PIN, HIGH);  // control motor A spins anti-clockwise
    }

    prev_direction = direction;
  }
}

Video Tutorial 视频教程

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
我们正在考虑制作视频教程。如果您认为视频教程是必不可少的,请订阅我们的 YouTube 频道,为我们制作视频提供动力。

See Also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔚蓝慕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值