📖 Scenario: You are working on a small embedded system that needs to communicate with multiple sensors and devices using only a few wires.
🎯 Goal: Understand why the I2C communication protocol is used by creating a simple program that sets up device addresses and demonstrates communication over I2C.
📋 What You'll Learn
Create a list of device addresses connected to the I2C bus
Set a variable for the maximum number of devices allowed on the bus
Write a loop to check which devices are connected by comparing addresses
Print the addresses of connected devices
💡 Why This Matters
🌍 Real World
I2C is used in embedded systems to connect sensors, displays, and other components with minimal wiring.
💼 Career
Understanding I2C helps in designing efficient hardware communication for microcontrollers and embedded devices.
Progress0 / 4 steps
1
Create a list of I2C device addresses
Create an array called device_addresses with these exact values: 0x3C, 0x27, 0x68, 0x40.
Embedded C
Hint
Use curly braces to list the addresses inside the array.
2
Set the maximum number of devices
Create an integer variable called max_devices and set it to 4.
Embedded C
Hint
Use int to declare the variable and assign the value 4.
3
Check connected devices using a loop
Write a for loop using int i from 0 to less than max_devices. Inside the loop, create an if statement that checks if device_addresses[i] is not zero. If true, call a function check_device(device_addresses[i]).
Embedded C
Hint
Use a for loop with i from 0 to max_devices - 1. Inside, use if to check the address and call the function.
4
Print connected device addresses
Write a for loop using int i from 0 to less than max_devices. Inside the loop, print the text "Device found at address: 0x" followed by the hexadecimal value of device_addresses[i] using printf.
Embedded C
Hint
Use printf("Device found at address: 0x%X\n", device_addresses[i]); inside the loop.
Practice
(1/5)
1. Why is I2C commonly used in embedded systems?
easy
A. It uses wireless signals for communication.
B. It requires a separate wire for each device.
C. It only supports one device at a time.
D. It allows multiple devices to communicate using only two wires.
Solution
Step 1: Understand I2C wiring
I2C uses two wires: SDA (data) and SCL (clock) to connect devices.
Step 2: Recognize multi-device communication
Multiple devices share these two wires, communicating by unique addresses.
Final Answer:
It allows multiple devices to communicate using only two wires. -> Option D
Quick Check:
I2C uses 2 wires for many devices [OK]
Hint: I2C uses 2 wires for many devices, not one per device [OK]
Common Mistakes:
Thinking each device needs its own wire
Confusing I2C with wireless communication
Believing only one device can connect
2. Which of the following is the correct way to describe I2C communication lines?
easy
A. Separate data and clock lines for each device
B. Only one wire for both data and clock
C. Single data line and single clock line shared by all devices
D. Wireless data and wired clock lines
Solution
Step 1: Identify I2C lines
I2C uses two lines: SDA for data and SCL for clock.
Step 2: Understand line sharing
Both lines are shared by all devices on the bus, not separate per device.
Final Answer:
Single data line and single clock line shared by all devices -> Option C
Quick Check:
I2C shares SDA and SCL lines [OK]
Hint: I2C shares one data and one clock line for all devices [OK]
Common Mistakes:
Assuming separate lines per device
Thinking data and clock share one wire
Confusing wireless with wired lines
3. You want to connect 5 sensors to a microcontroller using I2C. What is the main advantage of using I2C here?
easy
A. Each sensor needs its own data and clock wires
B. You can use only two wires total for all sensors
C. Sensors communicate wirelessly with microcontroller
D. I2C requires no addressing for devices
Solution
Step 1: Understand wiring for multiple devices
I2C allows multiple devices to share the same two wires: SDA and SCL.
Step 2: Recognize wiring advantage
This saves pins and wiring complexity compared to separate wires per sensor.
Final Answer:
You can use only two wires total for all sensors -> Option B
Quick Check:
Two wires for many devices saves pins [OK]
Hint: I2C uses 2 wires total regardless of device count [OK]
Common Mistakes:
Thinking each sensor needs separate wires
Assuming I2C is wireless
Believing devices don't need addresses
4. Consider this I2C communication snippet in embedded C: