Labs / Bipes + MicroPython / 5 Fundamentals of Communication Peripherals

5.2 I2C Communication: One Teacher Leading Multiple Students

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

I2C is another commonly used communication method, which is particularly suitable for one-to-many scenarios. You can think of it like this: a teacher communicates with multiple students at the same time in class without having to pull a separate line for each student, which is extremely efficient!

1. The Lively "Mini Classroom"

1.1 Questions about daily life scenarios

Think about this: How can a teacher talk to each student individually at the same time in one classroom without getting into chaos?

This is the core concept of I2C communication: the development board (the teacher) and multiple peripherals (the students) share a single "communication bus", eliminating the need for separate wiring for each peripheral. The main controller can accurately access each slave device individually, which is both wiring-saving and highly efficient.

1.2 Understand I2C in a Nutshell

The development board acts as the "teacher", and the multiple peripherals serve as the "students";with just two wires, you can set up the "classroom". The teacher can either call on a specific student to speak individually, or send announcements to the entire class at once.

1.3 Interactive Mini-Discussion

What devices in daily life use this "one-to-many chat" method?

Reference answer: The sensor cluster in a smartwatch, the multiple indicator lights on a router, and the combination of the OLED screen and temperature-humidity sensor on a development board — all these are examples of one main controller driving multiple peripherals.

2. Understand the "Classroom Rules" of I2C

Just as a classroom requires discipline, I2C communication also has 3 mandatory rules to follow — miss even one, and everything will descend into total chaos and loss of control!

2.1 SDA/SCL Lines

The core reason why I2C can save wires lies in these two "shared lines", which have clear division of labor and will not conflict with each other:

Analogy extension: If the teacher (development board) speaks too fast for the students (peripheral devices) to keep up, they will miss what is being said; the SCL line acts as a "metronome" — with each beat, one data bit is transmitted, and as long as both parties follow the rhythm strictly, even the most complex data will not be messed up.

2.2 Device Address

Every I2C peripheral has a unique "address"(similar to a student ID number), which is the key to enabling "one-to-one addressing" in I2C:

2.3 Interactive Mini Exercise

If two students have the same student ID (i. e., two I2C peripherals share the same address), will the teacher mistake one for the other?

Quick reminder: When building an I2C multi-device circuit, first check the default address of each peripheral to ensure there is no duplicate; if duplicates exist, adjust them using peripherals with modifiable addresses.

The core of I2C communication lies in " two wires, multiple devices, and address-based addressing ", which operates efficiently and orderly like a teacher leading students. It helps us solve the problem of "numerous peripherals but limited interfaces", making it the most commonly used communication method in comprehensive projects. You will definitely use it when building complex systems later, such as simultaneously monitoring temperature, humidity, and light while displaying the data. ~

3. Complete Analysis of I2C Building Blocks

5.2 I2C Communication: One Teacher Leading Multiple Students

3.1 Initialization Block (I2C.init) —— Set up the classroom and enable the chat mode between teachers and students

Young makers, I2C communication is just like the "teacher-student interaction" in a classroom. Before the communication starts, we need to set up the classroom and arrange the seats properly so that both the teacher and students can hear each other clearly. The initialization block is the first step to set up the classroom, which gets the I2C bus ready for data transmission.

5.2 I2C Communication: One Teacher Leading Multiple Students

Chinese meaning: Initialize the I2C bus, configure the SCL (clock line) and SDA (data line) pins of the main control board to prepare for I2C communication.

Core Features:

Example in daily life:

  1. Classroom setup: Arrange the desks, chairs and blackboard first so that teachers and students can have classes smoothly;
  2. Preparation for a conference call: First connect the line, turn on the microphone and speaker, so that the call can proceed smoothly;
  3. Set up the venue before the party: Prepare the lighting, sound system and seating so that guests can interact upon arrival.

Practical Example 1: Initialize the I2C Bus

