Bird
0
0
Arduinoprogramming~10 mins

Wire library for I2C in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Wire library for I2C
Start
Initialize Wire
Begin Transmission
Send Data
End Transmission
Request Data
Read Data
Stop
This flow shows how the Wire library starts, sends data, requests data, reads it, and stops communication on the I2C bus.
Execution Sample
Arduino
Wire.begin();
Wire.beginTransmission(8);
Wire.write(42);
Wire.endTransmission();
Wire.requestFrom(8, 1);
int x = Wire.read();
This code initializes I2C, sends the number 42 to device 8, then requests and reads one byte from device 8.
Execution Table
StepActionWire FunctionData Sent/ReceivedResult
1Initialize I2C busWire.begin()NoneI2C bus ready
2Start sending to device 8Wire.beginTransmission(8)NoneTransmission started
3Write byte 42Wire.write(42)42Byte queued to send
4End transmissionWire.endTransmission()42 sentData sent to device 8
5Request 1 byte from device 8Wire.requestFrom(8, 1)Request sentWaiting for data
6Read received byteWire.read()Received byteByte stored in variable x
7EndNoneNoneCommunication complete
💡 All steps complete, data sent and received successfully
Variable Tracker
VariableStartAfter Step 6Final
xundefinedReceived byte from device 8Received byte from device 8
Key Moments - 3 Insights
Why do we call Wire.beginTransmission() before Wire.write()?
Wire.beginTransmission() tells the I2C bus which device we want to talk to. Without it, Wire.write() wouldn't know where to send data. See step 2 and 3 in the execution_table.
What happens if we call Wire.read() before Wire.requestFrom()?
Wire.read() reads data from the buffer. If we don't request data first, the buffer is empty, so we get no useful data. See step 5 and 6 in the execution_table.
Why do we call Wire.endTransmission() after Wire.write()?
Wire.endTransmission() actually sends the queued data to the device. Wire.write() only queues it. See step 3 and 4 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what function is called to start sending data to device 8?
AWire.write(42)
BWire.beginTransmission(8)
CWire.requestFrom(8, 1)
DWire.endTransmission()
💡 Hint
Check step 2 in the execution_table where transmission starts.
At which step is the data actually sent to the device?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the action 'End transmission' in step 4.
If we want to read 2 bytes instead of 1, which function call changes?
AWire.requestFrom(8, 2)
BWire.write(42)
CWire.beginTransmission(8)
DWire.endTransmission()
💡 Hint
Check step 5 where data is requested from the device.
Concept Snapshot
Wire library manages I2C communication.
Use Wire.begin() to start.
Wire.beginTransmission(address) sets device.
Wire.write(data) queues bytes to send.
Wire.endTransmission() sends data.
Wire.requestFrom(address, count) asks for bytes.
Wire.read() reads received bytes.
Full Transcript
The Wire library helps Arduino talk to devices using I2C. First, we start the bus with Wire.begin(). To send data, we tell which device with Wire.beginTransmission(address), then queue data with Wire.write(), and finally send it with Wire.endTransmission(). To get data, we ask with Wire.requestFrom(address, number_of_bytes) and read it with Wire.read(). This step-by-step flow ensures data goes to and comes from the right device.