arduino教学:红外遥控车

arduino 红外遥控车

红外线控制器

One of the most exciting projects for Arduino beginners is creating an Arduino robot car. In this tutorial, we will discover how to use Arduino to build a robot car and control it using an IR remote controller. We will also explore another tutorial where we will learn to transform it into a Bluetooth-controlled car.
对于Arduino初学者来说,最令人兴奋的项目之一是创建Arduino机器人汽车。在本教程中,我们将了解如何使用Arduino构建机器人汽车并使用红外遥控器对其进行控制。我们还将探索另一个教程,我们将学习如何将其转换为蓝牙控制的汽车。

Arduino Tutorials - Quick Look v1
Arduino 教程 - 快速查看 v1

请添加图片描述

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×RC Car 遥控车
1×L298N Motor Driver Module L298N 电机驱动模块
1×IR Remote Controller Kit 红外遥控器套件
1×CR2025 Battery (for IR Remote controller) CR2025电池(红外遥控器用)
1×1.5V AA Battery (for Arduino and Car) 1.5V AA电池(用于Arduino和汽车)
1×Jumper Wires 跳线
1×(Recommended) Screw Terminal Block Shield for Arduino Uno (推荐)用于Arduino Uno的螺钉接线端子屏蔽层
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno (可选)Arduino Uno透明亚克力外壳

About Robot Car 关于机器人汽车

In the Arduino context, the robot car is commonly referred to as the robot car, RC car, remote control car, smart car, or DIY car. It can be controlled remotely and wirelessly using either an IR remote controller or a smartphone app via Bluetooth/WiFi… The robot car can turn left or right and move forward or backward.
在Arduino上下文中,机器人汽车通常被称为机器人汽车,遥控汽车,遥控汽车,智能汽车或DIY汽车。它可以通过蓝牙/WiFi 使用红外遥控器或智能手机应用程序进行远程和无线控制…机器人汽车可以向左或向右转,向前或向后移动。

A 2WD (Two-Wheel Drive) car for Arduino is a small robotic vehicle that you can build and control using an Arduino board. It typically consists of the following components:
Arduino的2WD(两轮驱动)汽车是一种小型机器人车辆,您可以使用Arduino板构建和控制它。它通常由以下组件组成:

  • Chassis: The base or frame of the car,where all other components are mounted.
    底盘:汽车的底座或车架,安装所有其他部件的地方。
  • Wheels: The two wheels that provide locomotion to the car. They are attached to two DC motor.
    车轮:为汽车提供运动的两个车轮。它们连接到两个直流电机。
  • Motors: Two DC motors are used to drive the two wheels.
    电机:两个直流电机用于驱动两个轮子。
  • Motor Driver: The motor driver board is an essential component that interfaces between the Arduino and the motors. It takes signals from the Arduino and provides the necessary power and control to the motors.
    电机驱动器:电机驱动板是连接Arduino和电机之间的重要组件。它从Arduino接收信号,并为电机提供必要的电源和控制。
  • Arduino Board: The brain of the car. It reads inputs from sensors and user commands and controls the motors accordingly.
    Arduino板:汽车的大脑。它从传感器和用户命令中读取输入,并相应地控制电机。
  • Power Source: The 2WD car requires a power source, usually batteries and a battery holder, to power the motors and the Arduino board.
    电源:2WD汽车需要电源,通常是电池和电池座,为电机和Arduino板供电。
  • Wireless receiver: an infrared, Bluetooth or WiFi module for wireless communication with a remote control or smartphone.
    无线接收器:用于与遥控器或智能手机进行无线通信的红外线、蓝牙或 WiFi 模块。
  • Optional Components: Depending on your project’s complexity, you may add various optional components like sensors (e.g., ultrasonic sensors for obstacle avoidance, line-following sensors), and more.
    可选组件:根据项目的复杂程度,您可以添加各种可选组件,如传感器(例如,用于避障的超声波传感器、线路跟踪传感器)等。

In this tutorial, to make it simple, we will use:
在本教程中,为了简单起见,我们将使用:

  • 2WD Car kit (including Chassis, wheels, motors, battery holder)
    2WD汽车套件(包括底盘,车轮,电机,电池座)
  • L298N Motor Driver L298N 电机驱动器
  • IR infrared kit (including IR controller and IR receiver)
    红外线套件(包括红外控制器和红外接收器)

Check the hardware list at the top of this page.
查看本页顶部的硬件列表。

