# LIS3DH interface

**URL:** <https://forum.core-electronics.com.au/t/lis3dh-interface/3957>\
**Category:** Uncategorized\
**Created:** [19 November 2018 09:24 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957 "2018-11-19T09:24:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [19 November 2018 09:24 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/1 "2018-11-19T09:24:18Z")

</div>

> // Basic demo for tap/doubletap readings from Adafruit LIS3DH
> 
> #include \<Wire.h\>  
> #include \<SPI.h\>  
> #include \<Adafruit\_LIS3DH.h\>  
> #include \<Adafruit\_Sensor.h\>
> 
> // Used for software SPI  
> #define LIS3DH\_CLK 13  
> #define LIS3DH\_MISO 12  
> #define LIS3DH\_MOSI 11  
> // Used for hardware & software SPI  
> #define LIS3DH\_CS 10
> 
> // software SPI  
> //Adafruit\_LIS3DH lis = Adafruit\_LIS3DH(LIS3DH\_CS, LIS3DH\_MOSI, LIS3DH\_MISO, LIS3DH\_CLK);  
> // hardware SPI  
> //Adafruit\_LIS3DH lis = Adafruit\_LIS3DH(LIS3DH\_CS);  
> // I2C  
> Adafruit\_LIS3DH lis = Adafruit\_LIS3DH();
> 
> #if defined(ARDUINO\_ARCH\_SAMD)  
> // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!  
> #define Serial SerialUSB  
> #endif
> 
> // Adjust this number for the sensitivity of the ‘click’ force  
> // this strongly depend on the range! for 16G, try 5-10  
> // for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80  
> #define CLICKTHRESHHOLD 80
> 
> void setup(void) {  
> #ifndef ESP8266  
> while (!Serial); // will pause Zero, Leonardo, etc until serial console opens  
> #endif
> 
> Serial.begin(9600);  
> Serial.println(“Adafruit LIS3DH Tap Test!”);
> 
> if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address  
> Serial.println(“Couldnt start”);  
> while (1);  
> }  
> Serial.println(“LIS3DH found!”);
> 
> lis.setRange(LIS3DH\_RANGE\_2\_G); // 2, 4, 8 or 16 G!
> 
> Serial.print("Range = "); Serial.print(2 \<\< lis.getRange());  
> Serial.println(“G”);
> 
> // 0 = turn off click detection & interrupt  
> // 1 = single click only interrupt output  
> // 2 = double click only interrupt output, detect single click  
> // Adjust threshhold, higher numbers are less sensitive  
> lis.setClick(2, CLICKTHRESHHOLD);  
> delay(100);  
> }
> 
> void loop() {  
> uint8\_t click = lis.getClick();  
> if (click == 0) return;  
> if (! (click & 0x30)) return;  
> Serial.print(“Click detected (0x”); Serial.print(click, HEX); Serial.print(“): “);  
> if (click & 0x10) Serial.print(” single click”);  
> if (click & 0x20) Serial.print(" double click");  
> Serial.println();
> 
> delay(100);  
> return;  
> }

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [19 November 2018 09:26 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/2 "2018-11-19T09:26:20Z")

</div>

Sorry still working out how to post on forum.

My Question:

It is unclear to me which pins of the sensor need to be connected Clk. D, CE to which pins of the MAXIM board in these examples and is this using I2C or SPI communication. ??? And do they use different pins for each type of communication? This is a simple question but I have no idea how to easily navigate the code to find out and how to change the pins for same function.

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [19 November 2018 23:24 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/3 "2018-11-19T23:24:30Z")

</div>

Hi Clem,

Here is a hookup guide that shows you how to hook up the LIS3DH:

> **[Arduino | Adafruit LIS3DH Triple-Axis Accelerometer Breakout | Adafruit Learning...](https://learn.adafruit.com/adafruit-lis3dh-triple-axis-accelerometer-breakout/arduino)**
>
> Triple Axis, Tap & Double Tap, & 3 ADCs!

You should be able to use any digital pin for those connections.

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 00:43 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/4 "2018-11-20T00:43:25Z")

</div>

I get this error when I try to compile the example code: acceldemo :

exit status 1  
Error compliling for board Arduino Mega ADK.

 ![Error_](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/2/2aa61b3406e965dd6b144aed34307ca68974b784.jpeg)

// Basic demo for accelerometer readings from Adafruit LIS3DH

#include \<Wire.h\>  
#include \<SPI.h\>  
#include \<Adafruit\_LIS3DH.h\>  
#include \<Adafruit\_Sensor.h\>

// Used for software SPI  
#define LIS3DH\_CLK 13  
#define LIS3DH\_MISO 12  
#define LIS3DH\_MOSI 11  
// Used for hardware & software SPI  
#define LIS3DH\_CS 10

// software SPI  
//Adafruit\_LIS3DH lis = Adafruit\_LIS3DH(LIS3DH\_CS, LIS3DH\_MOSI, LIS3DH\_MISO, LIS3DH\_CLK);  
// hardware SPI  
//Adafruit\_LIS3DH lis = Adafruit\_LIS3DH(LIS3DH\_CS);  
// I2C  
Adafruit\_LIS3DH lis = Adafruit\_LIS3DH();

