پروتکل I۲C و ارتباط سنسورها در رباتیک: گذرگاه دو سیمه حسگرها / I۲C Protocol and Sensor Communication in Robotics: The Two-Wire Sensor Bus
پروتکل I2C (Inter-Integrated Circuit) یکی از پرکاربردترین گذرگاه های ارتباطی در رباتیک است که برای اتصال حسگرها و ماژول ها به میکروکنترلر طراحی شده است. امروز در بیشتر بردهای Arduino و ESP32 به صورت سخت افزاری پشتیبانی می شود و بزرگ ترین مزیت آن سادگی سیم کشی است: کل گذرگاه فقط با دو سیم کار می کند.
I2C گذرگاهی سریال، سنکرون و نیم دوطرفه است. دو خط آن SDA برای داده و SCL برای ساعت نام دارند؛ هر دو open-drain هستند و با مقاومت pull-up به تغذیه متصل می شوند. هر تراکنش با شرط START آغاز و با STOP پایان می یابد. هر دستگاه آدرسی یکتا دارد؛ آدرس دهی معمولا ۷ بیتی است و تا ۱۲۸ آدرس را پشتیبانی می کند. پس از هر بایت ۸ بیتی، گیرنده با یک بیت ACK دریافت را تایید می کند.
سرعت I2C در چند حالت استاندارد تعریف شده است: استاندارد ۱۰۰ کیلوبیت بر ثانیه، سریع ۴۰۰ کیلوبیت بر ثانیه، سریع پلاس ۱ مگابیت بر ثانیه و پرسرعت ۳٫۴ مگابیت بر ثانیه. ظرفیت گذرگاه حدود ۴۰۰ پیکوفاراد و مقاومت pull-up معمولا ۱ تا ۱۰ کیلواهم است؛ این دو عامل زمان صعود سیگنال و در نتیجه حداکثر سرعت و طول سیم را محدود می کنند.
با همین دو سیم می توان چندین حسگر را هم زمان به یک میکروکنترلر وصل کرد: IMUهایی مانند MPU9250، حسگرهای فاصله و دما، حسگر فشار و نمایشگرهای OLED همگی از I2C استفاده می کنند. در Arduino کتابخانه Wire و در ESP32 دو کنترلر سخت افزاری I2C این ارتباط را مدیریت می کنند.
اما I2C محدودیت هایی هم دارد: نیم دوطرفه بودن و سرعت نسبتا پایین آن را برای انتقال حجم زیاد داده مناسب نمی کند؛ فاصله ارتباط کوتاه و درون بردی است و گذرگاه به نویز و ظرفیت سیم حساس است. تداخل آدرس دو دستگاه با پایه های انتخاب آدرس، مالتی پلکسر یا تغییر آدرس حل می شود و کشش طولانی ساعت می تواند گذرگاه را قفل کند. کنترلر ESP32 در حالت اسلیو از clock stretching پشتیبانی نمی کند.
جایگزین ها عبارت اند از SPI سریع تر و تمام دوطرفه با چهار سیم، UART ساده و نقطه به نقطه، و CAN با سیگنالینگ دیفرانسیلی که در برابر نویز مقاوم است و برای فاصله های بیشتر مناسب تر است. I2C برای شبکه حسگرهای کم سرعت روی یک برد، همچنان انتخاب اول بسیاری از طراحان ربات است.
The I2C (Inter-Integrated Circuit) protocol is one of the most widely used communication buses in robotics, designed to connect sensors and modules to a microcontroller. It is now supported in hardware on most Arduino and ESP32 boards, and its greatest advantage is wiring simplicity: the entire bus works with just two wires.
I2C is a synchronous, half-duplex serial bus. Its two lines are SDA for data and SCL for the clock; both are open-drain and connect to the supply through pull-up resistors. Each transaction begins with a START condition and ends with a STOP condition. Every device has a unique address, usually 7-bit, supporting up to 128 addresses. After each 8-bit byte, the receiver confirms with an ACK bit.
I2C speed is defined in standard modes: Standard at 100 kbit/s, Fast at 400 kbit/s, Fast-mode Plus at 1 Mbit/s, and High-speed at 3.4 Mbit/s. In practice, most robotics applications use 100 or 400 kHz. Bus capacitance is about 400 pF and pull-up resistors are typically 1 to 10 kΩ; these limit the signal rise time and therefore the maximum speed and wire length.
With just two wires, several sensors can connect to one microcontroller at once: IMUs such as the MPU9250, distance and temperature sensors, pressure sensors, and OLED displays all use I2C. On Arduino, the Wire library manages this communication, and the ESP32 has two hardware I2C controllers.
I2C has limitations, too. Being half-duplex and relatively slow makes it unsuitable for large data volumes; its range is short and on-board, and the bus is sensitive to noise and wire capacitance. Address conflicts are solved with address-select pins, a multiplexer, or changing the address, and prolonged clock stretching can lock the bus. The ESP32 controller does not support clock stretching in slave mode.
Alternatives include SPI, faster and full-duplex but needing four wires; UART, simple and point-to-point; and CAN, whose differential signalling resists noise and suits longer distances. The choice depends on speed, distance, and device count, and for a network of low-speed sensors on one board, I2C remains the first choice of many robot designers.
کلیدواژه ها: پروتکل I2C، گذرگاه سریال، حسگر، میکروکنترلر، Arduino، ESP32، pull-up، clock stretching؛ I2C protocol, serial bus, sensor, microcontroller, Arduino, ESP32, pull-up, clock stretching
منابع:
- Texas Instruments, "A Basic Guide to I2C", https://www.ti.com/lit/an/sbaa565/sbaa565.pdf
- NXP Semiconductors, "I2C-bus specification and user manual (UM10204)", https://www.nxp.com/docs/en/user-guide
- Espressif, "Inter-Integrated Circuit (I2C) — ESP32", ESP-IDF Programming Guide v6.1, https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html
- Arduino Documentation, "Inter-Integrated Circuit (I2C) Protocol" (Wire library), https://docs.arduino.cc/learn/communication/wire/
سروو موتور و کنترل زاویه ای در ربات ها: از سیگنال PWM تا بازخورد موقعیت / Servo Motors and Angular Position Control in Robots: From PWM Signals to Position Feedback
بینایی ماشین در ربات ها با OpenCV: از کالیبراسیون دوربین تا مارکر ArUco / Machine Vision in Robots with OpenCV: From Camera Calibration to ArUco Markers