lsm303dhlc not working

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
RayJorgensen
 
Posts: 18
Joined: Sun Apr 15, 2018 2:32 pm

lsm303dhlc not working

Post by RayJorgensen »

As the subject reads - I have a lsm303dhlc that is not behaving!!

I have it connected to an uno - on a bread board with about 1.5' of wire on it. I can hold it in the air and get similar results as laying flat on the counter top.

3.5v - gnd - scl - sda connections

I set my phone on the counter with the x axis of the LSM right up against it, rotating them very slowly, and here are the readings I get. I get very similar reading with the phone removed so the phone next to it doesn't appear to be an issue.


LSM Phone
345 2
20 90
320 180
308 270

At this point I'm not concerned about true north, If I could get just get 360 reading as I rotate it would be a start!!

I'm using the code provided from the Adafruit site. (included below)

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);

void setup(void) 
{
  Serial.begin(9600);
  Serial.println("Magnetometer Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the LSM303 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while(1);
  }
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mag.getEvent(&event);
  
  float Pi = 3.14159;
  
  // Calculate the angle of the vector y,x
  float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
  
  // Normalize to 0-360
  if (heading < 0)
  {
    heading = 360 + heading;
  }
  Serial.print("Compass Heading: ");
  Serial.println(heading);
  delay(500);
}
At some point I'd like to get tilt comp working but that's probably for another post.

I have another UNO and a mega that I could steal if need be.

Thanks

edit - pins 4 and 5 are scl - scd? can I try those? If so how does the code need to be modified?

Thanks

User avatar
RayJorgensen
 
Posts: 18
Joined: Sun Apr 15, 2018 2:32 pm

Re: lsm303dhlc not working

Post by RayJorgensen »

i'm getting closer - I've got it plugged into the arduino. (Got rid of all the wires) I'm getting more readings. I'm thinking that I need to calibrate. The calibration code seems to work but I find no example of how to transfer that Info to the compass code.

All help is appreciated!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”