如何将LSM303连接到Arduino

概述
lsm303分支板结合了磁力计/罗盘模块和三轴加速度计,构成了紧凑的导航子系统。 i2c接口与3.3v和5v处理器兼容,并且两个引脚可以由其他i2c设备共享。结合l3gd20等3轴陀螺仪,您将拥有完整的imu(惯性测量单元)所需的所有传感器,以用于空中,陆地或海洋导航。
在本教程中,我们将向您展示如何将lsm303连接到arduino,circuitpython开发板或blinka支持的设备,以及如何使用它来测量相对于地球磁场的方向以及在三个轴上的加速度。
新版lsm303agr的一个重要功能是它包括一个 stemma qt 连接器,无需焊接即可使用。 只需将stemma qt插入公头连接线,即可将其连接到带有母头的面包板或开发板,重新参加比赛!您甚至可以使用stemma qt电缆将其与其他stemma qt分支连接起来
i》
工作原理:
mems-微机电系统
传感器组成硅晶片上的微机械结构。有一些用于测量x,y和z轴上的加速度和磁场的结构
加速度测量
这些结构被多晶硅弹簧悬挂,当它们在x,y和/或z轴上受加速度作用时,它们可以偏转。挠曲会导致固定板和连接到悬挂结构的板之间的电容发生变化。每个轴上的电容变化都会转换为与该轴上的加速度成比例的输出电压。
磁场测量
这些结构与加速度计的结构相似,但是用微观线圈蚀刻。励磁电流流经线圈,由于磁场而产生的洛伦兹力使结构发生偏转。偏转再次转换为与该轴上的磁场强度成比例的输出电压。
我拥有哪个lsm303?
现在有两个由adafruit制造的lsm303突破口! schnikes!它们的功能几乎相同,但又有足够的差异,以至于它们需要磁力计的不同驱动器。为什么只有磁力计?好吧,我要告诉你一个小秘密:lsm303是两个传感器贴在同一个盒子里!
因为两个封装都具有相同的加速度传感器,且寄存器布局相同,所以我们可以使用两者都使用相同的加速度计驱动器。磁力计尽管都具有相同的i2c地址(0x1e),但寄存器布局却完全不同,这意味着它们各自需要自己的驱动程序库。
因此,您将需要知道你有哪个版本。这是区分两个 adafruit 突破的一种高度科学的方法:
lsm303agr是黑色的,说“ lsm303agr”不同于
lsm303dlhc是蓝色并且说“ lsm303dlhc”
复杂,我知道,但我相信您。下面概述了更多细微的差异
lsm303agr
adafruit块上的新孩子是lsm303agr。这是对lsm303系列的更新,该系列体积更小,内部装有不同的磁力计芯片。我们的突破如下:
除了体积更小,更容易丢失筹码外,还有其他一些区别。芯片本身暴露了磁力计(intm)的中断引脚(也许是内置的新型磁力计传感器的功能?)以及加速度计和磁力计的芯片选择引脚( csa 和 csm )。
两者之间的另一个令人兴奋的区别是增加了两个 stemma qt 连接器,每个连接器的一侧板。这样一来,您无需焊接即可使用该传感器,并且可以与其他stemma qt传感器链接。
请注意,虽然lsm303agr分支具有cs引脚,但它仅支持3线spi,而我们无法使用它。喜欢冒险的人可以参考数据表并尝试一下!欢迎pr;)
lsm303/lsm303dlhc
adafruitlsm303分支使用lsm303 dlhc ,看起来像这样:

