Go Down

Topic: I2c sensor programming help needed. (Read 886 times) previous topic - next topic


I have a small problem I hope someone having experience with sensors can resolve. I'm using a digital compass sensor hmc5883l. The code to read the compass heading is for a different compass model hmc6352. For some reason I need to use this code for my sensor. So I changed the slave read/write address in the code but it still doesn't work. What else do I need to change. I have very little knowledge about i2c devices.

slave read address for hmc5883l= 0x3D
slave write address for hmc5883l= 0x3C

Code for hmc6352 is posted below

Code: [Select]
#include <Wire.h>

int HMC6352Address = 0x42;
int slaveAddress;
byte headingData[2];
int i, headingValue;
int headingcompass;

void setup(){
slaveAddress = HMC6352Address >> 1;  


void loop(){

Wire.beginTransmission(slaveAddress);        //the wire stuff is for the compass module




 Wire.requestFrom(slaveAddress, 2);      

 i = 0;

 while(Wire.available() && i < 2)


   headingData[i] = Wire.receive();



 headingValue = headingData[0]*256 + headingData[1];

int pracheading = headingValue / 10;      // this is the heading of the compass




Serial.println("current heading:");



For some reason I need to use this code for my sensor.

Why use the code for a thingy when your talking to a whatsit and why expect it to work.



as far as I know, all I2c devices should be able to work with the same code. only the address needs to be changed to specify which device we want to communicate with. Correct me if I'm wrong.


You might want to check if your new sensor operates at the same voltage level as the old sensor and your Arduino.  If your sensor is 3.3v only, and your Arduino is 5v, you probably need a level shifter.  Obviously you would also want to check that you have correctly wired the new sensor, and have appropriate pullups for SCA/SCL.


Arduino uno has a 3.3v output so no level shifter is necessary. SCL/SDA ports are wired properly. The fact is, I've tested it with a different code and I'm getting the correct reading.But for some reason I need to specifically use this code.


Try this code instead... http://classic.parallax.com/portals/0/downloads/docs/prod/sens/29133-CompassPDEDemo.zip ... off parallax's web site, and taylor it to your needs, chances are that the other compass module doesn't store its data in the same registers...

Go Up