This tutorial will guide you on how to use the Arduino development board and sensors to make a smart obstacle avoidance trolley. We will start with basic circuit design and gradually learn how to use Arduino programming to achieve automatic obstacle avoidance. Whether you are a novice or an expert looking to learn Arduino technology in depth, this tutorial will provide you with the necessary guidance and support.
Make Arduino obstacle avoidance car tutorial.
Introduction.
In today's era of rapid technological development, automation and intelligent equipment are gradually penetrating into all aspects of our lives. Among them, as a small robot integrating mechanical, electronic and computer technology, the smart car is favored by the majority of DIY enthusiasts and the field of science and technology education.
This article will detail how to use the Arduino development board and sensors to make a smart car that can automatically avoid obstacles.
Whether you are a beginner or an electronic enthusiast with some experience, through the study of this tutorial, you will master the key techniques and methods of making obstacle avoidance trolleys.
Required materials.
1. Arduino UNO development board
2. L298N motor drive module
3. Ultrasonic sensor (e.g. HC-SR04)
4. DC motors (two)
5. Wheels and wheel brackets
6. Battery case and battery
7. Several DuPont lines
8. Bread board and jumper
9. Screwdrivers, screws and other tools
Circuit Design.
\n#1. Motor connection.
First, we need to connect two DC motors to the L298N motor drive module. L298N is a commonly used H-bridge motor drive chip, which can control the forward and reverse rotation of two DC motors.
The specific connection method is as follows:
-Connect the two wires of motor 1 to the OUT 1 and OUT 2 ports of L298N respectively.
-Connect the two wires of the motor 2 to the OUT 3 and OUT 4 ports of the L298N respectively.
Next, connect the ENA and ENB ports of the L298N to Arduino's PWM output pins (such as D3 and D9), respectively, for controlling the speed of the motor.
The IN1, IN2, IN3, and IN4 ports are respectively connected to Arduino's digital I/O pins (such as D2, D7, D5, and D10), which are used to control the direction of the motor.
\n#
2. Ultrasonic sensor connection.
Ultrasonic sensors are used to detect the distance of obstacles ahead. We connect the sensor's VCC pin to the 5V power supply, the GND pin to the ground, the Trig (trigger) pin to Arduino's digital I/O pin (such as D12), and the Echo (echo) pin to another digital I/O pin (such as D11).
\n#
3. Power connection.
The positive and negative poles of the battery box are respectively connected to the bread plate, and power is supplied to the Arduino and L298N motor drive modules through the DuPont cable. Once we've made sure all the connections are correct, we're ready to start programming.
Program.
\n#1. Introduce library files.
#include // 引入NewPing库,用于处理超声波传感器数据
#define TRIG_PIN 12 // Trig引脚连接到D12
#define ECHO_PIN 11 // Echo引脚连接到D11
#define MAX_DISTANCE 200 // 最大检测距离(单位:厘米)
\n#2. Initialize variables and objects.
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // 创建NewPing对象
int motorSpeed = 255; // 电机速度初始值
bool obstacleDetected = false; // 障碍物检测标志位
\n#3. The setup function.
void setup() {
pinMode(3, OUTPUT); // IN1引脚设为输出模式
pinMode(7, OUTPUT); // IN2引脚设为输出模式
pinMode(5, OUTPUT); // ENA引脚设为输出模式
pinMode(10, OUTPUT); // IN3引脚设为输出模式
pinMode(2, OUTPUT); // IN4引脚设为输出模式
pinMode(9, OUTPUT); // ENB引脚设为输出模式
}
\n#4. Loop function.
void loop() {
// 读取超声波传感器数据
delay(50); // 短暂延时以稳定信号
long distance = sonar.ping_cm(); // 获取距离数据
// 根据距离判断是否有障碍物
if (distance < 30) { // 如果距离小于30厘米,则认为有障碍物
obstacleDetected = true;
} else {
obstacleDetected = false;
}
// 根据障碍物检测结果控制电机方向
if (obstacleDetected) {
// 后退并左转避障
digitalWrite(3, HIGH);
digitalWrite(7, LOW);
digitalWrite(5, LOW);
digitalWrite(10, HIGH);
analogWrite(9, motorSpeed);
analogWrite(2, motorSpeed);
delay(1000); // 后退一秒
// 停止电机
digitalWrite(3, LOW);
digitalWrite(7, LOW);
digitalWrite(5, LOW);
digitalWrite(10, LOW);
analogWrite(9, 0);
analogWrite(2, 0);
// 左转
digitalWrite(3, LOW);
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
digitalWrite(10, LOW);
analogWrite(9, motorSpeed);
analogWrite(2, motorSpeed);
delay(1000); // 左转一秒
// 恢复前进状态
digitalWrite(3, HIGH);
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
digitalWrite(10, LOW);
analogWrite(9, motorSpeed);
analogWrite(2, motorSpeed);
} else {
// 无障碍物时正常前进
digitalWrite(3, HIGH);
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
digitalWrite(10, LOW);
analogWrite(9, motorSpeed);
analogWrite(2, motorSpeed);
}
}
Summarize.
Through the above steps, we successfully produced an Arduino-based obstacle avoidance trolley. This car can detect obstacles in front of it through ultrasonic sensors, and automatically adjust the driving direction according to the detection results to achieve obstacle avoidance function.
I hope this tutorial can help you better understand and master the use of Arduino technology in practical applications.
If you have any questions or suggestions about this project, please feel free to ask questions and communicate!