Practical Example 2: Connecting Multiple I2C Devices After Initialization

Quick Reminder

  1. Initialize first before operating the device — the order is extremely important;
  2. The SCL/SDA pins must be correct; otherwise, the device cannot communicate normally.
  3. The I2C bus supports multiple devices, but their addresses must not be duplicated, otherwise a conflict will occur;
  4. Only needs to be initialized once, unless the device is restarted or the pin settings are modified.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of the I2C.init block? (Answer: Initialize the I2C bus, configure the SCL/SDA pins, and prepare for communication)
  2. Think about it: If you forget to initialize I2C, can the bus devices work properly? (Answer: No, communication cannot proceed.)
  3. Scenario-based Judgment: When connecting both an OLED and a temperature-humidity sensor at the same time, why is only one initialization required? (Answer: All devices on the same bus share the SCL/SDA pins, so the pins only need to be configured once.)
  4. Mini Challenge: Try to initialize I2C, connect an OLED display to show "Hello", and observe the display effect.

Summary:

  1. Initialization block (I2C.init): Set up the "classroom" for the I2C bus to enable communication between devices;
  2. Must be used before operating any I2C device, and ensure that the SCL/SDA pins are correctly connected;
  3. After initialization, the I2C bus can be connected to multiple devices simultaneously to transmit and receive data smoothly.

3.2 Turn off the block (I2C.deinit) — End the class and stop the communication between the teacher and students

Young makers, the I2C bus is just like a classroom in session: the initialization block sets up the "classroom" so that teachers and students can communicate smoothly. When the class is over or the I2C is not in use temporarily, we need to suspend the class and tidy up the classroom to avoid bus conflicts or resource waste. The shutdown block is designed for this step, which safely stops the I2C bus from working.

5.2 I2C Communication: One Teacher Leading Multiple Students

Chinese meaning: Shut down the I2C bus, release the SCL/SDA pins, so that the development board no longer communicates with I2C devices.

Core Features:

Example in daily life:

  1. Tidying up the classroom after class: Teachers and students put away their books, turn off the lights, and walk out of the classroom;
  2. Hang up the call: Release the line and free up resources once the call ends;
  3. End Meeting: Terminate the video conference and turn off the microphone and camera.

Practical Example 1: Turn off the I2C bus after class

Practical Example 2: Temporarily Switching I2C Pins

Quick Reminder

  1. Stop and then reinitialize to ensure safe pin switching;
  2. Turning off the bus will not delete data; it only stops communication.
  3. After the bus is shut down, all I2C devices will fail to respond, and re-initialization is required for subsequent communication;
  4. Develop good habits: Power off the I2C peripheral in time when it is not in use to prevent the bus from being occupied.

✨ Exclusive Mini Interaction

  1. Quick Response Quiz 1: What is the core function of the I2C.deinit block? (Answer: To shut down the I2C bus and release the SCL/SDA pins)
  2. Think about it: What will happen if you initialize a new pin without disabling the bus first? (Answer: Pin conflicts may occur, leading to communication failure.)
  3. Scenario Judgment: After the display finishes its operation, if you want to temporarily use the SCL/SDA pins to control other devices, what should you do first? (Answer: First use I2C.deinit to disable the bus)
  4. Small challenge: Try turning off the bus after the OLED display finishes outputting content, then reinitialize the display to show other content, and observe the result.

Summary:

  1. Deinitialize the block (I2C.deinit): End the "session" of I2C communication and release the bus and pins;
  2. It must be used when I2C is no longer in use or reinitialization is to be performed, so as to ensure safety and rational utilization of resources;
  3. After use, the bus device no longer responds and requires reinitialization to communicate properly.

3.3 Device Scanning Block (I2C.scan) —— Just like a teacher taking attendance to check which students (devices) are present in class

Young makers, the I2C bus is just like a lively classroom: the initialization block sets up the "classroom", and each I2C device is like a student sitting in the classroom. To confirm who is present and who is absent, we need to "call the roll". The device scanning block is the teacher's roll-calling tool that helps you check exactly which I2C devices on the bus have been properly connected.

Chinese meaning: Scan the I2C bus and return all detected device addresses (such as OLEDs, sensors, etc.) to let you know which devices are online.

Core Features:

Example in daily life:

  1. The teacher calls the roll to confirm which students are present and who is absent;
  2. Check meeting participants: The meeting software displays who is online and who is offline;
  3. Seat inventory check: Confirm that all equipment and items are in place before the event.

Practical Example 1: Scanning Devices on the Bus

Practical Example 2: Debugging Device Connection

Quick Reminder

  1. Must be used after I2C initialization, otherwise scanning will not be possible;
  2. The scanning result is a list of device addresses, not device names, which requires confirmation in combination with the documentation;
  3. Multiple scans are supported to check for newly added or removed devices;
  4. Debugging the bus is of great importance, as it helps confirm whether the device is online.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of the I2C.scan block? (Answer: Scan the I2C bus and list all online device addresses)
  2. Think about it: If a certain device address is not found in the scan results, what could be the possible reasons? (Answer: The device is not connected, there is a wiring error, an address conflict occurs, or the bus is not initialized.)
  3. Scenario Judgment: If you connect an OLED and a temperature-humidity sensor at the same time, and the scan result shows two addresses, what does this indicate? (Answer: Both devices are normally online.)
  4. Mini Challenge: After scanning the bus, display the device addresses on the OLED to see which devices are "in the classroom".

Summary:

  1. Device scanning block (I2C.scan): It works like a teacher taking attendance, to confirm which I2C devices are already connected;
  2. Must be used after initializing the I2C bus, and returns a list of device addresses;
  3. The scan results are used for debugging and confirming that the device is online, helping ensure smooth subsequent block-based operations.

3.4 Communication Start Block (I2C.start) —— Just like a teacher calling "class begins" to start interacting with students

Young makers, the I2C bus is just like a classroom: the initialization block sets up the classroom, and the device scanning block checks which "students" are online. Next, the teacher will officially start the class and tell the students "it's time to start interacting". The communication start block is the signal for the teacher to call "class begins", which puts the bus into data communication mode.

Chinese meaning: Send a start signal on the I2C bus to inform all devices that the bus is ready for data transmission.

Core Features:

Example in daily life:

  1. When the teacher calls out "Class begins", students know that they should listen attentively and answer questions.
  2. Before the meeting, the host announces the start: Participants know the meeting has officially begun;
  3. Race Start Command: Competitors shall not start the race until they hear the commands "Get set, go".

Practical Example 1: Starting Communication with the Device

Practical Example 2: Multi-device Communication

Quick Reminder

  1. It must be activated before each transmission or reception, otherwise the bus will not be able to recognize the data;
  2. The bus must be initialized and devices must be scanned to ensure that the target device is online;
  3. The start block does not transmit data; it only marks the start of communication.
  4. Used in conjunction with I2C.stop to form a complete data transmission process.

✨ Exclusive Mini Interaction

  1. Quick Response Quiz 1: What is the core function of the I2C.start block? (Answer: Send a start signal on the bus to prepare for communication)
  2. Think about it: What will happen to the data sending block if the start block is not activated? (Answer: The bus will not recognize the data, and the communication will fail.)
  3. Scenario Judgment: You want to send text to an OLED for display. What is the correct sequence of steps? (Answer: Initialization → Scanning → Startup → Transmission → Stop)
  4. Mini Challenge: Try to send data to two I2C devices separately, using I2C.start each time, and observe whether the communication proceeds smoothly.

Summary:

  1. Communication Start Block (I2C.start): Just like a teacher calling "Class begins", it readies the bus to interact with the device;
  2. It must be used before each data transmission to ensure the device responds correctly;
  3. Cooperate with I2C.stop to form a complete communication process.