#if defined(ARDUINO\_ARCH\_SAMD)  
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!  
#define Serial SerialUSB  
#endif

void setup(void) {  
#ifndef ESP8266  
while (!Serial); // will pause Zero, Leonardo, etc until serial console opens  
#endif

Serial.begin(9600);  
Serial.println(“LIS3DH test!”);

if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address  
Serial.println(“Couldnt start”);  
while (1);  
}  
Serial.println(“LIS3DH found!”);

lis.setRange(LIS3DH\_RANGE\_4\_G); // 2, 4, 8 or 16 G!

Serial.print("Range = "); Serial.print(2 \<\< lis.getRange());  
Serial.println(“G”);  
}

void loop() {  
lis.read(); // get X Y and Z data at once  
// Then print out the raw data  
Serial.print("X: “); Serial.print(lis.x);  
Serial.print(” \tY: “); Serial.print(lis.y);  
Serial.print(” \tZ: "); Serial.print(lis.z);

/\* Or…get a new sensor event, normalized \*/  
sensors\_event\_t event;  
lis.getEvent(&event);

/\* Display the results (acceleration is measured in m/s^2) \*/  
Serial.print("\t\tX: “); Serial.print(event.acceleration.x);  
Serial.print(” \tY: “); Serial.print(event.acceleration.y);  
Serial.print(” \tZ: “); Serial.print(event.acceleration.z);  
Serial.println(” m/s^2 ");

Serial.println();

delay(200);  
}

> Blockquote

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [20 November 2018 01:06 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/5 "2018-11-20T01:06:41Z")

</div>

Clem,

Are you really using the Mega ADK? I thought you were using the MAXIM board. You will need to select the correct board in the Arduino IDE.

Let me know how it works out!

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 01:09 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/6 "2018-11-20T01:09:49Z")

</div>

IDUINO MEGA it says on the board?

board model mega 2560 A3  
made by IDUINO

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [20 November 2018 01:15 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/7 "2018-11-20T01:15:34Z")

</div>

Hi Clem,

Under Tools\>Board:

select Arduino/Genuino Mega or Mega 2560

Let me know if that works.

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 01:18 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/8 "2018-11-20T01:18:57Z")

</div>

Tried both of these ; same result.

It is compile error, not even gets to download

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 01:19 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/9 "2018-11-20T01:19:44Z")

</div>

Board:  
Arduino Mega ADK  
Arduino/Genuino Mega or Mega 2560

Processor:  
ATmega2560 (Mega 2560)

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 01:31 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/10 "2018-11-20T01:31:05Z")

</div>

I have a blink sketch and it compiles and runs for both board settings?

neither board settings compile for this example code acceldemo

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 02:04 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/11 "2018-11-20T02:04:48Z")

</div>

It is obviously missing a sensor.h file

how do I install this?

 ![error2](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/0/0d95e2615086fd836325c204899f3a4c49e4c736.jpeg)

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [20 November 2018 02:05 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/12 "2018-11-20T02:05:18Z")

</div>

Hi Clem,

You can find the Library here:

> **[adafruit/Adafruit\_LIS3DH](https://github.com/adafruit/Adafruit_LIS3DH)**
>
> Arduino Library for Adafruit LIS3DH breakout board - adafruit/Adafruit\_LIS3DH

You can find instructions on how to manually install a library here:

> **[Installing Arduino Library from GitHub - Bald Engineer](https://www.baldengineer.com/installing-arduino-library-from-github.html)**
>
> What happens when you have a random chip or sensor you want to interface with an Arduino, but don’t know how? You search for a pre-existing library of course. And where are many libraries hosted and available from?  A place where eager developers...

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 03:02 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/13 "2018-11-20T03:02:05Z")

</div>

Next error:

 ![error3](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/3/3543c6d5fabae726dfabdb86d24538bc7e9911c4.jpeg)

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/stephen/32/1599_2.png) [@Stephen](https://forum.core-electronics.com.au/u/Stephen)\
**Post date:** [20 November 2018 04:35 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/14 "2018-11-20T04:35:07Z")

</div>

Clem,

You can’t have a “-master” in your library file and have it still recognized by Arduino IDE.

---

<div class="post-metadata">

**Author:** ![Clem73666](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Clem73666](https://forum.core-electronics.com.au/u/Clem73666)\
**Post date:** [20 November 2018 04:59 UTC](https://forum.core-electronics.com.au/t/lis3dh-interface/3957/15 "2018-11-20T04:59:15Z")

</div>

Hi,

I try to do things in an automated way when it comes to things like installing libraries because I know there are non readable files etc which I do not and cannot easily find out what they contain and what they mean and I expect that there are hidden things which are done to place information in hidden areas which I do not know.

But in desperation I have simply ripped out files and directories which I think might have caused multiple library errors and invalid library errors and I have found somewhere a file called sensors.h and I have simply copied this into the LIS3DH library folder and now it compiles and works as it advertised.

Regards
