Bird
Raised Fist0
Arduinoprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the Wire.begin() function in Arduino's Wire library?
easy
A. To request data from a device on the I2C bus
B. To send data to a device on the I2C bus
C. To end the I2C communication
D. To initialize the I2C bus and join the I2C network

Solution

  1. Step 1: Understand the role of Wire.begin()

    The Wire.begin() function sets up the Arduino as a master or slave on the I2C bus and starts the communication.
  2. Step 2: Differentiate from other Wire functions

    Functions like Wire.beginTransmission() and Wire.requestFrom() handle sending and receiving data, not initialization.
  3. Final Answer:

    To initialize the I2C bus and join the I2C network -> Option D
  4. Quick Check:

    Wire.begin() starts I2C communication [OK]
Hint: Wire.begin() always starts I2C communication [OK]
Common Mistakes:
  • Confusing Wire.begin() with data sending functions
  • Using Wire.beginTransmission() to start communication
  • Thinking Wire.begin() sends or receives data
2. Which of the following is the correct syntax to start sending data to an I2C device with address 0x3C using the Wire library?
easy
A. Wire.start(0x3C);
B. Wire.beginTransmission(0x3C);
C. Wire.send(0x3C);
D. Wire.requestFrom(0x3C);

Solution

  1. Step 1: Identify the function to start transmission

    Wire.beginTransmission(address) is used to begin sending data to the device at the given address.
  2. Step 2: Check other options for correctness

    Wire.requestFrom() is for receiving data, Wire.send() is deprecated, and Wire.start() does not exist.
  3. Final Answer:

    Wire.beginTransmission(0x3C); -> Option B
  4. Quick Check:

    Use Wire.beginTransmission(address) to send data [OK]
Hint: Use Wire.beginTransmission(address) to start sending [OK]
Common Mistakes:
  • Using Wire.requestFrom() to send data
  • Using deprecated Wire.send() instead of Wire.write()
  • Trying to use non-existent Wire.start() function
3. What will be the output of the following Arduino code snippet using the Wire library?
Wire.begin();
Wire.beginTransmission(0x50);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(0x50, 1);
int data = Wire.read();
Serial.println(data);
medium
A. Prints the byte received from device at 0x50
B. Prints the byte 0x01
C. Prints 0 because Wire.read() returns 0 by default
D. Compilation error due to missing Wire.read()

Solution

  1. Step 1: Analyze the code flow

    The code initializes I2C, sends one byte (0x01) to device 0x50, then requests 1 byte back from the same device.
  2. Step 2: Understand Wire.read() behavior

    Wire.read() reads the byte received from the device. It does not return the sent byte but the device's response.
  3. Final Answer:

    Prints the byte received from device at 0x50 -> Option A
  4. Quick Check:

    Wire.read() returns received data, not sent data [OK]
Hint: Wire.read() reads incoming data, not what was sent [OK]
Common Mistakes:
  • Assuming Wire.read() returns the sent byte
  • Thinking Wire.read() returns 0 by default
  • Confusing Wire.write() and Wire.read() roles
4. Identify the error in this Arduino I2C code snippet:
Wire.begin();
Wire.beginTransmission(0x40);
Wire.write(0x10);
Wire.requestFrom(0x40, 1);
Wire.endTransmission();
int val = Wire.read();
medium
A. Calling Wire.requestFrom() before Wire.endTransmission()
B. Missing Wire.beginTransmission() before Wire.write()
C. Using Wire.read() without requesting data
D. Not calling Wire.begin() to start I2C

Solution

  1. Step 1: Check the order of I2C functions

    Wire.endTransmission() must be called to end sending data before requesting data with Wire.requestFrom().
  2. Step 2: Identify the incorrect sequence

    The code calls Wire.requestFrom() before Wire.endTransmission(), which is incorrect and causes communication errors.
  3. Final Answer:

    Calling Wire.requestFrom() before Wire.endTransmission() -> Option A
  4. Quick Check:

    End transmission before requesting data [OK]
Hint: Always call Wire.endTransmission() before Wire.requestFrom() [OK]
Common Mistakes:
  • Swapping the order of endTransmission and requestFrom
  • Forgetting to call endTransmission() after beginTransmission()
  • Calling Wire.read() without data available
5. You want to read 3 bytes from an I2C sensor at address 0x68 and store them in an array. Which code snippet correctly does this using the Wire library?
hard
A. Wire.beginTransmission(0x68); for (int i = 0; i < 3; i++) { data[i] = Wire.read(); } Wire.endTransmission();
B. Wire.beginTransmission(0x68); Wire.requestFrom(0x68, 3); for (int i = 0; i < 3; i++) { data[i] = Wire.read(); } Wire.endTransmission();
C. Wire.requestFrom(0x68, 3); for (int i = 0; i < 3; i++) { data[i] = Wire.read(); }
D. Wire.requestFrom(0x68, 3); Wire.endTransmission(); for (int i = 0; i < 3; i++) { data[i] = Wire.read(); }

Solution

  1. Step 1: Understand how to receive data with Wire.requestFrom()

    Wire.requestFrom(address, quantity) requests bytes from the device. Then Wire.read() reads each byte.
  2. Step 2: Check the correct sequence for reading data

    Only Wire.requestFrom() is needed to receive data. Wire.beginTransmission() and Wire.endTransmission() are for sending data, not receiving.
  3. Final Answer:

    Wire.requestFrom(0x68, 3); for (int i = 0; i < 3; i++) { data[i] = Wire.read(); } -> Option C
  4. Quick Check:

    Use requestFrom() then read() in loop to get bytes [OK]
Hint: Use requestFrom() then read() in loop to get bytes [OK]
Common Mistakes:
  • Using beginTransmission() when only reading data
  • Calling endTransmission() after requestFrom()
  • Reading data before requesting it
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