与lsm303agr相比,您可以看到传感器本身(中间的黑色矩形)实际上更大。此外,引脚的顺序不同,并且lsm303dlh公开了 drdy (数据就绪)引脚。
插脚
电源引脚
vin -这是电源引脚。由于传感器芯片使用的是3.3 vdc,因此我们在板载了一个稳压器,该稳压器将采用3-5vdc的电压并将其安全地向下转换。要为电路板供电,请为其提供与微控制器逻辑电平相同的功率-例如,对于像arduino这样的5v微控制器,请使用5v
3vo -这是稳压器的3.3v输出,如果您愿意,可以从中获得100ma的电流。
gnd -电源和逻辑的公共接地
i2c逻辑引脚
scl -i2c时钟引脚,连接到微控制器的i2c时钟线。该引脚经过电平转换,因此您可以使用3-5v逻辑,并且该引脚上具有 10k上拉。
sda -i2c数据引脚,连接到微控制器的i2c数据线。该引脚经过电平转换,因此您可以使用3-5v逻辑,并且该引脚上具有 10k上拉。
stemma qt- ,这些连接器允许您可以连接到具有 stemma qt 连接器的开发板或具有各种关联附件的其他东西的连接器
其他通用引脚
ia1 -加速度计的第一个中断引脚。仅限3v逻辑
ia2-加速度计的第二个中断引脚。仅3v逻辑
lsm303agr仅限
intm -磁力计的中断引脚。仅限3v逻辑
csa-加速度计的cs引脚。仅限3v逻辑
csm-磁力计的cs引脚。仅限3v逻辑
请注意,虽然lsm303agr突破口具有cs引脚,仅支持我们无法使用的3线spi。喜欢冒险的人可以参考数据表并尝试一下!欢迎pr;)
仅适用于lsm303dlh
drdy -这是数据就绪引脚。仅限3v逻辑
程序集
电路板组装:所有表面安装组件都已预先焊接到电路板上。您可以将连接直接焊接到板上,也可以安装标题栏(已提供)以简化在面包板上的使用。
放置页眉,如有必要,将页眉切成一定长度,然后将长针向下插入面包板。
放置板子将板子放在排针顶部。支撑必要的背面,以便在焊接之前将电路板弄平。
然后焊接!焊接每个引脚以确保良好的电气连接。
如果您是焊接新手,请查阅我们的出色焊接指南。
arduino
arduino接线
connect 板载vcc(红线)5v arduino板(uno等),则将strong》改为 arduino 5v 。如果您的板为 3v, 相反。
连接板子 gnd(黑线)到 arduino gnd
将板 scl(黄线)连接到 arduino scl
连接板 sda(蓝线)到 arduino sda
ul》
安装库
要开始使用lsm303,您需要安装加速度计库,并板的磁力计库。另外,您将需要adafruit_sensor库以及adafruit_busio库,该库允许它以与其他类似传感器一致的方式返回数据。可以使用arduino ide中的库管理器安装所有库:
在继续之前,请确保您知道传感器的版本,
安装加速计库
点击管理库。.. 菜单项,搜索 adafruit lsm303 accel ,然后选择 adafruit_lsm303_accel 库:
安装磁力计库
最后,您需要在lsm303中安装磁力计库。确保下载适用于您的分组讨论板的正确驱动程序。
lsm303agr
在库管理器中搜索 adafruit_lsm303agr_mag 库:
lsm303/lsm303dlhc
在库管理器中搜索 adafruit_lsm303dlh_mag 库:
安装帮助程序库
对 adafruit busio 库执行相同的过程:
最后,对 adafruit unified sensor 库:
加速度计演示
第一个演示将向您展示如何获得加速度计最有效的读数:测量加速度!
打开文件-》示例-》 adafruit lsm303 accel -》 accelsensor 并上传到与传感器连接的arduino。
将草图上传到板上并打开serial monitor(工具-》 serial monitor )以 115200波特。您应该看到启动时打印的当前配置设置的值,然后是与x,y和z轴类似的加速度读数:
adafruit_lsm303_accel_unified 传感器adafruit_lsm303_accel库中的类报告x,y和z轴加速度计的读数直接以米/秒为平方。库中的accelsensor示例代码从传感器读取并将加速度读数打印到串行监视器中。
在静止状态下,传感器不应报告任何加速度,除非是由于重力(大约9.8米/秒平方)。通过计算重力矢量相对于x,y和z轴的角度,该设备可以用作倾角仪。
基本磁力计读数
第一个演示将向您展示如何获得加速度计最有效的读数:测量加速度!
lsm303agr
打开文件-》示例-》 adafruit lsm303agr mag -》磁传感器并上传到您的arduino接线端
lsm303/lsm303dlh
打开文件-》示例-》 adafruit lsm303dlh mag -》磁传感器,然后
将草图上传到您的电路板上,并以 115200波特打开串行监视器(工具-》串行监视器)。您应该看到启动时打印的当前配置设置的值,然后是x,y和z轴的磁场读数,类似于:
磁力仪库中的传感器类报告x,y和z轴磁力仪读数直接在微型teslas中。 magsensor 示例代码从传感器读取并将微型特斯拉读数打印到串行监视器。
在没有任何强磁场的情况下,传感器读数应反映地球的磁场(介于20到60微特拉斯之间)。当传感器保持水平时,通过计算磁场相对于x和y轴的角度,该设备可用作指南针。
计算指南针航向
要将microtesla读数转换为0-360度指南针航向,我们可以使用atan2()函数来计算由y和x轴读数定义的向量的角度。结果将以弧度为单位,因此我们将其乘以180度并除以pi以将其转换为度。
lsm303agr
打开文件-》示例-》 adafruit lsm303agr mag -》罗盘,并上传到与传感器连接的arduino。
lsm303/lsm303dlh
打开文件-》示例-》 adafruit lsm303dlh mag -》罗盘,并通过传感器连接到arduino。
将草图上传到板上并打开串行监视器(工具-》串行监视器),位于 115200波特。您将看到航向计算打印到串行监视器。如果在传感器运行时旋转传感器,则会看到航向更改:
加速度计演示代码
下载:project zip 或 accelsensor.ino | 在github上查看
复制代码
#include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303_accel_unified accel = adafruit_lsm303_accel_unified(54321);
void displaysensordetails(void) {
sensor_t sensor;
accel.getsensor(&sensor);
serial.println(“------------------------------------”);
serial.print(“sensor: ”);
serial.println(sensor.name);
serial.print(“driver ver: ”);
serial.println(sensor.version);
serial.print(“unique id: ”);
serial.println(sensor.sensor_id);
serial.print(“max value: ”);
serial.print(sensor.max_value);
serial.println(“ m/s^2”);
serial.print(“min value: ”);
serial.print(sensor.min_value);
serial.println(“ m/s^2”);
serial.print(“resolution: ”);
serial.print(sensor.resolution);
serial.println(“ m/s^2”);
serial.println(“------------------------------------”);
serial.println(“”);
delay(500);
}
void setup(void) {
#ifndef esp8266
while (!serial)
; // will pause zero, leonardo, etc until serial console opens
#endif
serial.begin(9600);
serial.println(“accelerometer test”);
serial.println(“”);
/* initialise the sensor */
if (!accel.begin()) {
/* there was a problem detecting the adxl345 。.. check your connections */
serial.println(“ooops, no lsm303 detected 。.. check your wiring!”);
while (1)
;
}
/* display some basic information on this sensor */
displaysensordetails();
accel.setrange(lsm303_range_4g);
serial.print(“range set to: ”);
lsm303_accel_range_t new_range = accel.getrange();
switch (new_range) {
case lsm303_range_2g:
serial.println(“+- 2g”);
break;
case lsm303_range_4g:
serial.println(“+- 4g”);
break;
case lsm303_range_8g:
serial.println(“+- 8g”);
break;
case lsm303_range_16g:
serial.println(“+- 16g”);
break;
}
accel.setmode(lsm303_mode_normal);
serial.print(“mode set to: ”);
lsm303_accel_mode_t new_mode = accel.getmode();
switch (new_mode) {
case lsm303_mode_normal:
serial.println(“normal”);
break;
case lsm303_mode_low_power:
serial.println(“low power”);
break;
case lsm303_mode_high_resolution:
serial.println(“high resolution”);
break;
}
}
void loop(void) {
/* get a new sensor event */
sensors_event_t event;
accel.getevent(&event);
/* display the results (acceleration is measured in m/s^2) */
serial.print(“x: ”);
serial.print(event.acceleration.x);
serial.print(“ ”);
serial.print(“y: ”);
serial.print(event.acceleration.y);
serial.print(“ ”);
serial.print(“z: ”);
serial.print(event.acceleration.z);
serial.print(“ ”);
serial.println(“m/s^2”);
/* delay before the next sample */
delay(500);
}
#include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303_accel_unified accel = adafruit_lsm303_accel_unified(54321);
void displaysensordetails(void) {
sensor_t sensor;
accel.getsensor(&sensor);
serial.println(“------------------------------------”);
serial.print(“sensor: ”);
serial.println(sensor.name);
serial.print(“driver ver: ”);
serial.println(sensor.version);
serial.print(“unique id: ”);
serial.println(sensor.sensor_id);
serial.print(“max value: ”);
serial.print(sensor.max_value);
serial.println(“ m/s^2”);
serial.print(“min value: ”);
serial.print(sensor.min_value);
serial.println(“ m/s^2”);
serial.print(“resolution: ”);
serial.print(sensor.resolution);
serial.println(“ m/s^2”);
serial.println(“------------------------------------”);
serial.println(“”);
delay(500);
}
void setup(void) {
#ifndef esp8266
while (!serial)
; // will pause zero, leonardo, etc until serial console opens
#endif
serial.begin(9600);
serial.println(“accelerometer test”);
serial.println(“”);
/* initialise the sensor */
if (!accel.begin()) {
/* there was a problem detecting the adxl345 。.. check your connections */
serial.println(“ooops, no lsm303 detected 。.. check your wiring!”);
while (1)
;
}
/* display some basic information on this sensor */
displaysensordetails();
accel.setrange(lsm303_range_4g);
serial.print(“range set to: ”);
lsm303_accel_range_t new_range = accel.getrange();
switch (new_range) {
case lsm303_range_2g:
serial.println(“+- 2g”);
break;
case lsm303_range_4g:
serial.println(“+- 4g”);
break;
case lsm303_range_8g:
serial.println(“+- 8g”);
break;
case lsm303_range_16g:
serial.println(“+- 16g”);
break;
}
accel.setmode(lsm303_mode_normal);
serial.print(“mode set to: ”);
lsm303_accel_mode_t new_mode = accel.getmode();
switch (new_mode) {
case lsm303_mode_normal:
serial.println(“normal”);
break;
case lsm303_mode_low_power:
serial.println(“low power”);
break;
case lsm303_mode_high_resolution:
serial.println(“high resolution”);
break;
}
}
void loop(void) {
/* get a new sensor event */
sensors_event_t event;
accel.getevent(&event);
/* display the results (acceleration is measured in m/s^2) */
serial.print(“x: ”);
serial.print(event.acceleration.x);
serial.print(“ ”);
serial.print(“y: ”);
serial.print(event.acceleration.y);
serial.print(“ ”);
serial.print(“z: ”);
serial.print(event.acceleration.z);
serial.print(“ ”);
serial.println(“m/s^2”);
/* delay before the next sample */
delay(500);
}
lsm303agr磁力计和指南针代码
下载:project zip 或 magsensor.ino | 在github上查看
复制代码
#include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303agr_mag_unified mag = adafruit_lsm303agr_mag_unified(12345);
void displaysensordetails(void) {
sensor_t sensor;
mag.getsensor(&sensor);
serial.println(“------------------------------------”);
serial.print(“sensor: ”);
serial.println(sensor.name);
serial.print(“driver ver: ”);
serial.println(sensor.version);
serial.print(“unique id: ”);
serial.println(sensor.sensor_id);
serial.print(“max value: ”);
serial.print(sensor.max_value);
serial.println(“ ut”);
serial.print(“min value: ”);
serial.print(sensor.min_value);
serial.println(“ ut”);
serial.print(“resolution: ”);
serial.print(sensor.resolution);
serial.println(“ ut”);
serial.println(“------------------------------------”);
serial.println(“”);
delay(500);
}
void setup(void) {
#ifndef esp8266
while (!serial)
; // will pause zero, leonardo, etc until serial console opens
#endif
serial.begin(115200);
serial.println(“magnetometer test”);
serial.println(“”);
/* enable auto-gain */
mag.enableautorange(true);
/* initialise the sensor */
if (!mag.begin()) {
/* there was a problem detecting the lsm303 。.. check your connections */
serial.println(“ooops, no lsm303agr detected 。.. check your wiring!”);
while (1)
;
}
/* display some basic information on this sensor */
displaysensordetails();
}
void loop(void) {
/* get a new sensor event */
sensors_event_t event;
mag.getevent(&event);
/* display the results (magnetic vector values are in micro-tesla (ut)) */
serial.print(“x: ”);
serial.print(event.magnetic.x);
serial.print(“ ”);
serial.print(“y: ”);
serial.print(event.magnetic.y);
serial.print(“ ”);
serial.print(“z: ”);
serial.print(event.magnetic.z);
serial.print(“ ”);
serial.println(“ut”);
/* note: you can also get the raw (non unified values) for */
/* the last data sample as follows. the .getevent call populates */
/* the raw values used below. */
// serial.print(“x raw: ”); serial.print(mag.raw.x); serial.print(“ ”);
// serial.print(“y raw: ”); serial.print(mag.raw.y); serial.print(“ ”);
// serial.print(“z raw: ”); serial.print(mag.raw.z); serial.println(“”);
/* delay before the next sample */
delay(500);
}
#include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303agr_mag_unified mag = adafruit_lsm303agr_mag_unified(12345);
void displaysensordetails(void) {
sensor_t sensor;
mag.getsensor(&sensor);
serial.println(“------------------------------------”);
serial.print(“sensor: ”);
serial.println(sensor.name);
serial.print(“driver ver: ”);
serial.println(sensor.version);
serial.print(“unique id: ”);
serial.println(sensor.sensor_id);
serial.print(“max value: ”);
serial.print(sensor.max_value);
serial.println(“ ut”);
serial.print(“min value: ”);
serial.print(sensor.min_value);
serial.println(“ ut”);
serial.print(“resolution: ”);
serial.print(sensor.resolution);
serial.println(“ ut”);
serial.println(“------------------------------------”);
serial.println(“”);
delay(500);
}
void setup(void) {
#ifndef esp8266
while (!serial)
; // will pause zero, leonardo, etc until serial console opens
#endif
serial.begin(115200);
serial.println(“magnetometer test”);
serial.println(“”);
/* enable auto-gain */
mag.enableautorange(true);
/* initialise the sensor */
if (!mag.begin()) {
/* there was a problem detecting the lsm303 。.. check your connections */
serial.println(“ooops, no lsm303agr detected 。.. check your wiring!”);
while (1)
;
}
/* display some basic information on this sensor */
displaysensordetails();
}
void loop(void) {
/* get a new sensor event */
sensors_event_t event;
mag.getevent(&event);
/* display the results (magnetic vector values are in micro-tesla (ut)) */
serial.print(“x: ”);
serial.print(event.magnetic.x);
serial.print(“ ”);
serial.print(“y: ”);
serial.print(event.magnetic.y);
serial.print(“ ”);
serial.print(“z: ”);
serial.print(event.magnetic.z);
serial.print(“ ”);
serial.println(“ut”);
/* note: you can also get the raw (non unified values) for */
/* the last data sample as follows. the .getevent call populates */
/* the raw values used below. */
// serial.print(“x raw: ”); serial.print(mag.raw.x); serial.print(“ ”);
// serial.print(“y raw: ”); serial.print(mag.raw.y); serial.print(“ ”);
// serial.print(“z raw: ”); serial.print(mag.raw.z); serial.println(“”);
/* delay before the next sample */
delay(500);
}
下载:project zip 或 compass.ino | 在github上查看
复制代码
#include
#include
#include
adafruit_lsm303agr_mag_unified mag = adafruit_lsm303agr_mag_unified(12345);
void setup(void)
{
serial.begin(115200);
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);
}
#include
#include
#include
adafruit_lsm303agr_mag_unified mag = adafruit_lsm303agr_mag_unified(12345);
void setup(void)
{
serial.begin(115200);
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);
}
lsm303/lsm303dlh磁力计和指南针代码
下载:project zip 或 magsensor.ino | 在github上查看
复制代码
#include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303dlh_mag_unified mag = adafruit_lsm303dlh_mag_unified(12345);
void displaysensordetails(void) {
sensor_t sensor;
mag.getsensor(&sensor);
serial.println(“------------------------------------”);
serial.print(“sensor: ”);
serial.println(sensor.name);
serial.print(“driver ver: ”);
serial.println(sensor.version);
serial.print(“unique id: ”);
serial.println(sensor.sensor_id);
serial.print(“max value: ”);
serial.print(sensor.max_value);
serial.println(“ ut”);
serial.print(“min value: ”);
serial.print(sensor.min_value);
serial.println(“ ut”);
serial.print(“resolution: ”);
serial.print(sensor.resolution);
serial.println(“ ut”);
serial.println(“------------------------------------”);
serial.println(“”);
delay(500);
}
void setup(void) {
#ifndef esp8266
while (!serial)
; // will pause zero, leonardo, etc until serial console opens
#endif
serial.begin(115200);
serial.println(“magnetometer test”);
serial.println(“”);
/* enable auto-gain */
mag.enableautorange(true);
/* 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)
;
}
/* display some basic information on this sensor */
displaysensordetails();
}
void loop(void) {
/* get a new sensor event */
sensors_event_t event;
mag.getevent(&event);
/* display the results (magnetic vector values are in micro-tesla (ut)) */
serial.print(“x: ”);
serial.print(event.magnetic.x);
serial.print(“ ”);
serial.print(“y: ”);
serial.print(event.magnetic.y);
serial.print(“ ”);
serial.print(“z: ”);
serial.print(event.magnetic.z);
serial.print(“ ”);
serial.println(“ut”);
/* delay before the next sample */
delay(500);
} #include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303dlh_mag_unified mag = adafruit_lsm303dlh_mag_unified(12345);
void displaysensordetails(void) {
sensor_t sensor;
mag.getsensor(&sensor);
serial.println(“------------------------------------”);
serial.print(“sensor: ”);
serial.println(sensor.name);
serial.print(“driver ver: ”);
serial.println(sensor.version);
serial.print(“unique id: ”);
serial.println(sensor.sensor_id);
serial.print(“max value: ”);
serial.print(sensor.max_value);
serial.println(“ ut”);
serial.print(“min value: ”);
serial.print(sensor.min_value);
serial.println(“ ut”);
serial.print(“resolution: ”);
serial.print(sensor.resolution);
serial.println(“ ut”);
serial.println(“------------------------------------”);
serial.println(“”);
delay(500);
}
void setup(void) {
#ifndef esp8266
while (!serial)
; // will pause zero, leonardo, etc until serial console opens
#endif
serial.begin(115200);
serial.println(“magnetometer test”);
serial.println(“”);
/* enable auto-gain */
mag.enableautorange(true);
/* 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)
;
}
/* display some basic information on this sensor */
displaysensordetails();
}
void loop(void) {
/* get a new sensor event */
sensors_event_t event;
mag.getevent(&event);
/* display the results (magnetic vector values are in micro-tesla (ut)) */
serial.print(“x: ”);
serial.print(event.magnetic.x);
serial.print(“ ”);
serial.print(“y: ”);
serial.print(event.magnetic.y);
serial.print(“ ”);
serial.print(“z: ”);
serial.print(event.magnetic.z);
serial.print(“ ”);
serial.println(“ut”);
/* delay before the next sample */
delay(500);
}
下载:project zip 或 compass.ino | 在github上查看
复制代码
#include
#include
#include
adafruit_lsm303dlh_mag_unified mag = adafruit_lsm303dlh_mag_unified(12345);
void setup(void) {
serial.begin(115200);
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);
} #include
#include
#include
adafruit_lsm303dlh_mag_unified mag = adafruit_lsm303dlh_mag_unified(12345);
void setup(void) {
serial.begin(115200);
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);
}
adafruit_lsm303dlhc库已存档
较早的lsm303dlh分支的adafruit_lsm303dlhc库已归档,并已由上面使用的adafruit_lsm303_accel和adafruit_lsm303dlh_mag库替换。我们建议您切换到较新的库,因为不支持旧的库或添加了功能。
如果您仍然希望使用旧的库,则可以单击下面的按钮转到存储库或以zip格式下载。
存档的adafruit_lsmdlhc存储库
下载adafruit_lsm303dlhc
python和circuitpython
很容易将lsm303传感器与circuitpython和adafruit circuitpython lsm303加速度计以及adafruit circuitpython lsm303dlh磁力计库一起使用。这些库可让您轻松编写可从传感器读取加速度和磁力计值的python代码。
由于adafruit_blinka,您可以将此传感器与任何circuitpython微控制器板或具有gpio和python的计算机一起使用,我们的circuitpython-for-python兼容性库。
在继续之前,请确保您知道传感器的版本
circuitpython微控制器布线
首先连接lsm303使用i2c连接完全按照arduino的前几页中的说明连接到板上。以下是使用i2c将feather m0连接到传感器的示例:
li》
板3v 到传感器vin(红线)
板gnd 到传感器gnd (黑线)
板scl 到传感器scl(黄线)
板sda 到传感器sda(蓝线)
python计算机接线
因为有几十个 您可以使用的linux计算机/主板,我们将显示raspberry pi的接线。对于其他平台,请访问linux上的circuitpython指南,以了解您的平台是否受支持。
以下是与i2c相连的raspberry pi:
pi 3v 到传感器vin(红线)
pi gnd 到传感器gnd(黑线)
pi scl 到传感器scl(黄线)
pi sda 到传感器sda(蓝线)

