Skip to content

Commit 08509c3

Browse files
author
Nathan Seidle
committed
Update examples to use Wire. Update image in readme.
1 parent 2b0732c commit 08509c3

File tree

6 files changed

+57
-114
lines changed

6 files changed

+57
-114
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SparkFun LIS2DH12 Arduino Library
22
===========================================================
33

4-
![SparkFun Qwiic LIS2DH12](https://cdn.sparkfun.com//assets/parts/1/3/4/3/3/Qwiic_Twist_Hookup_Guide.jpg)
4+
![SparkFun Qwiic LIS2DH12](https://cdn.sparkfun.com//assets/parts/1/4/3/5/5/15760-Triple_Axis_Accelerometer_Breakout_-_LIS2DH12__Qwiic_-01.jpg)
55

66
[*SparkFun Qwiic LIS2DH12 Accelerometer (DEV-15760)*](https://www.sparkfun.com/products/15760)
77

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/*
1+
/*
22
Reading and controlling the very low power LIS2DH12
33
Author: Nathan Seidle
44
Created: Septempter 18th, 2019
55
License: This code is Lemonadeware; do whatever you want with this code.
66
If you see me (or any other SparkFun employee) at the
77
local, and you've found our code helpful, please buy us a round!
8-
8+
99
This example demonstrates how to read XYZ from the LIS2DH12.
1010
1111
Feel like supporting open source hardware?
@@ -23,96 +23,55 @@
2323
#include <Wire.h>
2424

2525
#include "SparkFun_LIS2DH12.h" //Click here to get the library: http://librarymanager/All#SparkFun_LIS2DH12
26-
27-
SPARKFUN_LIS2DH12 accel; //Create instance
28-
29-
#define ACCEL_ADDRESS 0x19 //Default address on the LIS2DH12 breakout board, Edge, and Edge2
30-
//#define ACCEL_ADDRESS 0x18 //Address if the address jumper is closed on the breakout board
26+
SPARKFUN_LIS2DH12 accel; //Create instance
3127

3228
void setup()
3329
{
3430
Serial.begin(115200);
3531
Serial.println("SparkFun Accel Example");
3632

37-
Wire.begin(); //Accel is on IOM3 and defined in the variant file as Wire1.
33+
Wire.begin();
3834

39-
//By default the SparkFun library uses Wire. We need to begin
40-
//with Wire1 on the Edge/Edge2.
41-
if (accel.begin(ACCEL_ADDRESS, Wire) == false)
35+
if (accel.begin() == false)
4236
{
43-
Serial.println("Accelerometer not detected. Are you sure you did a Wire1.begin()? Freezing...");
44-
while (1);
37+
Serial.println("Accelerometer not detected. Check address jumper and wiring. Freezing...");
38+
while (1)
39+
;
4540
}
46-
47-
accel.setDataRate(LIS2DH12_ODR_100Hz); //1 to 5.376kHz
48-
int currentRate = accel.getDataRate();
49-
Serial.print("Current rate (0 to 9): ");
50-
Serial.println(currentRate);
51-
52-
accel.setScale(LIS2DH12_2g);
53-
//accel.setScale(LIS2DH12_16g);
54-
int currentScale = accel.getScale();
55-
Serial.print("Current scale (0 to 3): ");
56-
Serial.println(currentScale);
57-
58-
accel.setMode(LIS2DH12_LP_8bit);
59-
//accel.setMode(LIS2DH12_NM_10bit);
60-
//accel.setMode(LIS2DH12_HR_12bit);
61-
int currentMode = accel.getMode();
62-
Serial.print("Current mode (0 to 2): ");
63-
Serial.println(currentMode);
64-
65-
accel.enableTemperature();
66-
//accel.disableTemperature();
67-
68-
accel.enableTapDetection();
69-
accel.setTapThreshold(40); //7-bit value. 4 is a little low. 7 is ok. 10 is a bit hard.
70-
71-
while (accel.isTapped() == true) delay(10); //Clear any initial event that may be in the buffer
7241
}
7342

74-
int tapCounter = 0;
75-
7643
void loop()
7744
{
78-
if (accel.isTapped())
79-
{
80-
Serial.print("Tap: ");
81-
Serial.println(tapCounter++);
82-
83-
while (accel.isTapped() == true) delay(10); //Clear any initial event that may be in the buffer
84-
}
8545
//Print accel values only if new data is available
8646
if (accel.available())
8747
{
88-
// float accelX = accel.getX();
89-
// float accelY = accel.getY();
90-
// float accelZ = accel.getZ();
91-
// float tempC = accel.getTemperature();
92-
//
93-
// Serial.print("Acc [mg]: ");
94-
// Serial.print(accelX, 1);
95-
// Serial.print(" x, ");
96-
// Serial.print(accelY, 1);
97-
// Serial.print(" y, ");
98-
// Serial.print(accelZ, 1);
99-
// Serial.print(" z, ");
100-
// Serial.print(tempC, 1);
101-
// Serial.print("C");
102-
// Serial.println();
48+
float accelX = accel.getX();
49+
float accelY = accel.getY();
50+
float accelZ = accel.getZ();
51+
float tempC = accel.getTemperature();
10352

104-
int rawX = accel.getRawX();
105-
int rawY = accel.getRawY();
106-
int rawZ = accel.getRawZ();
107-
108-
Serial.print("Acc raw: ");
109-
Serial.print(rawX);
53+
Serial.print("Acc [mg]: ");
54+
Serial.print(accelX, 1);
11055
Serial.print(" x, ");
111-
Serial.print(rawY);
56+
Serial.print(accelY, 1);
11257
Serial.print(" y, ");
113-
Serial.print(rawZ);
114-
Serial.print(" z");
58+
Serial.print(accelZ, 1);
59+
Serial.print(" z, ");
60+
Serial.print(tempC, 1);
61+
Serial.print("C");
11562
Serial.println();
116-
}
11763

64+
// int rawX = accel.getRawX();
65+
// int rawY = accel.getRawY();
66+
// int rawZ = accel.getRawZ();
67+
//
68+
// Serial.print("Acc raw: ");
69+
// Serial.print(rawX);
70+
// Serial.print(" x, ");
71+
// Serial.print(rawY);
72+
// Serial.print(" y, ");
73+
// Serial.print(rawZ);
74+
// Serial.print(" z");
75+
// Serial.println();
76+
}
11877
}

examples/Example2_Settings/Example2_Settings.ino

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@
2525
#include <Wire.h>
2626

2727
#include "SparkFun_LIS2DH12.h" //Click here to get the library: http://librarymanager/All#SparkFun_LIS2DH12
28-
29-
SPARKFUN_LIS2DH12 accel; //Create instance
30-
31-
#define ACCEL_ADDRESS 0x19 //Default address on the LIS2DH12 breakout board, Edge, and Edge2
32-
//#define ACCEL_ADDRESS 0x18 //Address if the address jumper is closed on the breakout board
28+
SPARKFUN_LIS2DH12 accel; //Create instance
3329

3430
void setup()
3531
{
3632
Serial.begin(115200);
3733
Serial.println("SparkFun Accel Example");
3834

39-
Wire1.begin(); //Accel is on IOM3 and defined in the variant file as Wire1.
35+
Wire.begin();
4036

41-
if (accel.begin(ACCEL_ADDRESS, Wire1) == false)
37+
if (accel.begin() == false)
4238
{
4339
Serial.println("Accelerometer not detected. Freezing...");
44-
while (1);
40+
while (1)
41+
;
4542
}
4643

4744
accel.setScale(LIS2DH12_2g);
@@ -73,7 +70,7 @@ void setup()
7370
//accel.setDataRate(LIS2DH12_ODR_5kHz376_LP_1kHz344_NM_HP);
7471
int currentRate = accel.getDataRate();
7572
Serial.print("Current rate (0 to 9): ");
76-
Serial.println(currentRate);
73+
Serial.println(currentRate);
7774
}
7875

7976
void loop()

examples/Example3_ReadRaw/Example3_ReadRaw.ino

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,20 @@
2323
#include <Wire.h>
2424

2525
#include "SparkFun_LIS2DH12.h" //Click here to get the library: http://librarymanager/All#SparkFun_LIS2DH12
26-
27-
SPARKFUN_LIS2DH12 accel; //Create instance
28-
29-
#define ACCEL_ADDRESS 0x19 //Default address on the LIS2DH12 breakout board, Edge, and Edge2
30-
//#define ACCEL_ADDRESS 0x18 //Address if the address jumper is closed on the breakout board
26+
SPARKFUN_LIS2DH12 accel; //Create instance
3127

3228
void setup()
3329
{
3430
Serial.begin(115200);
3531
Serial.println("SparkFun Accel Example");
3632

37-
Wire1.begin(); //Accel is on IOM3 and defined in the variant file as Wire1.
33+
Wire.begin();
3834

39-
//By default the SparkFun library uses Wire. We need to begin
40-
//with Wire1 on the Edge/Edge2.
41-
if (accel.begin(ACCEL_ADDRESS, Wire1) == false)
35+
if (accel.begin() == false)
4236
{
4337
Serial.println("Accelerometer not detected. Are you sure you did a Wire1.begin()? Freezing...");
44-
while (1);
38+
while (1)
39+
;
4540
}
4641
}
4742

@@ -64,5 +59,4 @@ void loop()
6459
Serial.print(" z");
6560
Serial.println();
6661
}
67-
6862
}

examples/Example4_TapDetection/Example4_TapDetection.ino

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,20 @@
2626

2727
SPARKFUN_LIS2DH12 accel; //Create instance
2828

29-
#define ACCEL_ADDRESS 0x19 //Default address on the LIS2DH12 breakout board, Edge, and Edge2
30-
//#define ACCEL_ADDRESS 0x18 //Address if the address jumper is closed on the breakout board
31-
3229
int tapCounter = 0;
3330

3431
void setup()
3532
{
3633
Serial.begin(115200);
3734
Serial.println("SparkFun Accel Example");
3835

39-
Wire1.begin(); //Accel is on IOM3 and defined in the variant file as Wire1.
36+
Wire.begin();
4037

41-
//By default the SparkFun library uses Wire. We need to begin
42-
//with Wire1 on the Edge/Edge2.
43-
if (accel.begin(ACCEL_ADDRESS, Wire1) == false)
38+
if (accel.begin() == false)
4439
{
4540
Serial.println("Accelerometer not detected. Are you sure you did a Wire1.begin()? Freezing...");
46-
while (1);
41+
while (1)
42+
;
4743
}
4844

4945
//By default, sensor is set to 25Hz, 12bit, 2g at .begin()
@@ -52,7 +48,8 @@ void setup()
5248
accel.enableTapDetection();
5349
accel.setTapThreshold(40); //7-bit value. Max value is 127. 10 is a little low. 40 is ok. 100 is a bit hard.
5450

55-
while (accel.isTapped() == true) delay(10); //Clear any initial event that may be in the buffer
51+
while (accel.isTapped() == true)
52+
delay(10); //Clear any initial event that may be in the buffer
5653
}
5754

5855
void loop()
@@ -62,6 +59,7 @@ void loop()
6259
Serial.print("Tap: ");
6360
Serial.println(++tapCounter);
6461

65-
while (accel.isTapped() == true) delay(10); //Wait for event to complete
62+
while (accel.isTapped() == true)
63+
delay(10); //Wait for event to complete
6664
}
6765
}

examples/Example5_LowestPower/Example5_LowestPower.ino

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,20 @@
2424
#include <Wire.h>
2525

2626
#include "SparkFun_LIS2DH12.h" //Click here to get the library: http://librarymanager/All#SparkFun_LIS2DH12
27-
28-
SPARKFUN_LIS2DH12 accel; //Create instance
29-
30-
#define ACCEL_ADDRESS 0x19 //Default address on the LIS2DH12 breakout board, Edge, and Edge2
31-
//#define ACCEL_ADDRESS 0x18 //Address if the address jumper is closed on the breakout board
27+
SPARKFUN_LIS2DH12 accel; //Create instance
3228

3329
void setup()
3430
{
3531
Serial.begin(115200);
3632
Serial.println("SparkFun Accel Example");
3733

38-
Wire1.begin(); //Accel is on IOM3 and defined in the variant file as Wire1.
34+
Wire.begin();
3935

40-
//By default the SparkFun library uses Wire. We need to begin
41-
//with Wire1 on the Edge/Edge2.
42-
if (accel.begin(ACCEL_ADDRESS, Wire1) == false)
36+
if (accel.begin() == false)
4337
{
4438
Serial.println("Accelerometer not detected. Are you sure you did a Wire1.begin()? Freezing...");
45-
while (1);
39+
while (1)
40+
;
4641
}
4742

4843
//8bit, 10Hz = 3uA

0 commit comments

Comments
 (0)