MPU6050 accelerometer and gyro sensor with Nextion


A small project demonstrating how to use the MPU6050 accelerometer and send values to Nextion's waveform and progress bar.

/*
 * Waveform.ino - Simple example code for EasyNextionLibrary
 * Copyright (c) 2020 Athanasios Seitanis < seithagta@gmail.com >. 
 * All rights reserved. EasyNextionLibrary is licensed under the MIT License
 * https://opensource.org/licenses/MIT
 */

/* This project aims to show how to manage waveforms,
 * how to send different data on Nextion and how to process them
 */


#include "EasyNextionLibrary.h"  // Include EasyNextionLibrary 

EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
                       // Set as parameter the Serial you are going to use
                       
#define DATA_REFRESH_RATE 500 // The time between each Data refresh of the page
                               // Depending on the needs of the project, the DATA_REFRESH_RATE can be set
                               // to 50ms or 100ms without a problem. In this example, we use 1000ms, 
                               

unsigned long pageRefreshTimer = millis(); // Timer for DATA_REFRESH_RATE

//--------------------------------------------------------------------------------
//_________ MPU6050 Accelerometer and Magnetometer sensor Configuration
//--------------------------------------------------------------------------------
#include <MPU6050_tockn.h>      // MPU6050_tockn Library
                               //  Get the library  HERE: https://github.com/tockn/MPU6050_tockn
                              //   or from Library manager on Arduino IDE <MPU6050_tockn>
 #include <Wire.h>                              
                          // Connect SCL to D21 pin    SDA to D20 for Mega
                         //  Connect SCL to A5 pin     SDA to A4 for UNO - Nano 

MPU6050 mpu6050(Wire);       // create an object of MPU6050_tockn library with the name <mpu6050> so that we can access functions related to it

float accelerationX = 0;
float accelerationY = 0;
float accelerationZ = 0;

float lastSentAccelerationX = 0;
float lastSentAccelerationY = 0;
float lastSentAccelerationZ = 0;

float angleX = 0;
float angleY = 0;
float angleZ = 0;

float lastSentAngleX = 0;
float lastSentAngleY = 0;
float lastSentAngleZ = 0;           

#define PIN_CALIBRATION_MPU6050 10  // when D10 is LOW with a switch on GND. MPU6050 NOT calibrating on start up
                                    // the INPUT_PULLUP is going to activated later
#define MPU6050_GET_DATA_EVERY 400   // request data from sensor every 400ms 
unsigned long mpu6050Timer = millis();


void setup(){

  //__________ Nextion setup    

   myNex.begin(9600); // Begin the object with a baud rate of 9600
                     // If no parameter was given in the begin(), the default baud rate of 9600 will be used 
  
  //--------------------------------------------------------------------------------
//_________ MPU6050 Accelerometer and Magnetometer sensor setup
//--------------------------------------------------------------------------------
  pinMode(PIN_CALIBRATION_MPU6050, INPUT_PULLUP);  
  Wire.begin();
  mpu6050.begin();
  
  if(digitalRead(PIN_CALIBRATION_MPU6050) == HIGH) {
    mpu6050.calcGyroOffsets(true);
  }
  
} 

void loop(){

 
 mpu6050GetData();
 refreshNextion();
}

void mpu6050GetData() {

  if((millis()-mpu6050Timer) > MPU6050_GET_DATA_EVERY){
      mpu6050.update();
      
      if((millis()-mpu6050Timer) > (MPU6050_GET_DATA_EVERY + 10)){
         accelerationX = mpu6050.getAccX();
         accelerationY = mpu6050.getAccY();
         accelerationZ = mpu6050.getAccZ();
      
         angleX = mpu6050.getAngleX();
         angleY = mpu6050.getAngleY();
         angleZ = mpu6050.getAngleZ();
         
         mpu6050Timer = millis();
      }
  }
}

void refreshNextion() {
    
    if((millis()-pageRefreshTimer) > DATA_REFRESH_RATE){
    
      if(lastSentAccelerationX != accelerationX){
        int accelerationXInt = accelerationX*10;  // multiply x10 and convert it to int
        myNex.writeNum("accelX.val", accelerationXInt); // accelX is a variable that we have create on Nextion

        lastSentAccelerationX = accelerationX;
      }

      if(lastSentAccelerationY != accelerationY){
        int accelerationYInt = accelerationY*10;
        myNex.writeNum("accelY.val", accelerationYInt); // accelY is a variable that we have create on Nextion

        lastSentAccelerationY = accelerationY;
      }      
      
      if(lastSentAccelerationZ != accelerationZ){
        int accelerationZInt = accelerationZ*10;  
        myNex.writeNum("accelZ.val", accelerationZInt); // accelZ is a variable that we have create on Nextion

        lastSentAccelerationZ = accelerationZ;
      }

      if(lastSentAngleX != angleX){
        int angleXInt = angleX*10;  
        myNex.writeNum("angleX.val", angleXInt); // angleX is a variable that we have create on Nextion        

        lastSentAngleX = angleX;
      }

      if(lastSentAngleY != angleY){
        int angleYInt = angleY*10; 
        myNex.writeNum("angleY.val", angleYInt); // angleY is a variable that we have create on Nextion 

        lastSentAngleY = angleY;
      } 

      if(lastSentAngleZ != angleZ){
        int angleZInt = angleZ*10;  
        myNex.writeNum("angleZ.val", angleZInt); // angleZ is a variable that we have create on Nextion 

        lastSentAngleZ = angleZ;
      }      
      pageRefreshTimer = millis();
    }

}

The .zip file contains:



File downloaded 522 times.