使用继电器切换市电设备
通过数字引脚切换灯或风扇
使用继电器切换市电设备 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Controlling Big Things
Sometimes you want your Arduino to switch a lamp or a fan, not just an LED. For mains-powered gear you reach for a relay. 💡
A Relay Is a Switch
A relay is an electrically controlled switch. A small coil current pulls a metal contact closed, connecting a totally separate, much higher-power circuit.
Electrical Isolation
The coil and the contacts never touch electrically. This isolation is why a 5V Arduino can safely command a 220V device without sharing wires.
Use a Relay Module
Grab a ready-made relay module. It already has the driving transistor, a flyback diode, and screw terminals, so you just supply a control signal.
The Three Control Wires
A typical module has three pins: VCC, GND, and a signal input. Connect signal to any Arduino digital pin to control the relay.
Driving It in Code
Treat the relay like an LED. Set the pin HIGH or LOW to close or open the contact, depending on how your module is wired.
int relay = 7;
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH); // switch onActive-Low Surprise
Many modules are active-low: LOW turns the load on and HIGH turns it off. Test yours so the logic in your sketch matches reality.
NO and NC Terminals
The output side offers NO and NC contacts. Normally Open stays off until triggered; Normally Closed is on until the relay switches.
Wire the Load Through COM
Run one mains wire to the COM terminal and the other to NO. When the relay clicks, the circuit closes and your device powers up.
Respect Mains Safety
Mains voltage can kill. Never touch live terminals, keep the high-voltage side enclosed, and have an adult or pro check anything wired to the wall. ⚠️
Hear the Click
When the relay switches you hear a satisfying click as the coil snaps the contact. That sound confirms your code reached the real world.
Quick Check
What is the main reason a relay is used to switch mains-powered devices from an Arduino?
Recap
A relay is an isolated switch: a small signal closes contacts that power big loads. Use a module, mind active-low logic, and respect mains safety. 🚀
常见问题解答
「使用继电器切换市电设备」课时是免费的吗?
是的 — 「使用继电器切换市电设备」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。
「使用继电器切换市电设备」这节课中我会学到什么?
通过数字引脚切换灯或风扇 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Arduino & IoT Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「使用继电器切换市电设备」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?
能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 为什么需要驱动器
- 使用 H 桥控制速度与方向
- 使用继电器切换市电设备
- 防止反电动势尖峰