0Pricing
Arduino & IoT Academy · บทเรียน

สแกนหาที่อยู่ I2C

ค้นหาอุปกรณ์ทุกตัวบนบัสด้วยเครื่องสแกน

สแกนหาที่อยู่ I2C เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สแกนหาที่อยู่ I2C”

ค้นหาอุปกรณ์ทุกตัวบนบัสด้วยเครื่องสแกน คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “สแกนหาที่อยู่ I2C” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม

ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. I2C สื่อสารผ่านสายสองเส้นอย่างไร
  2. สแกนหาที่อยู่ I2C
  3. SPI ส่งข้อมูลได้รวดเร็วอย่างไร
  4. ใช้อุปกรณ์หลายตัวบนบัสเดียว
← กลับไปที่ Arduino & IoT Academy