# LAB: I2C Protocol

## จุดประสงค์

1. เพื่อศึกษาการอ่านค่าข้อมูล sensor ที่ส่งข้อมูลผ่านสาย I2C
2. เพื่อศึกษาการส่งข้อมูลระหว่าง Microcontroller ไปยัง Microcontroller โดยทั้งสองใช้การสื่อสารกันผ่าน I2C interface

## LAB1: Basic I2C Communication

&#x20;   การทดลองนี้เป็นการเขียนโปรแกรมลงบอร์ด M5Stack ให้ค้นหา Address I2C ของ sensor ที่เชื่อมต่อกับบอร์ด M5Stack&#x20;

### อุปกรณ์ทดลอง

<table><thead><tr><th width="374">Components</th><th>Quantity</th></tr></thead><tbody><tr><td><a href="https://docs.m5stack.com/#/en/app/demo-board">M5Stack Demo Board</a></td><td>1</td></tr><tr><td><a href="http://www.qdkingst.com/en">Logic Analyzer (รุ่น LA1016 or LA2016) </a></td><td>1</td></tr></tbody></table>

![อุปกรณ์การทดลอง](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F3NUT8OsxzpVsvC0754z3%2FUntitled.png?alt=media\&token=6bf3a1ba-be1b-446c-a291-135e8e8dcf99)

### โปรแกรม

1. Arduino IDE
2. โปรแกรม KingstVIS

### ตั้งค่าโปรแกรม KingstVIS&#x20;

* เชื่อมต่อ logic Analyzer กับ คอมพิวเตอร์&#x20;
* เปิดโปรแกรม Kingst VIS&#x20;
* ไปที่ Analyzers >> คลิ๊ก icon add(+) >> เลือก I2C&#x20;

![เลือกประเภท protocol ที่ต้องการวิเคราะห์](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI35iTX6rYT_LpZqH8g%2F-MI37KOB41mUkhwzsGLP%2Fimage.png?alt=media\&token=ff9b29a6-1177-44d8-9ab4-0005da66c156)

* ไปที่ Analyzers >> คลิ๊ก icon setting >> Edit  ตั้งค่าตามรูปด้านล่าง >> OK

![ตั้งค่า channel สำหรับวิเคราะห์ การส่งข้อมูลของ I2C โปรโตคอล  ](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIYSRtwET1VlZVyHwsK%2F-MIYd12XgBHjwvBxJq7d%2Fimage.png?alt=media\&token=ef238288-c455-41fc-8cba-e499daf1a16c)

* ไปที่ Analyzers อีกครั้ง >> คลิ๊ก icon setting >> Display Format >>  Dec เพื่อให้  decoder เป็นข้อมูลชุดตัว แบบเลขฐาน 10 (DEC)

![ตั้งค่า Display Format ](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIYSRtwET1VlZVyHwsK%2F-MIYfWGOfi7C0NhexQVy%2Fimage.png?alt=media\&token=30cbeadd-99b6-4d70-ab66-c7343ac1ca74)

* ไปที่ Channel 0  เลือกกด Indicates "rising edge trigger"

![ตั้งค่า Trigger Indicates "rising edge trigger"](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIYSRtwET1VlZVyHwsK%2F-MIYgB6tHst-TXbrwy0w%2Fimage.png?alt=media\&token=ee29715e-72ca-4655-ae32-98e92e8594a2)

### PIN Connect

