accelerometer LIS3DH and arduino nano
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- aaronmx
- Posts: 9
- Joined: Wed Feb 17, 2016 8:21 pm
- adafruit_support_bill
- Posts: 88088
- Joined: Sat Feb 07, 2009 10:11 am
Re: accelerometer LIS3DH and arduino nano
Please post the actual code you are using.
- aaronmx
- Posts: 9
- Joined: Wed Feb 17, 2016 8:21 pm
Re: accelerometer LIS3DH and arduino nano
The acceldemo sketch:
Code: Select all
// 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);
}
Last edited by adafruit_support_bill on Sun Sep 11, 2016 6:37 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.
- adafruit_support_bill
- Posts: 88088
- Joined: Sat Feb 07, 2009 10:11 am
Re: accelerometer LIS3DH and arduino nano
The SPI code is commented out here. You are initializing it for i2c communication.// 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();
- aaronmx
- Posts: 9
- Joined: Wed Feb 17, 2016 8:21 pm
Re: accelerometer LIS3DH and arduino nano
Ok, I'm aware that this is basic but as I don't yet comprehend it I'm struggling. I've tried every combination I can think of to no avail. Deleted the I2C, commenting, uncommenting, etc. Can you help me identify what I'm missing? Thank you in advance!
// 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
// hardware SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS); //uncommented this line
// software SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK); //uncommented this line
//deleted the I2C line
#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
// 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
// hardware SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS); //uncommented this line
// software SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK); //uncommented this line
//deleted the I2C line
#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
- adafruit_support_bill
- Posts: 88088
- Joined: Sat Feb 07, 2009 10:11 am
Re: accelerometer LIS3DH and arduino nano
Code: Select all
// hardware SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS); //uncommented this line
// software SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK); //uncommented this line
- aaronmx
- Posts: 9
- Joined: Wed Feb 17, 2016 8:21 pm
Re: accelerometer LIS3DH and arduino nano
Thank you Bill(?), that got it. Well, that and changing wiring to the written order. I VERY MUCH appreciate the help, I'd have never figured it out. Quality comes at a price and that's why I buy from you guys. -Aaron
- adafruit_support_bill
- Posts: 88088
- Joined: Sat Feb 07, 2009 10:11 am
Re: accelerometer LIS3DH and arduino nano
Good to hear you have it working. Thanks for the follow-up.
Please be positive and constructive with your questions and comments.