Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.beginTransmission() instead of Wire.begin() to start I2C.
Forgetting to call Wire.begin() before scanning.
✗ Incorrect
Wire.begin() initializes the I2C bus as a master device.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wire.requestFrom() instead of Wire.endTransmission() to check device.
Not checking the return value of Wire.endTransmission().
✗ Incorrect
Wire.endTransmission() sends the address and returns 0 if a device acknowledges.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function call like address() which does not exist.
Using a variable name with wrong capitalization.
✗ Incorrect
The variable holding the address is named 'address', so use it directly.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The variable 'address' is used consistently to scan and print device addresses.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing compilation errors.
Forgetting to set deviceFound to true when a device is found.
✗ Incorrect
The variable 'address' is used consistently to scan and print device addresses.