การต่อวงจร: บอร์ด M5Stack ต่อกับ เซนเซอร์ [DHT12 ](https://pdf1.alldatasheet.com/datasheet-pdf/view/1222805/ETC1/DHT12.html)และ [BMP280](https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/datasheet/hat/BMP280-DS001-11_en.pdf)

<table><thead><tr><th width="149" align="center">M5Stack</th><th align="center">Sensor BMP280</th><th align="center">Sensor DHT12</th></tr></thead><tbody><tr><td align="center">I2C (21D)</td><td align="center">SDA</td><td align="center">SDA</td></tr><tr><td align="center">I2C (22C)</td><td align="center">SCL</td><td align="center">SCL</td></tr><tr><td align="center">GND</td><td align="center">GND</td><td align="center">GND</td></tr></tbody></table>

ตัวอย่างวงจร

![การเชื่อมต่อวงจรเซนเซอร์ DHT12 และ BH280 กับบอร์ด M5Stack ](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIbM_VN9mZMLTvW-Li0%2F-MIbT2VHlAtx1wimw2LD%2Fimage.png?alt=media\&token=5b3bd7e1-1573-4314-adc9-46a8db3b388d)

### **Code**&#x20;

M5Stack scan I2C address

```arduino
// Code I2C scanner
#include <Wire.h>

void setup() {
  Wire.begin();

  Serial.begin(115200);
  while (!Serial); // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}

void loop() {
  int nDevices = 0;

  Serial.println("Scanning...");

  for (byte address = 1; address < 127; ++address) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

      ++nDevices;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  } else {
    Serial.println("done\n");
  }
  delay(5000); // Wait 5 seconds for next scan
}
```

* การเขียนโปรแกรม scan หา Address ของเซนเซอร์นั้นใช้เพื่อทดสอบว่าเซนเซอร์ที่เชื่อมต่อนั้นอยู่ในเครือข่าย I2C ของบอร์ด Microcontroller  แล้ว
* เราสามารถหาค่า address ของเซนเซอร์ได้จาก datasheet ของเซนเซอร์ หรือ จาก code การทดลองนี้ ถ้าต้องการหาค่า I2C address ของ เซนเซอร์ตัวใดให้เชื่อมต่อเพียงเซนเซอร์ที่ต้องการหาและถอดวงจรเซนเซอร์ที่ไม่ต้องการออก

### บันทึกผลการทดลอง

1. หา I2C Address  ของทั้งสอง sensor และ I2C Address ของ M5Stack แล้วเติมลงในตารางด้านล่าง

|            Devices            | I2C Address |
| :---------------------------: | :---------: |
|  AIR PRESSURE Sensor (BMP280) |    .....    |
| Temp, Humidity Sensor (DHT12) |    .....    |
|            M5Stack            |    .....    |

2\. บันทึกภาพวงจรการทดลองและภาพผลที่แสดงบน Serial monitor ผลการ Scan หาค่า I2C Address ของเซนเซอร์

3\. อธิบายว่าเพราะอะไรเราต้องรู้ค่า I2C Address ของอุปกรณ์ เมื่อเราต้องการใช้งานโปรโตคอล I2C

## LAB2: M5Stack อ่านค่า DHT12 Sensor &#x20;

&#x20;    ทดลองให้ M5Stack อ่านค่า DHT12 Sensor การทดลองนี้เป็นการเขียนโปรแกรมลงบอร์ด M5Stack ให้อ่านค่า DHT12 sensor และใช้ logic analyzer แสดงค่า sensor เมื่อ M5Stack ส่งคำสั่งอ่านค่า DHT12 sensor

### อุปกรณ์ทดลอง

<table><thead><tr><th width="374">Components</th><th>Quantity</th></tr></thead><tbody><tr><td><a href="https://docs.m5stack.com/#/en/app/demo-board">M5Stack Demo Board</a></td><td>1</td></tr><tr><td><a href="http://www.qdkingst.com/en">Logic Analyzer (รุ่น LA1016 or LA2016) </a></td><td>1</td></tr></tbody></table>

![อุปกรณ์การทดลอง](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F3NUT8OsxzpVsvC0754z3%2FUntitled.png?alt=media\&token=6bf3a1ba-be1b-446c-a291-135e8e8dcf99)

### โปรแกรม

1. Arduino IDE
2. โปรแกรม KingstVIS

### ตั้งค่าโปรแกรม KingstVIS&#x20;

*"ใช้แบบเดิม"*

### PIN Connect

การต่อวงจร: บอร์ด M5Stack ต่อกับเซนเซอร์ DHT12 และ Logic analyzer

|  M5Stack  | Sensor DHT12 | Logic Analyzer |
| :-------: | :----------: | :------------: |
| I2C (21D) |      SDA     |    Channel 0   |
| I2C (22C) |      SCL     |    Channel 1   |
|    GND    |      GND     |       GND      |

![การเชื่อมต่อ DHT12 Sensor และ Logic analyzer กับบอร์ด M5Stack](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIb7sRct0hC3ZL3uBwl%2F-MIbI9ulp-eIQFxhqCS8%2Fimage.png?alt=media\&token=dc875bde-d1aa-48fa-93b9-aecc569ce34f)

{% hint style="danger" %}
ติดตั้งไลบรารี่ DHT12 สำหรับเขียนโปรแกรม Arduino IDE อ่านค่า DHT12 sensor&#x20;

* เปิดโปรแกรม Arduino IDE&#x20;
* ไปที่ Sketch >> Include Library >>Manage Libraries...
* พิมพ์ ค้นหา DHT12 >> เลือก DHT12 sensor library >> Install
* หลังจากติดตั้งเรียบร้อยให้ปิดโปรแกรมแล้วเปิดใหม่&#x20;
  {% endhint %}

สร้างไฟล์แล้วเขียนโปรแกรมตาม code ด้านล่าง แล้ว upload code ลง M5Stack&#x20;

### Code

M5Stack read data DHT12 Sensor

```arduino
//#include <M5Stack.h>
#include "DHT12.h"
#include <Wire.h> //The DHT12 uses I2C comunication.

DHT12 dht12; //Preset scale CELSIUS and ID 0x5c.

void setup() {
//    M5.begin();
  Serial.begin(115200);
  Wire.begin();
//    M5.Lcd.setCursor(80, 0, 4);
//    M5.Lcd.print("TEMPERATURE");
 
}

void loop() {
 
    float tmp = dht12.readTemperature();
    float hum = dht12.readHumidity();
//    M5.Lcd.setCursor(30, 100, 4); //display via moniter
//    M5.Lcd.printf("Temp: %2.1f  Humi: %2.0f%%", tmp, hum); //display via moniter
    Serial.printf("Temp: %2.1f  Humi: %2.0f%% \n", tmp, hum); //display via serial monitor
    delay(100);
}
```

### บันทึกผลการทดลอง

1. บันทึกผลการทดลองที่ Logic analyzer แสดง เทียบกับ Serial monitor และอธิบายว่าข้อมูลที่ได้เป็นอย่างไร

## LAB3: MCU to MCU communication

&#x20;      การทดลองนี้เป็นการเขียนโปรแกรมลงบอร์ด M5Stack ให้ทำหน้าที่เป็น Master อ่านข้อความจาก Arduino UNO ที่ทำหน้าที่เป็น Slave ผ่าน I2C bus

### อุปกรณ์ทดลอง

<table><thead><tr><th width="374">Components</th><th>Quantity</th></tr></thead><tbody><tr><td><a href="https://docs.m5stack.com/#/en/app/demo-board">M5Stack Demo Board</a></td><td>1</td></tr><tr><td><a href="http://www.qdkingst.com/en">Logic Analyzer (รุ่น LA1016 or LA2016) </a></td><td>1</td></tr><tr><td>Arduino UNO</td><td>1</td></tr></tbody></table>

![อุปกรณ์การทดลอง](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI7e2JzqSuDnogRcPUF%2F-MI7ykoGlh7U3utnXOB7%2Fimage.png?alt=media\&token=7751c5ef-ee9c-4c8c-92a4-7513f7981c35)

### ตั้งค่าโปรแกรม KingstVIS&#x20;

* เชื่อมต่อ logic Analyzer กับ คอมพิวเตอร์&#x20;
* เปิดโปรแกรม Kingst VIS&#x20;
* ไปที่ Analyzers >> คลิ๊ก icon add(+) >> เลือก I2C&#x20;

![เลือกประเภท protocol ที่ต้องการวิเคราะห์](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI35iTX6rYT_LpZqH8g%2F-MI37KOB41mUkhwzsGLP%2Fimage.png?alt=media\&token=ff9b29a6-1177-44d8-9ab4-0005da66c156)

* ไปที่ Analyzers >> คลิ๊ก icon setting >> Edit  ตั้งค่าตามรูปด้านล่าง >> OK

![ตั้งค่า channel สำหรับวิเคราะห์ การส่งข้อมูลของ I2C โปรโตคอล  ](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIYSRtwET1VlZVyHwsK%2F-MIYd12XgBHjwvBxJq7d%2Fimage.png?alt=media\&token=ef238288-c455-41fc-8cba-e499daf1a16c)

* ไปที่ Analyzers อีกครั้ง >> คลิ๊ก icon setting >> Display Format >>  ASCII เพื่อให้  decoded เป็นข้อมูลชุด แบบตัวอักษร (ASCII)

![ตั้งค่า Display Format](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIbp4JGzzI9wrGZEqzt%2F-MIbxHjOeCZZikMNs99R%2Fimage.png?alt=media\&token=e469702d-338d-48aa-88e4-addec0bd101f)

* ไปที่ Channel 0  เลือกกด Indicates "rising edge trigger"

![ตั้งค่า Trigger Indicates "rising edge trigger"](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIYSRtwET1VlZVyHwsK%2F-MIYgB6tHst-TXbrwy0w%2Fimage.png?alt=media\&token=ee29715e-72ca-4655-ae32-98e92e8594a2)

### PIN Connect

การต่อวงจร: บอร์ด M5Stack ต่อกับบอร์ด Arduino UNO และ Logic Analyzer&#x20;

|    M5Stack    |  Arduino UNO  | Logic Analyzer |
| :-----------: | :-----------: | :------------: |
| SDA (pin 21D) | SDA  (pin A4) |    Channel 0   |
| SCL (pin 22C) | SCL ( pin A5) |    Channel 1   |
|      GND      |      GND      |       GND      |

ตัวอย่างวงจร

![การต่อวงจร I2C Pin ของบอร์ด Arduino UNO และ Logic Analyzer กับบอร์ด M5Stack ](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MIbcVLk7QZpSRKo_O57%2F-MIbjhf9sPDWTSLVxJVt%2Fimage.png?alt=media\&token=9b5689e3-ca4d-4b4e-9656-7366a65d5254)

### Code: M5Stack is a master reader

```arduino
// Wire Master Reader

#include <Wire.h>
int i =0;
void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(115200);  // start serial for output
}

void loop() {
  if(i==0){
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);   
  
    // print the character
 }
 i++;
  }

  delay(500);
}
```

### Code: Arduino UNO is a slave&#x20;

```arduino
// Wire Slave Sender

#include <Wire.h>

void setup() {
  Wire.begin(8);        // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
   
}
```

### บันทึกผลการทดลอง

1. บันทึกผล Serial monitor แสดงข้อความที่บอร์ด M5Stack (Master) ได้รับ และข้อความบนโปรแกรม KingstVIS ของ logic analyzer
2. อธิบายผลการทดลองที่ logic analyzer แสดงเทียบกับ serial monitor ของ M5Stack ที่ทำหน้าที่เป็น master
