Feather (esp32) and accelerometers

In this post we are going to explore how to connect an adaFruit Huzzah32 Feather (esp32) to a grove accelerometer.

I have covered this sensor in a previous tangible post so if you want more detail than offered here, jump over to that video. In the tangible post we used an Arduino Uno which is a 5V device and the V1.0 version of the Arduino IDE.

In this video we are connecting a grove (Grove 3-axis digital accelerometer (LIS3DHTR)) accelerometer to a 3.3V feather. The grove sensor tolerates 3.3V and a 5V power supplies so it is a great crossover sensor for us, allowing us to work with Uno’s or huzzah32s.

Communication between the microcontroller and the sensor uses the I2C interface. I2c is pronounced i-squared-c, i have a bad habit if calling i-two-c (it will come up in the video). But it is properly known as i-squared-c.

I2C is a synchronous serial protocol. This means that the devices speaking to each other are synchronized by a shared clock signal. This is different than the serial communication that happens over USB to our microcontroller — a form called asynchronous (no clock, out of sync).

To get this sensor working, you need to build a simple circuit (covered in video) and then we can use the same code as we used with the UNO in the video linked above.

Part List:

  • huzzah32
  • breadboard
  • jumper wires (red / black various lengths)
  • grove LIS3DHTR 3axis sensor
  • jst to pin connector cable for sensor
  • 2 x 10k resistors (brown, black, orange)

Circuit

The accelerometer we are using has an I2C interface. The Huzzah pins we are using have special functions — you cannot move this circuit to other pins the way you might other kinds of circuits.

The pull up resistors are 10k ( brown. black, orange).

Video

Code

The code for this example is also available from github. [download]

The baud rate in this example is 115200 – you need to change baud rates in your serial monitor and/or plotter.

// Simplified example -- see video for details.
// This example shows the 3 axis acceleration.

//you need all four  of these declarations
#include "LIS3DHTR.h"
#include <Wire.h>
LIS3DHTR<TwoWire> LIS; //IIC
#define WIRE Wire

// these are not needed here 
// #define I2C_SDA 23
// #define I2C_SCL 22

void setup() {

  // woah! look at the BAUD rate
    Serial.begin(115200); // change plotter or serial monitor
    while (!Serial) {}; // for testing not production - this BLOCKS forever is no serial
    
    // variables you need to init accellerometer
    LIS.begin(WIRE, 0x19); //IIC init 
    delay(100); // give it a moment to start
    
    LIS.setFullScaleRange(LIS3DHTR_RANGE_2G); //sensitivity
    LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ); // data rate
    LIS.setHighSolution(true); //High solution enable


    Serial.println("setup complete");

}

void loop() {
    if (!LIS) {
        Serial.println("LIS3DHTR didn't connect.");
        while (1);
        return;
    }
    //3 axis
    Serial.print("x:"); Serial.print(LIS.getAccelerationX()); Serial.print("  ");
    Serial.print("y:"); Serial.print(LIS.getAccelerationY()); Serial.print("  ");
    Serial.print("z:"); Serial.println(LIS.getAccelerationZ());

    delay(50);
}