扫描 I2C 地址
使用扫描器查找总线上的每个设备
扫描 I2C 地址 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Find What Is Connected
Before coding a sensor you need its address. An I2C scanner sketch finds every device on the bus for you. 🔍
Why Addresses Matter
Two devices with the same address will clash on the bus. Scanning first tells you what is there and warns of conflicts.
The Datasheet Can Lie
A datasheet lists a default address, but solder jumpers can change it. Scanning gives you the real, current value every time.
How a Scan Works
The scanner tries every address from 1 to 127 and checks who answers. Each reply means a live device sits at that spot.
Start a Transmission
To test one address you open a message with beginTransmission. It targets the device without sending real data yet.
Wire.beginTransmission(addr);End and Read the Result
Calling endTransmission returns 0 if a device acknowledged. Any other number means nobody answered at that address.
byte error = Wire.endTransmission();Loop Through Addresses
Wrap the test in a loop that walks from 1 to 127, checking each address once and noting which ones reply.
for (byte a = 1; a < 127; a++) {
Wire.beginTransmission(a);
}Print Found Devices
When a device acknowledges, print its address to the Serial Monitor so you can copy it straight into your real sketch.
Serial.print("Found device at 0x");
Serial.println(a, HEX);Read Addresses in Hex
I2C addresses are usually shown in hex, like 0x27 or 0x3C. Printing with HEX matches the format libraries expect.
No Devices Found?
An empty scan usually means missing pull-ups, swapped SDA and SCL, or no power. Check the wiring and try again.
Your First Step Every Time
Make scanning a habit. It confirms wiring works and hands you the exact address before you write any sensor code. 🛠️
Quick Check
What does endTransmission returning 0 mean during a scan?
Recap
You can now scan the bus, walk addresses 1 to 127, and print the hex address of every device before writing real code. ✅
常见问题解答
「扫描 I2C 地址」课时是免费的吗?
是的 — 「扫描 I2C 地址」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。
「扫描 I2C 地址」这节课中我会学到什么?
使用扫描器查找总线上的每个设备 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Arduino & IoT Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「扫描 I2C 地址」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?
能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- I2C 如何通过两根线通信
- 扫描 I2C 地址
- SPI 如何快速传输数据
- 让多个设备共用一条总线