I am using the Adafruit LIS3DH accelerometer. I got it working with the Arduino UNO with both I2C and SPI.
I decided to switch to the Teensy since I will eventually need to log to an SD card at higher speeds.
I managed to get it working with I2C on the Teensy 4.1
However, am unable to get it working in the SPI mode.
Note that I initially used pins 10 -13 for SPI. But based on similar forum discussions where a bad connection was suggested as the possible problem, I decided to try CS1-SCK1. I have since swapped back to the Arduino and tried 2 other LIS3DH adafruit boards. They continue to work with the Arduino; but not with the Teensy.
Note that I purchased a pre-soldered Teensy and the LIS3DH soldering must be OK since it worked with the Arduino.
The code and the console output are shown below. I have tried with default settings without using the Sections of the code in bold and it still doesn't work.
// 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 27
#define LIS3DH_MISO 39
#define LIS3DH_MOSI 26
// Used for hardware & software SPI
#define LIS3DH_CS 38
// 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();
void setup(void) {
Serial.begin(115200);
while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
SPI.begin();
pinMode(LIS3DH_CLK, OUTPUT);
pinMode(LIS3DH_MISO, INPUT);
pinMode(LIS3DH_MOSI, OUTPUT);
pinMode(LIS3DH_CS, OUTPUT);
Serial.println("one");
//SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
Serial.println("two");
//digitalWrite(LIS3DH_CS, HIGH);
digitalWrite(LIS3DH_CS, LOW);
lis.setDataRate(LIS3DH_DATARATE_LOWPOWER_5KHZ);
Serial.println("four");
//SPI.transfer(0x20);
//Serial.println("five");
//SPI.transfer(0x8F);
Serial.println("three");
Serial.println("LIS3DH test!");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
//while (1) yield();
}
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");
// lis.setDataRate(LIS3DH_DATARATE_50_HZ);
Serial.print("Data rate set to: ");
-----------------------------------------------------------
OUTPUT on Serial Port is below
-----------------------------------------------------------
one
two
four
three
LIS3DH test!
Couldnt start
LIS3DH found!
Range = 2G
Data rate set to: Powered Down
X: 0 Y: 0 Z: 0 X: 0.00 Y: 0.00 Z: 0.00 m/s^2
I decided to switch to the Teensy since I will eventually need to log to an SD card at higher speeds.
I managed to get it working with I2C on the Teensy 4.1
However, am unable to get it working in the SPI mode.
Note that I initially used pins 10 -13 for SPI. But based on similar forum discussions where a bad connection was suggested as the possible problem, I decided to try CS1-SCK1. I have since swapped back to the Arduino and tried 2 other LIS3DH adafruit boards. They continue to work with the Arduino; but not with the Teensy.
Note that I purchased a pre-soldered Teensy and the LIS3DH soldering must be OK since it worked with the Arduino.
The code and the console output are shown below. I have tried with default settings without using the Sections of the code in bold and it still doesn't work.
// 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 27
#define LIS3DH_MISO 39
#define LIS3DH_MOSI 26
// Used for hardware & software SPI
#define LIS3DH_CS 38
// 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();
void setup(void) {
Serial.begin(115200);
while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
SPI.begin();
pinMode(LIS3DH_CLK, OUTPUT);
pinMode(LIS3DH_MISO, INPUT);
pinMode(LIS3DH_MOSI, OUTPUT);
pinMode(LIS3DH_CS, OUTPUT);
Serial.println("one");
//SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
Serial.println("two");
//digitalWrite(LIS3DH_CS, HIGH);
digitalWrite(LIS3DH_CS, LOW);
lis.setDataRate(LIS3DH_DATARATE_LOWPOWER_5KHZ);
Serial.println("four");
//SPI.transfer(0x20);
//Serial.println("five");
//SPI.transfer(0x8F);
Serial.println("three");
Serial.println("LIS3DH test!");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
//while (1) yield();
}
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");
// lis.setDataRate(LIS3DH_DATARATE_50_HZ);
Serial.print("Data rate set to: ");
-----------------------------------------------------------
OUTPUT on Serial Port is below
-----------------------------------------------------------
one
two
four
three
LIS3DH test!
Couldnt start
LIS3DH found!
Range = 2G
Data rate set to: Powered Down
X: 0 Y: 0 Z: 0 X: 0.00 Y: 0.00 Z: 0.00 m/s^2