How It Works 它是如何工作的

  • Arduino connects to the DC motors of the robot car through L298N motor driver module.
    Arduino通过L298N电机驱动模块连接到机器人汽车的直流电机。
  • Arduino connects to an IR receiver.
    Arduino连接到红外接收器。
  • The battery powers Arduino, DC motors, motor driver, and IR receiver.
    电池为 Arduino、直流电机、电机驱动器和 IR 接收器供电。
  • Users press the UP/DOWN/LEFT/RIGHT/OK keys on the IR remote controller.
    用户按 IR 遥控器上的 UP/DOWN/LEFT/RIGHT/OK 键。
  • Arduino receives the UP/DOWN/LEFT/RIGHT/OK commands through the IR receiver.
    Arduino通过IR接收器接收UP/DOWN/LEFT/RIGHT/OK命令。
  • Arduino controls the car to move FORWARD/BACKWARD/LEFT/RIGHT/STOP by controlling the DC motor via the motor driver.
    Arduino通过电机驱动器控制直流电机,控制汽车向前/向后/向左/向右/停止。

Wiring Diagram 接线图

请添加图片描述

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

Usually, we need two power sources:
通常,我们需要两个电源:

  • One power source for the motor (indirectly via L298N module).
    电机的一个电源(通过 L298N 模块间接)。
  • The other power source for the Arduino board, the L298N module, the IR receiver.
    Arduino板的另一个电源,L298N模块,IR接收器。

However, there’s a way to use just one power source for everything. You can use four 1.5V batteries (totaling 6V) for this purpose. Here’s how we can do it:
但是,有一种方法可以只使用一个电源来处理所有事情。为此,您可以使用四节 1.5V 电池(总计 6V)。以下是我们的方法:

  • Connect the batteries to the L298N module by following the above diagram.
    按照上图将电池连接到 L298N 模块。
  • Remove two small connectors (jumpers) that link the ENA and ENB pins to 5 volts on the L298N module.
    取下连接 L298N 模块上 ENA 和 ENB 引脚与 5 伏电压的两个小连接器(跳线)。
  • Put a special connector (jumper) labeled 5VEN, marked with a yellow circle on the above diagram.
    在上图上放置一个标有 5VEN 的特殊连接器(跳线),并用黄色圆圈标记。
  • Connect the 12V pin on the screw terminal of the L298N module to the Vin pin on the Arduino board. This powers the Arduino directly from battery (6V in total).
    将 L298N 模块螺丝端子上的 12V 引脚连接到 Arduino 电路板上的 Vin 引脚。这样 Arduino 就能直接从电池供电(共 6V)。

Please note that during the development period, when you need to connect the USB cable to the Arduino for programming, you MUST disconnect the power between the battery and the Arduino board by remove a wiring from Vin pin. The Arduino board should not be powered by two power sources simultaneously.
请注意,在开发期间,当您需要将 USB 电缆连接到 Arduino 进行编程时,您必须通过从 Vin 引脚上拔下接线来断开电池和 Arduino 板之间的电源。Arduino板不应同时由两个电源供电。

Arduino Code Arduino代码

