Compare commits

..

8 Commits

Author SHA1 Message Date
1bda6b2f52 Finalized 2024-04-12 07:42:24 +02:00
c2690e2d5e Merge branch 'main' of https://git.cps.unileoben.ac.at/obermayer/esp32_MPU6050 2024-04-12 07:40:37 +02:00
1b3ee52ad2 Finalized 2024-04-11 13:01:50 +02:00
cbd1750219 Upload files to '' 2024-04-11 05:52:53 +00:00
a348350474 Upload files to ''
The README.md content, but in .docx format.
There are pictures there for further help on the intructions if needed!
2024-04-11 05:29:59 +00:00
bb36373b21 Update 'README.md' 2024-04-04 12:37:56 +00:00
5353b36e01 Update 'README.md' 2024-04-04 12:36:50 +00:00
16f2dacb04 Add 'REDME.md' 2024-04-04 12:33:36 +00:00
6 changed files with 296 additions and 37 deletions

BIN
MPU6050_datasheet.pdf Normal file

Binary file not shown.

38
README.md Normal file
View File

@ -0,0 +1,38 @@
Work yourself from the Main, to the rest of the branches step by step, as they build on each other. Follow the following steps in this README before doing the branches and Main though for a good setup.
Tutorial additions (Everything not present in the Git folder) (origin: docx file) Installing the PlatformIO IDE extension In Visual Studio Code, go to “Extensions”. Search for “PlatformIO“ and install said extension. A little Icon should appear on the left sidebar, click on it to initialize everything. And youre good to go!
Basics of the PlatformIO IDE There are the 3 most important features it provides:
Building your project and flashing it to the Board of choice (example: ESP32)
A .ini file to manage everything you need for a setup (for example what framework to use, what board, library dependencies etc)
Practical inbuilt terminal
How do you build a project? You click on the little checkmark at the bottom bar. How do you flash / install your program on the board of choice? Right next to said little checkmark is an arrow pointing right. Click on it, and it builds your program and flashes it. How do you access the terminal quickly? You click on the cable-head symbol on the same bar.
“Oh no, none of those symbols are there?!” Do not worry. Check whether you initialized the extension by clicking on it and checking if
“PIO Home” -> “Open” opens a site or not. If the second is not the case, deinstall and reinstall the extension to see if that solves the issue.
Creating a new project in PlatformIO You have now successfully installed the extension. Well done! Now you can start the coding. Almost. Go to
“PlatformIO” -> “PIO Home” -> “Open” -> “New Project”
If you are working on the ESP32 S1, you can use the exact board selected in the picture. If not, you must check what other board selection works for your board.
Once that is all said and done, click Finish and your new project opens. You want to mainly work in “src”, “main.cpp” for now.
Installing the library you need for the MPU6050 This is one method as to how to include and install this library for your project, but I personally deem this method the best. Here are the steps:
Go to the little icon of the PlatformIO extension and click on it. Then go to the “Libraries” option.
Once there, search for “Adafruit MPU6050”, there should be 5 results.
Click on it, and then on “Add to Project”
Select your project and the version, then click “Add”.
This will take a little moment before its added, but once thats done, youre all good to go regarding including the .h files in your program.
The test and setup of the ESP32 itself. What do you need?
An esp32 (s1 optionally, mind the board selection in vs code)
An MPU6050 (Check datasheet if unsure)
A few cables of your choice (Jumpwires for example)
A USB cable to sustain the ESP
Here are the pins you need to connect to each other: (ESP Pin -> MPU6050 Pin)
3v3 -> VCC (IMPORTANT! Not 5V) | GND -> GND | G22 -> SCL | G21 -> SDA
The other pins can be used for other specific stuff if curious check the datasheet.

BIN
Tutorial additions.docx Normal file

Binary file not shown.

View File

@ -12,3 +12,9 @@
platform = espressif32
board = upesy_wroom
framework = arduino
lib_deps = adafruit/Adafruit MPU6050@^2.2.6
; How to change bit-per-second rate for your project:
; Add right under [env:upesy_wroom]:
; "monitor_speed = x" (without the "")
; x == baudrate of selected choice, bsp: 115200, 8000, 300

96
src/Second_approach.txt Normal file
View File

