使用 H 桥控制速度与方向
使用 L298N 模块控制 DC 电机
使用 H 桥控制速度与方向 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Spinning Both Ways
A plain transistor runs a motor in one direction. To make wheels go forward and back, you need a smarter circuit called an H-bridge. 🔁
Why the H Shape
An H-bridge uses four switches arranged like the letter H. The motor sits in the middle, and flipping pairs of switches reverses the current through it.
Meet the L298N
The L298N is a popular H-bridge module. It can drive two DC motors and handles enough current for hobby robots and small carts.
The Direction Pins
Each motor on the L298N has two direction inputs, called IN1 and IN2. Their combination decides which way the motor turns or whether it stops.
int IN1 = 7, IN2 = 8;
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);Forward and Reverse
Set IN1 HIGH and IN2 LOW to spin one way; swap them to spin the other. The motor simply follows whichever input is higher. ➡️
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW); // forwardReversing the Spin
Flip the two inputs and the current through the motor reverses. This is the magic the H-bridge gives you that a lone transistor cannot.
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH); // reverseStopping the Motor
Set both direction inputs the same, like LOW and LOW, and the motor coasts to a halt because no net current flows through it.
The Enable Pin Controls Speed
Each motor also has an enable pin. Feed it a PWM signal and the duty cycle sets how fast the motor spins, just like dimming an LED.
Speed with analogWrite
Send PWM to the enable pin with analogWrite. A value of 255 is full speed and lower numbers slow the motor down smoothly.
int ENA = 9;
analogWrite(ENA, 180); // about 70% speedPowering the Module
The L298N takes its own motor voltage on a separate terminal. Always tie its ground to the Arduino ground so signals stay in sync.
Two Motors, Full Control
With direction inputs plus PWM enable pins, one H-bridge lets you steer a two-wheel robot at any speed and in any direction. 🤖
Quick Check
On an L298N, what does the enable pin control?
Recap
An H-bridge like the L298N reverses current to change direction, while a PWM enable pin sets the speed. Share ground and you control motors fully. 🚀
常见问题解答
「使用 H 桥控制速度与方向」课时是免费的吗?
是的 — 「使用 H 桥控制速度与方向」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。
「使用 H 桥控制速度与方向」这节课中我会学到什么?
使用 L298N 模块控制 DC 电机 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Arduino & IoT Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 H 桥控制速度与方向」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?
能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 为什么需要驱动器
- 使用 H 桥控制速度与方向
- 使用继电器切换市电设备
- 防止反电动势尖峰