Sei sulla pagina 1di 16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

LSM9DS1BreakoutHookupGuide
CONTRIBUTORS:
FAVORITE

JIMB0
1

Introduction
TheLSM9DS1isaversatile,motionsensingsysteminachip.Ithousesa3axisaccelerometer,3axis
gyroscope,and3axismagnetometerninedegreesoffreedom(9DOF)inasingleIC!Eachsensorin
theLSM9DS1supportsawiderangeofranges:theaccelerometersscalecanbesetto2,4,8,or16
g,thegyroscopesupports245,500,and2000/s,andthemagnetometerhasfullscalerangesof2,
4,12,or16gauss.TheIMUinachipissocoolweputitonaquartersizedbreakoutboard.

TheLSM9DS1isequippedwithadigitalinterface,buteventhatisflexible:itsupportsbothI2CandSPI,
soyoullbehardpressedtofindamicrocontrolleritdoesntworkwith.

CoveredInThisTutorial
ThistutorialisdevotedtoallthingsLSM9DS1.Wellintroduceyoutothechipitself,thenthebreakout
board.Thenwellswitchovertoexamplecode,andshowyouhowtointerfacewiththeboardusingan
ArduinoandourLSM9DS1Arduinolibrary.
Thetutorialissplitintothefollowingpages:
LSM9DS1OverviewAnoverviewoftheLSM9DS1,examiningitsfeaturesandcapabilities.
BreakoutBoardOverviewThispageexaminestheLSM9DS1BreakoutBoardtopicslikethe
pinout,jumpers,andschematicarecovered.
HardwareAssemblyAssemblytipsandtricks,plussomeinformationaboutthebreakouts
dimensions.
HardwareHookupExampleI2CandSPIwiringdiagrams.
InstlalingtheArduinoLibraryHowtoinstalltheArduinolibrary,anduseasimpleexample
sketchtoverifythatyourhookupworks.
UsingtheArduinoLibraryAnoverviewoftheSFE_LSM9DS1Arduinolibrarysfunctionsand
variables.
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

1/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

RequiredMaterials
ThistutorialexplainshowtousetheLSM9DS1BreakoutBoardwithanArduino.Tofollowalong,youll
needthefollowingmaterials:

LSM9DS1BreakoutBoard
ArduinoUNO,RedBoard,oranotherArduinocompatibleboard
StraightMaleHeadersOrwire.Somethingtoconnectbetweenthebreakoutandabreadboard.
BreadboardAnysize(evenmini)shoulddo.
M/MJumperWiresToconnectbetweenArduinoandbreadboard.
TheLSM9DS1isa3.3Vdevice!Supplyingvoltagesgreaterthan~3.6Vcanpermanentlydamage
theIC.AslongasyourArduinohasa3.3Vsupplyoutput,andyou'reOKwithusingI2C,you
shouldn'tneedanyextralevelshifting.ButifyouwanttouseSPI,youmayneedalevelshifter.

Alogiclevelshifterisrequiredforany5VoperatingArduino(UNO,RedBoard,Leonardo,etc).Ifyouuse
a3.3VbasedduinoliketheArduinoPro3.3Vor3.3VProMinithereisnoneedforlevelshifting.

SuggestedReading
Ifyourenotfamiliarwithsomeoftheconceptsbelow,werecommendcheckingoutthattutorialbefore
continuingon.
AccelerometerBasics
Gyroscopes
SerialPeripheralInterface(SPI)
InterICCommunication(I2C)
LogicLevels
BiDirectionalLevelShifterHookupGuide

LSM9DS1Overview
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

2/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

