Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the I2C bus.
Arduino
#include <Wire.h> void setup() { Wire.[1](); } void loop() {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.start() instead of Wire.begin()
Forgetting to include the Wire library
✗ Incorrect
The Wire.begin() function initializes the I2C bus on Arduino.
2fill in blank
mediumComplete the code to request 6 bytes from an I2C device with address 0x3C.
Arduino
Wire.[1](0x3C, 6);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.write() to request data
Using Wire.send() which is deprecated
✗ Incorrect
The Wire.requestFrom(address, quantity) function requests bytes from a device on the I2C bus.
3fill in blank
hardFix the error in the code to write a byte 0x01 to device address 0x50.
Arduino
Wire.beginTransmission(0x50); Wire.[1](0x01); Wire.endTransmission();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.send() which is deprecated
Using Wire.requestFrom() which is for reading
✗ Incorrect
The Wire.write() function sends data bytes to the I2C device.
4fill in blank
hardFill both blanks to create a dictionary of device addresses and their names.
Arduino
const char* devices[] = {"Sensor", "Display", "EEPROM"};
int addresses[] = {0x40, 0x3C, 0x50};
for (int i = 0; i < 3; i++) {
Serial.print(devices[[1]]);
Serial.print(" at address 0x");
Serial.println(addresses[[2]], HEX);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixed indices instead of the loop variable
Mixing different indices for devices and addresses
✗ Incorrect
Using the loop variable i accesses the corresponding device name and address.
5fill in blank
hardFill all three blanks to read 4 bytes from device 0x3C and store them in an array.
Arduino
byte data[4]; Wire.beginTransmission([1]); Wire.endTransmission(); Wire.requestFrom([2], [3]); for (int i = 0; i < 4; i++) { if (Wire.available()) { data[i] = Wire.read(); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different addresses for transmission and request
Requesting wrong number of bytes
✗ Incorrect
The device address is 0x3C and we request 4 bytes to read into the array.
