How to make QT PY ESP32-S2 and accelerometer LSM6DSOX work together

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dokurobey
 
Posts: 2
Joined: Thu Nov 03, 2022 8:01 pm

How to make QT PY ESP32-S2 and accelerometer LSM6DSOX work together

Post by dokurobey »

Hi
I'm beginner, and I try to make QT PY ESP32-S2 work with LSM6DSOX.

I get this code sample

Code: Select all

// Basic demo for accelerometer & gyro readings from Adafruit
// LSM6DSOX sensor

#include <Adafruit_LSM6DSOX.h>

// For SPI mode, we need a CS pin
#define LSM_CS 10
// For software-SPI mode we need SCK/MOSI/MISO pins
#define LSM_SCK 13
#define LSM_MISO 12
#define LSM_MOSI 11

Adafruit_LSM6DSOX sox;
void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit LSM6DSOX test!");

  if (!sox.begin_I2C()) {
    // if (!sox.begin_SPI(LSM_CS)) {
    // if (!sox.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
    // Serial.println("Failed to find LSM6DSOX chip");
    while (1) {
      delay(10);
    }
  }

  Serial.println("LSM6DSOX Found!");

  // sox.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
  Serial.print("Accelerometer range set to: ");
  switch (sox.getAccelRange()) {
  case LSM6DS_ACCEL_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case LSM6DS_ACCEL_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case LSM6DS_ACCEL_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case LSM6DS_ACCEL_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }

  // sox.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS );
  Serial.print("Gyro range set to: ");
  switch (sox.getGyroRange()) {
  case LSM6DS_GYRO_RANGE_125_DPS:
    Serial.println("125 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_250_DPS:
    Serial.println("250 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_500_DPS:
    Serial.println("500 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_1000_DPS:
    Serial.println("1000 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_2000_DPS:
    Serial.println("2000 degrees/s");
    break;
  case ISM330DHCX_GYRO_RANGE_4000_DPS:
    break; // unsupported range for the DSOX
  }

  // sox.setAccelDataRate(LSM6DS_RATE_12_5_HZ);
  Serial.print("Accelerometer data rate set to: ");
  switch (sox.getAccelDataRate()) {
  case LSM6DS_RATE_SHUTDOWN:
    Serial.println("0 Hz");
    break;
  case LSM6DS_RATE_12_5_HZ:
    Serial.println("12.5 Hz");
    break;
  case LSM6DS_RATE_26_HZ:
    Serial.println("26 Hz");
    break;
  case LSM6DS_RATE_52_HZ:
    Serial.println("52 Hz");
    break;
  case LSM6DS_RATE_104_HZ:
    Serial.println("104 Hz");
    break;
  case LSM6DS_RATE_208_HZ:
    Serial.println("208 Hz");
    break;
  case LSM6DS_RATE_416_HZ:
    Serial.println("416 Hz");
    break;
  case LSM6DS_RATE_833_HZ:
    Serial.println("833 Hz");
    break;
  case LSM6DS_RATE_1_66K_HZ:
    Serial.println("1.66 KHz");
    break;
  case LSM6DS_RATE_3_33K_HZ:
    Serial.println("3.33 KHz");
    break;
  case LSM6DS_RATE_6_66K_HZ:
    Serial.println("6.66 KHz");
    break;
  }

  // sox.setGyroDataRate(LSM6DS_RATE_12_5_HZ);
  Serial.print("Gyro data rate set to: ");
  switch (sox.getGyroDataRate()) {
  case LSM6DS_RATE_SHUTDOWN:
    Serial.println("0 Hz");
    break;
  case LSM6DS_RATE_12_5_HZ:
    Serial.println("12.5 Hz");
    break;
  case LSM6DS_RATE_26_HZ:
    Serial.println("26 Hz");
    break;
  case LSM6DS_RATE_52_HZ:
    Serial.println("52 Hz");
    break;
  case LSM6DS_RATE_104_HZ:
    Serial.println("104 Hz");
    break;
  case LSM6DS_RATE_208_HZ:
    Serial.println("208 Hz");
    break;
  case LSM6DS_RATE_416_HZ:
    Serial.println("416 Hz");
    break;
  case LSM6DS_RATE_833_HZ:
    Serial.println("833 Hz");
    break;
  case LSM6DS_RATE_1_66K_HZ:
    Serial.println("1.66 KHz");
    break;
  case LSM6DS_RATE_3_33K_HZ:
    Serial.println("3.33 KHz");
    break;
  case LSM6DS_RATE_6_66K_HZ:
    Serial.println("6.66 KHz");
    break;
  }
}

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);

  Serial.print("\t\tTemperature ");
  Serial.print(temp.temperature);
  Serial.println(" deg C");

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

  /* Display the results (rotation is measured in rad/s) */
  Serial.print("\t\tGyro X: ");
  Serial.print(gyro.gyro.x);
  Serial.print(" \tY: ");
  Serial.print(gyro.gyro.y);
  Serial.print(" \tZ: ");
  Serial.print(gyro.gyro.z);
  Serial.println(" radians/s ");
  Serial.println();

  delay(100);

  //  // serial plotter friendly format

  //  Serial.print(temp.temperature);
  //  Serial.print(",");

  //  Serial.print(accel.acceleration.x);
  //  Serial.print(","); Serial.print(accel.acceleration.y);
  //  Serial.print(","); Serial.print(accel.acceleration.z);
  //  Serial.print(",");

  // Serial.print(gyro.gyro.x);
  // Serial.print(","); Serial.print(gyro.gyro.y);
  // Serial.print(","); Serial.print(gyro.gyro.z);
  // Serial.println();
  //  delayMicroseconds(10000);
}
that you have provided, and obviously it doesnt work (as it's for other device).
Thing is, I dont know how to adjust it for ESP with STEEMA (port scanner works btw, and I get results of 2 ports, so it's not hardware issue).

I have added:

Code: Select all

Wire1.setPins(SDA1, SCL1)
but I think I'm missing port setup section.

I have tried to connect other sensors via STEMA, but that didnt work as well.
1. Can you please direct me on how to adjust sample codes for ESP
2. Bit guidance on how Adafruit Unified Sensor Driver match with ESP32 would be helpful too (how to adjust it, use it).

Thank you in advance for guidance.

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to make QT PY ESP32-S2 and accelerometer LSM6DSOX work together

Post by dastels »

What do you mean by "it's for other device".

Anyway, to use the STEMMA connector you will need to use Wire1 in place of Wire. It will already use the correct pins.

You can't just use

Code: Select all

sox.begin_I2C()
since that defaults to using Wire (the I2C connections on the header pins, not the STEMMA)

As shown in the docs (https://adafruit.github.io/Adafruit_LSM ... bf40f8b94a) you'll need to specify an address and the TwoWire object to use (i.e. Wire1):

Code: Select all

sox.begin_I2C(LSM6DS_I2CADDR_DEFAULT, &Wire1)
Dave

User avatar
trippedio
 
Posts: 3
Joined: Wed Aug 14, 2019 6:04 pm

Re: How to make QT PY ESP32-S2 and accelerometer LSM6DSOX work together

Post by trippedio »

Thanks, Dave...

This is the answer to why my setup with a different sensor, the Adafruit HTU31 Temperature & Humidity Sensor Breakout Board - STEMMA QT / Qwiic
(Product ID: 4832) was not able to communicate with the Adafruit QT Py ESP32-S2 WiFi Dev Board with STEMMA QT (Product ID: 5325) using the Adafruit built-in HTU31D example found in the Arduino IDE (I'm using version 2.0.1), using the STEMMA QT connector cables!

The example will work if using 'discrete' wires attached/soldered to the pads on both boards: SDA, SCL, +3.3V, GND. To use the QT connectors, I had to replace the

Code: Select all

htu.begin(0x40);

function call with one that specifies the TwoWire object:

Code: Select all

htu.begin(0x40, &Wire1);
I did not know there was a possible second parameter. (Not so obvious!)

One of the nice features in the recent Arduino 2.x IDE is the ability to mouse hover over a keyword or function
( 'begin' in this case), and you can right-click->Go to Definition to see how a method is defined:

Code: Select all

class Adafruit_HTU31D {
public:
...
bool begin(uint8_t i2c_addr = HTU31D_DEFAULT_I2CADDR,
             TwoWire *theWire = &Wire);

User avatar
dokurobey
 
Posts: 2
Joined: Thu Nov 03, 2022 8:01 pm

Re: How to make QT PY ESP32-S2 and accelerometer LSM6DSOX work together

Post by dokurobey »

Thank you Dave.
It worked: much appreciated.
From the same doc I see that in case I will use more sensor I will have to add another parameter. Will test later thought.

Code: Select all

sox.begin_I2C(LSM6DS_I2CADDR_DEFAULT, &Wire1,0)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”