Learn how to scan I2C slaves using an Arduino with this step-by-step guide. Use the Wire library to easily communicate with I2C devices, and scan the bus to detect connected devices. Find the addresses of all detected I2C slaves and troubleshoot any connection issues with this comprehensive I2C scanner tutorial.
Disclaimer:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
What is I2C:
I2C stands for the inter-integrated controller. This is a serial communication protocol that can connect low-speed devices. It is a master-slave communication in which we can connect and control multiple slaves from a single master. In this, each slave device has a specific address.
In I2C communication, there are two types of devices: the master and the slave. The master initiates the communication, while the slave responds to the master’s requests. Multiple slaves can be connected to a single I2C bus, and each slave has a unique address that the master can use to address it individually.
The I2C protocol is often used in embedded systems, microcontrollers, and other electronic applications where low-speed communication between devices is required. It is also used in many consumer electronics devices such as TVs, DVD players, and smartphones for communication between internal components.
Scanning I2C devices:
Sometimes, we need to scan the connected I2C devices such as sensors or other modules. Sometimes we know the address but sometimes, we really need to scan the address because it doesn’t work.
To scan I2C slaves using an Arduino, you can use the Wire library, which provides functions to communicate with I2C devices. Here are the steps to scan for I2C slaves using an Arduino:
- Connect your Arduino to the I2C bus by connecting the SDA and SCL pins of the Arduino to the corresponding pins on the I2C bus.
- Open the Arduino IDE and create a new sketch.
- Include the Wire library at the top of the sketch by adding the following line of code:
#include <Wire.h>
- In the setup() function, initialize the Wire library by adding the following line of code:
Wire.begin();
- In the loop() function, use the Wire library to scan the I2C bus for connected devices. Here’s the code to scan the I2C bus:
void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) { Serial.print("0"); } Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address<16) { Serial.print("0"); } Serial.println(address,HEX); } } if (nDevices == 0) { Serial.println("No I2C devices found\n"); } else { Serial.println("done\n"); } delay(5000); // wait 5 seconds for next scan }
- Upload the sketch to the Arduino and open the Serial Monitor to view the output. The sketch will scan the I2C bus and print out the addresses of all detected I2C slaves.
Note that the I2C addresses are shown in hexadecimal format (0xXX). If an I2C slave is not detected, it may not be connected properly, or it may have a different address than the one the scanner is looking for. Also, make sure that the correct value of pull-up resistors is used in the I2C bus.
Conclusion:
If everything is ok, you’ll get a result like this:
So you can scan your connected sensors or modules. See you soon, Happy coding, Thanks.
Read more on:
- How to implement a maximum power point tracking (MPPT) algorithm with an Arduino
- Interfacing ID20LA with Arduino
- How to make a digital clock with ST7567 & RTC with Arduino
- Digital Clock with Temperature & Humidity display
- How to interface the EM-18 RFID reader module with an Arduino
- How to interface SHT31 with STM32
- How to interface an OLED display with an STM32
For Professional Designs or Help:
0 Comments