Bird
0
0
Arduinoprogramming~10 mins

Reading I2C sensor data in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A<Wire.h>
B<SPI.h>
C<Servo.h>
D<EEPROM.h>
Attempts:
3 left
💡 Hint
Common Mistakes
Including SPI or Servo libraries instead of Wire.
Forgetting to include any library.
2fill in blank
medium

Complete the code to start I2C communication as a master device.

Arduino
void setup() {
  [1]();
}
Drag options to blanks, or click blank then click option'
ASerial.begin
BWire.begin
CWire.start
DSerial.start
Attempts:
3 left
💡 Hint
Common Mistakes
Using Serial.begin instead of Wire.begin.
Using Wire.start which does not exist.
3fill in blank
hard

Fix 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'
A0x20
B0x04
C0x40
D40
Attempts:
3 left
💡 Hint
Common Mistakes
Using decimal 40 instead of hex 0x40.
Using wrong sensor address.
4fill in blank
hard

Fill 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'
Aread
B<<
C>>
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.available() instead of Wire.read() to get the byte.
Using right shift (>>) instead of left shift (<<).
5fill in blank
hard

Fill 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'
A0x20
Bwrite
CendTransmission
D0x40
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong sensor address 0x20.
Using print() instead of write() to send data.
Forgetting to end transmission.