@ -0,0 +1,96 @@
//--------------------------------------------------------------------------------------------------------------------------//
// This is the seconed way to use the MPU sensor:
//--------------------------------------------------------------------------------------------------------------------------//
// Include the library you want to use: //
#include "Wire.h"
// Define the address to the chip: //
#define MPU6050_ADDR 0x68 // Alternatively set AD0 to HIGH --> Address = 0x69
// Set up some variables //
int16_t _accX; // Acceleration
int16_t _accY;
int16_t _accZ;
int16_t _gyroX; // Gyro
int16_t _gyroY;
int16_t _gyroZ;
int16_t _tRaw; // Raw register values (accelaration, gyroscope, temperature)
// Another for specific use (in this case, convertion) //
char _result[7]; // Temporary variable used in convert function
// Prototype to function at the end //
char* toStr(int16_t _character);
void setup()
{
Serial.begin(115200); // Mind the Bit per second rate you selected here if blindly copying!
// Start service //
Wire.begin();
Wire.beginTransmission(MPU6050_ADDR);
// Set up register //
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // wake up!
Wire.endTransmission(true);
}
void loop()
{
// Begin exchange with the sensor //
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false); // The parameter indicates that the Arduino will send a restart.
// As a result, the connection is kept active.
Wire.requestFrom(MPU6050_ADDR, 14, true); // request a total of 7*2=14 registers
// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same int16_t variable //
_accX = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
_accY = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
_accZ = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
_tRaw = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
_gyroX = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
_gyroY = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
_gyroZ = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
// Printing the read in values in a String fromat //
Serial.print("AcX = ");
Serial.print(toStr(accX));
Serial.print(" | AcY = ");
Serial.print(toStr(accY));
Serial.print(" | AcZ = ");
Serial.print(toStr(accZ));
// from data sheet: //
Serial.print(" | tmp = "); Serial.print((tRaw + 12412.0) / 340.0);
Serial.print(" | GyX = "); Serial.print(toStr(gyroX));
Serial.print(" | GyY = "); Serial.print(toStr(gyroY));
Serial.print(" | GyZ = "); Serial.print(toStr(gyroZ));
Serial.println();
delay(1000); // NOTE! This is just for demonstration purposes.
// The function "delay({insert wanted value in ms})" stops your ENTIRE programm,
// not allowing the ESP to do ANYTHING until the delay is canceled after the listed milliseconds have passed.
// If possible, avoid using it at all.
}
// Function to convert the incoming values to String (for printing purposes) //
char* toStr(int16_t _character)
{
// Formats "int16_t" to "String" (Attention! Not "std::string", the two are)
// (two seperate classes and are not the same.)
sprintf(_result, "%6d", _character);
return _result;
}
// Note that all the values are purely raw. Further computing is required for them to actually be of use.
// This is a more watered down version as to how to do this, but let's be real, you really won't need to make everything
// harder if this delivers all you need.
// If there is interest on how to do this on an even lower and detailed level, talk to me and I'll try to explain.

View File

