NodeMCU I2C với Arduino IDE – Giao tiếp Arduino với NodeMCU thông qua I2C

Giới thiệu

I2C (Inter-Integrated Circuit) là giao thức kết nối giao diện bus nối tiếp. Nó cũng được gọi là TWI (hai dây giao diện) vì nó chỉ sử dụng hai dây để giao tiếp. Hai dây này là SDA (đường dữ liệu) và SCL (đường xung nhịp đồng hồ).

I2C là giao thức truyền thông dựa trên sự thừa nhận,tức là phát tín hiệu kiểm tra xác nhận từ người nhận sau khi truyền dữ liệu để biết liệu dữ liệu có được nhận thành công bởi người nhận hay không.

I2C có hai chế độ làm việc:

  • Master mode
  • Slave mode

Dây SDA (đường dữ liệu) được sử dụng để trao đổi dữ liệu giữa thiết bị master và thiết bị slave. SCL (đường xung nhịp đồng hồ) được sử dụng cho đồng hồ đồng bộ ở giữa thiết bị master và slave.

Thiết bị master bắt đầu giao tiếp với một thiết bị slave. Thiết bị master yêu cầu địa chỉ thiết slave để bắt đầu giao tiếp với thiết bị slave. Thiết bị Slave phản hồi cho thiết bị master khi thiết bị master được giải quyết.

NodeMCU có hỗ trợ chức năng I2C trên các chân GPIO của nó. Do chức năng nội bộ trên ESP-12E, không thể sử dụng tất cả các GPIO cho chức năng I2C. Vì vậy, hãy kiểm tra trước khi sử dụng bất kỳ GPIO nào cho ứng dụng I2C.

Ví dụ

Viết chương trình Arduino cho NodeMCU như thiết bị I2C master và viết chương trình Arduino cho Arduino như thiết bị I2C Slave. Thiết bị master sẽ gửi chuổi hello đến thiết bị slave và thiết bị slave sẽ gửi chuổi hello phản hồi cho thiết bị master.

Master Device: NodeMCU

Slave Device: Arduino Uno

Slave Device Address: 8

Sơ đồ kết nối Arduino với NodeMCU

NodeMCU I2C với Arduino IDE - Giao tiếp Arduino với NodeMCU thông qua I2C

Arduino Sketch for NodeMCU (Master I2C Device)

#include <Wire.h>

void setup() {
 Serial.begin(9600); /* begin serial for debug */
 Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}

void loop() {
 Wire.beginTransmission(8); /* begin with device address 8 */
 Wire.write("Hello Arduino");  /* sends hello string */
 Wire.endTransmission();    /* stop transmitting */

 Wire.requestFrom(8, 13); /* request & read data of size 13 from slave */
 while(Wire.available()){
    char c = Wire.read();
  Serial.print(c);
 }
 Serial.println();
 delay(1000);
}

Arduino Sketch for Arduino Uno (Slave I2C Device)

#include <Wire.h>

void setup() {
 Wire.begin(8);                /* join i2c bus with address 8 */
 Wire.onReceive(receiveEvent); /* register receive event */
 Wire.onRequest(requestEvent); /* register request event */
 Serial.begin(9600);           /* start serial for debug */
}

void loop() {
 delay(100);
}

// function that executes whenever data is received from master
void receiveEvent(int howMany) {
 while (0 <Wire.available()) {
    char c = Wire.read();      /* receive byte as a character */
    Serial.print(c);           /* print the character */
  }
 Serial.println();             /* to newline */
}

// function that executes whenever data is requested from master
void requestEvent() {
 Wire.write("Hello NodeMCU");  /*send string on request */
}

Kết quả

Kết quả chương trình trên màn hình Serial thiết bị Slave (Arduino)
Kết quả chương trình trên màn hình Serial thiết bị Master (NodeMCU)

Download File

Source Code

Bài viết các bạn có thể tham khảo:

Nguồn tham khảo: electronicwings.com

Viết một bình luận

This site uses Akismet to reduce spam. Learn how your comment data is processed.