system
January 14, 2014, 5:58am
1
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.
system
January 14, 2014, 6:12am
3
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.
system
January 14, 2014, 12:00pm
6
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
system
January 14, 2014, 12:15pm
7
Its address 0xE1
But when i connect them together the value that I got are wrong
Provide a link to the data sheet!
Mark
system
January 14, 2014, 1:25pm
9
knut_ny
January 14, 2014, 1:26pm
10
system
January 14, 2014, 1:36pm
11
I didn't exactly understand
can you explain more please
system
January 14, 2014, 1:52pm
12
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?
system
January 14, 2014, 2:16pm
13
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
As predicted in reply #4 .
So see if you can understand this:-
[TodBot] shares his latest knoledge about I2C: Ever wanted to use any pair of pins for I2C on Arduino, not just the dedicated pins on Analog 4 & 5? Me too, so I made a quick little Arduino library called “SoftI2CMaster”, available in the...
Est. reading time: 1 minute