Skip to content

Commit 73b4a88

Browse files
committed
added accelerometer examples
1 parent 1621277 commit 73b4a88

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Simplified example for the Adafruit LIS3DH accelerometer
3+
breakout board (https://www.adafruit.com/products/2809)
4+
based on Kevin Townsend's sample code.
5+
6+
Note: this code sets the accelerometer to 2G (aka 2x the acceleration
7+
due to gravity). The accelerometer has a 14-bit range, or -8193 to 8192.
8+
So at 1G, when the full force of gravity is on one of the axes,
9+
the reading should be around 4095 for that axis (or -4096, depending on
10+
the orientation).
11+
12+
modified 24 Nov 2015
13+
by Tom Igoe
14+
*/
15+
16+
#include <Wire.h>
17+
#include <Adafruit_LIS3DH.h>
18+
19+
Adafruit_LIS3DH accelerometer = Adafruit_LIS3DH();
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
while (!Serial);
24+
if (! accelerometer.begin(0x18)) {
25+
Serial.println("Couldn't start. Check wiring.");
26+
while (true); // stop here and do nothing
27+
}
28+
29+
accelerometer.setRange(LIS3DH_RANGE_2_G); // 2, 4, 8 or 16 G!
30+
Serial.print(accelerometer.getRange());
31+
}
32+
33+
void loop() {
34+
accelerometer.read(); // get X Y and Z data at once
35+
// Then print out the raw data
36+
Serial.print(convertReading(accelerometer.x));
37+
Serial.print(",");
38+
Serial.print(convertReading(accelerometer.y));
39+
Serial.print(",");
40+
Serial.println(convertReading(accelerometer.z));
41+
42+
}
43+
44+
float convertReading(int reading) {
45+
float divisor = 2 << (13 - accelerometer.getRange());
46+
float acceleration = (float)reading / divisor;
47+
return acceleration;
48+
}
49+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Simplified example for the Adafruit LIS3DH accelerometer
3+
breakout board (https://www.adafruit.com/products/2809)
4+
based on Kevin Townsend's sample code.
5+
6+
Note: this code sets the accelerometer to 2G (aka 2x the acceleration
7+
due to gravity). The accelerometer has a 14-bit range, or -8193 to 8192.
8+
So at 1G, when the full force of gravity is on one of the axes,
9+
the reading should be around 4095 for that axis (or -4096, depending on
10+
the orientation).
11+
12+
modified 24 Nov 2015
13+
by Tom Igoe
14+
*/
15+
16+
#include <Wire.h>
17+
#include <Adafruit_LIS3DH.h>
18+
19+
Adafruit_LIS3DH accelerometer = Adafruit_LIS3DH();
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
if (! accelerometer.begin(0x18)) {
24+
Serial.println("Couldn't start. Check wiring.");
25+
while (true); // stop here and do nothing
26+
}
27+
28+
accelerometer.setRange(LIS3DH_RANGE_2_G); // 2, 4, 8 or 16 G!
29+
30+
}
31+
32+
void loop() {
33+
int adc;
34+
float volt;
35+
String output;
36+
// read the ADCs
37+
for (int channel = 1; channel < 4; channel++) {
38+
adc = accelerometer.readADC(channel);
39+
// LIS3DH ADCs are only sensitive in a range from 1.7V to 0.9V:
40+
volt = map(adc, -32512, 32512, 1700, 900);
41+
output = "ADC ";
42+
output += channel;
43+
output += ": ";
44+
output += adc;
45+
output += " ";
46+
output += volt;
47+
output += " mV\t";
48+
Serial.print(output);
49+
delay(10);
50+
}
51+
Serial.println();
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Simplified tap example for the Adafruit LIS3DH accelerometer
3+
breakout board (https://www.adafruit.com/products/2809)
4+
based on Kevin Townsend's sample code.
5+
6+
modified 22 Jan 2016
7+
by Tom Igoe
8+
*/
9+
10+
#include <Wire.h>
11+
#include <Adafruit_LIS3DH.h>
12+
13+
Adafruit_LIS3DH accelerometer = Adafruit_LIS3DH();
14+
// Adjust this number for the sensitivity of the 'click' force
15+
// this strongly depend on the range! for 16G, try 5-10
16+
// for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80
17+
#define CLICKTHRESHHOLD 40
18+
19+
long steps = 0;
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
// 0x18 is the accelerometer's default I2C address:
24+
if (! accelerometer.begin(0x18)) {
25+
Serial.println("Couldn't start. Check wiring.");
26+
while (true); // stop here and do nothing
27+
}
28+
// accelerometer range can be 2, 4, 8, or 16G:
29+
accelerometer.setRange(LIS3DH_RANGE_2_G);
30+
31+
// 0 = turn off click detection & interrupt
32+
// 1 = single click only interrupt output
33+
// 2 = double click only interrupt output, detect single click
34+
// Adjust threshhold, higher numbers are less sensitive
35+
accelerometer.setClick(1, CLICKTHRESHHOLD);
36+
delay(100);
37+
}
38+
39+
void loop() {
40+
41+
/*
42+
* Each bit of the click byte value means something different:
43+
* bit 0: there's been a tap on the X axis
44+
* bit 1: tap on the Y axis
45+
* bit 2: tap on the Z axis
46+
* bit 3: direction of the tap
47+
* bit 4: enable single click detection
48+
* bit 5: enable double click detection
49+
*
50+
* The example below prints out the click value in binary,
51+
* so you can see each bit in action. Try tapping on all three
52+
* axes to see how it works:
53+
*/
54+
byte click = accelerometer.getClick();
55+
if (click > 0) {
56+
// print the whole byte in binary:
57+
Serial.println(click, BIN);
58+
// print which axis was tapped:
59+
if (bitRead(click, 0) == 1) {
60+
Serial.println("tap on X axis");
61+
}
62+
if (bitRead(click, 1) == 1) {
63+
Serial.println("tap on Y axis");
64+
}
65+
if (bitRead(click, 2) == 1) {
66+
Serial.println("tap on Z axis");
67+
}
68+
}
69+
70+
// adjust the delay depending on how frequently you
71+
// plan to detect clicks. For a step detector, 30 - 80 ms works:
72+
delay(30);
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Simplified example for the Adafruit MMA8451 accelerometer
3+
breakout board (https://www.adafruit.com/products/2019)
4+
based on Kevin Townsend's sample code.
5+
6+
Note: this code sets the accelerometer to 2G (aka 2x the acceleration
7+
due to gravity). The accelerometer has a 14-bit range, or -8193 to 8192.
8+
So at 1G, when the full force of gravity is on one of the axes,
9+
the reading should be around 4095 for that axis (or -4096, depending on
10+
the orientation).
11+
12+
modified 23 Nov 2015
13+
by Tom Igoe
14+
*/
15+
16+
#include <Wire.h>
17+
#include <Adafruit_MMA8451.h>
18+
19+
Adafruit_MMA8451 mma = Adafruit_MMA8451();
20+
21+
void setup(void) {
22+
Serial.begin(9600);
23+
// initialize communications with the accelerometer:
24+
if (! mma.begin()) {
25+
Serial.println("Couldnt start. Check wiring the to the sensor.");
26+
while (true); // stop here and do nothing else
27+
}
28+
// set accelerometer range to 2G max:
29+
mma.setRange(MMA8451_RANGE_2_G);
30+
}
31+
32+
void loop() {
33+
// Read the accelerometer data
34+
// (ranges from -8193 to 8192 at max acceleration):
35+
mma.read();
36+
Serial.print("X: ");
37+
Serial.print(mma.x);
38+
Serial.print("\tY: ");
39+
Serial.print(mma.y);
40+
Serial.print("\tZ: ");
41+
Serial.println(mma.z);
42+
43+
}

0 commit comments

Comments
 (0)