TheLSM9DS1isoneofonlyahandfulofICsthatcanmeasurethreekeypropertiesofmovement
angularvelocity,acceleration,andheadinginasingleIC.
Thegyroscopecanmeasureangularvelocitythatishowfast,andalongwhichaxis,amIrotating?
AngularvelocitiesaremeasuredindegreespersecondusuallyabbreviatedtoDPSor/s.The
LSM9DS1canmeasureupto2000DPS,thoughthatscalecanalsobesettoeither245or500DPSto
getafinerresolution.
Anaccelerometermeasuresacceleration,whichindicateshowfastvelocityischanginghowfastamI
speedinguporslowingdown?Accelerationisusuallyeithermeasuredinm/s2(meterspersecondper
second)orgs(gravities[about9.8m/s2]).Ifanobjectissittingmotionlessitfeelsabout1gof
accelerationtowardstheground(assumingthatgroundisonearth,andtheobjectisnearsealevel).
TheLSM9DS1measuresitsaccelerationings,anditsscalecanbesettoeither2,4,8,or16g.
Finally,theresthemagnetometer,whichmeasuresthepoweranddirectionofmagneticfields.Though
theyrenoteasilyvisible,magneticfieldsexistallarounduswhetheryoureholdingatinyferromagnet
orfeelinganattractiontoEarthsmagneticfield.TheLSM9DS1measuresmagneticfieldsinunitsof
gauss(Gs),andcansetitsmeasurementscaletoeither4,8,12,or16Gs.
Bymeasuringthesethreeproperties,youcangainagreatdealofknowledgeaboutanobjects
movementandorientation.9DOFshavetonsandtonsofapplications.Measuringtheforceanddirection
ofEarthsmagneticfieldwithamagnetometer,youcanapproximateyourheading.Anaccelerometerin
yourphonecanmeasurethedirectionoftheforceofgravity,andestimateorientation(portrait,
landscape,flat,etc.).Quadcopterswithbuiltingyroscopescanlookoutforsuddenrollsorpitches,and
correcttheirmomentumbeforethingsgetoutofhand.

AxisorientationsoftheLSM9DS1IC.NotetheICspolaritymarkingdot(forsomereasontheyrotated
themagnetometerinthedatasheet).
TheLSM9DS1measureseachofthesemovementpropertiesinthreedimensions.Thatmeansit
producesninepiecesofdata:accelerationinx/y/z,angularrotationinx/y/z,andmagneticforcein
x/y/z.TheLSM9DS1Breakouthaslabelsindicatingtheaccelerometerandgyroscopeaxisorientations,
whichsharearighthandrulerelationshipwitheachother.Notethat,accordingtothedatasheet,thex
andyaxesofthemagneticsensorareflipped(theimageaboveiscopiedfromthedatasheet).
TheLSM9DS1is,inasense,twoICssmashedintoonepackagelikeifyoucombinedanLSM6DS3
accel/gryowithanLSM303DLMTRaccel/mag.Onehalfofthedevicetakescareofallthingsgyroscope
andaccelerometer,andtheotherhalfmanagessolelythemagnetometer.Infact,afewofthecontrol
pinsarededicatedtoasinglesensortherearetwochipselectpins(CS_AGfortheaccel/gyroand
CS_Mforthemag)andtwoserialdataoutpins(SDO_AGandSDO_M).

ChooseYourOwnAdventure:SPIorI2C
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

3/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

Inadditiontobeingabletomeasureawidevarietyofmovementvectors,theLSM9DS1isalsomulti
featuredonthecommunicationinterfaceend.ItsupportsbothSPIandI2C,soyoushouldhaveno
difficultyfindingamicrocontrollerthatcantalktoit.
SPIisgenerallytheeasierofthetwotoimplement,butitalsorequiresmorewiresfourversusI2Cs
two.
BecausetheLSM9DS1supportsbothmethodsofcommunication,somepinshavetopulldouble
duty.TheSerialDataOut(SDO)pinforexample,doesjustthatforSPImode,butifyoureusingthe
deviceoverI2Citbecomesanaddressselector.Thechipselect(CS_MandCS_AG)pinsactivateSPI
modewhenlow,butiftheyrepulledhighthedeviceassumesI2Ccommunication.Inthesectionbelow
wediscusseachoftheLSM9DS1spins,watchoutforthosepinsthatsupportbothinterfaces.
FormuchmoredetailedinformationabouttheIC,weencourageyoutocheckoutthedatasheet!

BreakoutBoardOverview
NowthatyouknoweverythingyouneedtoabouttheLSM9DS1IC,letstalkabitaboutthebreakout
boarditsrestingon.Onthispagewelldiscussthepinsthatarebrokenout,andsomeoftheother
featuresontheboard.

ThePinout
Intotal,theLSM9DS1Breakoutbreaksout13pins.

Thebareminimumconnectionsrequiredarebrokenoutontheleftsideoftheboard.Thesearethe
powerandI2Cpins(thecommunicationinterfacetheboarddefaultsto):
Pin
Label

PinFunction

Notes

GND

Ground

0Vvoltagesupply

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

4/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

VDD

PowerSupply

Supplyvoltagetothechip.Shouldberegulatedbetween2.4Vand
3.6V.

