Bird
Raised Fist0
Embedded Cprogramming~10 mins

Reading data from I2C device in Embedded C - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start an I2C transmission to the device address.

Embedded C
HAL_I2C_Master_Transmit(&hi2c1, [1], data, size, timeout);
Drag options to blanks, or click blank then click option'
A0xFF
B0x50
C0x00
D0xA0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the 7-bit address without shifting.
Using the read address instead of write address.
2fill in blank
medium

Complete the code to read data from the I2C device.

Embedded C
HAL_I2C_Master_Receive(&hi2c1, [1], buffer, length, timeout);
Drag options to blanks, or click blank then click option'
A0xA1
B0x50
C0xA0
D0x00
Attempts:
3 left
💡 Hint
Common Mistakes
Using the write address instead of the read address.
Not shifting the address before use.
3fill in blank
hard

Fix the error in the code to correctly read a register from the I2C device.

Embedded C
uint8_t reg = 0x10;
HAL_I2C_Master_Transmit(&hi2c1, [1], &reg, 1, timeout);
HAL_I2C_Master_Receive(&hi2c1, 0xA1, &data, 1, timeout);
Drag options to blanks, or click blank then click option'
A0x10
B0xA1
C0xA0
D0x50
Attempts:
3 left
💡 Hint
Common Mistakes
Using the read address in the transmit function.
Mixing up device addresses.
4fill in blank
hard

Fill both blanks to read multiple bytes from a register.

Embedded C
uint8_t reg = [1];
HAL_I2C_Master_Transmit(&hi2c2, [2], &reg, 1, 100);
Drag options to blanks, or click blank then click option'
A0x20
B0xB0
C0xB1
D0x30
Attempts:
3 left
💡 Hint
Common Mistakes
Using the read address in the transmit function.
Using wrong register address.
5fill in blank
hard

Fill all three blanks to complete the I2C read sequence for a sensor register.

Embedded C
uint8_t reg = [1];
HAL_I2C_Master_Transmit(&hi2c3, [2], &reg, 1, 100);
HAL_I2C_Master_Receive(&hi2c3, [3], &value, 1, 100);
Drag options to blanks, or click blank then click option'
A0x15
B0xC0
C0xC1
D0x16
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up read and write addresses.
Using incorrect register address.

Practice

(1/5)
1. What is the main purpose of reading data from an I2C device in embedded C?
easy
A. To send commands to the device only
B. To get sensor or hardware information from the device
C. To reset the device hardware
D. To format the device memory

Solution

  1. Step 1: Understand I2C device communication

    I2C devices often provide sensor or hardware data that the program needs to read.
  2. Step 2: Identify the purpose of reading data

    Reading data means fetching information like sensor values, not sending commands or resetting.
  3. Final Answer:

    To get sensor or hardware information from the device -> Option B
  4. Quick Check:

    Reading I2C = Getting device data [OK]
Hint: Reading I2C means fetching data from device [OK]
Common Mistakes:
  • Confusing reading with sending commands
  • Thinking reading resets the device
  • Assuming reading formats memory
2. Which of the following is the correct way to declare a buffer to read 4 bytes from an I2C device in embedded C?
easy
A. int buffer[1];
B. int buffer = 4;
C. char buffer;
D. char buffer[4];

Solution

  1. Step 1: Understand buffer declaration for byte storage

    To store multiple bytes, we need an array of chars (bytes) of size 4.
  2. Step 2: Check each option

    char buffer[4]; declares a char array of size 4, suitable for 4 bytes. Others are either single variables or wrong types.
  3. Final Answer:

    char buffer[4]; -> Option D
  4. Quick Check:

    Buffer for 4 bytes = char array size 4 [OK]
Hint: Use char array for byte buffers [OK]
Common Mistakes:
  • Using single char instead of array
  • Using int for byte buffer
  • Declaring wrong array size
3. Given this code snippet, what will be the value of result after reading 2 bytes from the I2C device?
uint8_t buffer[2];
int result = i2c_read(device_addr, reg_addr, buffer, 2);
Assume i2c_read returns 0 on success and -1 on failure.
medium
A. 0
B. -1
C. 2
D. Undefined

Solution

  1. Step 1: Understand i2c_read return values

    The function returns 0 if the read was successful, -1 if it failed.
  2. Step 2: Analyze the code snippet

    The code reads 2 bytes and stores in buffer. If successful, result = 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Success return = 0 [OK]
Hint: i2c_read returns 0 on success [OK]
Common Mistakes:
  • Assuming return is number of bytes read
  • Confusing success with failure codes
  • Ignoring return value meaning
4. Identify the error in this code snippet for reading 1 byte from an I2C device:
uint8_t data;
int status = i2c_read(device_addr, reg_addr, &data, 1);
if (status = 0) {
    process(data);
}
medium
A. Using assignment (=) instead of comparison (==) in if condition
B. Incorrect buffer type for reading data
C. Missing initialization of device_addr
D. Wrong number of bytes requested

Solution

  1. Step 1: Check the if condition syntax

    The condition uses single '=' which assigns 0 instead of comparing.
  2. Step 2: Understand effect of assignment in if

    Assignment returns 0 (false), so process(data) never runs even on success.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in if condition -> Option A
  4. Quick Check:

    Use '==' for comparison in if [OK]
Hint: Use '==' to compare, not '=' in conditions [OK]
Common Mistakes:
  • Confusing '=' and '==' in if statements
  • Ignoring return status check
  • Not using address-of operator for buffer
5. You want to read 3 bytes from an I2C device register and combine them into a 24-bit integer. Which code snippet correctly reads and combines the data?
uint8_t buf[3];
int status = i2c_read(device_addr, reg_addr, buf, 3);
uint32_t value = ???;
hard
A. value = buf[0] | (buf[1] << 8) | (buf[2] << 16);
B. value = (buf[2] << 16) | (buf[1] << 8) | buf[0];
C. value = (buf[0] << 16) | (buf[1] << 8) | buf[2];
D. value = buf[0] + buf[1] + buf[2];

Solution

  1. Step 1: Understand byte order for 24-bit integer

    Assuming buf[0] is most significant byte, shift it left by 16 bits.
  2. Step 2: Combine bytes with bitwise OR

    Shift buf[1] by 8 bits, add buf[2] as least significant byte.
  3. Step 3: Check each option

    value = (buf[0] << 16) | (buf[1] << 8) | buf[2]; correctly combines bytes in big-endian order. Others mix order or sum bytes incorrectly.
  4. Final Answer:

    value = (buf[0] << 16) | (buf[1] << 8) | buf[2]; -> Option C
  5. Quick Check:

    Shift and OR bytes for 24-bit value [OK]
Hint: Shift bytes by 16, 8, 0 bits and OR [OK]
Common Mistakes:
  • Mixing byte order
  • Adding bytes instead of bitwise OR
  • Not shifting bytes correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard