#include void setup() { Wire.setClock(100000); Wire.begin(); Serial.begin(115200); // checking the I2c device on the line byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(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); 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("Unknow 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"); // setting up // Reset device by common 0x00 call Wire.beginTransmission(0x40); Wire.write(0x06); Serial.println(Wire.endTransmission()); delay(500); // Read the value from the register 0x00 - should return 0 as default Wire.beginTransmission(0x40); Wire.write(0x20); Serial.println(Wire.endTransmission()); Wire.requestFrom(0x40, 1); if (Wire.available() == 1) { int value = Wire.read(); Serial.println(value); } delay(100); // Write a value to the register 0x00 - 0xA7 - 1010 0111 : AIN2AGND, GAIN 0, 20SPS, Cont mode, External ref Wire.beginTransmission(0x40); Wire.write(0x40); Wire.write(0xA7); Serial.println(Wire.endTransmission()); delay(100); // Read the value from the register 0x00 - should return 0xA7 = 0d167 Wire.beginTransmission(0x40); Wire.write(0x20); Serial.println(Wire.endTransmission()); Wire.requestFrom(0x40, 1); if (Wire.available() == 1) { int value = Wire.read(); Serial.println(value); } delay(100); // Start conversion Wire.beginTransmission(0x40); Wire.write(0x08); Serial.println(Wire.endTransmission()); delay(1000); } void loop() { // // Start conversion // Wire.beginTransmission(0x40); // Wire.write(0x08); // // Wire.write(0x41); // Serial.println(Wire.endTransmission()); // delay(100); //Read data by cmd uint16_t value; // Reading data by cmd Wire.beginTransmission(0x40); Wire.write(0x10); Wire.endTransmission(false); Serial.print("Number of bytes requested :"); Serial.println(Wire.requestFrom(0x40, 2)); Serial.println(); byte value_1 = Wire.read(); byte value_2 = Wire.read(); Serial.print("Byte 0:"); Serial.println(value_1); Serial.print("Byte 1:"); Serial.println(value_2); value = (value_1 << 8 | value_2); if (value > 0x7FFF) { value = 0x0; } //Serial.println(value); float voltage = 5.0 * (float(value) / 32767) * 1; Serial.print("Vin :"); Serial.println(voltage); Serial.println(); delay(1000); }