Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to include the Wire library for I2C communication.
Arduino
#include [1] void setup() { Wire.begin(); } void loop() { // sensor reading code }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Including SPI or Servo libraries instead of Wire.
Forgetting to include any library.
✗ Incorrect
The Wire library is required for I2C communication on Arduino. Including allows you to use I2C functions.
2fill in blank
mediumComplete the code to start I2C communication as a master device.
Arduino
void setup() {
[1]();
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Serial.begin instead of Wire.begin.
Using Wire.start which does not exist.
✗ Incorrect
Wire.begin() initializes the Arduino as an I2C master device.
3fill in blank
hardFix the error in the code to request 2 bytes from the sensor at address 0x40.
Arduino
Wire.requestFrom([1], 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decimal 40 instead of hex 0x40.
Using wrong sensor address.
✗ Incorrect
The sensor address is 0x40 in hexadecimal. Using 0x40 correctly requests data from the sensor.
4fill in blank
hardFill both blanks to read two bytes from the sensor and combine them into a single integer.
Arduino
if (Wire.available()) { int highByte = Wire.read(); int lowByte = Wire.[1](); int value = (highByte [2] 8) | lowByte; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.available() instead of Wire.read() to get the byte.
Using right shift (>>) instead of left shift (<<).
✗ Incorrect
Wire.read() reads the next byte. Shifting highByte left by 8 bits combines it with lowByte to form a 16-bit value.
5fill in blank
hardFill all three blanks to write a command byte 0x01 to the sensor at address 0x40 and end transmission.
Arduino
Wire.beginTransmission([1]); Wire.[2](0x01); Wire.[3]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong sensor address 0x20.
Using print() instead of write() to send data.
Forgetting to end transmission.
✗ Incorrect
beginTransmission starts communication with sensor at 0x40, write sends the command byte, and endTransmission ends the I2C communication.
