├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── Adafruit_LSM303.cpp ├── Adafruit_LSM303.h ├── README.md ├── examples └── Test │ └── Test.ino └── library.properties /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /Adafruit_LSM303.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | This is a library for the LSM303 Accelerometer and magnentometer/compass 3 | 4 | Designed specifically to work with the Adafruit LSM303DLHC Breakout 5 | 6 | These displays use I2C to communicate, 2 pins are required to interface. 7 | 8 | Adafruit invests time and resources providing this open source code, 9 | please support Adafruit andopen-source hardware by purchasing products 10 | from Adafruit! 11 | 12 | Written by Kevin Townsend for Adafruit Industries. 13 | BSD license, all text above must be included in any redistribution 14 | ***************************************************************************/ 15 | #include 16 | 17 | /*************************************************************************** 18 | CONSTRUCTOR 19 | ***************************************************************************/ 20 | bool Adafruit_LSM303::begin() 21 | { 22 | Wire.begin(); 23 | 24 | // Enable the accelerometer 25 | write8(LSM303_ADDRESS_ACCEL, LSM303_REGISTER_ACCEL_CTRL_REG1_A, 0x27); 26 | 27 | // Enable the magnetometer 28 | write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_MR_REG_M, 0x00); 29 | 30 | return true; 31 | } 32 | 33 | /*************************************************************************** 34 | PUBLIC FUNCTIONS 35 | ***************************************************************************/ 36 | void Adafruit_LSM303::read() 37 | { 38 | // Read the accelerometer 39 | Wire.beginTransmission((byte)LSM303_ADDRESS_ACCEL); 40 | Wire.write(LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80); 41 | Wire.endTransmission(); 42 | Wire.requestFrom((byte)LSM303_ADDRESS_ACCEL, (byte)6); 43 | 44 | // Wait around until enough data is available 45 | while (Wire.available() < 6); 46 | 47 | uint8_t xlo = Wire.read(); 48 | uint8_t xhi = Wire.read(); 49 | uint8_t ylo = Wire.read(); 50 | uint8_t yhi = Wire.read(); 51 | uint8_t zlo = Wire.read(); 52 | uint8_t zhi = Wire.read(); 53 | 54 | // Shift values to create properly formed integer (low byte first) 55 | // KTOWN: 12-bit values are left-aligned, no shift needed 56 | // accelData.x = (xlo | (xhi << 8)) >> 4; 57 | // accelData.y = (ylo | (yhi << 8)) >> 4; 58 | // accelData.z = (zlo | (zhi << 8)) >> 4; 59 | accelData.x = (int16_t)((xhi << 8) | xlo); 60 | accelData.y = (int16_t)((yhi << 8) | ylo); 61 | accelData.z = (int16_t)((zhi << 8) | zlo); 62 | 63 | // Read the magnetometer 64 | Wire.beginTransmission((byte)LSM303_ADDRESS_MAG); 65 | Wire.write(LSM303_REGISTER_MAG_OUT_X_H_M); 66 | Wire.endTransmission(); 67 | Wire.requestFrom((byte)LSM303_ADDRESS_MAG, (byte)6); 68 | 69 | // Wait around until enough data is available 70 | while (Wire.available() < 6); 71 | 72 | // Note high before low (different than accel) 73 | xhi = Wire.read(); 74 | xlo = Wire.read(); 75 | zhi = Wire.read(); 76 | zlo = Wire.read(); 77 | yhi = Wire.read(); 78 | ylo = Wire.read(); 79 | 80 | // Shift values to create properly formed integer (low byte first) 81 | magData.x = (xlo | (xhi << 8)); 82 | magData.y = (ylo | (yhi << 8)); 83 | magData.z = (zlo | (zhi << 8)); 84 | 85 | // ToDo: Calculate orientation 86 | magData.orientation = 0.0; 87 | } 88 | 89 | void Adafruit_LSM303::setMagGain(lsm303MagGain gain) 90 | { 91 | write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_CRB_REG_M, (byte)gain); 92 | } 93 | 94 | /*************************************************************************** 95 | PRIVATE FUNCTIONS 96 | ***************************************************************************/ 97 | void Adafruit_LSM303::write8(byte address, byte reg, byte value) 98 | { 99 | Wire.beginTransmission(address); 100 | Wire.write(reg); 101 | Wire.write(value); 102 | Wire.endTransmission(); 103 | } 104 | 105 | byte Adafruit_LSM303::read8(byte address, byte reg) 106 | { 107 | byte value; 108 | 109 | Wire.beginTransmission(address); 110 | Wire.write(reg); 111 | Wire.endTransmission(); 112 | Wire.requestFrom(address, (byte)1); 113 | value = Wire.read(); 114 | Wire.endTransmission(); 115 | 116 | return value; 117 | } 118 | -------------------------------------------------------------------------------- /Adafruit_LSM303.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | This is a library for the LSM303 Accelerometer and magnentometer/compass 3 | 4 | Designed specifically to work with the Adafruit LSM303DLHC Breakout 5 | 6 | These displays use I2C to communicate, 2 pins are required to interface. 7 | 8 | Adafruit invests time and resources providing this open source code, 9 | please support Adafruit andopen-source hardware by purchasing products 10 | from Adafruit! 11 | 12 | Written by Kevin Townsend for Adafruit Industries. 13 | BSD license, all text above must be included in any redistribution 14 | ***************************************************************************/ 15 | #ifndef __LSM303_H__ 16 | #define __LSM303_H__ 17 | 18 | #if (ARDUINO >= 100) 19 | #include "Arduino.h" 20 | #else 21 | #include "WProgram.h" 22 | #endif 23 | #include "Wire.h" 24 | 25 | #define LSM303_ADDRESS_ACCEL (0x32 >> 1) // 0011001x 26 | #define LSM303_ADDRESS_MAG (0x3C >> 1) // 0011110x 27 | #define LSM303_ID (0b11010100) 28 | 29 | class Adafruit_LSM303 30 | { 31 | public: 32 | typedef enum 33 | { // DEFAULT TYPE 34 | LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw 35 | LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw 36 | LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw 37 | LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw 38 | LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw 39 | LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw 40 | LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r 41 | LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r 42 | LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28, 43 | LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29, 44 | LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A, 45 | LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B, 46 | LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C, 47 | LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D, 48 | LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E, 49 | LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F, 50 | LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30, 51 | LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31, 52 | LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32, 53 | LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33, 54 | LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34, 55 | LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35, 56 | LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36, 57 | LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37, 58 | LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38, 59 | LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39, 60 | LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A, 61 | LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B, 62 | LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C, 63 | LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D 64 | } lsm303AccelRegisters_t; 65 | 66 | typedef enum 67 | { 68 | LSM303_REGISTER_MAG_CRA_REG_M = 0x00, 69 | LSM303_REGISTER_MAG_CRB_REG_M = 0x01, 70 | LSM303_REGISTER_MAG_MR_REG_M = 0x02, 71 | LSM303_REGISTER_MAG_OUT_X_H_M = 0x03, 72 | LSM303_REGISTER_MAG_OUT_X_L_M = 0x04, 73 | LSM303_REGISTER_MAG_OUT_Z_H_M = 0x05, 74 | LSM303_REGISTER_MAG_OUT_Z_L_M = 0x06, 75 | LSM303_REGISTER_MAG_OUT_Y_H_M = 0x07, 76 | LSM303_REGISTER_MAG_OUT_Y_L_M = 0x08, 77 | LSM303_REGISTER_MAG_SR_REG_Mg = 0x09, 78 | LSM303_REGISTER_MAG_IRA_REG_M = 0x0A, 79 | LSM303_REGISTER_MAG_IRB_REG_M = 0x0B, 80 | LSM303_REGISTER_MAG_IRC_REG_M = 0x0C, 81 | LSM303_REGISTER_MAG_TEMP_OUT_H_M = 0x31, 82 | LSM303_REGISTER_MAG_TEMP_OUT_L_M = 0x32 83 | } lsm303MagRegisters_t; 84 | 85 | typedef enum 86 | { 87 | LSM303_MAGGAIN_1_3 = 0x20, // +/- 1.3 88 | LSM303_MAGGAIN_1_9 = 0x40, // +/- 1.9 89 | LSM303_MAGGAIN_2_5 = 0x60, // +/- 2.5 90 | LSM303_MAGGAIN_4_0 = 0x80, // +/- 4.0 91 | LSM303_MAGGAIN_4_7 = 0xA0, // +/- 4.7 92 | LSM303_MAGGAIN_5_6 = 0xC0, // +/- 5.6 93 | LSM303_MAGGAIN_8_1 = 0xE0 // +/- 8.1 94 | } lsm303MagGain; 95 | 96 | typedef struct lsm303AccelData_s 97 | { 98 | float x; 99 | float y; 100 | float z; 101 | } lsm303AccelData; 102 | 103 | typedef struct lsm303MagData_s 104 | { 105 | float x; 106 | float y; 107 | float z; 108 | float orientation; 109 | } lsm303MagData; 110 | 111 | bool begin(void); 112 | void read(void); 113 | void setMagGain(lsm303MagGain gain); 114 | 115 | lsm303AccelData accelData; // Last read accelerometer data will be available here 116 | lsm303MagData magData; // Last read magnetometer data will be available here 117 | 118 | void write8(byte address, byte reg, byte value); 119 | byte read8(byte address, byte reg); 120 | 121 | private: 122 | }; 123 | 124 | #endif 125 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Warning: Library Deprecated 2 | 3 | This library is deprecated, and as of 14 October 2016 this library will no longer appear 4 | in the Arduino Library Manager. Any new projects should use the newer library available here: https://github.com/adafruit/Adafruit_LSM303DLHC 5 | 6 | Adafruit_LSM303 7 | =============== 8 | 9 | This is a library for the Adafruit triple-axis accelerometer/magnetometer LSM303DLHC breakout 10 | 11 | Designed specifically to work with the Adafruit LSM303 Breakout 12 | ----> https://www.adafruit.com/products/1120 13 | 14 | These displays use I2C to communicate, 2 pins are required to interface 15 | Adafruit invests time and resources providing this open source code, 16 | please support Adafruit and open-source hardware by purchasing 17 | products from Adafruit! 18 | 19 | Check out the links above for our tutorials and wiring diagrams 20 | 21 | Written by Kevin Townsend for Adafruit Industries. 22 | BSD license, all text above must be included in any redistribution 23 | 24 | To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_LSM303. Check that the Adafruit_LSM303 folder contains Adafruit_LSM303.cpp and Adafruit_LSM303.h 25 | 26 | Place the Adafruit_LSM303 library folder your *arduinosketchfolder*/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. 27 | 28 | 29 | 30 | ## Compatibility 31 | 32 | MCU | Tested Works | Doesn't Work | Not Tested | Notes 33 | ------------------ | :----------: | :----------: | :---------: | ----- 34 | Atmega328 @ 16MHz | X | | | 35 | Atmega328 @ 12MHz | X | | | 36 | Atmega32u4 @ 16MHz | X | | | 37 | Atmega32u4 @ 8MHz | X | | | 38 | ESP8266 | X | | | 39 | Atmega2560 @ 16MHz | X | | | SDA/SCL D20/D21 40 | ATSAM3X8E | X | | | SDA/SCL D20/D21 41 | ATSAM21D | X | | | 42 | ATtiny85 @ 16MHz | X | | | 43 | ATtiny85 @ 8MHz | X | | | 44 | Intel Curie @ 32MHz | X | | | 45 | STM32F2 | | | X | 46 | 47 | * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini 48 | * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V 49 | * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0 50 | * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro 51 | * ESP8266 : Adafruit Huzzah 52 | * ATmega2560 @ 16MHz : Arduino Mega 53 | * ATSAM3X8E : Arduino Due 54 | * ATSAM21D : Arduino Zero, M0 Pro 55 | * ATtiny85 @ 16MHz : Adafruit Trinket 5V 56 | * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V 57 | 58 | 59 | -------------------------------------------------------------------------------- /examples/Test/Test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | Adafruit_LSM303 lsm; 6 | 7 | void setup() 8 | { 9 | #ifndef ESP8266 10 | while (!Serial); // will pause Zero, Leonardo, etc until serial console opens 11 | #endif 12 | Serial.begin(9600); 13 | 14 | // Try to initialise and warn if we couldn't detect the chip 15 | if (!lsm.begin()) 16 | { 17 | Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); 18 | while (1); 19 | } 20 | } 21 | 22 | void loop() 23 | { 24 | lsm.read(); 25 | Serial.print("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" "); 26 | Serial.print("Y: "); Serial.print((int)lsm.accelData.y); Serial.print(" "); 27 | Serial.print("Z: "); Serial.println((int)lsm.accelData.z); Serial.print(" "); 28 | Serial.print("Mag X: "); Serial.print((int)lsm.magData.x); Serial.print(" "); 29 | Serial.print("Y: "); Serial.print((int)lsm.magData.y); Serial.print(" "); 30 | Serial.print("Z: "); Serial.println((int)lsm.magData.z); Serial.print(" "); 31 | delay(1000); 32 | } 33 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit LSM303 2 | version=1.0.1 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Driver for Adafruit's LSM303DLHC 9-DOF breakout 6 | paragraph=Driver for Adafruit's LSM303DLHC 9-DOF breakout 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_LSM303 9 | architectures=* 10 | --------------------------------------------------------------------------------