LIS3DH interface

// 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;
}

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.

Hi Clem,

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

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

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

exit status 1
Error compliling for board Arduino Mega ADK.

// 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

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!

IDUINO MEGA it says on the board?

board model mega 2560 A3
made by IDUINO

Hi Clem,

Under Tools>Board:

select Arduino/Genuino Mega or Mega 2560

Let me know if that works.

Tried both of these ; same result.

It is compile error, not even gets to download

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

Processor:
ATmega2560 (Mega 2560)

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

neither board settings compile for this example code acceldemo

It is obviously missing a sensor.h file

how do I install this?

Hi Clem,

You can find the Library here:

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

Next error:

Clem,

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

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

1 Like

This post was flagged by the community and is temporarily hidden.