#include<Wire.h>#defineslave_address0x44#defineSensor_power_port6 // Arduino uno, Arduino mkr 1010, esp32// #define Sensor_power_port 16 // esp8266#definePower_enabledigitalWrite(Sensor_power_port, HIGH)#definePower_disabledigitalWrite(Sensor_power_port, LOW)#defineHumidity_Resolution_read_commandWire.write(0x06) //Humidity Sensor Resolution – Read Register (bits [11:10])#define Temperature_Resolution_read_command Wire.write(0x11) //Temperature Sensor Resolution – Read Register (bits [11:10])
#defineHumidity_Resolution_write_commandWire.write(0x46) //Humidity Sensor Resolution – Write Register (bits [11:10])#define Temperature_Resolution_write_command Wire.write(0x51) //Temperature Sensor Resolution – Write Register (bits [11:10])
voidsetup(){Wire.begin();// arduino uno, Arduino mkr 1010//Wire.begin(7,8,5000); //esp32//Wire.begin(4,5,5000); //esp8266Serial.begin(9600);pinMode(Sensor_power_port, OUTPUT);}voidloop(){int Status;int RegisterValueHigh;int RegisterValueLow;//===Module Power Reset===Power_disable; //전원 끔delay(1);Power_enable;delay(2); //10msec 이내에 신호 전송되어야함// Step 1. ETH-01D I2C command 전송//프로그래밍 모드로 들어가기 위한 명령, 명령어 처리 시까지 120usec 시간이 소요됨.Wire.beginTransmission(slave_address);Wire.write(0xA0);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delayMicroseconds(120);//Step 2. 온습도 Resolution 요청 CommandWire.beginTransmission(slave_address);Humidity_Resolution_read_command;Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delay(1);//Step 3. 온습도 Resolution 응답Wire.requestFrom(slave_address,3);if (Wire.available()) {Status =Wire.read();RegisterValueHigh =Wire.read();RegisterValueLow =Wire.read();Serial.print(Status, HEX);Serial.print(" ");Serial.print(RegisterValueHigh,HEX);Serial.print(" ");Serial.print(RegisterValueLow, HEX);Serial.println(" <= Read the register contents");}//Step 4. 온습도 Resolution 쓰기 Command, 데이터 기록시간은 14msec가 필요함Wire.beginTransmission(slave_address);Humidity_Resolution_write_command;Wire.write(0xC); // Sensor Resolution Change high bitSerial.print("Write 0xC ");Serial.println(" <= Write the register contents");Wire.write(0x12); // Sensor Resolution low bitWire.endTransmission();delay(14);//Step 5. 프로그래밍 모드에서 일반 모드로 전환Wire.beginTransmission(slave_address);Wire.write(0x80);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delay(1000);}
센서 ID 읽기 예제 코드
#include<Wire.h>#defineslave_address0x44#defineSensor_power_port6 // Arduino uno, Arduino mkr 1010, esp32//#define Sensor_power_port 16 //esp8266#definePower_enabledigitalWrite(Sensor_power_port, HIGH)#definePower_disabledigitalWrite(Sensor_power_port, LOW)voidsetup(){Wire.begin(); // Arduino uno, Arduino mkr 1010// Wire.begin(7,8,5000); //esp32// Wire.begin(4,5,5000); //esp8266Serial.begin(9600);pinMode(Sensor_power_port, OUTPUT);}voidloop(){int Status;int RegisterValueHigh;int RegisterValueLow;//===Module Power Reset===Power_disable;delay(1);Power_enable;delay(2); //10msec 이내에 신호 전송되어야함// 레지스터 주소 쓰기===//프로그래밍 모드로 들어가기 위한 명령, 명령어 처리 시까지 120usec 시간이 소요됨.Wire.beginTransmission(slave_address);Wire.write(0xA0);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delayMicroseconds(120);//Sensor 상위 id 요청Wire.beginTransmission(slave_address);Wire.write(0x1E);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delay(1);//Sensor 상위 id 응답Wire.requestFrom(slave_address,3);if (Wire.available()) {Status =Wire.read();RegisterValueHigh =Wire.read();RegisterValueLow =Wire.read();Serial.print(" Sensor id_high: ");Serial.print(Status, HEX); // status success = 0x81Serial.print(" ");Serial.print(RegisterValueHigh, HEX);Serial.print(" ");Serial.println(RegisterValueLow, HEX);}//Sensor 하위 id 요청Wire.beginTransmission(slave_address);Wire.write(0x1F);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delay(1);//Sensor 하위 id 응답Wire.requestFrom(slave_address,3);if (Wire.available()) {Status =Wire.read();RegisterValueHigh =Wire.read();RegisterValueLow =Wire.read();Serial.print(" Sensor id_low: ");Serial.print(Status, HEX); // status success = 0x81Serial.print(" ");Serial.print(RegisterValueHigh, HEX);Serial.print(" ");Serial.println(RegisterValueLow, HEX);}// 프로그래밍 모드에서 일반 모드로 전환Wire.beginTransmission(slave_address);Wire.write(0x80);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delay(1000);}
Address 읽기 및 쓰기 예제 코드
#include<Wire.h>#defineslave_address0x44#definechange_slave_address0x44 // <- This address is subject to change#defineSensor_power_port6 // Arduino uno, Arduino mkr 1010, esp32// #define Sensor_power_port 16 // esp8266#definePower_enabledigitalWrite(Sensor_power_port, HIGH)#definePower_disabledigitalWrite(Sensor_power_port, LOW)#defineAddress_read_commandWire.write(0x1C)#defineAddress_write_commandWire.write(0x5C) voidsetup(){Wire.begin();// arduino uno, Arduino mkr 1010//Wire.begin(7,8,5000); //esp32//Wire.begin(4,5,5000); //esp8266Serial.begin(9600);pinMode(Sensor_power_port, OUTPUT);}voidloop(){int Status;int RegisterValueHigh;int RegisterValueLow;//===Module Power Reset=== Power_disable; //전원 끔delay(1); Power_enable;delay(2); //10msec 이내에 신호 전송되어야함// Step 1. ETH-01D I2C command 전송//프로그래밍 모드로 들어가기 위한 명령, 명령어 처리 시까지 120usec 시간이 소요됨.Wire.beginTransmission(slave_address);Wire.write(0xA0);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delayMicroseconds(120);//Step 2. 온습도 Address 요청 CommandWire.beginTransmission(slave_address); Address_read_command;Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delay(1);//Step 3. 온습도 Address 응답 ex)81 00 44Wire.requestFrom(slave_address,3);if (Wire.available()) { Status =Wire.read(); RegisterValueHigh =Wire.read(); RegisterValueLow =Wire.read();Serial.print(Status, HEX); // 0x81 = status succesSerial.print(" ");Serial.print(RegisterValueHigh,HEX);Serial.print(" ");Serial.print(RegisterValueLow, HEX);Serial.println(" <= Read the register contents"); }// adress 읽은 뒤 [6:0] 비트만 변경 #if0 // adress 읽은 뒤 #if 0 -> #if 1//Step 4. 온습도 Address 쓰기 Command, 데이터 기록시간은 14msec가 필요함 Wire.beginTransmission(slave_address); Address_write_command; Wire.write(0x00); Wire.write(change_slave_address); // 변경 할 address write Wire.endTransmission(); delay(14); Serial.print(change_slave_address, HEX); Serial.println(" <= Write the register contents");#endif//Step 5. 프로그래밍 모드에서 일반 모드로 전환Wire.beginTransmission(slave_address);Wire.write(0x80);Wire.write(0x00);Wire.write(0x00);Wire.endTransmission();delay(1000);}