I connected an Adafruit LSM303AGR to a Nano board and uploaded the LIS2MDL>compass sketch from examples in the Arduino IDE. It worked corrrectly and displayed a stable compass reading that changed as I rotated the sensor.
I then added code for a 128x32 OLED and connected the OLED to the I2C pins along with the LSM303AGR. I uploaded and it displays the compass heading on the serial monitor as well as the OLED, except now the compass heading jumps all over the place. Not minor fluctuations, hundreds of degrees.
Any Idea why this is happening?
I want to post my code and I think I should attach it in the proper format. Hopefully I did it right.
LSM303AGRcompass_OLED_test_230212.ino (3.1 KB)
Thanks for your help,
Jim
Sorry I don't have a link to the cheap AliExpress 128x32 OLED
/**************************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/category/63_98
This example is for a 128x64 pixel display using I2C to communicate
3 pins are required to interface (two I2C and one reset).
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source
hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries,
with contributions from the open source community.
BSD license, check license.txt for more information
All text above, and the splash screen below must be
included in any redistribution.
**************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LIS2MDL.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_LIS2MDL mag = Adafruit_LIS2MDL(12345);
float cHeading = 0;
void setup() {
Serial.begin(9600);
Serial.println("Magnetometer Test"); Serial.println("");
//*****OLED Display***********
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
delay(2000);
display.clearDisplay();
// Modify these lines to change the splash screen on startup
display.setTextSize(3);
display.setTextColor(WHITE);
display.setCursor(0, 8);
// Display static text
display.println("Pegasus");
display.display();
delay(2000);
//*****END OLED Display***********
}
//*****LOOP***********
void loop() {
/* 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);
cHeading = heading;
Display();
delay(2000);
}
////*******DISPLAY*****************************
void Display()
{
// char buff[10];
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);
display.setCursor(30, 8);
// sprintf(buff, "%d" , disp);
// Display Throttle Setting on OLED
// if (disp == 0) {
// display.println("IDLE");
// } else {
display.println(cHeading);
// }
// display.println((char*)disp);
display.display();
Serial.println(cHeading);
//}
}