3.5 Communication Stop Block (I2C.stop): When the teacher says "class dismissed", it pauses the communication with the students

Young creators, the I2C bus is just like a classroom: the communication start block (I2C.start) is equivalent to the teacher calling "class begins", prompting the students (devices) to start interacting. When the class is over or the current communication finishes, we need to tell the students "class is dismissed" to pause the interaction temporarily. The communication stop block is the signal of the teacher saying "class is dismissed", which makes the I2C bus suspend data transmission.

Chinese meaning: Issue a stop signal on the I2C bus to terminate the current data transmission and return the bus to an idle state.

Core Features:

Example in daily life:

  1. When the teacher says "class is dismissed", students stop answering questions and the class comes to an end.
  2. Hang up the call: The call ends and the line returns to idle status;
  3. Match termination signal: The referee blows the whistle, and the players stop the match.

Practical Example 1: End Sending Data to OLED

Practical Example 2: Multi-Device Loop Communication

Quick Reminder

  1. Stop after each communication is completed to avoid occupying the bus;
  2. The stop block will not delete the sent data; it only terminates the transmission.
  3. The next communication must be restarted, that is, the I2C.start shall be used again;
  4. Used in conjunction with the start block, it ensures that data transmission has clear start and end signals.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of the I2C.stop block? (Answer: Send a stop signal on the bus to terminate the current communication)
  2. Think about it: What problem will occur with the bus if the stop block is not used? (Answer: The bus may be occupied, resulting in subsequent communication failures.)
  3. Scenario Judgment: What is the sequence of the start and stop blocks before and after sending data to the OLED? (Answer: First execute I2C.start, then send the data, and finally execute I2C.stop)
  4. Small challenge: Try to send data to two devices consecutively, using the start and stop blocks each time, and observe whether the communication runs smoothly.

Summary:

  1. Communication Stop Block (I2C.stop): Just like a teacher saying "class dismissed", it terminates the ongoing I2C data transmission;
  2. It must be used in conjunction with I2C.start to ensure that the bus is in a safe idle state;
  3. Each communication forms a complete "class start - class end" process to ensure smooth operation of multiple devices.

3.6 Store Data into the Building Block (I2C.readinto): Guide students to write the data into their notebooks

Young creators, the I2C bus is just like a classroom: the teacher communicates with devices (students) via I2C.start / I2C.write to send commands or data. Sometimes, we want to save the data from the device, just like a student writing down what the teacher says in a notebook. The "store data" block is a tool for having students record information — it stores the data sent by an I2C device into a variable or buffer in the program for later use.

Chinese meaning: Read data from an I2C device and store it in a specified buffer (such as a variable or array) to save the information transmitted by the device.

Core Features:

Example in daily life:

  1. Students take notes: they write down what the teacher says so that they can review it later;
  2. Meeting Minutes: Document the discussion outcomes and store them properly;
  3. Sensor Log: Store data such as temperature and humidity into an array to facilitate chart plotting or data processing.

Practical Example 1: Store the data of temperature and humidity sensor into an array

Practical Example 2: Reading Multi-Byte Data in a Single Operation

Quick Reminder

  1. The front bus must be initialized and started before use;
  2. The length of the buffer for storing data shall be sufficient; otherwise, bytes may be lost;
  3. Building blocks only store data and do not process it automatically, so other building blocks are required for subsequent analysis or display;
  4. Suitable for reading multi-byte or continuous data, which can be conveniently stored at one time to facilitate subsequent operations.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of the I2C.readinto block? (Answer: Store the data sent by the I2C device into a buffer/variable)
  2. Think about it: If you don't use the storage block, can the data be used? (Answer: It cannot be saved, making subsequent processing difficult.)
  3. Scenario Judgment: If you read 5 bytes of sensor data at one time while the buffer length is only 3, what will happen? (Answer: The last 2 bytes will be lost.)
  4. Small challenge: Try to read the data from the temperature and humidity sensor, store it in an array, and then display the value of the second byte.

