ESP32 IDF Interface I2C

I2C Interface

Follow these steps:

This table provides a concise overview of the steps involved in setting up I2C communication on ESP32 using the ESP-IDF with pins SDA-7 and SDL-10.

Code

I2C Scanner in ESP32 IDF Platform

An I2C (Inter-Integrated Circuit) scanner is a useful tool in embedded systems for identifying and troubleshooting issues with I2C devices connected to a microcontroller or other embedded platforms

I2C scanner simplifies the process of working with I2C devices in embedded systems, making it easier to identify, diagnose, and resolve issues related to I2C communication. It’s a handy tool for both development and production environments.

How an I2C scanner is used in embedded systems?

Consideration of I2C Scanner

I2C Scanner Design Outline

Code

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"

#define I2C_MASTER_SCL_IO 10        // Set the SCL pin
#define I2C_MASTER_SDA_IO 7         // Set the SDA pin
#define I2C_MASTER_NUM I2C_NUM_0    // I2C port number for master dev

void i2c_scanner() {
    printf("Scanning I2C bus...\n");
    i2c_port_t i2c_master_port = I2C_MASTER_NUM;
    i2c_config_t conf;
    conf.mode = I2C_MODE_MASTER;
    conf.sda_io_num = I2C_MASTER_SDA_IO;
    conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
    conf.scl_io_num = I2C_MASTER_SCL_IO;
    conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
    conf.master.clk_speed = 100000; // 100kHz clock frequency
    conf.clk_flags = 0; // Use default clock
    i2c_param_config(i2c_master_port, &conf);
    i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);
    for (int i = 0; i < 128; i++) {
        i2c_cmd_handle_t cmd = i2c_cmd_link_create();
        i2c_master_start(cmd);
        i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, true);
        i2c_master_stop(cmd);
        esp_err_t ret = i2c_master_cmd_begin(i2c_master_port, cmd, 10 / portTICK_PERIOD_MS);
        if (ret == ESP_OK) {
            printf("I2C device found at address 0x%X\n", i);
        }
        i2c_cmd_link_delete(cmd);
    }
}

void app_main() {
    i2c_scanner();
}

Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off
Index

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading