echo '' ;

Archives

ESP8266 NodeMCU Module – I2C

The code provided demonstrates the usage of the I2C (Inter-Integrated Circuit) protocol on an ESP8266 NodeMCU module. I2C is a popular serial communication protocol used to connect multiple peripherals with a microcontroller or a microprocessor. In this context, the code initializes the I2C interface on the NodeMCU module and showcases how to read data from a specific register of an I2C device.

Pr-Request to Lean

ESP8266 I2C Core

ESP8266 hardware supports only one i2c. But in  bit-banking(Bar Metal code) method you can use all your gpio pin as i2c pins. ESP8266 NodeMCU firmware platform supports only standard speed mode (Sm) 100khz . That means ESP8266 NodeMCU firmware supports 100Kbps for data transfer.


ESP8266 I2C Scanner

  • Required NodeMCU Modules (Firmware) : GPIO Module, I2C Module
  • Required hardware : ESP8266 with Programmer (or)  NodeMCU Dev Kit
  • Required software tools : ESPlorer IDE Tool

Code

This script helps identify I2C devices connected to the ESP8266 NodeMCU board by scanning through all possible device addresses and checking for responses. It’s useful for troubleshooting and verifying the connectivity of I2C devices in the system.

lua
-- https://www.aruneworld.com/embedded/espressif/esp8266/esp8266_nodemcu/
-- Tested By: Arun(20170112)
-- Example Name: AEW_I2C_DeviceScaner.lua
----------------------------------------

-- Setup
local id = 0 -- always 0
local pinSDA = 2 -- 1~12, IO index
local pinSCL = 1 -- 1~12, IO index
local 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.
    local 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")

Explanation

This Lua script is designed to scan for I2C devices connected to an ESP8266 NodeMCU board. Here’s a breakdown of its functionality:

Setup:

  • It initializes the variables id, pinSDA, pinSCL, and speed.
  • id is set to 0, which typically refers to the first I2C interface on ESP8266 NodeMCU.
  • pinSDA and pinSCL are set to the GPIO pins used for SDA (data line) and SCL (clock line), respectively.
  • speed is set to i2c.SLOW, indicating the desired speed for I2C communication.

Initialization:

  • It initializes the I2C module using i2c.setup() with the specified id, pinSDA, pinSCL, and speed.

Scanning for Devices:

  • It iterates through possible device addresses from 0 to 127.
  • For each address, it attempts to establish communication with the device by sending a start condition (i2c.start()), setting the address (i2c.address()), and then stopping the communication (i2c.stop()).
  • If communication is successful (Status == true), it prints the detected device address in hexadecimal and decimal format.
  • If communication fails (Status == false), it prints “nil” for that address.

Printing Results:

  • It prints “Scanning Started….” before starting the scanning loop.
  • It prints “Scanning End” after the scanning loop is complete.

This table breaks down the code into its key components, making it easier to understand the flow and purpose of each part of the script.

LineExplanation
1-4Introduction and setup of necessary variables and constants.
6Initialization of the I2C module with specified parameters.
8-15Loop to iterate through possible device addresses (0 to 127).
10Start condition for I2C communication.
11Attempt to set the address and read/write mode for the next transfer.
12Stop condition for I2C communication.
13-15Check if the status of the communication is successful and print the detected device address.
17Print “Scanning Started….” to indicate the beginning of the scanning process.
18-21Loop to scan through all possible device addresses and attempt communication.
22-25Print “Scanning End” to indicate the completion of the scanning process.

ESP8266 I2C Example

Code

id = 0
sda = 1
scl = 2

-- Initialize I2C, set pin1 as SDA, set pin2 as SCL
i2c.setup(id, sda, scl, i2c.SLOW)

-- User-defined function: Read from reg_addr content of dev_addr
function read_reg(dev_addr, reg_addr)
    i2c.start(id)
    i2c.address(id, dev_addr, i2c.TRANSMITTER)
    i2c.write(id, reg_addr)
    i2c.stop(id)
    i2c.start(id)
    i2c.address(id, dev_addr, i2c.RECEIVER)
    c = i2c.read(id, 1)
    i2c.stop(id)
    return c
end

-- Get content of register 0xAA of device 0x77
reg = read_reg(0x77, 0xAA)
print(string.byte(reg))

Code Explanation

This code sets up an I2C interface, defines a function to read data from a specific register of an I2C device, and then uses this function to read the content of a specific register from a specific device address.

Line(s)Explanation
1-3Define the I2C interface parameters: id for the I2C bus, sda for the data line, and scl for the clock line.
5Initialize the I2C communication with the specified parameters (I2C bus, SDA pin, SCL pin, and speed).
8-16Define a custom function read_reg to read data from a specified register address of a given device address.
9-11Start the I2C communication and address the device in write mode to specify the register to read from.
12Write the register address to the device.
13Stop the I2C communication after writing the register address.
14-16Restart the I2C communication, address the device in read mode, read one byte of data from the device, and then stop the I2C communication.
17-19Call the read_reg function to read the content of register 0xAA from device address 0x77.
20Print the byte value of the register content returned by the read_reg function.

See Also

NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 Home Automation
Others
NodeMCU All Post
Sitemap

ESP32 NodeMCU Module – I2C Interface

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 Interface

Pr-Request to Lean


ESP32 NodeMCU Module I2C Functions

FunctionDescription
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.

SectionExplanation
Setup Section
idDefines the I2C interface as software-based.
pinSDASpecifies the GPIO pin used for the data line (SDA) of the I2C interface.
pinSCLSpecifies the GPIO pin used for the clock line (SCL) of the I2C interface.
speedSets 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 addressesIterates 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 StatusChecks 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


NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 Home Automation
Others
NodeMCU All Post
Sitemap

Embedded Protocol – I2C

 I²C (Inter-Integrated Circuit) is a bi-directional two wires and serial data transmission communication protocol developed by  Philips (Now NXP Semiconductor) in 1982. I2C is a Half-duplex communication protocol  – (I2c can’t send and receive same time in the bus-data line). Multi-master can communicate with multi-salve. (Note: Communication is restricted between two masters; however, a single master can communicate with either a single slave or multiple slaves.) I2C is a Level Triggering. A device that sends data onto the bus is defined as a transmitter, and a device receiving data is defined as a receiver.

The bus must be controlled by a master device, responsible for generating the Start and Stop conditions. In contrast, certain devices such as LCDs, EEPROMs, and RTCs function as slaves. It’s important to note that both master and slave devices can operate as either transmitters or receivers. However, the master device plays a pivotal role in determining which mode is activated.

Read more… →