Summary:

  1. Storing data into the block (I2C.readinto): This is like a student writing data into a notebook, saving the information sent by the device;
  2. Must be used after the I2C bus has been initialized and started;
  3. The saved data can be used for subsequent display, analysis, or control of other modules;
  4. It can store multiple bytes at a time, which facilitates the processing of continuous data or arrays.

3.7 Command Sending Block (I2C.write): The teacher sends a command to the student with the specified student ID

Young makers, the I2C bus is just like a classroom, where teachers and students communicate via the bus. The instruction sending block is the operation of the teacher sending a specific instruction to a certain student, which lets the device know what to do next.

Chinese meaning: Send commands or data to the specified I2C device to make the device perform corresponding operations.

Core Features:

Example in daily life:

  1. The teacher said, "Will the third student please answer the question? "
  2. Parents tell their children to "go wash your hands";
  3. The match referee gives the signal to get started.

Practical Example:

Quick Reminder

  1. The bus must be started (I2C.start) before transmission;
  2. The data length must match the requirements of the device; otherwise, the device may not execute properly.
  3. The sent data will not be read back automatically; if you need to check the result, you should use the read block.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of I2C.write? (Answer: To send commands or data to the specified device)
  2. Consider: If you don't use I2C.write, can the device perform any operations? (Answer: No, the device will not receive the command.)
  3. Scenario Judgment: When sending multi-byte commands to an OLED, do you need to initiate the bus first? (Answer: Yes, I2C.start is required first)

Summary:

  1. Command sending block (I2C.write): Sends specific operation commands to the device;
  2. Must be used after I2C.start and ended with I2C.stop;
  3. Single-byte or multi-byte data can be transmitted to ensure that the data length meets the equipment requirements;
  4. Used in conjunction with the read block, it enables a complete communication process.

3.8 Data Reading Block (I2C.readfrom) —— Instruct the student with the specified student ID to report data

Young makers, sometimes teachers need to know about students' homework status or class performance, I2C.readfrom is the block that instructs the specified device to report its data back, so that we can obtain the content sent by the device.

Chinese Meaning: Read data from the specified I2C device and store it in a program variable or buffer for subsequent use or processing.

Core Features:

Example in daily life:

  1. The teacher asked the 5th student to report on the completion of their homework.
  2. Parents ask their children, "What did you learn today? ";
  3. The instrument sends the measurement results back to the computer or the main control board.

Practical Example 1: Reading Data from Temperature and Humidity Sensors

Practical Example 2: Reading Module Status

Quick Reminder

  1. The bus must be started (I2C.start) before reading;
  2. The data length must match the value returned by the device; otherwise, information may be lost;
  3. The read data needs to be stored in a buffer or variable; otherwise, it cannot be used subsequently.
  4. The block is only responsible for reading and will not process automatically, and subsequent blocks such as display and calculation can be used for processing.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of I2C.readfrom? (Answer: Read data from the specified device and store it)
  2. Think about it: If you don't use the read block, can you use the device data? (Answer: No, the data will not automatically return to the program.)
  3. Scenario judgment: If you read 4 bytes of data at a time but the buffer length is only 2, what will happen? (Answer: Only the first 2 bytes can be saved, and the last 2 bytes will be lost)

Summary:

  1. Data Reading Block (I2C.readfrom): Instructs the specified device to send information back to the main control board;
  2. Must be used after I2C.start, and the communication shall be terminated by I2C.stop;
  3. It can be read by byte or by length, so ensure that the buffer size is sufficient;
  4. When used together with I2C.write, it can complete the full transmission and reception process.

3.9 Read Student Notebook Block (I2C.readfrom_mem) —— Instruct the specified student to take out their dedicated notebook and read the content inside

Young makers, every I2C device has its own internal memory (notebook) for recording certain data or statuses. I2C.readfrom_mem is the block that works like a teacher asking a specific student to take out their notebook and read the data at a designated location.

Chinese meaning: Read data from the specified register (memory address) of an I2C device and store the data into a program variable or buffer.

Core Features:

Example in daily life:

  1. The teacher checks the answers on page 3 of the second student's exercise book;
  2. Parents check specific content in their children's diaries;
  3. Read historical data stored in the internal memory of the instrument.

Practical Example 1: Reading EEPROM Memory Data

Practical Example 2: Reading Sensor History Records

Quick Reminder

  1. The bus must be started (I2C.start) before reading;
  2. The register address must be accurate; otherwise, incorrect data may be read.
  3. The buffer size shall be sufficient to ensure that the read data can be completely stored;
  4. The block is only responsible for reading, and subsequent display or processing requires cooperation with other blocks.

✨ Exclusive Mini Interaction

  1. Quick Response Quiz 1: What is the core function of I2C.readfrom_mem? (Answer: Read data from the internal storage of a device at the specified register)
  2. Think about it: What will happen to the read result if the register address is written incorrectly? (Answer: Incorrect data or invalid values may be read.)
  3. Scenario Judgment: You need to read 3 bytes of data, but the buffer only has 2 slots. Can the data be saved completely? (Answer: No, only the first 2 bytes will be saved, and the last one will be lost.)

Summary:

  1. Read from Student Notebook Building Block (I2C.readfrom_mem): Reads data from the device's internal register;
  2. Must be used after I2C.start, and I2C.stop is used to terminate the communication;
  3. Data can be read according to the register address and length, and the buffer shall be large enough;
  4. When used together with the write block, a complete read-write operation can be realized.

3.10 Student Note Copying Block (I2C.readfrom_mem_into) — Copy the content from the student's notebook to the teacher's notebook

Young makers, the internal memory of an I2C device is just like each student's notebook. I2C.readfrom_mem_into is a block that works like a teacher asking a student to copy the content from their notebook directly into the teacher's notebook, which lets programs use the data directly without having to copy it byte by byte on their own.

Chinese meaning: Directly copy the content of the specified register (memory address) of the I2C device into the buffer or array of the program.

Core Features:

Example in daily life:

  1. The teacher asked the 4th student to copy all the answers on Page 2 of the exercise notebook onto the teacher's notebook;
  2. Parents directly copy a page from their child's diary into a memo;
  3. The instrument directly imports the internally stored historical data into the computer array for easy analysis.

Practical Example 1: Reading Batch Data from EEPROM

Practical Example 2: Reading Multi-byte Historical Records of Sensors