@ -1,50 +1,169 @@
//--------------------------------------------------------------------------------------------------------------------------//
// Handling Interrupts correctly
// Setting up the Adafruit_MPU6050 library for the accelerator chip
//--------------------------------------------------------------------------------------------------------------------------//
// What are Interrupts? //
// You can see them as certain parts of a programm reacting to a condition at ANY time and place your programm is currently in.
// You use them to time reads for example, or for signals, etc etc.
// Check the other guide in the .docx as to how to INSTALL the necessary library //
#include <Arduino.h>
// Must be included for most things! //
#include <Arduino.h> // Only include once of course.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// Create instance of the sensor library //
Adafruit_MPU6050 mpu;
void setup()
{
// Initializing the timer //
void IRAM_ATTR onTimer();
Serial.begin(115200); // For Serial communication, check if the right bit-per-second rate is selected!
// If you want to change said bit-per-second, look in the .ini file for information.
// How do you know the one selected does not work / there's something wrong in the .ini file?
// Check the terminal, if that is the case everything printed is Unicode gibberish.
hw_timer_t *My_timer = 0;
My_timer = timerBegin(0, 80, true);
timerAttachInterrupt(My_timer, &onTimer, true);
timerAlarmWrite(My_timer, 5000, true);
timerAlarmEnable(My_timer);
while(!Serial) delay(10); // Wait until the Serial communication is set up and initialized.
Serial.println("Starting...");
// This is crutial and has to be in the setup.
// You can change the frequency as to when it gets called (Check it out online), as well as the reason.
if(!mpu.begin()) // Exception for the case of the mpu not starting up
{
// If this is the case, check the wiring and whether or not the right pins are connected.
// Another issue that could lead to this is too little or too much current going through the chip.
Serial.println("Failed to find MPU6050 chip.");
while(1)
{
delay(10);
}
}
Serial.println("MPU6050 has been found.");
// You can attach a timer to a Pin for example as well.
// That would look something like this:
//
// pinMode(SWITCH_PIN, INPUT_PULLUP);
// attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), switchChanged, CHANGE);
// But it's not necessary if you're using Interrupts as a timer.
mpu.setAccelerometerRange(MPU6050_RANGE_8_G); // Set to the range of your choice for the accelerator values
// depending on the range you want your read in values to vary in.
Serial.println("Accelerator range set to: ");
switch(mpu.getAccelerometerRange()) // Check setting and print for insight.
{
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange())
{
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth())
{
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
delay(100);
// What is the difference between "void setup()" and "void loop()"? Why put all this in here? //
// The setup fuction is only initialized once, and though that's true for the entire lifetime of the programm,
// the started services continue to run in here on this threat, allowing the loop to be executed simultaniously.
// Mind, and that's IMPORTANT!!, that you should refer from using "while(1)" in this function as much as possible.
// (I'm aware there is one in here in this project, but only because it is handled correctly and carefully.)
}
// Put your variables here for testing, otherwise I'd recommend putting it in a class //
float _x_accel = 0; // x acceleration
float _y_accel = 0; // y acceleration
float _calc = 0; // for calculations
void loop()
{
// NOTICE! You might wonder why this looks different to the earlier commit.
// This is a better example to understand what's going on, run it to find out how to read in the meassuremnts.
// Get new sensor events with the readings //
sensors_event_t _a, _g, _temp;
mpu.getEvent(&_a, &_g, &_temp);
// "_a" == acceleration
// "_g" == gyroscope values
// "_temp" == temperature
// Reading acceleration //
Serial.print("Acceleration X: "); // Note that all the values are in "m/s^2" for further calculations!
Serial.print(_a.acceleration.x);
Serial.print(", Y: ");
Serial.print(_a.acceleration.y);
Serial.print(", Z: ");
Serial.print(_a.acceleration.z);
Serial.println(" m/s^2");
// You can of course use those values to calculate the position in 3D if so needed.
// For that, please read into it and try to write it yourself.
// Reading rotation from the gyroscope //
Serial.print("Rotation X: ");
Serial.print(_g.gyro.x);
Serial.print(", Y: ");
Serial.print(_g.gyro.y);
Serial.print(", Z: ");
Serial.print(_g.gyro.z);
Serial.println(" rad/s");
// Rotation in directions of x, y and z, as for acceleration as well as you can see.
// If you're getting confused as to what is what IRL, look at the top of
// the MPU sensor to see the small directions symboles for reorientation.
// Reading in temperature //'
Serial.print("Temperature: ");
Serial.print(_temp.temperature);
Serial.println(" degC");
// Mind that it's read in in Celcius, if needed for further calculations.
}
// Handling the function //
void IRAM_ATTR onTimer()
{
// This function is called when the interrupt has been triggered.
// Little side information //
// If you are wondering why there are two pins called "XDA" and "XCL", educate yourself on it, but you
// won't need those most likely.
// Keep your operations in here as SIMPLE as possible, and as quick as possible.
// DO NOT do heavy processing on this threat, or perform long lasting operations.
// ^ That includes any sort of printing or writing to a file for example.
// Also refrain from using while(1) AT ALL COSTS. Short lasting while loops are fine, but also make sure they don't take too long to break out again.
}
// Tecnically, you only need the library asset "Wire.h" for all of this and not the whole Adafruit library.
// But then again, that's a harder, and not exactly better way to use this sensor if not all too familiar with the
// syntax of C++, but if you'd want to, try it out!
// I've left a ".txt" file in this "src" folder that has the code tackling the second option.
// Try it out if you'd like, or go with the method described in here. Your choice.
// That is basically it for now regarding simple interrupts.
// It's useful regarding sending data only like every 5 minutes, or every 10 milliseconds for example. Use it carefully, and happy coding. :D
// And done! //
// Mind that everything in this loop repeats itself over and over, only stopping until the programm fails to execute or
// the current is cut. If you'd like to restart everything, press the little button titled "RST" or "BOOT" (if the first does nothing) on the ESP.