The ESP32 NodeMCU module supports the I2C (Inter-Integrated Circuit) communication protocol. With its built-in hardware support for I2C, the ESP32 NodeMCU module can easily interface with various I2C devices such as sensors, displays, and other peripherals. This allows for efficient data exchange between the ESP32 and I2C devices, enabling a wide range of applications in IoT, robotics, automation, and more.
Read more: ESP32 NodeMCU Module – I2C InterfaceContents
Pr-Request to Lean
- Embedded Protocol I2C : https://aruneworld.com/embedded/embedded-protocol/i2c/
ESP32 NodeMCU Module I2C Functions
Function | Description |
---|---|
i2c.address() | Send (SW) or queue (HWx) I²C address and read/write mode for the next transfer. |
i2c.read() | Read (SW) or queue (HWx) data for a variable number of bytes. |
i2c.setup() | Initialize the I²C interface for master mode. |
i2c.start() | Send (SW) or queue (HWx) an I²C start condition. |
i2c.stop() | Send (SW) or queue (HWx) an I²C stop condition. |
i2c.transfer() | Starts a transfer for the specified hardware module. |
i2c.write() | Write (SW) or queue (HWx) data to the I²C bus. |
i2c.slave.on() | Registers or unregisters an event callback handler. |
i2c.slave.setup() | Initialize the I²C interface for slave mode. |
i2c.slave.send() | Writes send data for the master into the transmit buffer. |
Code of ESP32 I2C Scanner
- In this code used to can the i2c Devices
- Used Software i2c and GPIO-23 as SDA and GPIO-22 as SCL of I2C Device
- Connected DS3231 Device and check i2c address of DS3231
-- setup id = i2c.SW -- Software pinSDA = 23 -- 1~12, IO index pinSCL = 22 -- 1~12, IO index speed = i2c.SLOW -- only i2c.SLOW supported --Initialize i2c.setup(id, pinSDA, pinSCL, speed) -- Initialize the I²C module. print("Scanning Started....") for count = 0,127 do i2c.start(id) -- Send an I²C start condition. Status = i2c.address(id, count, i2c.TRANSMITTER) -- Setup I²C address and read/write mode for the next transfer. i2c.stop(id) -- Send an I²C stop condition. if Status == true then print("Addrss - "..count.." Detected device address is 0x" .. string.format("%02x", count) .. " (" .. count ..")") elseif Status == false then print("Addrss - "..count.." nil") end end print("Scanning End")
Results
Scanning Started.... Addrss - 0 nil Addrss - 1 nil Addrss - 2 nil Addrss - 3 nil Addrss - 4 nil Addrss - 5 nil Addrss - 6 nil Addrss - 7 nil Addrss - 8 nil Addrss - 9 nil Addrss - 10 nil Addrss - 11 nil Addrss - 12 nil Addrss - 13 nil Addrss - 14 nil Addrss - 15 nil Addrss - 16 nil Addrss - 17 nil Addrss - 18 nil Addrss - 19 nil Addrss - 20 nil Addrss - 21 nil Addrss - 22 nil Addrss - 23 nil Addrss - 24 nil Addrss - 25 nil Addrss - 26 nil Addrss - 27 nil Addrss - 28 nil Addrss - 29 nil Addrss - 30 nil Addrss - 31 nil Addrss - 32 nil Addrss - 33 nil Addrss - 34 nil Addrss - 35 nil Addrss - 36 nil Addrss - 37 nil Addrss - 38 nil Addrss - 39 nil Addrss - 40 nil Addrss - 41 nil Addrss - 42 nil Addrss - 43 nil Addrss - 44 nil Addrss - 45 nil Addrss - 46 nil Addrss - 47 nil Addrss - 48 nil Addrss - 49 nil Addrss - 50 nil Addrss - 51 nil Addrss - 52 nil Addrss - 53 nil Addrss - 54 nil Addrss - 55 nil Addrss - 56 nil Addrss - 57 nil Addrss - 58 nil Addrss - 59 nil Addrss - 60 nil Addrss - 61 nil Addrss - 62 nil Addrss - 63 nil Addrss - 64 nil Addrss - 65 nil Addrss - 66 nil Addrss - 67 nil Addrss - 68 nil Addrss - 69 nil Addrss - 70 nil Addrss - 71 nil Addrss - 72 nil Addrss - 73 nil Addrss - 74 nil Addrss - 75 nil Addrss - 76 nil Addrss - 77 nil Addrss - 78 nil Addrss - 79 nil Addrss - 80 nil Addrss - 81 nil Addrss - 82 nil Addrss - 83 nil Addrss - 84 nil Addrss - 85 nil Addrss - 86 nil Addrss - 87 Detected device address is 0x57 (87) Addrss - 88 nil Addrss - 89 nil Addrss - 90 nil Addrss - 91 nil Addrss - 92 nil Addrss - 93 nil Addrss - 94 nil Addrss - 95 nil Addrss - 96 nil Addrss - 97 nil Addrss - 98 nil Addrss - 99 nil Addrss - 100 nil Addrss - 101 nil Addrss - 102 nil Addrss - 103 nil Addrss - 104 Detected device address is 0x68 (104) Addrss - 105 nil Addrss - 106 nil Addrss - 107 nil Addrss - 108 nil Addrss - 109 nil Addrss - 110 nil Addrss - 111 nil Addrss - 112 nil Addrss - 113 nil Addrss - 114 nil Addrss - 115 nil Addrss - 116 nil Addrss - 117 nil Addrss - 118 nil Addrss - 119 nil Addrss - 120 nil Addrss - 121 nil Addrss - 122 nil Addrss - 123 nil Addrss - 124 nil Addrss - 125 nil Addrss - 126 nil Addrss - 127 nil Scanning End >
ESP32 NodeMCU I2C Interface: Code Explanation
This table format provides a structured breakdown of each section of the code along with its explanation.
Section | Explanation |
---|---|
Setup Section | |
id | Defines the I2C interface as software-based. |
pinSDA | Specifies the GPIO pin used for the data line (SDA) of the I2C interface. |
pinSCL | Specifies the GPIO pin used for the clock line (SCL) of the I2C interface. |
speed | Sets the speed of the I2C communication to slow. Only i2c.SLOW speed is supported. |
Initialization | |
i2c.setup() | Initializes the I2C module with the specified parameters: interface ID, SDA pin, SCL pin, and speed. |
Scanning for Devices | |
print(“Scanning Started….”) | Prints a message indicating the start of device scanning. |
Looping through device addresses | Iterates through device addresses from 0 to 127. |
i2c.start() | Sends an I2C start condition. |
i2c.address() | Sets up the I2C address and read/write mode for the next transfer. |
i2c.stop() | Sends an I2C stop condition. |
Check Status | Checks the status of the address detection: If Status is true, prints the detected device address in hexadecimal and decimal format. If Status is false, prints “nil” for the address. |
print(“Scanning End”) | Prints a message indicating the end of device scanning. |
Suggest
- ESP8266 NodeMCU Module – I2C : https://aruneworld.com/embedded/esp8266/esp8266-nodemcu/esp8266-nodemcu-module-i2c/