SDA

SPI:MOSI
I2C:Serial
Data

SPI:Devicedatain(MOSI)
I2C:Serialdata(bidirectional)

SCL

SerialClock

I2CandSPIserialclock.

Theremainingpinsarebrokenoutontheotherside.ThesepinsbreakoutSPIfunctionalityandinterrupt
outputs:
Pin
Label

PinFunction

Notes

DEN

Gyroscope
DataEnable

Mostlyunknown.TheLSM9DS1datasheetdoesn'thavemuchtosayabout
thispin.

INT2

Accel/Gyro
Interrupt2

INT1andINT2areprogrammableinterruptsfortheaccelerometerand
gyroscope.Theycanbesettoalertonover/underthresholds,dataready,
orFIFOoverruns.

INT1

Accel/Gyro
Interrupt1

INTM

Magnetometer
Interrupt

Aprogrammableinterruptforthemagnetometer.Canbesettoalerton
overunderthresholds.

RDY

Magnetometer
DataReady

Aninterruptindicatingnewmagnetometerdataisavailable.Non
programmable.

CSM

Magnetometer
ChipSelect

ThispinselectsbetweenI2CandSPIonthemagnetometer.KeepitHIGH
forI2C,oruseitasan(activelow)chipselectforSPI.
HIGH(1):SPIidlemode/I2Cenabled
LOW(0):SPIenabled/I2Cdisabled.

CS
AG

Accel/Gyro
ChipSelect

ThispinselectsbetweenI2CandSPIontheaccel/gyro.KeepitHIGHfor
I2C,oruseitasan(activelow)chipselectforSPI.
HIGH(1):SPIidlemode/I2Cenabled
LOW(0):SPIenabled/I2Cdisabled.

SDO
M

SPI:
Magnetometer
MISO
I2C:
Magnetometer
Address

InSPImode,thisisthemagnetometerdataoutput(SDO_M).
InI2Cmode,thisselectstheLSboftheI2Caddress(SA0_M)

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

5/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

Select
SDO
AG

SPI:
Accel/Gyro
MISO
I2C:
Accel/Gryo
Address
Select

InSPImode,thisistheaccel/gryodataoutput(SDO_AG).
InI2Cmode,thisselectstheLSboftheI2Caddress(SA0_AG)

PowerSupply
TheVDDandGNDpinsarewhereyoullsupplyavoltageand0VreferencetotheIC.Thebreakout
boarddoesnotregulatethisvoltage,somakesureitfallswithintheallowedsupplyvoltagerangeofthe
LSM9DS1:2.4Vto3.6V.Belowistheelectricalcharacteristicstablefromthedatasheet.

Thecommunicationpinsarenot5Vtolerant,sotheyllneedtoberegulatedtowithinafewmVofVDD.
Anotherverycoolthingaboutthissensorishowlowpoweritis.Innormaloperationwitheverysensor
turnedonitllpullaround4.5mA.

Communication
CS_AG,CS_M,SDO_AG,SDO_M,SCL,andSDAareallusedfortheI2CandSPIinterfaces.The
functionofthesepinsdependsuponwhichofthetwointerfacesyoureusing.
IfyoureusingusingI2Chereshowyoumightconfigurethesepins:
PullCS_AGandCS_MHIGH.Thiswillsetboththeaccel/gyroandmagnetometertoI2Cmode.
SetSDO_AGandSDO_MeitherHIGHorLOW.ThesepinssettheI2Caddressofthegyroand
accel/magsensors.
ConnectSCLtoyourmicrocontrollersSCLpin.
ConnectSDAtoyourmicrocontrollersSDApin.
Theboardhasabuiltin10kpullupresistoronbothSDAandSCLlines.Ifthatvalueistoohigh,
youcanaddasecondresistorinparalleltodividethepullupresistancedown(another10kin
parallel,forexample,wouldcreateanequivalent5kresistance).
Or,ifyoureusingSPI:
ConnectCS_AGandCS_Mtotwoindividuallycontrollablepinsonyourmicrocontroller.These
chipselectsareactivelowwhenthepingoesLOW,SPIcommunicationwitheitherthe
accel/gyro(CS_AG)ormagnetometer(CS_M)isenabled.
SDO_AGandSDO_Maretheserialdataoutpins.Inmanycasesyoullwanttoconnectthem
together,andwirethemtoyourmicrocontrollersMISO(masterin,slaveout)pin.
ConnectSCLtoyourmicrocontrollersSCLK(serialclock)pin.
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