/*

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

#include <DIYables_IRcontroller.h>  // DIYables_IRcontroller library

#define IR_RECEIVER_PIN 9  // The Arduino pin connected to IR receiver
#define ENA_PIN 7          // The Arduino pin connected to the ENA 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
#define IN3_PIN 4          // The Arduino pin connected to the IN3 pin L298N
#define IN4_PIN 3          // The Arduino pin connected to the IN4 pin L298N
#define ENB_PIN 2          // The Arduino pin connected to the ENB pin L298N

DIYables_IRcontroller_17 irController(IR_RECEIVER_PIN, 200);  // debounce time is 200ms

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

  pinMode(ENA_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);
  pinMode(IN3_PIN, OUTPUT);
  pinMode(IN4_PIN, OUTPUT);
  pinMode(ENB_PIN, OUTPUT);

  digitalWrite(ENA_PIN, HIGH);  // set full speed
  digitalWrite(ENB_PIN, HIGH);  // set full speed
}

void loop() {
  Key17 key = irController.getKey();
  if (key != Key17::NONE) {
    switch (key) {
      case Key17::KEY_UP:
        Serial.println("MOVING FORWARD");
        CAR_moveForward();
        break;

​      case Key17::KEY_DOWN:
​        Serial.println("MOVING BACKWARD");
​        CAR_moveBackward();
​        break;

​      case Key17::KEY_LEFT:
​        Serial.println("TURNING LEFT");
​        CAR_turnLeft();
​        break;

​      case Key17::KEY_RIGHT:
​        Serial.println("TURNING RIGHT");
​        CAR_turnRight();
​        break;

​      case Key17::KEY_OK:
​        Serial.println("STOP");
​        CAR_stop();
​        break;

​      default:
​        Serial.println("WARNING: unused key:");
​        break;
​    }
  }
}

vvoid CAR_moveForward() {
  digitalWrite(IN1_PIN, HIGH);
  digitalWrite(IN2_PIN, LOW);
  digitalWrite(IN3_PIN, HIGH);
  digitalWrite(IN4_PIN, LOW);
}

void CAR_moveBackward() {
  digitalWrite(IN1_PIN, LOW);
  digitalWrite(IN2_PIN, HIGH);
  digitalWrite(IN3_PIN, LOW);
  digitalWrite(IN4_PIN, HIGH);
}

void CAR_turnLeft() {
  digitalWrite(IN1_PIN, HIGH);
  digitalWrite(IN2_PIN, LOW);
  digitalWrite(IN3_PIN, LOW);
  digitalWrite(IN4_PIN, LOW);
}

void CAR_turnRight() {
  digitalWrite(IN1_PIN, LOW);
  digitalWrite(IN2_PIN, LOW);
  digitalWrite(IN3_PIN, HIGH);
  digitalWrite(IN4_PIN, LOW);
}

void CAR_stop() {
  digitalWrite(IN1_PIN, LOW);
  digitalWrite(IN2_PIN, LOW);
  digitalWrite(IN3_PIN, LOW);
  digitalWrite(IN4_PIN, LOW);
}
Quick Steps 快速步骤
  • Install DIYables_IRcontroller library on Arduino IDE by following this instruction
    按照以下说明在Arduino IDE上安装DIYables_IRcontroller库

  • Do the wiring as the diagram shown above.
    如上图所示进行接线。

  • Disconnect the wire from the Vin pin on the Arduino because we will power Arduino via USB cable when uploading code.
    断开 Arduino 上的 Vin 引脚的电线,因为我们将在上传代码时通过 USB 电缆为 Arduino 供电。

  • Flip the car upside down so that the wheels are on top.
    将汽车倒置,使车轮在上面。

  • Connect the Arduino to your computer using the USB cable.
    使用 USB 电缆将 Arduino 连接到您的计算机。

  • Copy the provided code and open it in the Arduino IDE.
    复制提供的代码并在Arduino IDE中打开它。

  • Click the Upload button in the Arduino IDE to transfer the code to the Arduino.
    单击Arduino IDE中的“上传”按钮,将代码传输到Arduino。

  • Use the IR remote controller to make the car go forward, backward, left, right, or stop.
    使用红外遥控器使汽车前进、后退、向左、向右或停止。

  • Check if the wheels move correctly according to your commands.
    根据您的命令检查车轮是否正确移动。

  • If the wheels move the wrong way, swap the wires of the DC motor on the L298N module.
    如果车轮移动方向错误,请更换 L298N 模块上直流电机的电线。

  • You can also see the results on the Serial Monitor in the Arduino IDE.
    您还可以在Arduino IDE的串行监视器上查看结果。

  • If everything is working fine, disconnect the USB cable from the Arduino, and connect back the wire to Vin pin to power the Arduino from the battery.
    如果一切正常,请断开 USB 电缆与 Arduino 的连接,然后将电线连接回 Vin 引脚以从电池为 Arduino 供电。

  • Flip the car back to its normal position with the wheels on the ground.
    将汽车翻转回正常位置,车轮放在地面上。

  • Have fun controlling the car!
    玩得开心控制汽车!

Code Explanation 代码说明

Read the line-by-line explanation in comment lines of code!
阅读代码注释行中的逐行说明!

You can learn more about the code by checking the following tutorials:
可以通过查看以下教程来了解有关代码的详细信息:

You can extend this project by:
您可以通过以下方式扩展此项目:

  • Adding obstacle avoidance sensors to immidilately stop the car if an obstacle is detected.
    添加避障传感器,以便在检测到障碍物时立即停止汽车。
  • Adding function to control the speed of car (see Arduino - DC motor tutorial). The provided code controls car with full speed.
    增加控制车速的功能(参见Arduino - DC电机教程)。提供的代码控制全速行驶的汽车。

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

另一个代码

参考:

  1. https://create.arduino.cc/projecthub/ryanchan/how-to-use-the-l298n-motor-driver-b124c5
  2. https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/
#include "IRremote.h"
#define IRRECV_1 0xFFA25D
#define IRRECV_2 0xFF629D
#define IRRECV_3 0xFFE21D
#define IRRECV_4 0xFF22DD
#define IRRECV_5 0xFF02FD
#define IRRECV_6 0xFFC23D
#define IRRECV_7 0xFFA25D
#define IRRECV_8 0xFFE01F
#define IRRECV_9 0xFF906F
#define IRRECV_0 0xFF9867
#define IRRECV_Hashtag 0xFFB04F    // #号
#define IRRECV_Asterisk 0xFF6897   //  *号
#define IRRECV_OK    0xFF38C7
#define IRRECV_UP    0xFF18E7
#define IRRECV_LEFT  0xFF10EF
#define IRRECV_RIGHT 0xFF5AA5
#define IRRECV_DOWN  0xFF4AB5

#define BAUD 115200    //串口波特率
#define BOARDLEDPIN 13 //板载led接口

#define L298ENA 9
#define L298ENB 10
#define L298IN1 7
#define L298IN2 8
#define L298IN3 5
#define L298IN4 6

#define highSPEED 90
#define lowSPEED 70
#define RECV_PIN 11

IRrecv irrecv(RECV_PIN);
decode_results IRResults;
unsigned int lastIRKey = 0;
bool running = false;

void alertBlink(unsigned int t, unsigned long deltms);
void (*resetFunc)(void) = 0; //控制主控板自动重启
void setup()
{
  Serial.begin(BAUD);
  irrecv.enableIRIn();
  pinMode(BOARDLEDPIN,INPUT);
  // wait for Leonardo enumeration, others continue immediately
  while (!Serial)
    ;
  motorStop();
  alertBlink(10, 500);
}

void loop()
{
  if (irrecv.decode(&IRResults))
  {
    Serial.println(IRResults.value,HEX);
    switch (IRResults.value)
    {
    case IRRECV_OK:
      lastIRKey = 20;
      running = !running;
      break;
    case IRRECV_LEFT:
      lastIRKey = 21;
      break;
    case IRRECV_UP:
      lastIRKey = 22;
      break;
    case IRRECV_RIGHT:
      lastIRKey = 23;
      break;
    case IRRECV_DOWN:
      lastIRKey = 24;
      break;
    default:
      break;
    }
    irrecv.resume();    // Receive the next value
  }
  Serial.println(String("lastIRKey is :")+lastIRKey);
  motorMotion(lastIRKey);
}

/**
 * 功能:让板载led以一定的时间间隔闪烁
 * t:闪烁次数,当t=0时,不闪烁,一直亮。
 * deltms:闪烁间隔的毫秒数。
 */