circuitpython安装lsm303库
接下来,您需要安装adafruit circuitpython lsm303加速度计和adafruit circuitpython lsm303agr或adafruit circuitpython lsm303dlh magn etometer(取决于您拥有的lsm303)库。
首先请确保您正在为板运行最新版本的adafruit circuitpython。
下一步,您需要安装使用硬件所需的库-请仔细按照以下步骤从adafruit的circuitpython库捆绑包中查找和安装这些库。我们的简介指南上有一个很棒的页面,介绍如何为快速和非表达板安装库包。
请记住非表达板,例如,您需要从束中手动安装必要的库:
adafruit_lsm303agr_mag.mpy或adafruit_lsm303dlh_mag.mpy
adafruit_lsm303_accel.mpy
adafruit_bus_device
adafruit_register
在继续之前,请确保您董事会的lib文件夹或根文件系统具有 adafruit_lsm303agr_mag .mpy或adafruit_lsm303dlh_mag.mpy, adafruit_lsm303_accel.mpy,adafruit_register,和 adafruit_bus_device 文件和文件夹
下一步连接到开发板的串行repl,因此您位于circuitpython的》》》 提示符下。
lsm303 li的python安装braries
您需要安装 adafruit_blinka 库,该库在python中提供了circuitpython支持。这可能还需要在您的平台上启用i2c并验证您正在运行python3。由于每个平台都有所不同,并且linux经常更改,请访问linux上的circuitpython指南以准备好您的计算机!
完成后,从命令行运行以下命令:
sudo pip3 install adafruit-circuitpython-lsm303-accel
然后为您的lsm303版本安装磁力计库。 :
lsm303agr:
sudo pip3 install adafruit-circuitpython-lsm303agr-mag
lsm303dlh:
sudo pip3 install adafruit-circuitpython-lsm303dlh-mag
如果您的默认python是版本3,则可能需要运行“ pip”。只要确保您不尝试在python 2.x上使用circuitpython,就不支持它!
circuitpython和python用法
这些示例使用adafruit_lsm303agr_mag库。如果您拥有lsm303dlh,则必须更改代码以使用adafruit_lsm303dlh_mag库
为演示传感器的用法,我们将对其进行初始化并读取
运行以下代码以导入必要的模块,并初始化与传感器的i2c连接:
》 lsm303agr
下载:文件
复制代码
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303agr_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303agr_mag.lsm303agr_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c) import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303agr_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303agr_mag.lsm303agr_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
lsm303dlh
下载:文件
复制代码
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303dlh_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303dlh_mag.lsm303dlh_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c) import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303dlh_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303dlh_mag.lsm303dlh_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
现在,您可以使用以下属性之一从传感器读取值:
加速度计
加速度-x,y, z加速度值,单位为米每秒每秒(m/s ^ 2)。
磁力计
磁性-微型特斯拉中x,y,z磁力计读数的三元组。
下载:文件
复制代码
print(“acceleration (m/s^2): x=%0.3f y=%0.3f z=%0.3f”%accel.acceleration)
print(“magnetometer (micro-teslas)): x=%0.3f y=%0.3f z=%0.3f”%mag.magnetic) print(“acceleration (m/s^2): x=%0.3f y=%0.3f z=%0.3f”%accel.acceleration)
print(“magnetometer (micro-teslas)): x=%0.3f y=%0.3f z=%0.3f”%mag.magnetic)
点击检测
lsm303的加速度计还可以检测单次或两次点击!
set_tap 方法可用于设置要检测的抽头数(1或2),以及设置抽头阈值。阈值越低,加速度计对检测抽头越敏感。您可能必须使用这个数字来找到适合您需求的值
在这里,我们还将使用 range 属性将加速度计的测量范围更改为 +/- 8g
下载:文件
复制代码
accel.range = adafruit_lsm303_accel.range.range_8g
accel.set_tap(1, 30)
while true:
if accel.tapped:
print(“tapped! ”) accel.range = adafruit_lsm303_accel.range.range_8g
accel.set_tap(1, 30)
while true:
if accel.tapped:
print(“tapped! ”)
当您在repl中键入上面的代码并给传感器一个良好的固定点击时,您应该会看到类似于输出如下。如果看不到“已点击!” 消息,则可能需要调整阈值。
as您可以从上面看到,tapped属性将在一个单击事件的多个周期内返回true。您必须在代码中对此进行说明。不幸的是,这似乎只是传感器的一个怪癖
这是将lsm303与circuitpython一起使用的全部内容。
下面是一个每秒读取传感器并打印其值的完整示例。将其另存为板上的 code.py 并打开repl以查看输出。
点击检测代码
下载:project zip 或 tap_detection.py | 在github上查看
复制代码
import board
import busio
import adafruit_lsm303_accel
i2c = busio.i2c(board.scl, board.sda)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
accel.range = adafruit_lsm303_accel.range.range_8g
accel.set_tap(1, 30)
while true:
if accel.tapped:
print(“tapped! ”)
import board
import busio
import adafruit_lsm303_accel
i2c = busio.i2c(board.scl, board.sda)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
accel.range = adafruit_lsm303_accel.range.range_8g
accel.set_tap(1, 30)
while true:
if accel.tapped:
print(“tapped! ”)
lsm303agr示例代码
下载:project zip 或 lsm303agr_combined .py | 在github上查看
复制代码
from time import sleep
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303agr_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303agr_mag.lsm303agr_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
while true:
print(“acceleration (m/s^2): x=%0.3f y=%0.3f z=%0.3f”%accel.acceleration)
print(“magnetometer (micro-teslas)): x=%0.3f y=%0.3f z=%0.3f”%mag.magnetic)
print(“”)
sleep(0.5)
from time import sleep
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303agr_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303agr_mag.lsm303agr_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
while true:
print(“acceleration (m/s^2): x=%0.3f y=%0.3f z=%0.3f”%accel.acceleration)
print(“magnetometer (micro-teslas)): x=%0.3f y=%0.3f z=%0.3f”%mag.magnetic)
print(“”)
sleep(0.5)
lsm303dlh示例代码
下载:project zip 或 lsm303dlh_combined.py | 在github上查看
复制代码
from time import sleep
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303dlh_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303dlh_mag.lsm303dlh_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
while true:
print(“acceleration (m/s^2): x=%0.3f y=%0.3f z=%0.3f”%accel.acceleration)
print(“magnetometer (micro-teslas)): x=%0.3f y=%0.3f z=%0.3f”%mag.magnetic)
print(“”)
sleep(0.5)
from time import sleep
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303dlh_mag
i2c = busio.i2c(board.scl, board.sda)
mag = adafruit_lsm303dlh_mag.lsm303dlh_mag(i2c)
accel = adafruit_lsm303_accel.lsm303_accel(i2c)
while true:
print(“acceleration (m/s^2): x=%0.3f y=%0.3f z=%0.3f”%accel.acceleration)
print(“magnetometer (micro-teslas)): x=%0.3f y=%0.3f z=%0.3f”%mag.magnetic)
print(“”)
sleep(0.5)
adafruit_circuitpython_lsm303库已存档
用于较早的lsm303dlh突破的adafruit_circuitpython_lsm303库已存档,并已由上面使用的adafruit_circuitpython_lsm303_accel和adafruit_circuitpython_lsm303dlh_mag库替换。我们建议您切换到较新的库,因为不支持旧的库或添加了功能。
如果您仍然希望使用旧的库,则可以单击下面的按钮转到存储库或以zip格式下载。
已存档的adafruit_circuitpython_lsm303存储库
下载adafruit_circuitpython_lsm303
校准
(fabio varesano的校准gui图像)
lsm303芯片在出厂时已校准到足以满足多种目的的精确度。但是对于imu等超临界应用程序,您可能需要进一步校准设备。
最终校准:
要进行超高精度加速度计校准,您将需要查看已故的fabio varesano提供的freeimu磁力计和加速度计gui。上面的图像(来自fabio网站)显示了传感器读数的图形表示以及根据原始数据计算出的校准偏差。
此全面的校准套件旨在在pc上运行。它太大了,无法在像arduino这样的微控制器上运行,但是它产生的校准偏差可以并入您的arduino草图中,以提高准确性。
简化的校准:
可以在arduino上完成仍然产生良好结果的更简单方法。该方法使用一个简单的草图记录所有3轴上的最小和最大读数。运行草图时,在所有三个轴上缓慢旋转lsm303模块多次。目的是记录每个轴的绝对最小值和最大值,因此旋转得越多,捕获绝对峰值的可能性就越大。
请确保将传感器缓慢绕其中心旋转,以使加速度计读数将仅表示由于重力引起的加速度,而不表示由于运动引起的传感器线性加速度。稍后,草图输出将稳定。显示的值将是每个轴的最小和最大范围,可用于重新缩放传感器的输出。
通过校准草图获得的值可用于执行在三个轴上分别进行两点校准:两点校准
校准草图:
lsm303agr
下载:project zip 或 calibration.ino | 在github上查看
复制代码
#include
#include
#include
adafruit_lsm303agr_mag_unified mag = adafruit_lsm303agr_mag_unified(12345);
float magminx, magmaxx;
float magminy, magmaxy;
float magminz, magmaxz;
long lastdisplaytime;
void setup(void)
{
serial.begin(115200);
serial.println(“lsm303 calibration”); serial.println(“”);
/* initialise the magnetometer */
if(!mag.begin())
{
/* there was a problem detecting the lsm303 。.. check your connections */
serial.println(“ooops, no lsm303 detected 。.. check your wiring!”);
while(1);
}
lastdisplaytime = millis();
}
void loop(void)
{
/* get a new sensor event */
sensors_event_t magevent;
mag.getevent(&magevent);
if (magevent.magnetic.x 《 magminx) magminx = magevent.magnetic.x;
if (magevent.magnetic.x 》 magmaxx) magmaxx = magevent.magnetic.x;
if (magevent.magnetic.y 《 magminy) magminy = magevent.magnetic.y;
if (magevent.magnetic.y 》 magmaxy) magmaxy = magevent.magnetic.y;
if (magevent.magnetic.z 《 magminz) magminz = magevent.magnetic.z;
if (magevent.magnetic.z 》 magmaxz) magmaxz = magevent.magnetic.z;
if ((millis() - lastdisplaytime) 》 1000) // display once/second
{
serial.print(“mag minimums: ”); serial.print(magminx); serial.print(“ ”);serial.print(magminy); serial.print(“ ”); serial.print(magminz); serial.println();
serial.print(“mag maximums: ”); serial.print(magmaxx); serial.print(“ ”);serial.print(magmaxy); serial.print(“ ”); serial.print(magmaxz); serial.println(); serial.println();
lastdisplaytime = millis();
}
}
#include
#include
#include
adafruit_lsm303agr_mag_unified mag = adafruit_lsm303agr_mag_unified(12345);
float magminx, magmaxx;
float magminy, magmaxy;
float magminz, magmaxz;
long lastdisplaytime;
void setup(void)
{
serial.begin(115200);
serial.println(“lsm303 calibration”); serial.println(“”);
/* initialise the magnetometer */
if(!mag.begin())
{
/* there was a problem detecting the lsm303 。.. check your connections */
serial.println(“ooops, no lsm303 detected 。.. check your wiring!”);
while(1);
}
lastdisplaytime = millis();
}
void loop(void)
{
/* get a new sensor event */
sensors_event_t magevent;
mag.getevent(&magevent);
if (magevent.magnetic.x 《 magminx) magminx = magevent.magnetic.x;
if (magevent.magnetic.x 》 magmaxx) magmaxx = magevent.magnetic.x;
if (magevent.magnetic.y 《 magminy) magminy = magevent.magnetic.y;
if (magevent.magnetic.y 》 magmaxy) magmaxy = magevent.magnetic.y;
if (magevent.magnetic.z 《 magminz) magminz = magevent.magnetic.z;
if (magevent.magnetic.z 》 magmaxz) magmaxz = magevent.magnetic.z;
if ((millis() - lastdisplaytime) 》 1000) // display once/second
{
serial.print(“mag minimums: ”); serial.print(magminx); serial.print(“ ”);serial.print(magminy); serial.print(“ ”); serial.print(magminz); serial.println();
serial.print(“mag maximums: ”); serial.print(magmaxx); serial.print(“ ”);serial.print(magmaxy); serial.print(“ ”); serial.print(magmaxz); serial.println(); serial.println();
lastdisplaytime = millis();
}
}
lsm303/lsm303dlh
下载:项目zip 或 calibration.ino | 在github上查看
复制代码
#include
#include
//#include
#include
adafruit_lsm303dlh_mag_unified mag = adafruit_lsm303dlh_mag_unified(12345);
float magminx, magmaxx;
float magminy, magmaxy;
float magminz, magmaxz;
long lastdisplaytime;
void setup(void) {
serial.begin(115200);
serial.println(“lsm303 calibration”);
serial.println(“”);
/* initialise the magnetometer */
if (!mag.begin()) {
/* there was a problem detecting the lsm303 。.. check your connections */
serial.println(“ooops, no lsm303 detected 。.. check your wiring!”);
while (1)
;
}
lastdisplaytime = millis();
}
void loop(void) {
/* get a new sensor event */
sensors_event_t magevent;
mag.getevent(&magevent);
if (magevent.magnetic.x 《 magminx)
magminx = magevent.magnetic.x;
if (magevent.magnetic.x 》 magmaxx)
magmaxx = magevent.magnetic.x;
if (magevent.magnetic.y 《 magminy)
magminy = magevent.magnetic.y;
if (magevent.magnetic.y 》 magmaxy)
magmaxy = magevent.magnetic.y;
if (magevent.magnetic.z 《 magminz)
magminz = magevent.magnetic.z;
if (magevent.magnetic.z 》 magmaxz)
magmaxz = magevent.magnetic.z;
if ((millis() - lastdisplaytime) 》 1000) // display once/second
{
serial.print(“mag minimums: ”);
serial.print(magminx);
serial.print(“ ”);
serial.print(magminy);
serial.print(“ ”);
serial.print(magminz);
serial.println();
serial.print(“mag maximums: ”);
serial.print(magmaxx);
serial.print(“ ”);
serial.print(magmaxy);
serial.print(“ ”);
serial.print(magmaxz);
serial.println();
serial.println();
lastdisplaytime = millis();
}
} #include
#include
//#include
#include
adafruit_lsm303dlh_mag_unified mag = adafruit_lsm303dlh_mag_unified(12345);
float magminx, magmaxx;
float magminy, magmaxy;
float magminz, magmaxz;
long lastdisplaytime;
void setup(void) {
serial.begin(115200);
serial.println(“lsm303 calibration”);
serial.println(“”);
/* initialise the magnetometer */
if (!mag.begin()) {
/* there was a problem detecting the lsm303 。.. check your connections */
serial.println(“ooops, no lsm303 detected 。.. check your wiring!”);
while (1)
;
}
lastdisplaytime = millis();
}
void loop(void) {
/* get a new sensor event */
sensors_event_t magevent;
mag.getevent(&magevent);
if (magevent.magnetic.x 《 magminx)
magminx = magevent.magnetic.x;
if (magevent.magnetic.x 》 magmaxx)
magmaxx = magevent.magnetic.x;
if (magevent.magnetic.y 《 magminy)
magminy = magevent.magnetic.y;
if (magevent.magnetic.y 》 magmaxy)
magmaxy = magevent.magnetic.y;
if (magevent.magnetic.z 《 magminz)
magminz = magevent.magnetic.z;
if (magevent.magnetic.z 》 magmaxz)
magmaxz = magevent.magnetic.z;
if ((millis() - lastdisplaytime) 》 1000) // display once/second
{
serial.print(“mag minimums: ”);
serial.print(magminx);
serial.print(“ ”);
serial.print(magminy);
serial.print(“ ”);
serial.print(magminz);
serial.println();
serial.print(“mag maximums: ”);
serial.print(magmaxx);
serial.print(“ ”);
serial.print(magmaxy);
serial.print(“ ”);
serial.print(magmaxz);
serial.println();
serial.println();
lastdisplaytime = millis();
}
}
制作曲目!
现在让我们使用lsm303模块进行一些简单的导航!
此页面上的图像显示较旧的lsm303dlh。您也可以使用更新的lsm303agr,但需要参考“引脚分配”页面来确定要使用的引脚
一天,制作曲目
在prax草原上,
来一个北向zax
和一个南向zax。
此zax-o-meter是北部或南部zax的理想导航工具。该项目演示了如何使用lsm303磁力计输出来实现简单的导航系统。无论您以哪种方式旋转,指针都将始终旋转到所需的行进方向。
永不退缩!这是我的规则。
从不退缩!
向西不远一英寸!
向东不一英寸!
zax-o-meter使用计算出的指南针航向作为连续旋转的反馈伺服。当指南针航向为零度(正北)时,伺服器停止旋转。与该值的任何偏差都会导致伺服器朝相反的方向旋转以进行补偿。负反馈的基本原理可用于构建自主机器人的导航系统。
材料:要构建zax-o-meter,您将需要:
adafruit lsm303接线板
arduino uno
arduino机箱
连续旋转伺服系统
跳线
卡片纸
纸板或泡沫芯
校准伺服器:为使zax-o-meter准确运行,您首先需要找到连续旋转伺服器的“中性”点。这是导致最小旋转的伺服输出值。该值通常约为90,但在伺服之间有所不同。运行此草图并修改值,直到获得最小旋转。那就是您应该在zax-o-meter草图中为servoneutral使用的值。
下载:文件
复制代码
#include
servo servo;
void setup()
{
servo.attach(9); // attaches the servo on pin 9 to the servo object
servo.write(90); // change this value to achieve minimum rotation!
}
void loop()
{
} #include
servo servo;
void setup()
{
servo.attach(9); // attaches the servo on pin 9 to the servo object
servo.write(90); // change this value to achieve minimum rotation!
}
void loop()
{
}
安装伺服器,标记并加宽外壳中的开口以适合伺服器。将伺服器的转子对准外壳的中心。向下按直到法兰与表面齐平,舵机应卡入到位并牢固固定。
安装uno并将其连接使用随附的螺钉将uno安装在机箱中。如下连接伺服和传感器:
伺服:
黑色-》 gnd
红色-》 5v
白色-》数字引脚9
lsm303:
gnd-》 gnd
vin-》 3.3v
sda-》模拟4
scl-》模拟5
然后将传感器导线穿过伺服器旁边的开口并关闭外壳。
添加指针从一些坚硬的硬纸板或泡沫芯,并用一些双面泡沫胶带将其粘贴到伺服喇叭上。
用一些双面泡沫胶带将传感器固定在箭头的下方。将传感器放置在尽可能远离伺服主体的位置,以避免电动机产生电磁干扰。
添加zaxen!查找您最喜欢的zax的图片。使用“画图”或其他图像编辑工具制作一对镜像图像。
将图像打印在一些较重的卡片纸上,然后将其折叠以制作双面图像。
将其切出并安装在其顶部。带有双面胶带的指示箭头。
代码:加载以下代码。不要忘记在“伺服校准”步骤中将“ servoneutral”更改为中性值。
示例代码适用于北向zax,目标标题为0。如果您是南向说服的zax ,targetheading为180将使您一直处在向南的位置。对于那些背叛的人,请在0-360度之间选择任何targetheading设置,然后开始在您选择的标题中进行跟踪!
下载:文件
复制代码
// **********************************************
// zax-o-meter sketch
// for the adafruit lsm303 magnetometer breakout
//
// written by bill earl for adafruit industries
//
// **********************************************
#include
#include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303_mag_unified mag = adafruit_lsm303_mag_unified(12345);
// this is our continuous rotation servo
servo servo;
// pi for calculations - not the raspberry type
const float pi = 3.14159;
// this is the value that gives you minimal rotation on
// a continuous rotation servo. it is usually about 90.
// adjust this value to give minimal rotation for your servo
const float servoneutral = 97;
// this is the desired direction of travel
// expressed as a 0-360 degree compass heading
// 0.0 = north
// 90.0 = east
// 180.0 = south
// 270 = west
const float targetheading = 0.0;
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);
}
servo.attach(9); // attach servo to pin 9
}
void loop(void)
{
/* get a new sensor event */
sensors_event_t event;
mag.getevent(&event);
// 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;
}
// calculate the error between tha measured heading and the target heading.
float error = heading - targetheading;
if (error 》 180)
{
error = error - 360; // for angles 》 180, correct in the opposite direction.
}
// a non-zero difference between the heading and the
// targetheading will bias the servoneutral value and
// cause the servo to rotate back toward the targetheading.
// the divisor is to reduce the reaction speed and avoid oscillations
servo.write(servoneutral + error / 4 );
delay(40);
} // **********************************************
// zax-o-meter sketch
// for the adafruit lsm303 magnetometer breakout
//
// written by bill earl for adafruit industries
//
// **********************************************
#include
#include
#include
#include
/* assign a unique id to this sensor at the same time */
adafruit_lsm303_mag_unified mag = adafruit_lsm303_mag_unified(12345);
// this is our continuous rotation servo
servo servo;
// pi for calculations - not the raspberry type
const float pi = 3.14159;
// this is the value that gives you minimal rotation on
// a continuous rotation servo. it is usually about 90.
// adjust this value to give minimal rotation for your servo
const float servoneutral = 97;
// this is the desired direction of travel
// expressed as a 0-360 degree compass heading
// 0.0 = north
// 90.0 = east
// 180.0 = south
// 270 = west
const float targetheading = 0.0;
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);
}
servo.attach(9); // attach servo to pin 9
}
void loop(void)
{
/* get a new sensor event */
sensors_event_t event;
mag.getevent(&event);
// 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;
}
// calculate the error between tha measured heading and the target heading.
float error = heading - targetheading;
if (error 》 180)
{
error = error - 360; // for angles 》 180, correct in the opposite direction.
}
// a non-zero difference between the heading and the
// targetheading will bias the servoneutral value and
// cause the servo to rotate back toward the targetheading.
// the divisor is to reduce the reaction speed and avoid oscillations
servo.write(servoneutral + error / 4 );
delay(40);
}
下载和链接
文件 strong》
lsm303agr
lsm303agr数据表
pcb文件(eagle格式)
adafruit fritzing库中的装饰对象
lsm303dlhc
lsm303dlhc数据表
pcb文件(鹰格式)
adafruit中的fritzing对象fritzing库
lsm0303agr示意图&fab print
lsm303dlhc示意图和构造打印