6/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

ConnectSDAtoyourmicrocontrollersMOSI(masterout,slavein)pin.

Interrupts
ThereareavarietyofinterruptsontheLSM9DS1.Whileconnectinguptotheseisnotascriticalasthe
communicationorpowersupplypins,usingthemwillhelpyougetthemostoutofthechip.
TheaccelerometerandgyroscopespecificinterruptsareINT1andINT2.Thesecanbothbe
programmedtointerruptaseitheractivehighoractivelow,triggeringoneventslikedatareadyorwhen
anaccelerationorangularrotationexceedsasetthreshold.
DRDYandINTMaredevotedmagnetometerinterrupts.DRDYwillgolowwhennewmagnetometer
readingsareavailable.INTMisalittlemorecustomizableitcanbeusedtotriggerwhenevermagnetic
fieldreadingsexceedathresholdonasetaxis.

TheJumpers
FlippingtheLSM9DS1breakoutoverrevealsatriooftwoway,surfacemountjumpers.Eachofthese
jumperscomesclosed.TheirpurposeistoautomaticallyputtheLSM9DS1intoI2Cmode.

Thethreetwowayjumpersonthebackoftheboard.Followthelabelstoseewhichpintheypullup.
EachofthesejumperspullsapairofpinsuptoVDD,througha10kresistor.Themiddlepadofthe
jumperconnectstotheresistor,andtheedgepadsconnecttoapin(followthelabelstofindoutwhich
one).Youcanseehowthosejumpersmatchupontheschematic:

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

7/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

ThetopjumperconnectsCS_AGandCS_MtoapullupthisllsettheLSM9DS1toI2Cmode.The
middlejumperpullsupSDO_AGandSDO_M,whichsetstheI2Caddressofthechip.Finally,thefarleft
jumperaddspullupresistorstotheI2CcommunicationpinsSDAandSCL.
Theintentionofthesejumpersistomakeitaseasyaspossibletousetheboardusingasfewwiresas
possible.IfyoureusingthebreakoutwithI2C,youcanignorethefourSDOandCSpins.
Todisableanyofthesejumpers,whipoutyourhandyhobbyknife,andcarefullycutthesmalltraces
betweenmiddlepadandedgepads.EvenifyoureusingSPI,though,thejumpersshouldnthinderyour
abilitytocommunicatewiththechip.

HardwareAssembly
Onthispagewelldiscussassemblyhints.Theresreallynotmuchtoassemblingthebreakoutboard
therealkeyissolderingsomethingintothebreakoutholes.

SolderSomething
TogetasolidelectricalandphysicalconnectiontotheLSM9DS1Breakout,youllneedtosoldereither
connectorsorwirestothebreakoutpins.What,exactly,yousolderintotheboarddependsonhow
youregoingtouseit.
Ifyouregoingtousethebreakoutboardinabreadboardorsimilar0.1"spacedperfboard,we
recommendsolderingstraightmaleheadersintothepins(therearealsolongheadersifyouneedem).

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

8/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

IfyoureonlygoingtousetheI2Cinterfaceandignoretheinterruptsyoucangetawaywithsoldering
justthefourpinheader.
Ifyouregoingtomountthebreakoutintoatightplace,youmaywanttooptforsolderingwires(stranded
orsolidcore)intothepins.

MountingtheBreakout
BecausetheLSM9DS1sensesmotion,itsimportant(formostapplications,atleast)tokeepitpinnedin
place.Sotheboardshavefourmountingholestowardthecorners.Thedrillholesare0.13"indiameter,
sotheyshouldaccommodateany4/40screw.

ConsulttheEAGLEPCBdesignfilestofindoutmoreabouttheBreakoutsdimensions.

HardwareHookup
2
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

9/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

TheLSM9DS1willworkovereitherI2CorSPI.Herearesomeexamplewiringdiagrams,demonstrating
howtowireupeachinterface.

I2CHardwareHookup
Outofthebox,theboardisconfiguredforanI2Cinterface,assuchwerecommendusingthishookupif
youreotherwiseagnostic.Allyouneedisfourwires!

