Interface LSM9DS1 9DOF IMU With TTGO T-Display ESP32

Getting Started With TTGO T-Display ESP32.

Introduction

TTGO T-Display ESP32 is a tiny microcontroller board. It comes with 1.14″ LCD, 2 programmable push button, battery connector with a charging circuit. Since it uses ESP32 module, so it has WiFi and Bluetooth capability. Plus, the price is affordable. Yes, I love it. In this tutorial, I will share how to get started with this board and interface LSM9DS1 9DOF IMU sensor module.

Video

This video shows how to get started with TTGO T-Display ESP32 and interface LSM9DS1 9DOF IMU sensor module.

Hardware Preparation

This is the list of items used in the video.

Wiring connection table.

TTGO T-Display ESP32LSM9DS1 9DOF IMU
3.3VVDD
GNDGND
21SDA
22SCL

Sample Program

This is Arduino sample program to read LSM9DS1 data and display to LCD. It requires 2 additional library from Arduino Library Manager:

  1. TFT_eSPI by Bodmer Version 1.4.16.
  2. Adafruit LSM9DS1 Library by Adafruit Version 1.0.1

/*
Tutorial: Interface LSM9DS1 with TTGO T-Display ESP32
Board: ESP32 Dev Module (TTGO LCD)
Ext libraries:
– TFT_eSPI by Bodmer Version 1.4.16
– Adafruit LSM9DS1 Library by Adafruit Version 1.0.1
*/
#include <SPI.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI(); // Resolution 135 x 240
#define FF17 &FreeSans9pt7b
#define FF21 &FreeSansBold9pt7b
#define ROW1 0,16
#define ROW2 0,38
#define ROW3 0,60
#define ROW4 0,82
#define ROW5 0,104
#define ROW6 0,126
#include <Wire.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h>
Adafruit_LSM9DS1 lsm9ds1 = Adafruit_LSM9DS1();
#define BUTTON1 35
#define BUTTON2 0
long previousMillis = 0;
int interval = 200;
float accX, accY, accZ;
float gyroX, gyroY, gyroZ;
float magnetX, magnetY, magnetZ;
boolean button1Pressed = false;
boolean button2Pressed = false;
int fontColor = 1;
void setup()
{
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
Serial.begin(115200);
Serial.println(F("Start"));
tft.init();
tft.setRotation(3); // Change 1 or 3 for display orientation
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF21);
tft.setTextColor(TFT_RED);
tft.setCursor(ROW1);
tft.print("LSM9DS1 Reading");
tft.setTextColor(TFT_WHITE);
tft.setCursor(ROW2);
tft.print("—————————————");
tft.setTextColor(TFT_BLUE);
tft.setCursor(ROW3);
tft.print("A:");
tft.setCursor(ROW4);
tft.print("M:");
tft.setCursor(ROW5);
tft.print("G:");
tft.setTextColor(TFT_WHITE);
tft.setCursor(ROW6);
tft.print("—————————————");
tft.setFreeFont(FF17);
if (!lsm9ds1.begin()) {
Serial.println(F("Oops… Unable to initialize the LSM9DS1. Check your wiring!"));
while (1);
}
Serial.println(F("Found LSM9DS1 9DOF"));
lsm9ds1.setupAccel(lsm9ds1.LSM9DS1_ACCELRANGE_2G); // Set the accelerometer range
lsm9ds1.setupMag(lsm9ds1.LSM9DS1_MAGGAIN_4GAUSS); // Set the magnetometer sensitivity
lsm9ds1.setupGyro(lsm9ds1.LSM9DS1_GYROSCALE_245DPS); // Setup the gyroscope
}
void loop()
{
if (millis() – previousMillis > interval) {
lsm9ds1.read();
sensors_event_t a, m, g, temp;
lsm9ds1.getEvent(&a, &m, &g, &temp);
accX = a.acceleration.x;
accY = a.acceleration.y;
accZ = a.acceleration.z;
magnetX = m.magnetic.x;
magnetY = m.magnetic.y;
magnetZ = m.magnetic.z;
gyroX = g.gyro.x;
gyroY = g.gyro.y;
gyroZ = g.gyro.z;
Serial.print(F("Accel X: ")); Serial.print(accX); Serial.print(F(" m/s^2"));
Serial.print(F("\tY: ")); Serial.print(accY); Serial.print(F(" m/s^2"));
Serial.print(F("\tZ: ")); Serial.print(accZ); Serial.print(F(" m/s^2"));
Serial.print("Mag X: "); Serial.print(magnetX); Serial.print(F(" gauss"));
Serial.print("\tY: "); Serial.print(magnetY); Serial.print(F(" gauss"));
Serial.print("\tZ: "); Serial.print(magnetZ); Serial.println(F(" gauss"));
Serial.print("Gyro X: "); Serial.print(gyroX); Serial.print(F(" dps"));
Serial.print("\tY: "); Serial.print(gyroY); Serial.print(F(" dps"));
Serial.print("\tZ: "); Serial.print(gyroZ); Serial.println(F(" dps"));
Serial.println();
tft.fillRect(40, 40, 200, 80, TFT_BLACK);
if (fontColor == 1) {
tft.setTextColor(TFT_GREEN);
}
else if (fontColor == 2) {
tft.setTextColor(TFT_YELLOW);
}
else if (fontColor == 3) {
tft.setTextColor(TFT_MAGENTA);
}
else if (fontColor == 4) {
tft.setTextColor(TFT_PURPLE);
}
else if (fontColor == 5) {
tft.setTextColor(TFT_CYAN);
}
else if (fontColor == 6) {
tft.setTextColor(TFT_ORANGE);
}
else if (fontColor == 7) {
tft.setTextColor(TFT_OLIVE);
}
tft.setCursor(40, 60);
tft.print(accX); tft.print("x ");
tft.print(accY); tft.print("y ");
tft.print(accZ); tft.print("z");
tft.setCursor(40, 82);
tft.print(magnetX); tft.print("x ");
tft.print(magnetY); tft.print("y ");
tft.print(magnetZ); tft.print("z");
tft.setCursor(40, 104);
tft.print(gyroX); tft.print("x ");
tft.print(gyroY); tft.print("y ");
tft.print(gyroZ); tft.print("z");
previousMillis = millis();
}
if (digitalRead(BUTTON1) == LOW &&
button1Pressed == false) {
button1Pressed = true;
fontColor++;
if (fontColor > 7) {
fontColor = 1;
}
}
else if (digitalRead(BUTTON1) == HIGH &&
button1Pressed == true) {
button1Pressed = false;
}
if (digitalRead(BUTTON2) == LOW &&
button2Pressed == false) {
button2Pressed = true;
fontColor–;
if (fontColor < 1) {
fontColor = 7;
}
}
else if (digitalRead(BUTTON2) == HIGH &&
button2Pressed == true) {
button2Pressed = false;
}
}

Thank You

References:

  • TTGO T-Display ESP32 Quick Start Guide – GitHub
  • Adafruit LSM9DS1 Arduino Library – GitHub

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Real-Time IoT Room Monitoring on Maker Pi Pico Using pyRTOS
Build an IoT Plant Monitoring System with MicroBit and Grove WiFi Module
Snap & Fit ABS Case and ICE Tower Cooling Fan Case Stress Test
Raspberry Pi Zero 2 W – Quad-core and 64-bit CPU
IoT Push Notification Using Rain Sensor with MicroBit
Tutorials of Cytron Technologies Scroll to Top