Example Code for Arduino
Explore how to integrate the LSM303 Breakout Board with Arduino UNO. This guide covers hardware and software preparation, connection diagrams, and provides sample codes to read navigation angles and raw data. It is an ideal resource for enthusiasts to delve into practical applications with Arduino and sensors, enhancing project capabilities with accurate measurements and data collection.
Hardware Preparation
-
DFRduino UNO R3 (or similar) x 1
Software Preparation
-
Download and install the LSM303 Library.
Connection Diagram

Sample Code 1
(Read Navigation Angle)
/*!
* @file NavigationAngleRead.ino
* @brief DFRobot's Manentic Sensor,This program continuously reads the accelerometer and magnetometer,
* communicating the readings over the serial interface. You can display the readings with the Arduino Serial Monitor.
* @n [Get the module here](https://www.dfrobot.com.cn/goods-326.html)
* @n This example get the four lightest positions of the IR sources.
* @n [Connection and Diagram](http://wiki.dfrobot.com.cn/index.php?title=(SKU:TOY0035)Gadgeteer_LSM303%E7%94%B5%E5%AD%90%E7%BD%97%E7%9B%98%E4%BC%A0%E6%84%9F%E5%99%A8)
*
* @copyright [DFRobot](https://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
*
* @author [carl]([email protected])
* @version V1.0
* @date 2016-07-11
*/
#include <MagneticSensorLsm303.h>
MagneticSensorLsm303 compass;
void setup() {
Serial.begin(9600);
compass.init();
compass.enable();
}
void loop() {
compass.read();
float heading = compass.getNavigationAngle();
Serial.print("Navigation Angle: ");
Serial.println(heading,3);
delay(500); // delay for serial readability
}
Sample Code 2
(Read Raw Data)
/*!
* @file MagneticRawData.ino
* @brief DFRobot's Manentic Sensor,This program continuously reads the accelerometer and magnetometer,
* communicating the readings over the serial interface. You can display the readings with the Arduino Serial Monitor.
* @n [Get the module here](https://www.dfrobot.com.cn/goods-326.html)
* @n This example get the four lightest positions of the IR sources.
* @n [Connection and Diagram](http://wiki.dfrobot.com.cn/index.php?title=(SKU:TOY0035)Gadgeteer_LSM303%E7%94%B5%E5%AD%90%E7%BD%97%E7%9B%98%E4%BC%A0%E6%84%9F%E5%99%A8)
*
* @copyright [DFRobot](https://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
*
* @author [carl]([email protected])
* @version V1.0
* @date 2016-07-11
*/
#include <MagneticSensorLsm303.h>
MagneticSensorLsm303 compass;
char report[120];
void setup()
{
Serial.begin(9600);
compass.init();
compass.enable();
}
void loop()
{
compass.read();
snprintf(report, sizeof(report), "Acceleration:(X:%6d Y:%6d Z:%6d) Magnetometer:(X:%6d Y:%6d Z:%6d)",
compass.accelerometer.x, compass.accelerometer.y, compass.accelerometer.z,
compass.magnetometer.x, compass.magnetometer.y, compass.magnetometer.z);
Serial.println(report);
delay(500);
}
Was this article helpful?