ConnectingtheLSM9DS1toaRedBoardviaI2C.
Thishookupreliesonallofthejumpersonthebackoftheboardbeingset(astheyshouldbe,unless
theyvebeensliced).Ifthejumpershavebeencut,connectallfourCSandSDOpinsto3.3V.
NolevelshifterseventhoughtheArduinosI/Opinsare5V?Well,I2Cisafunnyinterface:pinsarent
directlypulledhighbyaGPIO,insteadanopendrainMOSFETreliesonpullupresistorstocreatea
logichigh.Becausethebreakoutboardpullupresistorsarestronger(lessresistance)thantheArduinos
internalpullups,thevoltageonthelogicpinswillbemuchcloserto3.3V(thoughalittlehigher)than5V
withinatolerablerange.JustmakesureyoupowerthebreakoutoffoftheArduinos3.3Vpowerrail!

SPIHardwareHookup
FortheSPIhookup,wellusetheArduinoshardwareSPIpins11(MOSI),12(MISO),and13(SCLK)
inadditiontotwodigitalpinsofyourchoice(forthechipselects).Ifyoureusinga3.3VArduino,likethe
3.3V/8MHzProMini,youcanhookthemicrocontrollerpinsdirectlyuptotheLSM9DS1.

Ifyoureusinga5VArduino,youllalsoneedtograbalevelshiftertokeeptheSPIsignalswithinthe
tolerablevoltagerange.YoucouldusetheSparkFunBiDirectionalLogicLevelConverterforexample:
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

10/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

(Unlessyouenjoywiretangles,theI2Cor3.3VSPIhookupsarerecommended.)

InstallingtheArduinoLibrary
WevewrittenafullfeaturedArduinolibrarytohelpmakeinterfacingwiththeLSM9DS1sgyro,
accelerometer,andmagnetometeraseasyaspossible.VisittheGitHubrepositorytodownloadthemost
recentversionofthelibrary,orclickthelinkbelow:
DOWNLOADTHESPARKFUNLSM9DS1ARDUINOLIBRARY
Forhelpinstallingthelibrary,checkoutourHowToInstallAnArduinoLibrarytutorial.Youllneedto
movetheSparkFun_LSM9DS1_Arduino_LibraryfolderintoalibrariesfolderwithinyourArduino
sketchbook.

TheLSM9DS1_Basic_I2CExample
Toverifythatyourhookupworks,loaduptheLSM9DS1_Basic_I2CexamplebygoingtoFile>
Examples>LSM9DS1Breakout>LSM9DS1_Basic_I2C.(IfyoureusingtheSPIhookup,loadthe
LSM9DS1_Basic_SPIexampleinstead.)
Thedefaultvaluessetbythissketchshouldworkforafresh,outoftheboxLSM9DS1Breakoutit
assumesallofthejumpersonthebacksideareclosed.Uploadthesketch,thenopenupyourserial
monitor,settingthebaudrateto115200.Youshouldseesomethinglikethis:

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

11/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

Thecurrentreadingfromeachaxisoneachsensorisprintedout,thenthosevaluesareusedto
estimatethesensorsorientation.Pitchistheanglerotatedaroundtheyaxis,rollistheboardsrotation
aroundthexaxis,andheading(i.e.yaw)isthesensorsrotationaroundthezaxis.Tryrotatingthe
board(withoutpullingoutanywires!)toseehowthevalueschange.

UsingtheArduinoLibrary
Tohelpdemonstratethelibrarysfunctionality,ahandfulofexamplesareincludedwhichrangefrom
basictomoreadvanced.TobegintogetafeelforthelibrarysAPI,tryloadingupsomeoftheother
examples.Thecommentsatthetopofthesketchwillinstructyouonanyextrahookupthatmaybe
requiredtouseinterruptsorotherfeatures.
Onthispagewellquicklyrundownsomeofthemorebasic,fundamentalconceptsimplementedbythe
library.

SetupStuff
Toenablethelibrary,youllneedtoincludeit,andyoualsoneedtoincludetheSPIandWirelibraries:
#include<SPI.h>//SPIlibraryincludedforSparkFunLSM9DS1
#include<Wire.h>//I2ClibraryincludedforSparkFunLSM9DS1
#include<SparkFunLSM9DS1.h>//SparkFunLSM9DS1library

MakesuretheSPIandWireincludesareabovetheSparkFunLSM9DS1(eventhoughyoullonlybe
usingoneofthetwointerfaces).