厦门利用北斗导航和RFID等技术推进海洋数字经济
全国首个数字化云启产业基地正式在南京开园
5G工业互联网的“下半场”该拼什么?
台面型InGaAs/InP基PIN短波红外偏振探测器原型器件
获取大语言模型(LLM)核心开发技能,报名 NVIDIA DLI 实战培训
如何将LSM303连接到Arduino
无人驾驶的硝烟 “高精地图”成关键技术
常用元器件的识别
波音公司已持有150亿美元现金来渡过新型冠状病毒疫的危机
智慧屏新方案上市 | ESP32-S3 + 480*480分辨率的2.1寸圆屏旋钮方案全新来袭
讯飞智能办公本X2全新发布,重新定义智能办公时代
IC载板市场如火如荼 IC载板需要攻破的三大难关
可编程DSP实现802.16 PHY信号处理
窄带宽物联网与低功率广域网络的现状
锚定力测试仪在乳腺手术中起到什么作用?
为贯彻落实决策部署,我国已开展淘汰落后和化解过剩产能工作检查
基于嵌入式Linux的自助点菜终端设计
为提升市场竞争力 英唐智控拟出资7,500万元设子公司
特斯拉每辆车毛利润15653美元 这是马斯克发起电动汽车价格大战的底气
制作低成本高效率的家用逆变器