Bird
Raised Fist0
Arduinoprogramming~5 mins

Wire library for I2C in Arduino - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the Wire library used for in Arduino?
The Wire library is used to communicate with devices using the I2C protocol, allowing the Arduino to act as a master or slave on the I2C bus.
Click to reveal answer
beginner
How do you start the I2C communication as a master using the Wire library?
You call Wire.begin(); in the setup() function to start the I2C bus as a master.
Click to reveal answer
beginner
Which function is used to send data to a device on the I2C bus?
Use Wire.write() to send bytes of data to the device after starting a transmission with Wire.beginTransmission(address).
Click to reveal answer
beginner
How do you request data from a device using the Wire library?
Use Wire.requestFrom(address, quantity) to ask a device for data, then read it with Wire.read().
Click to reveal answer
beginner
What is the purpose of Wire.endTransmission()?
It ends the transmission to the I2C device and actually sends the data over the bus.
Click to reveal answer
Which function initializes the I2C bus as a master in Arduino?
AWire.begin()
BWire.start()
CWire.init()
DWire.open()
How do you send data to a device with address 0x3C using the Wire library?
AWire.send(0x3C, data);
BWire.write(0x3C, data);
CWire.transmit(0x3C, data);
DWire.beginTransmission(0x3C); Wire.write(data); Wire.endTransmission();
Which function reads a byte from the I2C device after requesting data?
AWire.get()
BWire.read()
CWire.receive()
DWire.fetch()
What does Wire.requestFrom(address, quantity) do?
ACloses the connection to the device at 'address'
BSends 'quantity' bytes to the device at 'address'
CRequests 'quantity' bytes from the device at 'address'
DStarts transmission to the device at 'address'
Which of these is NOT a typical step in sending data with Wire library?
AWire.receive()
BWire.write()
CWire.endTransmission()
DWire.beginTransmission()
Explain the basic steps to send data to an I2C device using the Wire library.
Think about starting, writing, and ending the communication.
You got /3 concepts.
    Describe how to read data from an I2C device using the Wire library.
    Request first, then read the data.
    You got /3 concepts.

      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