Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Created December 2, 2019 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IdrisCytron/096a8534738b88fb9fd023f4f71d0329 to your computer and use it in GitHub Desktop.
Save IdrisCytron/096a8534738b88fb9fd023f4f71d0329 to your computer and use it in GitHub Desktop.
Interface LSM9DS1 with TTGO T-Display ESP32
/*
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment