ESP32

I2C Scanner Code for the Arduino and ESP32

I2C Scanner:

I2C Scanner Code for the Arduino and ESP32- today’s article is about scanning I2C sensors using Arduino and ESP32 WiFi + Bluetooth Module. The I2C Scanner code I will share works with both Arduino and ESP32 WiFi + Bluetooth Module.

For demonstration, I will connect the BMP585 pressure and temperature sensor to the Arduino and the AMG8833 IR temperature sensor to the ESP32 WiFi + Bluetooth Module. You can use any other sensors instead of these.

I2C Scanner code for arduino and esp32

allpcb circuit




What is I²C?

I²C (Inter-Integrated Circuit) is a multi-master, multi-slave, packet-switched, single-ended, serial computer bus. Primarily used in short distance, intra-board communication it is often utilized to connect low-speed peripheral ICs and microcontrollers with a processor.

What are I²C Devices?

I²C devices include various types of Temperature sensors, Infrared sensors, pressure sensors, humidity sensors, memory devices like EEPROM, I2C LCDs, I2C Oled displays, Signal processor, and so many other types of sensors and IO devices. Each I²C device has a unique address, allowing multiple devices to share the same bus. So, this way, multiple I2C Sensors can be connected to the Arduino, ESP32, or any other controller board using only two IO pins.

Why Use an I²C Scanner?

An I2C scanner is a simple program with the help of which you can find the I2C address of all the I2C supported sensors. It scans all possible I²C addresses and checks if there is a device responding at each address. This can be extremely useful for:

Debugging: Ensuring that your I2C devices/Sensors are properly connected and accessible.

Address Discovery: Finding the address of new or unknown I2C devices.

Verification: Confirming the functionality of your I²C bus and Sensor.



Amazon Links:

ESP32 WiFi + Bluetooth Module (Recommended)

*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

I2C Sensor Interfacing:

On the Arduino

SDA = A4

SCL = A5

On ESP32

SDA = GPIO21

SCL = GPIO22

Connect the VCC and GND pins of the I2C sensor to the Arduino or ESP32 V and G pins.

Connect the I2C sensor SDA and SCL pins to the corresponding SDA and SCL pins.

how to scan i2c sensor with I2C Scanner using esp32 and arduino ide

Likewise, you can connect a sensor to the Arduino.

Upload the following Code to your Arduino or ESP32. Open the Serial port and select the desired baud rate.



I2C Scanner Code:

Using this scanner, you can easily identify and debug the I²C devices connected to your system.



How It Works

This line includes the Wire library, which allows the Arduino and ESP32 to communicate with I²C devices.

This begins the setup function, which runs once when the Arduino is powered ON or reset.

This initializes the I²C communication.

This starts serial communication at a baud rate of 115200, which is the speed at which data is transmitted.

This waits for the serial port to connect, which is necessary for some Arduino boards to ensure the serial communication is ready.

This prints “I2C Scanner” to the serial monitor, indicating the start of the I²C scanning process.

This calls the scanI2CDevices function to scan for I²C devices.




This ends the setup function.

This begins the loop function, which runs repeatedly after the setup function.

This pauses the program for 5 seconds.

This calls the scanI2CDevices function again to re-scan for I²C devices every 5 seconds.

This ends the loop function.

scanI2CDevices Function

This begins the scanI2CDevices function, which performs the actual I²C device scanning.

These lines declare variables: error and address are bytes (small integers), and nDevices is an integer to count the number of I²C devices found.

This prints “Scanning…” to the serial monitor, indicating that the scanning process is starting.

This initializes the number of devices found to zero.

This starts a for-loop to iterate through all possible I²C addresses from 1 to 126.

This begins communication with the device at the current address.



This ends the communication and captures the result. If a device acknowledges the address, error will be 0.

This checks if a device responded at the current address.

This prints a message indicating that a device was found.

This adds a leading zero for addresses less than 16 to maintain proper formatting.

This prints the address of the device in hexadecimal format.

This finishes the line with an exclamation mark.

This increments the count of devices found.

This checks if there was an unknown error during communication.

This prints a message indicating an unknown error.

This adds a leading zero for addresses less than 16.

This prints the address with the unknown error.

This ends the for-loop.

If no devices were found, this prints “No I2C devices found”.

If one or more devices were found, this prints “done”.



This ends the scanI2CDevices function.

Arduino, BMP585 I2C address:

esp32 I2C Scanner code using arduino ide



ESP32, AMG8833 I2C address:

I2C Scanner code usign arduino

Support me on Patreon for more articles and videos.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button