📖 Scenario: You are working on a small embedded system that communicates with a sensor using the I2C protocol. To control the sensor, you need to send specific data bytes to it over the I2C bus.
🎯 Goal: Build a simple program that writes a sequence of bytes to an I2C device using a basic I2C write function.
📋 What You'll Learn
Create an array of bytes to send to the I2C device
Define the I2C device address as a variable
Use a function call to write the data array to the I2C device
Print a confirmation message after writing the data
💡 Why This Matters
🌍 Real World
Embedded systems often communicate with sensors and other devices using I2C. Writing data correctly is essential for controlling hardware.
💼 Career
Understanding I2C communication is important for embedded software engineers working on hardware interfacing and device drivers.
Progress0 / 4 steps
1
Create the data array
Create a byte array called data_to_send with these exact values: 0x10, 0x20, 0x30, 0x40.
Embedded C
Hint
Use curly braces to list the bytes inside the array.
2
Define the I2C device address
Create an unsigned char variable called device_address and set it to 0x50.
Embedded C
Hint
Use the exact variable name device_address and assign the hex value 0x50.
3
Write data to the I2C device
Call the function i2c_write with these arguments in order: device_address, data_to_send, and the length 4.
Embedded C
Hint
Store the return value of i2c_write in a variable called result.
4
Print confirmation message
Use printf to print exactly this message: "Data written to device 0x50\n".
Embedded C
Hint
Use printf with the exact string including newline \n.
Practice
(1/5)
1. What is the first step when writing data to an I2C device in embedded C?
easy
A. Read the device address
B. Start the I2C communication
C. Stop the I2C communication
D. Send the data byte
Solution
Step 1: Begin communication with the device
Writing data to an I2C device always starts with initiating the communication by sending a start condition. After starting, the device address and data can be sent, but the first step must be the start condition.
Final Answer:
Start the I2C communication -> Option B
Quick Check:
Start communication = first step [OK]
Hint: Always start communication before sending data [OK]
Common Mistakes:
Sending data before starting communication
Stopping communication too early
Skipping the start condition
2. Which of the following is the correct way to specify the 7-bit I2C device address for writing in embedded C?
easy
A. device_address << 1 | 0
B. device_address >> 1
C. device_address << 1 | 1
D. device_address << 1 & ~1
Solution
Step 1: Understand I2C address format
The 7-bit device address is shifted left by one bit to make room for the read/write bit as the least significant bit. For writing, the least significant bit must be 0, so OR with 0 is correct.
Final Answer:
device_address << 1 | 0 -> Option A
Quick Check:
Write bit = 0, address shifted left = correct [OK]
Hint: Shift address left and set LSB to 0 for write [OK]
Common Mistakes:
Setting LSB to 1 for write operation
Shifting address right instead of left
Not shifting address at all
3. Given the following code snippet, what will be the result of the I2C write operation?
B. Data 0x10 is written to register 0xAB of device 0x50
C. Write operation fails due to wrong device address
D. Data 0xAB is written to register 0x10 of device 0x50
Solution
Step 1: Analyze the address and data sent
The device address is correctly shifted and write bit set to 0. Then register address 0x10 and data 0xAB are sent in order. The sequence Start -> Address+Write -> Register -> Data -> Stop is correct for writing data to a register.
Final Answer:
Data 0xAB is written to register 0x10 of device 0x50 -> Option D
Quick Check:
Correct sequence = data written properly [OK]
Hint: Check address shift and write bit, then data order [OK]
Common Mistakes:
Mixing register and data order
Not shifting device address
Forgetting to send stop condition
4. Identify the error in the following I2C write code snippet:
A. Device address is shifted right instead of left
B. Data byte is sent before register address
C. Stop condition is missing
D. Write bit is set to 1 instead of 0
Solution
Step 1: Check device address shifting
The device address is incorrectly shifted right instead of left, which corrupts the address sent on the bus. Register and data are sent in correct order, and stop condition is present, so no other errors.
Final Answer:
Device address is shifted right instead of left -> Option A
Quick Check:
Address must be shifted left for I2C write [OK]
Hint: Always shift device address left by 1 bit [OK]
Common Mistakes:
Shifting address right instead of left
Sending data before register address
Omitting stop condition
5. You need to write multiple bytes to consecutive registers starting at 0x30 on an I2C device with address 0x60. Which code snippet correctly performs this operation in embedded C?
uint8_t device_addr = 0x60;
uint8_t start_reg = 0x30;
uint8_t data[] = {0x01, 0x02, 0x03};
// Which snippet is correct?
hard
A. I2C_Start();
I2C_Write(device_addr << 1 | 0);
for (int i = 0; i < 3; i++) {
I2C_Write(start_reg + i);
I2C_Write(data[i]);
}
I2C_Stop();
B. I2C_Start();
I2C_Write(device_addr << 1 | 0);
for (int i = 0; i < 3; i++) {
I2C_Write(data[i]);
}
I2C_Stop();
C. I2C_Start();
I2C_Write(device_addr << 1 | 0);
I2C_Write(start_reg);
for (int i = 0; i < 3; i++) {
I2C_Write(data[i]);
}
I2C_Stop();
D. I2C_Start();
I2C_Write(device_addr << 1 | 1);
I2C_Write(start_reg);
for (int i = 0; i < 3; i++) {
I2C_Write(data[i]);
}
I2C_Stop();
Solution
Step 1: Start communication and send device address with write bit
The device address is shifted left and write bit set to 0, then start register address is sent once. After sending the start register, consecutive data bytes are sent to consecutive registers automatically by the device.
Final Answer:
Start register sent once, then data bytes sent in loop -> Option C
Quick Check:
Send start reg once, then data bytes [OK]
Hint: Send start register once, then all data bytes [OK]
Common Mistakes:
Sending register address before each data byte
Using read bit instead of write bit
Not sending start register at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.