Bird
0
0
Arduinoprogramming~20 mins

I2C scanner sketch in Arduino - Mini Project: Build & Apply

Choose your learning style9 modes available
I2C Scanner Sketch
📖 Scenario: You have connected several devices to your Arduino's I2C bus. You want to find out which devices are connected and their addresses.
🎯 Goal: Build a simple Arduino sketch that scans all possible I2C addresses and prints the addresses of connected devices to the Serial Monitor.
📋 What You'll Learn
Create a variable to store the starting I2C address
Create a variable to store the ending I2C address
Use a for loop to scan all addresses from start to end
Use Wire.beginTransmission() and Wire.endTransmission() to check if a device responds
Print the found device addresses in hexadecimal format to the Serial Monitor
💡 Why This Matters
🌍 Real World
This sketch helps you find the addresses of I2C devices connected to your Arduino, which is useful when setting up sensors or displays.
💼 Career
Understanding how to scan and communicate with I2C devices is important for embedded systems and hardware interfacing jobs.
Progress0 / 4 steps
1
Setup I2C scanning range
Create two integer variables called startAddress and endAddress. Set startAddress to 1 and endAddress to 127.
Arduino
Hint

Use int to create the variables and assign the numbers 1 and 127 exactly.

2
Initialize I2C and Serial communication
Add the Wire.h library include. In the setup() function, start the I2C bus with Wire.begin() and start serial communication at 9600 baud with Serial.begin(9600).
Arduino
Hint

Remember to include Wire.h at the top. Use Wire.begin() and Serial.begin(9600) inside setup().

3
Scan I2C addresses
Inside the setup() function, add a for loop that uses address from startAddress to endAddress. For each address, call Wire.beginTransmission(address) and then Wire.endTransmission(). If Wire.endTransmission() returns 0, print "Found device at 0x" followed by the address in hexadecimal format using Serial.print(address, HEX) and then print a new line with Serial.println().
Arduino
Hint

Use a for loop with address from startAddress to endAddress. Check if Wire.endTransmission() returns 0 to detect a device.

4
Print scan completion message
After the for loop inside setup(), add a line to print "Scan complete." to the Serial Monitor using Serial.println().
Arduino
Hint

Use Serial.println("Scan complete.") after the loop to show the scan is done.