//i2C用ライブラリー #include //i2Cアドレス #define ADS1115_ADDR 0x48 //ASS1115用 #define ASD1115_CONVERSION 0x00 // to "conversion" #define ASD1115_CONFIG 0x01 // 0x01 to "config" #define ASD1115_CONFREGHi 0xC3 // 0xc3 OS=1,MUX=1nn,PGA=001(x1),MODE=1(single) #define ASD1115_CONFREGLo 0xE3 // 0xe3 DR=111(860SPS),00011(no comparator) int Channel; int avrg; //平均数 int values[4][10]; int avval[4]; float dca[4]; //DC電流 void setup() { Serial.begin(115200); Wire.begin(2,0); //PinNo=SDA,SCL D4=SDA,D3=SCL に設定 ESP-12E //■ADS1115  ADS1115_request(Channel); avrg = 0; Channel=0; delay(1000); } void loop() { //■ADS1115 // read out the result values[Channel][avrg] = ADS1115_answer(); // trigger next conversion Channel = (Channel + 1) % 4; // 0,1,2,3,0,1,2,3... ADS1115_request(Channel); // delay (minimum 0.3ms) delay(10); // output to monitor if (Channel == 0) { for (int i1=0; i1<= 3 ; i1++){ avval[i1]=0; for(int i2=0; i2<=9 ; i2++){ avval[i1] = avval[i1] + values[i1][i2]; } avval[i1] = avval[i1]/10; } avrg = (avrg + 1) % 10; // 0,1,2,3,4,5,6,7,8,9,0,1... Serial.print(" A0="); Serial.print(avval[0],DEC); Serial.print(", A1="); Serial.print(avval[1],DEC); Serial.print(", A2="); Serial.print(avval[2],DEC); Serial.print(", A3="); Serial.print(avval[3],DEC); Serial.println(""); } } //############################ // //    サブルーチン // //############################ // triger conversion on An void ADS1115_request(int n) { // trigger conversion Wire.beginTransmission(ADS1115_ADDR); Wire.write(ASD1115_CONFIG); Wire.write(ASD1115_CONFREGHi | (n << 4)); Wire.write(ASD1115_CONFREGLo); Wire.endTransmission(); } // read conversion result int ADS1115_answer() { short int value; // read out the result Wire.beginTransmission(ADS1115_ADDR); Wire.write(ASD1115_CONVERSION); Wire.endTransmission(); Wire.requestFrom(ADS1115_ADDR, 2); // read out 2 bytes value = Wire.read() << 8; value |= Wire.read(); return value ; // 0V=0, 4.096V=32,768 return }