0Pricing
Arduino & IoT Academy · 课时

按钮为什么会“抖动”

了解触点产生的错误触发

按钮为什么会“抖动” 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

One Press, Many Signals

You press a button once, but your Arduino may see several presses. This sneaky problem is called bounce, and it trips up almost every beginner. 😅

Inside a Push Button

A button is just two metal contacts that touch when you press. They feel solid to your finger, but at the microsecond level they are anything but.

Metal That Chatters

When the contacts meet, they physically vibrate for a few milliseconds before settling. Each tiny touch and break is a separate electrical event.

What the Pin Sees

During that vibration, the pin reads a burst of rapid HIGH-LOW flips instead of one clean change. Your code can count every flip as a press.

Why the Loop Catches It

Your loop() runs millions of times per second, far faster than the bounce settles. So it samples the noise again and again.

A Counter Gone Wild

Picture a counter that adds one per press. One real tap can jump it by three or four because of multiple false triggers.

if (digitalRead(BTN) == LOW) {
  count++;  // may fire several times per tap
}

It Happens on Release Too

Bounce strikes when you let go as well, not just when you press. Both edges of the signal can stutter and confuse your logic.

How Long It Lasts

Most bounce settles within about 5 to 20 milliseconds. It is brief, but to a fast microcontroller that is an eternity of noise.

Spot It Yourself

Print a message every time the pin goes LOW and tap once. Seeing several lines in the Serial Monitor proves bounce is real.

if (digitalRead(BTN) == LOW)
  Serial.println("pressed");

Hardware or Software Fix

You can tame bounce with a small capacitor in the circuit, or with timing logic in code. Software is cheaper and far more common.

The Core Idea

The fix is to wait briefly after the first change and ignore the rest. Confirm the button is truly stable before you act on it.

Quick Check

Let's confirm what causes a button to bounce.

Recap: Bounce Basics

You learned that a button's contacts bounce, sending noisy flips that fool fast code. Next you'll silence that noise with simple timing. 🎯

常见问题解答

「按钮为什么会“抖动”」课时是免费的吗?

是的 — 「按钮为什么会“抖动”」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。

「按钮为什么会“抖动”」这节课中我会学到什么?

了解触点产生的错误触发 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Arduino & IoT Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「按钮为什么会“抖动”」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?

能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 按钮为什么会“抖动”
  2. 使用 millis() 消抖
  3. 将数值存入 EEPROM
  4. 复位后保留计数器数值
← 返回 Arduino & IoT Academy