Quick Reminder

  1. Before transcription, the bus must be started (I2C.start);
  2. Register addresses and buffer lengths must match, otherwise incomplete copying may occur;
  3. The block is only responsible for copying and will not process automatically, and other blocks are required for subsequent display or operations;
  4. The buffer must be prepared in advance to ensure it can accommodate the transcribed data.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of I2C.readfrom_mem_into? (Answer: Directly copy the data from the device's internal registers to the program buffer)
  2. Think about it: What will happen if the buffer length is shorter than the data to be copied? (Answer: Only the data that can fit in the buffer will be copied, and the excess part will be lost.)
  3. Scenario Judgment: When copying 5 bytes of data into an array at one time, and the array has only 5 slots, can the data be completely stored? (Answer: Yes, it fits exactly.)

Summary:

  1. Copy Student Notes Block (I2C.readfrom_mem_into): Directly copy the internal data of the device to the program buffer;
  2. Must be used after I2C.start, and the communication shall be terminated by I2C.stop;
  3. Multi-byte data can be copied in a single operation, and the buffer must be large enough;
  4. Used in conjunction with the writing block, it can complete a full read-write process and efficiently obtain the internal content of the device.

3.11 Write to Student Notebook Block (I2C.writeto_mem) —— The teacher writes new instructions in the dedicated notebook of the specified student

Young makers, every I2C device has its own internal storage (like a notebook) to record statuses or commands. I2C.writeto_mem is the block that works like a teacher writing new instructions or content into a student's notebook, to make the device perform operations as instructed by the program.

Chinese meaning: Write data to the specified register (memory address) of an I2C device to complete the update of internal device information or the delivery of instructions.

Core Features:

Example in daily life:

  1. The teacher wrote the latest revisions to the third student's homework into the notebook;
  2. A parent writes a new reminder in their child's diary;
  3. The main control board writes new thresholds or commands to the sensor to make the sensor operate as required.

Practical Example 1: Writing EEPROM Instructions

Practical Example 2: Writing Sensor Thresholds

Quick Reminder

  1. The bus must be started before writing (I2C.start);
  2. The register address must be accurate; otherwise, data may be written to the wrong location.
  3. The length of the data to be written must match the length allowed by the register;
  4. The block is only responsible for writing operations and will not automatically read or verify data; you can use a read block later to check whether the data has been written successfully.

✨ Exclusive Mini Interaction

  1. Quick Response 1: What is the core function of I2C.writeto_mem? (Answer: Write data to the internal registers of the device to update status or issue commands)
  2. Think: What will the device do if the register address is written incorrectly? (Answer: Data may be written to the wrong location, leading to incorrect device status)
  3. Scenario Judgment: What will happen if 3 bytes of data are written to a register at one time while the register can only store 2 bytes? (Answer: Only the first 2 bytes can be written, and the last byte will be lost.)

Summary:

  1. Student Notebook Block (I2C.writeto_mem): Writes data to the internal register of a device to implement instruction delivery or status update;
  2. Must be used after I2C.start, and I2C.stop is used to terminate the communication;
  3. Single-byte or multi-byte data can be written in a single operation, and the register length must match;
  4. When used together with the reading blocks, it can complete a full read-write operation and make the device execute tasks according to the program.

A Beginner’s Guide to Avoiding Common Pitfalls

  1. Are all the peripherals unresponsive?
    • Check whether the SDA/SCL lines are connected reversely. The SDA/SCL pins of all peripherals must correspond to the pins of the development board; reverse connection will directly lead to communication failure.
    • Make sure that the GND of all peripherals share a common ground with the development board, as common grounding is the foundation of I2C communication.
  2. Is only one peripheral working while the other one doesn't respond?
    • Most likely an address conflict! Check the default addresses of the two peripherals; if they are duplicated, adjust using the address-modifiable peripheral (e. g., change the address via the jumper cap on the module);
    • Check whether the cables of the non-working peripheral devices are loose, and try to reconnect them.
  3. Garbled data / intermittent connection?
    • Add pull-up resistors (4.7KΩ) to the SDA/SCL lines to enhance signal stability;
    • Reduce the number of peripherals or shorten the wiring length to avoid signal interference.

✨ Interactive Mini Quiz

  1. Think about it: I2C can connect multiple peripherals with only two wires. What is the core mechanism that enables it to "never misidentify devices"? (Answer: Device addresses!)
  2. Try this: If you add another I2C light sensor to the circuit, do you need to connect additional SDA/SCL wires? (Answer: No need! Just share the existing two wires, as long as there is no address conflict~)
  3. Extension Question: Why is I2C suitable for connecting multiple sensors while UART is not? (Hint: UART uses a dedicated one-to-one connection; connecting multiple peripherals requires multiple sets of TX/RX lines, which is far too cumbersome!)

Summary:

The core of I2C communication lies in " two wires, multiple devices, and address-based addressing ", which operates efficiently and orderly like a teacher leading students. It helps us solve the problem of "numerous peripherals but limited interfaces", making it the most commonly used communication method in comprehensive projects. You will definitely use it when building complex systems later, such as simultaneously monitoring temperature, humidity, and light while displaying the data. ~