Problem with reading HMC5883L's registers

I am reading the identify register which is Read Only Register but it displays ‘0’ on the serial monitor. Plz help me to fix this problem. Thanks in advance

#include <Wire.h>
int t;
#define address 0x1E
void setup()
{
  Serial.begin(9600);
Wire.begin();
  Wire.beginTransmission(address);
  Wire.write(0x10); //identify register
  Wire.endTransmission();

Wire.requestFrom(address, 1);
  if(1<=Wire.available())
  {
    t = Wire.read();     
  }
  // Show Values
Serial.print("Identify Register ");
Serial.println(t,HEX);

}

void loop() 
{

}

What is a HMC5883L?

Please post a link to its datasheet.

…R

What is the effect of continuously calling wire.begin() in loop()?

Please also post a wiring drawing, showing the size and location of the required pull-up resistors and explaining how you are using address 0x1E, instead of the default 0x3C/0x3D.

Wire.requestFrom(address, 1);
  if(2<Wire.available())

Oops.

Robin2:
What is a HMC5883L?

Please post a link to its datasheet.

...R

It is an electronic compass plz google it :drooling_face:

It is an electronic compass plz google it

YOU are asking for free help on this forum. The above suggestion is not a smart approach.

Why have you not fixed the ridiculous errors already pointed out in the replies above?

Noman_Malik:
It is an electronic compass plz google it :drooling_face:

If you want help then you do the Googling and post the link to the datasheet here.

I don't mind helping with things you can't do, or don't know how to do. But I don't plan to do things that you are perfectly capable of doing yourself.

...R

adwsystems:
What is the effect of continuously calling wire.begin() in loop()?

Please also post a wiring drawing, showing the size and location of the required pull-up resistors and explaining how you are using address 0x1E, instead of the default 0x3C/0x3D.

Sir I changed the code and then again uploaded it to the compass, but still it is showing '0' on serial monitor.

The wiring is GIVEN BELOW:

Vcc>>5v
GND>>GND
SCL>>A5
SDA>>A4

The pull-up resistors are connected with SCL and SDA through VCC

Please don’t modify code in previous posts. We can’t compare the changes nor do we know where to look for the updates.

You made many more changes than were commented on.

I’m going to guess that from the time Wire.requestFrom(address, 1); completes executing to the time the processor evaluates if(1<=Wire.available()) is much less than it takes for the chip to respond.

At this time I suggest running I2C_Scanner (a known working I2C sketch) to tell you if the Arduino and talk with the chip and on what address. Please post the serial monitor output when complete.

Sir, the edited code is given at the top. Someone asked me why i had used Wire.begin() in loop function so i just moved the code from loop function to setup function, apart from it the code is same.

Yes the typing is the same, but now the program is radically different. Instead of continuously executing all of the code, including the Wire.begin() command, it makes a single pass and stops.

Generally, and specifically in this case, the .begin() belong in setup() and the remainder in loop(). Setup is only for code that should be executed once at startup.

jremington:

Wire.requestFrom(address, 1);

if(2<Wire.available())



Oops.

Sir, I am asking the slave device for 1 byte, So what’s the error in this line. Kindly guide me

Noman_Malik:
Sir, the edited code is given at the top.

It is easier to help if you leave the code at the top in its original form and post the edited code in a new Reply in the correct chronolgical position so we can read the Thread from top to bottom and can compare the new version with the original version.

...R

Noman_Malik:
Sir, I am asking the slave device for 1 byte, So what's the error in this line. Kindly guide me

The error is the if statement is waiting to receive 2 bytes and you only asked for one. You already changed it above.

Noman_Malik:
Sir, I am asking the slave device for 1 byte,

Shouldn't the code be

if (Wire.available() >= 1) {

Your version is if (Wire.available() > 2) {

...R

adwsystems:
At this time I suggest running I2C_Scanner (a known working I2C sketch) to tell you if the Arduino and talk with the chip and on what address. Please post the serial monitor output when complete.

Sir I ran the I2C scanner code, it shows that device is available at address 0x1E.

I have read the output registers and they are giving values perfectly, but I was trying to read the "Read Only" registers which are not responding and continuously showing 0 on serial monitor

Sir it is still showing 0 on serial monitor

What do you think this

Wire.beginTransmission(address);
  Wire.write(0x10); //identify register
  Wire.endTransmission();

is doing?

adwsystems:
What do you think this

Wire.beginTransmission(address);

Wire.write(0x10); //identify register
  Wire.endTransmission();




is doing?

I am selecting the "identification register A" whose address is 0x10 which is 8 bit(1 byte) and requesting it to show the value it contains which is 01001000,fixed. but it is showing 0

That's what I thought you though it was doing. But not per the datasheet. Sorry to say, but RTFM.

Register Access
This section describes the process of reading from and writing to this device. The devices uses an address pointer to
indicate which register location is to be read from or written to. These pointer locations are sent from the master to this
slave device and succeed the 7-bit address (0x1E) plus 1 bit read/write identifier, i.e. 0x3D for read and 0x3C for write.
To minimize the communication between the master and this device, the address pointer updated automatically without
master intervention. The register pointer will be incremented by 1 automatically after the current register has been read
successfully.
The address pointer value itself cannot be read via the I
2C bus.
Any attempt to read an invalid address location returns 0’s, and any write to an invalid address location or an undefined bit
within a valid address location is ignored by this device.
To move the address pointer to a random register location, first issue a “write” to that register location with no data byte
following the commend. For example, to move the address pointer to register 10, send 0x3C 0x0A.