From 6810d1fe356efc92ea7cdfc3182462ce5ca276ac Mon Sep 17 00:00:00 2001 From: Jeremy Belpois Date: Thu, 14 Dec 2017 20:55:18 +0100 Subject: [PATCH 1/7] Moved examples --- .../MPU6050_accel_pitch_roll.ino | 47 ------ MPU6050_accel_simple/MPU6050_accel_simple.ino | 94 ----------- MPU6050_free_fall/MPU6050_free_fall.ino | 143 ---------------- .../MPU6050_gyro_pitch_roll_yaw.ino | 65 -------- MPU6050_gyro_simple/MPU6050_gyro_simple.ino | 103 ------------ MPU6050_motion/MPU6050_motion.ino | 156 ------------------ MPU6050_temperature/MPU6050_temperature.ino | 38 ----- 7 files changed, 646 deletions(-) delete mode 100644 MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino delete mode 100644 MPU6050_accel_simple/MPU6050_accel_simple.ino delete mode 100644 MPU6050_free_fall/MPU6050_free_fall.ino delete mode 100644 MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino delete mode 100644 MPU6050_gyro_simple/MPU6050_gyro_simple.ino delete mode 100644 MPU6050_motion/MPU6050_motion.ino delete mode 100644 MPU6050_temperature/MPU6050_temperature.ino diff --git a/MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino b/MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino deleted file mode 100644 index c1b0e2f..0000000 --- a/MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino +++ /dev/null @@ -1,47 +0,0 @@ -/* - MPU6050 Triple Axis Gyroscope & Accelerometer. Pitch & Roll Accelerometer Example. - Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - GIT: https://github.com/jarzebski/Arduino-MPU6050 - Web: http://www.jarzebski.pl - (c) 2014 by Korneliusz Jarzebski -*/ - -#include -#include - -MPU6050 mpu; - -void setup() -{ - Serial.begin(115200); - - Serial.println("Initialize MPU6050"); - - while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) - { - Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); - delay(500); - } -} - -void loop() -{ - // Read normalized values - Vector normAccel = mpu.readNormalizeAccel(); - - // Calculate Pitch & Roll - int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis*normAccel.YAxis + normAccel.ZAxis*normAccel.ZAxis))*180.0)/M_PI; - int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI; - - // Output - Serial.print(" Pitch = "); - Serial.print(pitch); - Serial.print(" Roll = "); - Serial.print(roll); - - Serial.println(); - - delay(10); -} - - diff --git a/MPU6050_accel_simple/MPU6050_accel_simple.ino b/MPU6050_accel_simple/MPU6050_accel_simple.ino deleted file mode 100644 index d327167..0000000 --- a/MPU6050_accel_simple/MPU6050_accel_simple.ino +++ /dev/null @@ -1,94 +0,0 @@ -/* - MPU6050 Triple Axis Gyroscope & Accelerometer. Simple Accelerometer Example. - Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - GIT: https://github.com/jarzebski/Arduino-MPU6050 - Web: http://www.jarzebski.pl - (c) 2014 by Korneliusz Jarzebski -*/ - -#include -#include - -MPU6050 mpu; - -void setup() -{ - Serial.begin(115200); - - Serial.println("Initialize MPU6050"); - - while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) - { - Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); - delay(500); - } - - // If you want, you can set accelerometer offsets - // mpu.setAccelOffsetX(); - // mpu.setAccelOffsetY(); - // mpu.setAccelOffsetZ(); - - checkSettings(); -} - -void checkSettings() -{ - Serial.println(); - - Serial.print(" * Sleep Mode: "); - Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Clock Source: "); - switch(mpu.getClockSource()) - { - case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; - case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; - case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; - case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; - case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; - } - - Serial.print(" * Accelerometer: "); - switch(mpu.getRange()) - { - case MPU6050_RANGE_16G: Serial.println("+/- 16 g"); break; - case MPU6050_RANGE_8G: Serial.println("+/- 8 g"); break; - case MPU6050_RANGE_4G: Serial.println("+/- 4 g"); break; - case MPU6050_RANGE_2G: Serial.println("+/- 2 g"); break; - } - - Serial.print(" * Accelerometer offsets: "); - Serial.print(mpu.getAccelOffsetX()); - Serial.print(" / "); - Serial.print(mpu.getAccelOffsetY()); - Serial.print(" / "); - Serial.println(mpu.getAccelOffsetZ()); - - Serial.println(); -} - -void loop() -{ - Vector rawAccel = mpu.readRawAccel(); - Vector normAccel = mpu.readNormalizeAccel(); - - Serial.print(" Xraw = "); - Serial.print(rawAccel.XAxis); - Serial.print(" Yraw = "); - Serial.print(rawAccel.YAxis); - Serial.print(" Zraw = "); - - Serial.println(rawAccel.ZAxis); - Serial.print(" Xnorm = "); - Serial.print(normAccel.XAxis); - Serial.print(" Ynorm = "); - Serial.print(normAccel.YAxis); - Serial.print(" Znorm = "); - Serial.println(normAccel.ZAxis); - - delay(10); -} - - diff --git a/MPU6050_free_fall/MPU6050_free_fall.ino b/MPU6050_free_fall/MPU6050_free_fall.ino deleted file mode 100644 index d2d9269..0000000 --- a/MPU6050_free_fall/MPU6050_free_fall.ino +++ /dev/null @@ -1,143 +0,0 @@ -/* - MPU6050 Triple Axis Gyroscope & Accelerometer. Free fall detection. - Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - GIT: https://github.com/jarzebski/Arduino-MPU6050 - Web: http://www.jarzebski.pl - (c) 2014 by Korneliusz Jarzebski -*/ - -#include -#include - -MPU6050 mpu; - -boolean ledState = false; -boolean freefallDetected = false; -int freefallBlinkCount = 0; - -void setup() -{ - Serial.begin(115200); - - Serial.println("Initialize MPU6050"); - - while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_16G)) - { - Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); - delay(500); - } - - mpu.setAccelPowerOnDelay(MPU6050_DELAY_3MS); - - mpu.setIntFreeFallEnabled(true); - mpu.setIntZeroMotionEnabled(false); - mpu.setIntMotionEnabled(false); - - mpu.setDHPFMode(MPU6050_DHPF_5HZ); - - mpu.setFreeFallDetectionThreshold(17); - mpu.setFreeFallDetectionDuration(2); - - checkSettings(); - - pinMode(4, OUTPUT); - digitalWrite(4, LOW); - - attachInterrupt(0, doInt, RISING); -} - -void doInt() -{ - freefallBlinkCount = 0; - freefallDetected = true; -} - -void checkSettings() -{ - Serial.println(); - - Serial.print(" * Sleep Mode: "); - Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Motion Interrupt: "); - Serial.println(mpu.getIntMotionEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Zero Motion Interrupt: "); - Serial.println(mpu.getIntZeroMotionEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Free Fall Interrupt: "); - Serial.println(mpu.getIntFreeFallEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Free Fal Threshold: "); - Serial.println(mpu.getFreeFallDetectionThreshold()); - - Serial.print(" * Free FallDuration: "); - Serial.println(mpu.getFreeFallDetectionDuration()); - - Serial.print(" * Clock Source: "); - switch(mpu.getClockSource()) - { - case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; - case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; - case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; - case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; - case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; - } - - Serial.print(" * Accelerometer: "); - switch(mpu.getRange()) - { - case MPU6050_RANGE_16G: Serial.println("+/- 16 g"); break; - case MPU6050_RANGE_8G: Serial.println("+/- 8 g"); break; - case MPU6050_RANGE_4G: Serial.println("+/- 4 g"); break; - case MPU6050_RANGE_2G: Serial.println("+/- 2 g"); break; - } - - Serial.print(" * Accelerometer offsets: "); - Serial.print(mpu.getAccelOffsetX()); - Serial.print(" / "); - Serial.print(mpu.getAccelOffsetY()); - Serial.print(" / "); - Serial.println(mpu.getAccelOffsetZ()); - - Serial.print(" * Accelerometer power delay: "); - switch(mpu.getAccelPowerOnDelay()) - { - case MPU6050_DELAY_3MS: Serial.println("3ms"); break; - case MPU6050_DELAY_2MS: Serial.println("2ms"); break; - case MPU6050_DELAY_1MS: Serial.println("1ms"); break; - case MPU6050_NO_DELAY: Serial.println("0ms"); break; - } - - Serial.println(); -} - -void loop() -{ - Vector rawAccel = mpu.readRawAccel(); - Activites act = mpu.readActivites(); - - Serial.print(act.isFreeFall); - Serial.print("\n"); - - if (freefallDetected) - { - ledState = !ledState; - - digitalWrite(4, ledState); - - freefallBlinkCount++; - - if (freefallBlinkCount == 20) - { - freefallDetected = false; - ledState = false; - digitalWrite(4, ledState); - } - } - - delay(100); - -} diff --git a/MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino b/MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino deleted file mode 100644 index c854158..0000000 --- a/MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino +++ /dev/null @@ -1,65 +0,0 @@ -/* - MPU6050 Triple Axis Gyroscope & Accelerometer. Pitch & Roll & Yaw Gyroscope Example. - Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - GIT: https://github.com/jarzebski/Arduino-MPU6050 - Web: http://www.jarzebski.pl - (c) 2014 by Korneliusz Jarzebski -*/ - -#include -#include - -MPU6050 mpu; - -// Timers -unsigned long timer = 0; -float timeStep = 0.01; - -// Pitch, Roll and Yaw values -float pitch = 0; -float roll = 0; -float yaw = 0; - -void setup() -{ - Serial.begin(115200); - - // Initialize MPU6050 - while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) - { - Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); - delay(500); - } - - // Calibrate gyroscope. The calibration must be at rest. - // If you don't want calibrate, comment this line. - mpu.calibrateGyro(); - - // Set threshold sensivty. Default 3. - // If you don't want use threshold, comment this line or set 0. - mpu.setThreshold(3); -} - -void loop() -{ - timer = millis(); - - // Read normalized values - Vector norm = mpu.readNormalizeGyro(); - - // Calculate Pitch, Roll and Yaw - pitch = pitch + norm.YAxis * timeStep; - roll = roll + norm.XAxis * timeStep; - yaw = yaw + norm.ZAxis * timeStep; - - // Output raw - Serial.print(" Pitch = "); - Serial.print(pitch); - Serial.print(" Roll = "); - Serial.print(roll); - Serial.print(" Yaw = "); - Serial.println(yaw); - - // Wait to full timeStep period - delay((timeStep*1000) - (millis() - timer)); -} diff --git a/MPU6050_gyro_simple/MPU6050_gyro_simple.ino b/MPU6050_gyro_simple/MPU6050_gyro_simple.ino deleted file mode 100644 index b0ff8d6..0000000 --- a/MPU6050_gyro_simple/MPU6050_gyro_simple.ino +++ /dev/null @@ -1,103 +0,0 @@ -/* - MPU6050 Triple Axis Gyroscope & Accelerometer. Simple Gyroscope Example. - Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - GIT: https://github.com/jarzebski/Arduino-MPU6050 - Web: http://www.jarzebski.pl - (c) 2014 by Korneliusz Jarzebski -*/ - -#include -#include - -MPU6050 mpu; - -void setup() -{ - Serial.begin(115200); - - // Initialize MPU6050 - Serial.println("Initialize MPU6050"); - while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) - { - Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); - delay(500); - } - - // If you want, you can set gyroscope offsets - // mpu.setGyroOffsetX(155); - // mpu.setGyroOffsetY(15); - // mpu.setGyroOffsetZ(15); - - // Calibrate gyroscope. The calibration must be at rest. - // If you don't want calibrate, comment this line. - mpu.calibrateGyro(); - - // Set threshold sensivty. Default 3. - // If you don't want use threshold, comment this line or set 0. - mpu.setThreshold(3); - - // Check settings - checkSettings(); -} - -void checkSettings() -{ - Serial.println(); - - Serial.print(" * Sleep Mode: "); - Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Clock Source: "); - switch(mpu.getClockSource()) - { - case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; - case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; - case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; - case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; - case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; - } - - Serial.print(" * Gyroscope: "); - switch(mpu.getScale()) - { - case MPU6050_SCALE_2000DPS: Serial.println("2000 dps"); break; - case MPU6050_SCALE_1000DPS: Serial.println("1000 dps"); break; - case MPU6050_SCALE_500DPS: Serial.println("500 dps"); break; - case MPU6050_SCALE_250DPS: Serial.println("250 dps"); break; - } - - Serial.print(" * Gyroscope offsets: "); - Serial.print(mpu.getGyroOffsetX()); - Serial.print(" / "); - Serial.print(mpu.getGyroOffsetY()); - Serial.print(" / "); - Serial.println(mpu.getGyroOffsetZ()); - - Serial.println(); -} - -void loop() -{ - Vector rawGyro = mpu.readRawGyro(); - Vector normGyro = mpu.readNormalizeGyro(); - - Serial.print(" Xraw = "); - Serial.print(rawGyro.XAxis); - Serial.print(" Yraw = "); - Serial.print(rawGyro.YAxis); - Serial.print(" Zraw = "); - Serial.println(rawGyro.ZAxis); - - Serial.print(" Xnorm = "); - Serial.print(normGyro.XAxis); - Serial.print(" Ynorm = "); - Serial.print(normGyro.YAxis); - Serial.print(" Znorm = "); - Serial.println(normGyro.ZAxis); - - delay(10); -} - - diff --git a/MPU6050_motion/MPU6050_motion.ino b/MPU6050_motion/MPU6050_motion.ino deleted file mode 100644 index 27c7c29..0000000 --- a/MPU6050_motion/MPU6050_motion.ino +++ /dev/null @@ -1,156 +0,0 @@ -/* - MPU6050 Triple Axis Gyroscope & Accelerometer. Motion detection. - Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - GIT: https://github.com/jarzebski/Arduino-MPU6050 - Web: http://www.jarzebski.pl - (c) 2014 by Korneliusz Jarzebski -*/ - -#include -#include - -MPU6050 mpu; - -void setup() -{ - Serial.begin(115200); - - Serial.println("Initialize MPU6050"); - - while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_16G)) - { - Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); - delay(500); - } - - mpu.setAccelPowerOnDelay(MPU6050_DELAY_3MS); - - mpu.setIntFreeFallEnabled(false); - mpu.setIntZeroMotionEnabled(false); - mpu.setIntMotionEnabled(false); - - mpu.setDHPFMode(MPU6050_DHPF_5HZ); - - mpu.setMotionDetectionThreshold(2); - mpu.setMotionDetectionDuration(5); - - mpu.setZeroMotionDetectionThreshold(4); - mpu.setZeroMotionDetectionDuration(2); - - checkSettings(); - - pinMode(4, OUTPUT); - digitalWrite(4, LOW); - - pinMode(7, OUTPUT); - digitalWrite(7, LOW); -} - -void checkSettings() -{ - Serial.println(); - - Serial.print(" * Sleep Mode: "); - Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Motion Interrupt: "); - Serial.println(mpu.getIntMotionEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Zero Motion Interrupt: "); - Serial.println(mpu.getIntZeroMotionEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Free Fall Interrupt: "); - Serial.println(mpu.getIntFreeFallEnabled() ? "Enabled" : "Disabled"); - - Serial.print(" * Motion Threshold: "); - Serial.println(mpu.getMotionDetectionThreshold()); - - Serial.print(" * Motion Duration: "); - Serial.println(mpu.getMotionDetectionDuration()); - - Serial.print(" * Zero Motion Threshold: "); - Serial.println(mpu.getZeroMotionDetectionThreshold()); - - Serial.print(" * Zero Motion Duration: "); - Serial.println(mpu.getZeroMotionDetectionDuration()); - - Serial.print(" * Clock Source: "); - switch(mpu.getClockSource()) - { - case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; - case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; - case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; - case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; - case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; - case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; - } - - Serial.print(" * Accelerometer: "); - switch(mpu.getRange()) - { - case MPU6050_RANGE_16G: Serial.println("+/- 16 g"); break; - case MPU6050_RANGE_8G: Serial.println("+/- 8 g"); break; - case MPU6050_RANGE_4G: Serial.println("+/- 4 g"); break; - case MPU6050_RANGE_2G: Serial.println("+/- 2 g"); break; - } - - Serial.print(" * Accelerometer offsets: "); - Serial.print(mpu.getAccelOffsetX()); - Serial.print(" / "); - Serial.print(mpu.getAccelOffsetY()); - Serial.print(" / "); - Serial.println(mpu.getAccelOffsetZ()); - - Serial.print(" * Accelerometer power delay: "); - switch(mpu.getAccelPowerOnDelay()) - { - case MPU6050_DELAY_3MS: Serial.println("3ms"); break; - case MPU6050_DELAY_2MS: Serial.println("2ms"); break; - case MPU6050_DELAY_1MS: Serial.println("1ms"); break; - case MPU6050_NO_DELAY: Serial.println("0ms"); break; - } - - Serial.println(); -} - -void loop() -{ - Vector rawAccel = mpu.readRawAccel(); - Activites act = mpu.readActivites(); - - if (act.isActivity) - { - digitalWrite(4, HIGH); - } else - { - digitalWrite(4, LOW); - } - - if (act.isInactivity) - { - digitalWrite(7, HIGH); - } else - { - digitalWrite(7, LOW); - } - - Serial.print(act.isActivity); - Serial.print(act.isInactivity); - - Serial.print(" "); - Serial.print(act.isPosActivityOnX); - Serial.print(act.isNegActivityOnX); - Serial.print(" "); - - Serial.print(act.isPosActivityOnY); - Serial.print(act.isNegActivityOnY); - Serial.print(" "); - - Serial.print(act.isPosActivityOnZ); - Serial.print(act.isNegActivityOnZ); - Serial.print("\n"); - delay(50); -} - - diff --git a/MPU6050_temperature/MPU6050_temperature.ino b/MPU6050_temperature/MPU6050_temperature.ino deleted file mode 100644 index d2d5958..0000000 --- a/MPU6050_temperature/MPU6050_temperature.ino +++ /dev/null @@ -1,38 +0,0 @@ -/* - MPU6050 Triple Axis Gyroscope & Accelerometer. Temperature Example. - Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - GIT: https://github.com/jarzebski/Arduino-MPU6050 - Web: http://www.jarzebski.pl - (c) 2014 by Korneliusz Jarzebski -*/ - -#include -#include - -MPU6050 mpu; - -void setup() -{ - Serial.begin(115200); - - Serial.println("Initialize MPU6050"); - - while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) - { - Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); - delay(500); - } -} - -void loop() -{ - float temp = mpu.readTemperature(); - - Serial.print(" Temp = "); - Serial.print(temp); - Serial.println(" *C"); - - delay(500); -} - - From 8345c2f42985b3d88d77717210cc34e3bc187a92 Mon Sep 17 00:00:00 2001 From: Jeremy Belpois Date: Thu, 14 Dec 2017 20:56:32 +0100 Subject: [PATCH 2/7] Moved examples --- .../MPU6050_accel_pitch_roll.ino | 47 ++++++ .../MPU6050_accel_simple.ino | 94 +++++++++++ .../MPU6050_free_fall/MPU6050_free_fall.ino | 143 ++++++++++++++++ .../MPU6050_gyro_pitch_roll_yaw.ino | 65 ++++++++ .../MPU6050_gyro_simple.ino | 103 ++++++++++++ examples/MPU6050_motion/MPU6050_motion.ino | 156 ++++++++++++++++++ .../MPU6050_temperature.ino | 38 +++++ 7 files changed, 646 insertions(+) create mode 100644 examples/MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino create mode 100644 examples/MPU6050_accel_simple/MPU6050_accel_simple.ino create mode 100644 examples/MPU6050_free_fall/MPU6050_free_fall.ino create mode 100644 examples/MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino create mode 100644 examples/MPU6050_gyro_simple/MPU6050_gyro_simple.ino create mode 100644 examples/MPU6050_motion/MPU6050_motion.ino create mode 100644 examples/MPU6050_temperature/MPU6050_temperature.ino diff --git a/examples/MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino b/examples/MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino new file mode 100644 index 0000000..c1b0e2f --- /dev/null +++ b/examples/MPU6050_accel_pitch_roll/MPU6050_accel_pitch_roll.ino @@ -0,0 +1,47 @@ +/* + MPU6050 Triple Axis Gyroscope & Accelerometer. Pitch & Roll Accelerometer Example. + Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + GIT: https://github.com/jarzebski/Arduino-MPU6050 + Web: http://www.jarzebski.pl + (c) 2014 by Korneliusz Jarzebski +*/ + +#include +#include + +MPU6050 mpu; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Initialize MPU6050"); + + while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) + { + Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); + delay(500); + } +} + +void loop() +{ + // Read normalized values + Vector normAccel = mpu.readNormalizeAccel(); + + // Calculate Pitch & Roll + int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis*normAccel.YAxis + normAccel.ZAxis*normAccel.ZAxis))*180.0)/M_PI; + int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI; + + // Output + Serial.print(" Pitch = "); + Serial.print(pitch); + Serial.print(" Roll = "); + Serial.print(roll); + + Serial.println(); + + delay(10); +} + + diff --git a/examples/MPU6050_accel_simple/MPU6050_accel_simple.ino b/examples/MPU6050_accel_simple/MPU6050_accel_simple.ino new file mode 100644 index 0000000..d327167 --- /dev/null +++ b/examples/MPU6050_accel_simple/MPU6050_accel_simple.ino @@ -0,0 +1,94 @@ +/* + MPU6050 Triple Axis Gyroscope & Accelerometer. Simple Accelerometer Example. + Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + GIT: https://github.com/jarzebski/Arduino-MPU6050 + Web: http://www.jarzebski.pl + (c) 2014 by Korneliusz Jarzebski +*/ + +#include +#include + +MPU6050 mpu; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Initialize MPU6050"); + + while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) + { + Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); + delay(500); + } + + // If you want, you can set accelerometer offsets + // mpu.setAccelOffsetX(); + // mpu.setAccelOffsetY(); + // mpu.setAccelOffsetZ(); + + checkSettings(); +} + +void checkSettings() +{ + Serial.println(); + + Serial.print(" * Sleep Mode: "); + Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Clock Source: "); + switch(mpu.getClockSource()) + { + case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; + case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; + case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; + case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; + case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; + } + + Serial.print(" * Accelerometer: "); + switch(mpu.getRange()) + { + case MPU6050_RANGE_16G: Serial.println("+/- 16 g"); break; + case MPU6050_RANGE_8G: Serial.println("+/- 8 g"); break; + case MPU6050_RANGE_4G: Serial.println("+/- 4 g"); break; + case MPU6050_RANGE_2G: Serial.println("+/- 2 g"); break; + } + + Serial.print(" * Accelerometer offsets: "); + Serial.print(mpu.getAccelOffsetX()); + Serial.print(" / "); + Serial.print(mpu.getAccelOffsetY()); + Serial.print(" / "); + Serial.println(mpu.getAccelOffsetZ()); + + Serial.println(); +} + +void loop() +{ + Vector rawAccel = mpu.readRawAccel(); + Vector normAccel = mpu.readNormalizeAccel(); + + Serial.print(" Xraw = "); + Serial.print(rawAccel.XAxis); + Serial.print(" Yraw = "); + Serial.print(rawAccel.YAxis); + Serial.print(" Zraw = "); + + Serial.println(rawAccel.ZAxis); + Serial.print(" Xnorm = "); + Serial.print(normAccel.XAxis); + Serial.print(" Ynorm = "); + Serial.print(normAccel.YAxis); + Serial.print(" Znorm = "); + Serial.println(normAccel.ZAxis); + + delay(10); +} + + diff --git a/examples/MPU6050_free_fall/MPU6050_free_fall.ino b/examples/MPU6050_free_fall/MPU6050_free_fall.ino new file mode 100644 index 0000000..d2d9269 --- /dev/null +++ b/examples/MPU6050_free_fall/MPU6050_free_fall.ino @@ -0,0 +1,143 @@ +/* + MPU6050 Triple Axis Gyroscope & Accelerometer. Free fall detection. + Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + GIT: https://github.com/jarzebski/Arduino-MPU6050 + Web: http://www.jarzebski.pl + (c) 2014 by Korneliusz Jarzebski +*/ + +#include +#include + +MPU6050 mpu; + +boolean ledState = false; +boolean freefallDetected = false; +int freefallBlinkCount = 0; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Initialize MPU6050"); + + while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_16G)) + { + Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); + delay(500); + } + + mpu.setAccelPowerOnDelay(MPU6050_DELAY_3MS); + + mpu.setIntFreeFallEnabled(true); + mpu.setIntZeroMotionEnabled(false); + mpu.setIntMotionEnabled(false); + + mpu.setDHPFMode(MPU6050_DHPF_5HZ); + + mpu.setFreeFallDetectionThreshold(17); + mpu.setFreeFallDetectionDuration(2); + + checkSettings(); + + pinMode(4, OUTPUT); + digitalWrite(4, LOW); + + attachInterrupt(0, doInt, RISING); +} + +void doInt() +{ + freefallBlinkCount = 0; + freefallDetected = true; +} + +void checkSettings() +{ + Serial.println(); + + Serial.print(" * Sleep Mode: "); + Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Motion Interrupt: "); + Serial.println(mpu.getIntMotionEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Zero Motion Interrupt: "); + Serial.println(mpu.getIntZeroMotionEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Free Fall Interrupt: "); + Serial.println(mpu.getIntFreeFallEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Free Fal Threshold: "); + Serial.println(mpu.getFreeFallDetectionThreshold()); + + Serial.print(" * Free FallDuration: "); + Serial.println(mpu.getFreeFallDetectionDuration()); + + Serial.print(" * Clock Source: "); + switch(mpu.getClockSource()) + { + case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; + case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; + case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; + case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; + case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; + } + + Serial.print(" * Accelerometer: "); + switch(mpu.getRange()) + { + case MPU6050_RANGE_16G: Serial.println("+/- 16 g"); break; + case MPU6050_RANGE_8G: Serial.println("+/- 8 g"); break; + case MPU6050_RANGE_4G: Serial.println("+/- 4 g"); break; + case MPU6050_RANGE_2G: Serial.println("+/- 2 g"); break; + } + + Serial.print(" * Accelerometer offsets: "); + Serial.print(mpu.getAccelOffsetX()); + Serial.print(" / "); + Serial.print(mpu.getAccelOffsetY()); + Serial.print(" / "); + Serial.println(mpu.getAccelOffsetZ()); + + Serial.print(" * Accelerometer power delay: "); + switch(mpu.getAccelPowerOnDelay()) + { + case MPU6050_DELAY_3MS: Serial.println("3ms"); break; + case MPU6050_DELAY_2MS: Serial.println("2ms"); break; + case MPU6050_DELAY_1MS: Serial.println("1ms"); break; + case MPU6050_NO_DELAY: Serial.println("0ms"); break; + } + + Serial.println(); +} + +void loop() +{ + Vector rawAccel = mpu.readRawAccel(); + Activites act = mpu.readActivites(); + + Serial.print(act.isFreeFall); + Serial.print("\n"); + + if (freefallDetected) + { + ledState = !ledState; + + digitalWrite(4, ledState); + + freefallBlinkCount++; + + if (freefallBlinkCount == 20) + { + freefallDetected = false; + ledState = false; + digitalWrite(4, ledState); + } + } + + delay(100); + +} diff --git a/examples/MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino b/examples/MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino new file mode 100644 index 0000000..c854158 --- /dev/null +++ b/examples/MPU6050_gyro_pitch_roll_yaw/MPU6050_gyro_pitch_roll_yaw.ino @@ -0,0 +1,65 @@ +/* + MPU6050 Triple Axis Gyroscope & Accelerometer. Pitch & Roll & Yaw Gyroscope Example. + Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + GIT: https://github.com/jarzebski/Arduino-MPU6050 + Web: http://www.jarzebski.pl + (c) 2014 by Korneliusz Jarzebski +*/ + +#include +#include + +MPU6050 mpu; + +// Timers +unsigned long timer = 0; +float timeStep = 0.01; + +// Pitch, Roll and Yaw values +float pitch = 0; +float roll = 0; +float yaw = 0; + +void setup() +{ + Serial.begin(115200); + + // Initialize MPU6050 + while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) + { + Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); + delay(500); + } + + // Calibrate gyroscope. The calibration must be at rest. + // If you don't want calibrate, comment this line. + mpu.calibrateGyro(); + + // Set threshold sensivty. Default 3. + // If you don't want use threshold, comment this line or set 0. + mpu.setThreshold(3); +} + +void loop() +{ + timer = millis(); + + // Read normalized values + Vector norm = mpu.readNormalizeGyro(); + + // Calculate Pitch, Roll and Yaw + pitch = pitch + norm.YAxis * timeStep; + roll = roll + norm.XAxis * timeStep; + yaw = yaw + norm.ZAxis * timeStep; + + // Output raw + Serial.print(" Pitch = "); + Serial.print(pitch); + Serial.print(" Roll = "); + Serial.print(roll); + Serial.print(" Yaw = "); + Serial.println(yaw); + + // Wait to full timeStep period + delay((timeStep*1000) - (millis() - timer)); +} diff --git a/examples/MPU6050_gyro_simple/MPU6050_gyro_simple.ino b/examples/MPU6050_gyro_simple/MPU6050_gyro_simple.ino new file mode 100644 index 0000000..b0ff8d6 --- /dev/null +++ b/examples/MPU6050_gyro_simple/MPU6050_gyro_simple.ino @@ -0,0 +1,103 @@ +/* + MPU6050 Triple Axis Gyroscope & Accelerometer. Simple Gyroscope Example. + Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + GIT: https://github.com/jarzebski/Arduino-MPU6050 + Web: http://www.jarzebski.pl + (c) 2014 by Korneliusz Jarzebski +*/ + +#include +#include + +MPU6050 mpu; + +void setup() +{ + Serial.begin(115200); + + // Initialize MPU6050 + Serial.println("Initialize MPU6050"); + while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) + { + Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); + delay(500); + } + + // If you want, you can set gyroscope offsets + // mpu.setGyroOffsetX(155); + // mpu.setGyroOffsetY(15); + // mpu.setGyroOffsetZ(15); + + // Calibrate gyroscope. The calibration must be at rest. + // If you don't want calibrate, comment this line. + mpu.calibrateGyro(); + + // Set threshold sensivty. Default 3. + // If you don't want use threshold, comment this line or set 0. + mpu.setThreshold(3); + + // Check settings + checkSettings(); +} + +void checkSettings() +{ + Serial.println(); + + Serial.print(" * Sleep Mode: "); + Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Clock Source: "); + switch(mpu.getClockSource()) + { + case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; + case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; + case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; + case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; + case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; + } + + Serial.print(" * Gyroscope: "); + switch(mpu.getScale()) + { + case MPU6050_SCALE_2000DPS: Serial.println("2000 dps"); break; + case MPU6050_SCALE_1000DPS: Serial.println("1000 dps"); break; + case MPU6050_SCALE_500DPS: Serial.println("500 dps"); break; + case MPU6050_SCALE_250DPS: Serial.println("250 dps"); break; + } + + Serial.print(" * Gyroscope offsets: "); + Serial.print(mpu.getGyroOffsetX()); + Serial.print(" / "); + Serial.print(mpu.getGyroOffsetY()); + Serial.print(" / "); + Serial.println(mpu.getGyroOffsetZ()); + + Serial.println(); +} + +void loop() +{ + Vector rawGyro = mpu.readRawGyro(); + Vector normGyro = mpu.readNormalizeGyro(); + + Serial.print(" Xraw = "); + Serial.print(rawGyro.XAxis); + Serial.print(" Yraw = "); + Serial.print(rawGyro.YAxis); + Serial.print(" Zraw = "); + Serial.println(rawGyro.ZAxis); + + Serial.print(" Xnorm = "); + Serial.print(normGyro.XAxis); + Serial.print(" Ynorm = "); + Serial.print(normGyro.YAxis); + Serial.print(" Znorm = "); + Serial.println(normGyro.ZAxis); + + delay(10); +} + + diff --git a/examples/MPU6050_motion/MPU6050_motion.ino b/examples/MPU6050_motion/MPU6050_motion.ino new file mode 100644 index 0000000..27c7c29 --- /dev/null +++ b/examples/MPU6050_motion/MPU6050_motion.ino @@ -0,0 +1,156 @@ +/* + MPU6050 Triple Axis Gyroscope & Accelerometer. Motion detection. + Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + GIT: https://github.com/jarzebski/Arduino-MPU6050 + Web: http://www.jarzebski.pl + (c) 2014 by Korneliusz Jarzebski +*/ + +#include +#include + +MPU6050 mpu; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Initialize MPU6050"); + + while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_16G)) + { + Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); + delay(500); + } + + mpu.setAccelPowerOnDelay(MPU6050_DELAY_3MS); + + mpu.setIntFreeFallEnabled(false); + mpu.setIntZeroMotionEnabled(false); + mpu.setIntMotionEnabled(false); + + mpu.setDHPFMode(MPU6050_DHPF_5HZ); + + mpu.setMotionDetectionThreshold(2); + mpu.setMotionDetectionDuration(5); + + mpu.setZeroMotionDetectionThreshold(4); + mpu.setZeroMotionDetectionDuration(2); + + checkSettings(); + + pinMode(4, OUTPUT); + digitalWrite(4, LOW); + + pinMode(7, OUTPUT); + digitalWrite(7, LOW); +} + +void checkSettings() +{ + Serial.println(); + + Serial.print(" * Sleep Mode: "); + Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Motion Interrupt: "); + Serial.println(mpu.getIntMotionEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Zero Motion Interrupt: "); + Serial.println(mpu.getIntZeroMotionEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Free Fall Interrupt: "); + Serial.println(mpu.getIntFreeFallEnabled() ? "Enabled" : "Disabled"); + + Serial.print(" * Motion Threshold: "); + Serial.println(mpu.getMotionDetectionThreshold()); + + Serial.print(" * Motion Duration: "); + Serial.println(mpu.getMotionDetectionDuration()); + + Serial.print(" * Zero Motion Threshold: "); + Serial.println(mpu.getZeroMotionDetectionThreshold()); + + Serial.print(" * Zero Motion Duration: "); + Serial.println(mpu.getZeroMotionDetectionDuration()); + + Serial.print(" * Clock Source: "); + switch(mpu.getClockSource()) + { + case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; + case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; + case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; + case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; + case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; + case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; + } + + Serial.print(" * Accelerometer: "); + switch(mpu.getRange()) + { + case MPU6050_RANGE_16G: Serial.println("+/- 16 g"); break; + case MPU6050_RANGE_8G: Serial.println("+/- 8 g"); break; + case MPU6050_RANGE_4G: Serial.println("+/- 4 g"); break; + case MPU6050_RANGE_2G: Serial.println("+/- 2 g"); break; + } + + Serial.print(" * Accelerometer offsets: "); + Serial.print(mpu.getAccelOffsetX()); + Serial.print(" / "); + Serial.print(mpu.getAccelOffsetY()); + Serial.print(" / "); + Serial.println(mpu.getAccelOffsetZ()); + + Serial.print(" * Accelerometer power delay: "); + switch(mpu.getAccelPowerOnDelay()) + { + case MPU6050_DELAY_3MS: Serial.println("3ms"); break; + case MPU6050_DELAY_2MS: Serial.println("2ms"); break; + case MPU6050_DELAY_1MS: Serial.println("1ms"); break; + case MPU6050_NO_DELAY: Serial.println("0ms"); break; + } + + Serial.println(); +} + +void loop() +{ + Vector rawAccel = mpu.readRawAccel(); + Activites act = mpu.readActivites(); + + if (act.isActivity) + { + digitalWrite(4, HIGH); + } else + { + digitalWrite(4, LOW); + } + + if (act.isInactivity) + { + digitalWrite(7, HIGH); + } else + { + digitalWrite(7, LOW); + } + + Serial.print(act.isActivity); + Serial.print(act.isInactivity); + + Serial.print(" "); + Serial.print(act.isPosActivityOnX); + Serial.print(act.isNegActivityOnX); + Serial.print(" "); + + Serial.print(act.isPosActivityOnY); + Serial.print(act.isNegActivityOnY); + Serial.print(" "); + + Serial.print(act.isPosActivityOnZ); + Serial.print(act.isNegActivityOnZ); + Serial.print("\n"); + delay(50); +} + + diff --git a/examples/MPU6050_temperature/MPU6050_temperature.ino b/examples/MPU6050_temperature/MPU6050_temperature.ino new file mode 100644 index 0000000..d2d5958 --- /dev/null +++ b/examples/MPU6050_temperature/MPU6050_temperature.ino @@ -0,0 +1,38 @@ +/* + MPU6050 Triple Axis Gyroscope & Accelerometer. Temperature Example. + Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + GIT: https://github.com/jarzebski/Arduino-MPU6050 + Web: http://www.jarzebski.pl + (c) 2014 by Korneliusz Jarzebski +*/ + +#include +#include + +MPU6050 mpu; + +void setup() +{ + Serial.begin(115200); + + Serial.println("Initialize MPU6050"); + + while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) + { + Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); + delay(500); + } +} + +void loop() +{ + float temp = mpu.readTemperature(); + + Serial.print(" Temp = "); + Serial.print(temp); + Serial.println(" *C"); + + delay(500); +} + + From e8b2b5ca1132399668ff030a33f24a17a44c7cfd Mon Sep 17 00:00:00 2001 From: Jeremy Belpois Date: Thu, 14 Dec 2017 21:05:57 +0100 Subject: [PATCH 3/7] Added basic error tracking and removed unnecessary while loops --- MPU6050.cpp | 32 +++++++++++++++++++++----------- MPU6050.h | 7 +++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/MPU6050.cpp b/MPU6050.cpp index d9cc131..8f116c4 100644 --- a/MPU6050.cpp +++ b/MPU6050.cpp @@ -353,9 +353,10 @@ Vector MPU6050::readRawAccel(void) Wire.endTransmission(); Wire.beginTransmission(mpuAddress); - Wire.requestFrom(mpuAddress, 6); - - while (Wire.available() < 6); + if (Wire.requestFrom(mpuAddress, 6) != 6) { + errno = ERR_NOT_ENOUGH_BYTES; + return 0; + } #if ARDUINO >= 100 uint8_t xha = Wire.read(); @@ -413,10 +414,12 @@ Vector MPU6050::readRawGyro(void) #endif Wire.endTransmission(); - Wire.beginTransmission(mpuAddress); - Wire.requestFrom(mpuAddress, 6); + + if (Wire.requestFrom(mpuAddress, 6) != 6) { + errno = ERR_NOT_ENOUGH_BYTES; + return 0; + } - while (Wire.available() < 6); #if ARDUINO >= 100 uint8_t xha = Wire.read(); @@ -627,7 +630,10 @@ uint8_t MPU6050::fastRegister8(uint8_t reg) Wire.endTransmission(); Wire.beginTransmission(mpuAddress); - Wire.requestFrom(mpuAddress, 1); + if (Wire.requestFrom(mpuAddress, 1) != 1) { + errno = ERR_NOT_ENOUGH_BYTES; + return 0; + } #if ARDUINO >= 100 value = Wire.read(); #else @@ -652,8 +658,10 @@ uint8_t MPU6050::readRegister8(uint8_t reg) Wire.endTransmission(); Wire.beginTransmission(mpuAddress); - Wire.requestFrom(mpuAddress, 1); - while(!Wire.available()) {}; + if (Wire.requestFrom(mpuAddress, 1) != 1) { + return 0; + } + #if ARDUINO >= 100 value = Wire.read(); #else @@ -691,8 +699,10 @@ int16_t MPU6050::readRegister16(uint8_t reg) Wire.endTransmission(); Wire.beginTransmission(mpuAddress); - Wire.requestFrom(mpuAddress, 2); - while(!Wire.available()) {}; + if (Wire.requestFrom(mpuAddress, 2) != 2) { + errno = ERR_NOT_ENOUGH_BYTES; + return 0; + } #if ARDUINO >= 100 uint8_t vha = Wire.read(); uint8_t vla = Wire.read(); diff --git a/MPU6050.h b/MPU6050.h index fbf077e..d069b3a 100644 --- a/MPU6050.h +++ b/MPU6050.h @@ -75,6 +75,11 @@ along with this program. If not, see . #ifndef VECTOR_STRUCT_H #define VECTOR_STRUCT_H + +// Errors +#define ERR_NOERR 0 +#define ERR_NOT_ENOUG_BYTES 1 + struct Vector { float XAxis; @@ -230,6 +235,7 @@ class MPU6050 Vector readNormalizeAccel(void); Vector readScaledAccel(void); + uint8_t getErrno(); private: Vector ra, rg; // Raw vectors Vector na, ng; // Normalized vectors @@ -242,6 +248,7 @@ class MPU6050 bool useCalibrate; int mpuAddress; + uint8_t errno; uint8_t fastRegister8(uint8_t reg); uint8_t readRegister8(uint8_t reg); From 8f352312330798bd730f4cbd184e37a8dbcdb380 Mon Sep 17 00:00:00 2001 From: Jeremy Belpois Date: Thu, 14 Dec 2017 21:16:42 +0100 Subject: [PATCH 4/7] Removed unnecessary while loops and added basic error reporting --- MPU6050.cpp | 10 +++++++--- MPU6050.h | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/MPU6050.cpp b/MPU6050.cpp index 8f116c4..436a61a 100644 --- a/MPU6050.cpp +++ b/MPU6050.cpp @@ -355,7 +355,7 @@ Vector MPU6050::readRawAccel(void) Wire.beginTransmission(mpuAddress); if (Wire.requestFrom(mpuAddress, 6) != 6) { errno = ERR_NOT_ENOUGH_BYTES; - return 0; + return ra; } #if ARDUINO >= 100 @@ -417,7 +417,7 @@ Vector MPU6050::readRawGyro(void) if (Wire.requestFrom(mpuAddress, 6) != 6) { errno = ERR_NOT_ENOUGH_BYTES; - return 0; + return rg; } @@ -615,7 +615,11 @@ void MPU6050::setThreshold(uint8_t multiple) // Remember old threshold value actualThreshold = multiple; } - +uint8_t MPU6050::getErrno(void) +{ + uint8_t errno; + return errno; +} // Fast read 8-bit from register uint8_t MPU6050::fastRegister8(uint8_t reg) { diff --git a/MPU6050.h b/MPU6050.h index d069b3a..a77c344 100644 --- a/MPU6050.h +++ b/MPU6050.h @@ -77,8 +77,8 @@ along with this program. If not, see . #define VECTOR_STRUCT_H // Errors -#define ERR_NOERR 0 -#define ERR_NOT_ENOUG_BYTES 1 +#define ERR_NOERR 0 +#define ERR_NOT_ENOUGH_BYTES 1 struct Vector { @@ -235,7 +235,7 @@ class MPU6050 Vector readNormalizeAccel(void); Vector readScaledAccel(void); - uint8_t getErrno(); + uint8_t getErrno(void); private: Vector ra, rg; // Raw vectors Vector na, ng; // Normalized vectors From 668b6031cde0c5545adee7a2a6b072789ffcc212 Mon Sep 17 00:00:00 2001 From: Jeremy Belpois Date: Thu, 14 Dec 2017 21:16:56 +0100 Subject: [PATCH 5/7] Updated readme --- README.md | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a2fedf0..770aa3f 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,6 @@ Arduino-MPU6050 =============== -MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library. +MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library based on work by @jarzebski -![MPU6050 Processing](http://www.jarzebski.pl/media/zoom/publish/2014/10/mpu6050-processing-2.png "MPU6050 Processing") - -Tutorials: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html - -This library use I2C to communicate, 2 pins are required to interface - -I need your help ----------------- - -July 31, 2017 - -In the near future I plan to refactoring the libraries. The main goal is to improve code quality, new features and add support for different versions of Arduino boards like Uno, Mega and Zero. - -For this purpose I need to buy modules, Arduino Boards and lot of beer. - -If you want to support the further and long-term development of libraries, please help. - -You can do this by transferring any amount to my PayPal account: paypal@jarzebski.pl - -Thanks! +This library use I2C to communicate, thus 2 pins are required to interface From 32e8e9e6d6aff44e813e29dede6570862301c589 Mon Sep 17 00:00:00 2001 From: Jeremy Belpois Date: Thu, 14 Dec 2017 21:21:29 +0100 Subject: [PATCH 6/7] Updated library structure --- library.properties | 9 +++++++++ MPU6050.cpp => src/MPU6050.cpp | 0 MPU6050.h => src/MPU6050.h | 0 3 files changed, 9 insertions(+) create mode 100644 library.properties rename MPU6050.cpp => src/MPU6050.cpp (100%) rename MPU6050.h => src/MPU6050.h (100%) diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..ec78d4e --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=MPU6050 +version=1.0 +author= +maintainer= +sentence=MPU6050 Triple Axis Gyroscope & Accelerometer +paragraph= +category=Uncategorized +url= +architectures=* diff --git a/MPU6050.cpp b/src/MPU6050.cpp similarity index 100% rename from MPU6050.cpp rename to src/MPU6050.cpp diff --git a/MPU6050.h b/src/MPU6050.h similarity index 100% rename from MPU6050.h rename to src/MPU6050.h From d31626a8e9cb9b641fa2b7eab930c12535e275ba Mon Sep 17 00:00:00 2001 From: Jeremy Belpois Date: Thu, 14 Dec 2017 21:33:21 +0100 Subject: [PATCH 7/7] Revert "Updated readme" This reverts commit 668b6031cde0c5545adee7a2a6b072789ffcc212. --- README.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 770aa3f..a2fedf0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,25 @@ Arduino-MPU6050 =============== -MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library based on work by @jarzebski +MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library. -This library use I2C to communicate, thus 2 pins are required to interface +![MPU6050 Processing](http://www.jarzebski.pl/media/zoom/publish/2014/10/mpu6050-processing-2.png "MPU6050 Processing") + +Tutorials: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html + +This library use I2C to communicate, 2 pins are required to interface + +I need your help +---------------- + +July 31, 2017 + +In the near future I plan to refactoring the libraries. The main goal is to improve code quality, new features and add support for different versions of Arduino boards like Uno, Mega and Zero. + +For this purpose I need to buy modules, Arduino Boards and lot of beer. + +If you want to support the further and long-term development of libraries, please help. + +You can do this by transferring any amount to my PayPal account: paypal@jarzebski.pl + +Thanks!