LabVIEW Interface for Arduino Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with Arduino reading HMC6352 Compass on I2C

Hi, I`ve just started experimenting with LV on Arduino and run into difficulties reading the heading from an HMC6352 breakbout board.

I can read the device no problem using the following Arduino code:

=================

#include <Wire.h>
int HMC6352Address = 0x42;
// This is calculated in the setup() function
int slaveAddress;
int ledPin = 13;
boolean ledState = false;
byte headingData[2];
int i, headingValue;
void setup()
{
// Shift the device's documented slave address (0x42) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)
slaveAddress = HMC6352Address >> 1;   // This results in 0x21 as the address to pass to TWI
Serial.begin(9600);
pinMode(ledPin, OUTPUT);      // Set the LED pin as output
Wire.begin();
}
void loop()
{
  // Flash the LED on pin 13 just to show that something is happening
  // Also serves as an indication that we're not "stuck" waiting for TWI data
  ledState = !ledState;
  if (ledState) {
    digitalWrite(ledPin,HIGH);
  }
  else
  {
    digitalWrite(ledPin,LOW);
  }
  // Send a "A" command to the HMC6352
  // This requests the current heading data
  Wire.beginTransmission(slaveAddress);
  Wire.send("A");              // The "Get Data" command
  Wire.endTransmission();
  delay(10);                   // The HMC6352 needs at least a 70us (microsecond) delay
  // after this command.  Using 10ms just makes it safe
  // Read the 2 heading bytes, MSB first
  // The resulting 16bit word is the compass heading in 10th's of a degree
  // For example: a heading of 1345 would be 134.5 degrees
  Wire.requestFrom(slaveAddress, 2);        // Request the 2 byte heading (MSB comes first)
  i = 0;
  while(Wire.available() && i < 2)
  {
    headingData = Wire.receive();
    i++;
  }
  headingValue = headingData[0]*256 + headingData[1];  // Put the MSB and LSB together
  Serial.print("Current heading: ");
  Serial.print(int (headingValue / 10));     // The whole number part of the heading
  Serial.print(".");
  Serial.print(int (headingValue % 10));     // The fractional part of the heading
  Serial.println(" degrees");
  delay(500);
}
===================

But I keep getting a 5003 error when trying to read it using the attached sketch, would appreciate and advice.

James

0 Kudos
Message 1 of 8
(6,305 Views)

James,

It looks like the arduino code and the labview code are doing two very different things.

In the LabIVEW Code you pass a slave address of 42 (assuming that is decimal) but your arduino code uses a slave address of 0x21 (33 decimal).

You also are not sending any data.  The 41 in the byte array wired to data is the index, not the value.  Set the first element in the byte array to the value to send which looks like 'A' or 0x41 or 65 in decimal.  Make sure you set the base correctly in LabVIEW.

Let us know if that helps.

-Sam K

LIFA Developer

0 Kudos
Message 2 of 8
(4,228 Views)

Hello James,

I'm novice on labview.

I'm trying to read this type of compas HMC6352 by using labview but no success.

I'm using a wiring board (similar of arduino I think) and I can read the value by the wiring software but nothing by labview.

You will found in attach my diagram

diagram compas test.jpg

Can you help me to be on a good way to solve my problem.

Thanks

Regards

Jeff

0 Kudos
Message 3 of 8
(4,228 Views)

Only the Arduino Uno and Arduino Mega 2560 were supported by LIFA.  Also, you should take a look at other example LIFA programs because you are missing the first critical VI (the initialization).  Take a look at one of my libraries to see how to interface with I2C devices (the specifics can vary depending on the actual sensor.  Here is a popular one that I created:  https://decibel.ni.com/content/docs/DOC-34263

Also, for I2C stuff, I recommend that you use block diagram constants for the values you pass into the I2C VIs.

0 Kudos
Message 4 of 8
(4,228 Views)

Thanks for your reply and your help Nathan.

I'm looking your librairies (seems to be that I need, cool!!!)

but I meet some difficulties to see your diagrams.

In fact, I've some errors appears, some icons have questions mark.

I digging on forums to solve that.

Still thank.

Regards

0 Kudos
Message 5 of 8
(4,228 Views)

You have to unzip the zip file first. Otherwise it can't find any of the

subVIs that I created.

0 Kudos
Message 6 of 8
(4,228 Views)

Ok thank, I understand better about the structure.

I have a question about my functions, I still have these question marks on icons, same after re-installed the software.

Have you an idea to solve that?

Regards

0 Kudos
Message 7 of 8
(4,228 Views)

You are missing those VI's.  You should try to re-install whatever module contains those VI's.  Since that is not related to LIFA, you should ask those types of questions in the NI forums:  http://forums.ni.com/

0 Kudos
Message 8 of 8
(4,228 Views)