Listing 1
Shown here is an Arduino sketch to allow an Arduino to read and write to an I2C slave device.
const String SignOn = “I2C Dual Current”;
#include <Wire.h>
int Index = 0;
unsigned char Data[6];
int I2C1 = 0x10;
int Current1 = 0;
int Current2 = 0;
int Scale = 0;
int I2CAddress = 0;
void setup()
{
// I2C Start
Wire.begin(); // join i2c bus
// I2C End
Serial.begin(9600); // start serial for output
Serial.println(SignOn); // print message
}
void loop()
{
// Get the Currents
SampleCurrent();
DisplayCurrent();
delay(1000);
}
void DisplayCurrent()
{
// Display the Currents
Serial.print(“Amps: “); // print the character
Serial.print(Current1Scale/1000.0); // print the character Serial.print(“ “); // print the character Serial.print(Current2Scale/1000.0); // print the character
Serial.print(“ Scale: “); // print the character
Serial.println(Scale); // print the character
}
int SampleCurrent()
{
ReadCurrent(I2C1);
Current1=Data[0]256; Current1=Current1+Data[1]; Current2=Data[2]256;
Current2=Current2+Data[3];
Scale=Data[4];
I2CAddress=Data[5];
}
}
int ReadCurrent(int Address)
{
Wire.beginTransmission(Address); // transmit to device
Wire.write(0x00); // sends pointer value byte
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(Address, 6); // request 6 bytes from slave device
int result = 0;
while (Wire.available())
{
Data[result] = Wire.read(); // receive a byte as character
result++;
}
return result;
}