How to connect two compasses using I2C and reading results

Hi everyone
I have problem that I have two compasses HCM5843 and I need to connect them on one Arduino UNO
So can you help me how to receive data from both?

this is my code for one compass:

#include <Wire.h>
#include <math.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
#define HMC5883_WriteAddress 0x1E
#define HMC5883_ModeRegisterAddress 0x02
#define HMC5883_ContinuousModeCommand 0x00
#define HMC5883_DataOutputXMSBAddress 0x03
int regb=0x01;
int regbdata=0x40;
int outputData[6];
void setup()
{
pinMode(A2 , INPUT);
pinMode(A3, INPUT);
Serial.begin(9600);
Wire.begin();
}
void loop()
{
int i,x,y,z;
double angle;
Wire.beginTransmission(HMC5883_WriteAddress);
Wire.write(regb);
Wire.write(regbdata);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(HMC5883_WriteAddress);
Wire.write(HMC5883_ModeRegisterAddress);
Wire.write(HMC5883_ContinuousModeCommand);
Wire.endTransmission();
delay(100);
Wire.beginTransmission(HMC5883_WriteAddress);
Wire.requestFrom(HMC5883_WriteAddress,6);
delay(500);
if(6<=Wire.available())
{
for(i=0;i<6;i++)
{
outputData=Wire.read();
}
}
x=outputData[0]<<8|outputData[1];
z=outputData[2]<<8|outputData[3];
y=outputData[4]<<8|outputData[5];
angle=atan2((double)y,(double)x)*(180/3.14159265)+180;

Serial.print("Heading ");
if((angle<22.5)||(angle>337.5))
Serial.print("S");
if((angle>22.5)&&(angle<67.5))
Serial.print("S-W");
if((angle>67.5)&&(angle<112.5))
Serial.print("W");
if((angle>112.5)&&(angle<157.5))
Serial.print("N-W");
if((angle>157.5)&&(angle<202.5))
Serial.print("N");
if((angle>202.5)&&(angle<247.5))
Serial.print("N");
if((angle>247.5)&&(angle<292.5))
Serial.print("E");
if((angle>292.5)&&(angle<337.5))
Serial.print("S");
lcd.begin(2, 20);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Heading is");
lcd.setCursor(0,1);
lcd.print(angle);lcd.print(" Degree");
Serial.print(": Angle ");
Serial.print(angle,2);
Serial.println(" Deg");
delay(100);
}

PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE
Read the how to use the forum sticky and post that code correctly.

Can I use I2C withe different A4,A5 ports ;

No

But you have no need to you can connect up to 128 devices on an I2C bus. So with your HCM5843 and the other Arduino that makes only 126 left to use.

Anyway why do you think you need two Arduinos, classic beginners mistake 99.9% of the time.

Thank you for help
But can you tell me how can I see the result from the two compasses to my LCD via I2C ?

Kamal-15:
Thank you for help
But can you tell me how can I see the result from the two compasses to my LCD via I2C ?

No not until you correct the post like I asked.

No, I mean what I said in the first reply.

Read the how to use the forum sticky and post that code correctly.

You modify that frst post.
If you are not smart enough or courteous to do this then you will not understand the answer or you don't deserve an answer.

Hi
I am beginner to I2C
I have a project need two compasses
can any one tell me how to connect them to my UNO and what I have to choose the i2c addresses of each

Its address 0xE1
But when i connect them together the value that I got are wrong

Provide a link to the data sheet!

Mark

here is the datasheet:

https://www.sparkfun.com/tutorials/301

It can be done.
For two identicals of this chip:
http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/HMC5883L_3-Axis_Digital_Compass_IC.pdf
you will need a multiplexer to switch between units.

I didn't exactly understand :frowning:
can you explain more please

If you have two devices with the same address, you can either out them on separated busses, or arrange the bus so that only one device at a time is connected to the bus.
This will require additional hardware.

Is this such a poorly-coupled system that different parts can be heading in different directions at the same time?

it's aiming system
one compass gives the direction and the second stil moving via motor till they equal in value

I thinking about supply them power from two digital pins and turn on and off while they reading data

Kamal-15:
I didn't exactly understand :frowning:

As predicted in reply #4.

So see if you can understand this:-