/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:
Tutorial: Make an Earthquake and Vibration Detector with Arduino!
Adafruit_GFX library:
Adafruit_ILI9341 library:
Adafruit_MPU6050 library:
Wire library:
The purpose of this sketch is to display the acceleration of x, y and z axis
on a TFT display and use a buzzer to sound an alarm if a vibration was detected.
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connections from the MPU-6050:
• MPU-6050 VCC pin → Breadboard 5V line
• MPU-6050 GND pin → Breadboard ground line
• MPU-6050 SDA pin → Arduino pin A4
• MPU-6050 SCL pin → Arduino pin A5
Connections from the passive buzzer:
• passive buzzer GND pin → Breadboard ground line
• passive buzzer VCC pin (+ pin) → Arduino pin 3
Connections from the TFT Display to the breadboard:
• TFT Display VCC pin → Breadboard 5V line
• TFT Display GND pin → Breadboard ground line
• TFT Display LED pin → Breadboard 5V line
Connections from the TFT Display to arduino :
• TFT Display CS pin → Arduino pin 10
• TFT Display RST pin → Arduino pin 8
• TFT Display DC pin → Arduino pin 9
• TFT Display MOSI pin → Arduino pin 11
• TFT Display SCK pin → Arduino pin 13
• TFT Display MISO pin → Arduino pin 12
*/
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Wire.h"
#include "SPI.h"
#include "Adafruit_MPU6050.h"
#include "Adafruit_Sensor.h"
//Define TFT Display pin numbers
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
Adafruit_MPU6050 mpu;
#define passive_buzzer 3 //define passive buzzer pin
char XAXISChar[10];
char YAXISChar[10];
char ZAXISChar[10];
void setup(){
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
//setupt motion detection
mpu.setHighPassFilter(MPU6050_HIGHPASS_0_63_HZ);
mpu.setMotionDetectionThreshold(1);
mpu.setMotionDetectionDuration(20);
mpu.setInterruptPinLatch(true); // Keep it latched. Will turn off when reinitialized.
mpu.setInterruptPinPolarity(true);
mpu.setMotionInterrupt(true);
Serial.println("");
delay(100);
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
pinMode(passive_buzzer,OUTPUT); //define passive buzzer pin as an output pin
Wire.begin();
printText("XAXIS acceleration", ILI9341_GREEN,20,20,2);
printText("YAXIS acceleration", ILI9341_GREEN,20,130,2);
printText("ZAXIS acceleration", ILI9341_GREEN,20,230,2);
}
void loop()
{
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float XAXIS =a.acceleration.x;
float YAXIS =a.acceleration.y;
float ZAXIS= a.acceleration.z;
int grafX = 0; //int for x value of graph
String XAXISString = String(XAXIS,1); //Convert float to string
XAXISString.toCharArray(XAXISChar,10);
tft.fillRect(30,70,120,40,ILI9341_BLACK); //Draw a Rectangle
printText(XAXISChar, ILI9341_WHITE,35,70,4); //printing the XAXIS acceleration on the TFT Display
grafX = map(XAXIS,0,15,0,127); //map value to screen width
tft.fillRect(0, 112, grafX, 10, ILI9341_WHITE); //print graph 400min 1000max
String YAXISString = String(YAXIS,1); //Convert float to string
YAXISString.toCharArray(YAXISChar,10);
tft.fillRect(30,170,120,40,ILI9341_BLACK); //Draw a Rectangle
printText(YAXISChar, ILI9341_WHITE,35,170,4); //printing the YAXIS acceleration on the TFT Display
grafX = map(YAXIS,0,15,0,127); //map value to screen width
tft.fillRect(0, 212, grafX, 10, ILI9341_WHITE); //print graph 400min 1000max
String ZAXISString = String(ZAXIS,1); //Convert float to string
ZAXISString.toCharArray(ZAXISChar,10);
tft.fillRect(30,270,120,40,ILI9341_BLACK); //Draw a Rectangle
printText(ZAXISChar, ILI9341_WHITE,35,270,4); //printing the ZAXIS acceleration on the TFT Display
grafX = map(ZAXIS,0,15,0,127); //map value to screen width
tft.fillRect(0, 312, grafX, 10, ILI9341_WHITE); //print graph 400min 1000max
if(mpu.getMotionInterruptStatus()) {
digitalWrite(passive_buzzer,HIGH); //turn on buzzer
}
else{ //if not
digitalWrite(passive_buzzer,LOW); //turn off buzzer
}
}
//The function of writing words on the TFT Display
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(textSize);
tft.setTextWrap(true);
tft.print(text);
}