344 Bachiochi – Listing 1

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;
}

Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.


Note: We’ve made the Dec 2022 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Supporting Companies

Upcoming Events


Copyright © KCK Media Corp.
All Rights Reserved

Copyright © 2023 KCK Media Corp.