Constructor
TheconstructorcreatesaninstanceoftheLSM9DS1class.Onceyouvecreatedtheinstance,thats
whatyoullusetocontrolthebreakoutfromthereon.Thissinglelineofcodeisusuallyplacedinthe
globalareaofyoursketch.
Theconstructorshouldbeleftwithoutanyparameters:
//UsetheLSM9DS1classtocreateanobject.[imu]canbe
//namedanything,we'llrefertothatthroughtthesketch.
LSM9DS1imu;

SettingUptheInterface
TheLSM9DS1hastonsofsettingstobeconfigured.Someareminute,othersaremorecritical.The
threemostcriticalsettingswellneedtoconfigurearethecommunicationinterfaceandthedevice
addresses.Toconfigurethesevalues,wellmakecallstotheIMUs settings struct.
HeresanexamplethatsetstheIMUupforI2Cmode,withthedefault(high)I2Caddresses:

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

12/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

//SDO_XMandSDO_Garebothpulledhigh,soouraddressesare:
#defineLSM9DS1_M0x1E//Wouldbe0x1CifSDO_MisLOW
#defineLSM9DS1_AG0x6B//Wouldbe0x6AifSDO_AGisLOW
...
imu.settings.device.commInterface=IMU_MODE_I2C;//SetmodetoI2C
imu.settings.device.mAddress=LSM9DS1_M;//Setmagaddressto0x1E
imu.settings.device.agAddress=LSM9DS1_AG;//Setagaddressto0x6B

Alternatively,ifyoureusingSPImode,the imu.settings.device.mAddress and


imu.settings.device.agAddress valuesbecomethechipselectpins.Forexample,ifyoureusingSPI
modewithCS_AGconnectedtoD10andCS_MconnectedtoD9,yoursettingconfigurationwouldlook
likethis:
imu.settings.device.commInterface=IMU_MODE_SPI;//SetmodetoSPI
imu.settings.device.mAddress=9;//MagCSpinconnectedtoD9
imu.settings.device.agAddress=10;//AGCSpinconnectedtoD10

Configuringanyvaluefromthe imu.settings.device canttakeplaceintheglobalareofasketch.If


yougetacompilationerror,like 'imu'doesnotnameatype ,youmayhavethoseinthewrongplace
putthemin setup() .

begin()ingandSettingSensorRanges
OnceyouvecreatedtheLSM9DS1object,andconfigureditsinterface,callthe begin() member
functiontoinitializetheIMU.
The begin() functionwilltakethesettingsyouveadjustedinthepreviousstep,andattemptto
communicatewithandinitializethesensors.Youcancheckthereturnvalueof begin() toverify
whetherornotthesetupwassuccessfulitwillreturn 0 ifsomethinggoeswrong.
if(!imu.begin())
{
Serial.println("FailedtocommunicatewithLSM9DS1.");
Serial.println("Loopingtoinfinity.");
while(1)
;
}

Once begin() hasreturnedasuccess,youcanstartreadingsomesensorvalues!

ReadingandInterpretingtheSensors
Whatgoodisthesensorifyoucantgetanydatafromit!?Herearethefunctionsyoullneedtoget
acceleration,rotationspeed,andmagneticfieldstrengthdatafromthelibrary.

readAccel(),readGyro(),andreadMag()
Thesethreefunctions readAccel() , readGyro() ,and readMag() polltheLSM9DS1togetthemost
uptodatereadingsfromeachofthethreesensors.
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

13/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

Thereadfunctionsdonttakeanyparameters,andtheydontreturnanything,sohowdoyougetthat
data?Afterthefunctionrunsitscourse,itllupdateasetofthreeclassvariables,whichwillhavethe
sensordatayoudesire. readAccel() willupdate ax , ay ,and az , readGyro() willupdate gx , gy ,
and gz ,and readMag() willupdate mx , my ,and mz .Heresanexample:
imu.readAccel();//Updatetheaccelerometerdata
Serial.print(imu.ax);//Printxaxisdata
Serial.print(",");
Serial.print(imu.ay);//printyaxisdata
Serial.print(",");
Serial.println(imu.az);//printzaxisdata

Anexampleofreadingandprintingallthreeaxesofaccelerometerdata.
Thosevaluesareallsigned16bitintegers,meaningtheyllrangefrom32,768to32,767.Thatvalue
doesntmeanmuchunlessyouknowthescaleofyoursensor,whichiswherethenextfunctionscome
intoplay.

calcAccel(),calcGyro(),andcalcMag()
Thelibrarykeepstrackofeachsensorsscale,anditimplementsthesehelperfunctionstomake
translatingbetweentherawADCreadingsofthesensortoactualunitseasy.
calcAccel() , calcGyro() ,and calcMag() alltakeasingleparameterasigned16bitintegerand

converttotheirrespectiveunits.Theyallreturnafloatvalue,whichyoucandowithasyouplease.
Heresanexampleofprintingcalculatedgyroscopevalues:
imu.readGyro();//Updategyroscopedata
Serial.print(imu.calcGyro(imu.gx));//PrintxaxisrotationinDPS
Serial.print(",");
Serial.print(imu.calcGyro(imu.gy));//PrintyaxisrotationinDPS
Serial.print(",");
Serial.println(imu.calcGyro(imu.gz));//PrintzaxisrotationinDPS

SettingSensorRangesandSampleRates
SomeofthemorecommonlyalteredattributesintheIMUarethesensorrangesandoutputdatarates.
Thesevaluescanbeconfigured,onceagain,bysettingavalueinthe settings struct.
Forexample,tosettheIMUsaccelerometerrangeto16g,gyroscopeto2000/s,andmagnetometer
to8Gs,dosomethinglikethis:
imu.settings.accel.scale=16;//Setaccelrangeto+/16g
imu.settings.gyro.scale=2000;//Setgyrorangeto+/2000dps
imu.settings.mag.scale=8;//Setmagrangeto+/8Gs
imu.begin();//Callbegintoupdatethesensor'snewsettings

Theoutputdataratesareabitmoreabstract.Thesevaluescanrangefrom16,where1istheslowest
updaterateand6isthefastest.
https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

14/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

Resources&GoingFurther
HopefullythatinfodumpwasenoughtogetyourollingwiththeLSM9DS1.Ifyouneedanymore
information,herearesomemoreresources:
LSM9DS1ProductGitHubRepositoryYourrevisioncontrolledsourceforallthingsLSM9DS1.
Hereyoullfindourmostuptodatehardwarelayoutsandcode.
LSM9DS1DatasheetThisdatasheetcoverseverythingfromthehardwareandpinoutoftheIC,
totheregistermappingofthegyroscopeandaccelerometer/magnetometer.
LSM9DS1BreakoutSchematic
LSM9DS1BreakoutEAGLEFiles

GoingFurther
NowthatyouvegottheLSM9DS1upandrunning,whatprojectareyougoingtoincorporatemotion
sensinginto?Needalittleinspiration?Checkoutsomeofthesetutorials!
DungeonsandDragonsDiceGauntletThisprojectusesanaccelerometertosensearollingthe
dicemotion.YoucouldswapintheLSM9DS1toaddmorefunctionalitylikecompassbased
damagemultipliers!
AreYouOkay?WidgetUseanElectricImpandaccelerometertocreateanAreYouOKwidget.
AcozypieceoftechnologyyourfriendorlovedonecannudgetoletyouknowtheyreOKfrom
halfaworldaway.
LeapMotionTeardownAnIMUsensoriscool,butimagebasedmotionsensingisthefuture.
CheckoutthisteardownoftheminiatureKinectlikeLeapMotion!
PushingDatatoData.SparkFun.comNeedanonlineplacetostoreyourIMUdata?Checkout
data.sparkfun.com!ThistutorialdemonstrateshowtouseahandfulofArduinoshieldstopostyour
dataonline.

LeapMotionTeardown

DungeonsandDragonsDiceGauntlet

Let'sseewhat'sinsidetheamazingnewLeap
Motioninputdevice!

Aplayful,geekytutorialforaleatherbracerthat
usesaLilyPadArduino,LilyPadaccelerometer,
andsevensegmentdisplaytorollvirtual4,6,8,
10,12,20,and100sidediceforgaming.

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

15/16

3/25/2016

LSM9DS1BreakoutHookupGuidelearn.sparkfun.com

AreYouOkay?Widget

PushingDatatoData.SparkFun.com

UseanElectricImpandaccelerometertocreate
an"AreYouOK"widget.Acozypieceof
technologyyourfriendorlovedonecannudgeto
letyouknowthey'reOKfromhalfaworldaway.

Agrabbagofexamplestoshowoffthevarietyof
routesyourdatacantakeonitswaytoa
Data.SparkFun.comstream.

https://learn.sparkfun.com/tutorials/lsm9ds1breakouthookupguide?_ga=1.1350513.1738047427.1456537067

16/16

Potrebbero piacerti anche