Bird
0
0
Arduinoprogramming~10 mins

I2C scanner sketch 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 start the I2C communication.

Arduino
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.[1]();
  Serial.println("I2C Scanner");
}

void loop() {
  // scanning code here
}
Drag options to blanks, or click blank then click option'
AbeginTransmission
BendTransmission
CrequestFrom
Dbegin
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.beginTransmission() instead of Wire.begin() to start I2C.
Forgetting to call Wire.begin() before scanning.
2fill in blank
medium

Complete the code to check if a device acknowledges at a given address.

Arduino
for (byte address = 1; address < 127; address++) {
  Wire.beginTransmission(address);
  byte error = Wire.[1]();
  if (error == 0) {
    Serial.print("Device found at address 0x");
    Serial.println(address, HEX);
  }
}
Drag options to blanks, or click blank then click option'
AendTransmission
BrequestFrom
Cbegin
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.requestFrom() instead of Wire.endTransmission() to check device.
Not checking the return value of Wire.endTransmission().
3fill in blank
hard

Fix the error in the code to print the address in hexadecimal format.

Arduino
Serial.print("Device found at address 0x");
Serial.println([1], HEX);
Drag options to blanks, or click blank then click option'
Aaddress()
Baddress
Caddr
DAddress
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function call like address() which does not exist.
Using a variable name with wrong capitalization.
4fill in blank
hard

Fill both blanks to complete the loop that scans all I2C addresses and prints found devices.

Arduino
for (byte [1] = 1; [1] < 127; [1]++) {
  Wire.beginTransmission([2]);
  byte error = Wire.endTransmission();
  if (error == 0) {
    Serial.print("Found device at 0x");
    Serial.println([2], HEX);
  }
}
Drag options to blanks, or click blank then click option'
Aaddress
Baddr
Ci
Ddevice
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop and function calls causing errors.
Using non-descriptive variable names that confuse the code.
5fill in blank
hard

Fill all three blanks to complete the I2C scanner sketch that prints 'No devices found' if none are detected.

Arduino
bool deviceFound = false;
for (byte [1] = 1; [1] < 127; [1]++) {
  Wire.beginTransmission([2]);
  byte error = Wire.endTransmission();
  if (error == 0) {
    Serial.print("Device at 0x");
    Serial.println([3], HEX);
    deviceFound = true;
  }
}
if (!deviceFound) {
  Serial.println("No devices found");
}
Drag options to blanks, or click blank then click option'
Aaddress
Baddr
Ddevice
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing compilation errors.
Forgetting to set deviceFound to true when a device is found.