void alertBlink(unsigned int t, unsigned long deltms)
{
  if (t == 0)
  {
    digitalWrite(BOARDLEDPIN, HIGH);
  }
  for (int i = 0; i < t; i++)
  {
    digitalWrite(BOARDLEDPIN, HIGH);
    delay(deltms);
    digitalWrite(BOARDLEDPIN, LOW);
    delay(deltms);
  }
}
void motorMotion(unsigned int lastIRKey)
{
  switch (lastIRKey)
  {
  case 20:
    if (running)
    {
      motorForward();
    }
    else
    {
      motorStop();
    }
    break;
  case 21:
    if (running)
      motorTurnLeft();
    break;
  case 22:
    if (running)
      motorForward();
    break;
  case 23:
    if (running)
      motorTurnRight();
    break;
  case 24:
    if (running)
      motorBack();
    break;

  default:
    break;
  }
}
void motorForward()
{
  analogWrite(L298ENA, highSPEED);
  digitalWrite(L298IN1, HIGH);
  digitalWrite(L298IN2, LOW);
  analogWrite(L298ENB, highSPEED);
  digitalWrite(L298IN3, HIGH);
  digitalWrite(L298IN4, LOW);
}

void motorBack()
{
  analogWrite(L298ENA, highSPEED);
  digitalWrite(L298IN1, LOW);
  digitalWrite(L298IN2, HIGH);
  analogWrite(L298ENB, highSPEED);
  digitalWrite(L298IN3, LOW);
  digitalWrite(L298IN4, HIGH);
}

void motorTurnLeft()
{
  analogWrite(L298ENA, highSPEED);
  digitalWrite(L298IN1, HIGH);
  digitalWrite(L298IN2, LOW);
  analogWrite(L298ENB, lowSPEED);
  digitalWrite(L298IN3, HIGH);
  digitalWrite(L298IN4, LOW);

}

void motorTurnRight()
{
  analogWrite(L298ENA, lowSPEED);
  digitalWrite(L298IN1, HIGH);
  digitalWrite(L298IN2, LOW);
  analogWrite(L298ENB, highSPEED);
  digitalWrite(L298IN3, HIGH);
  digitalWrite(L298IN4, LOW);
}

void motorStop()
{
  analogWrite(L298ENA, 0);
  analogWrite(L298ENB, 0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蔚蓝慕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值