Finalized

This commit is contained in:
Kosmo Obermayer 2024-04-11 13:01:50 +02:00
parent d5cc9cdf9c
commit 1b3ee52ad2
5 changed files with 183 additions and 21 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.

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

@ -114,28 +114,56 @@ float _calc = 0; // for calculations
void loop()
{
// Save vals if you want to send them: //
sensors_event_t _a;
sensors_event_t _g;
sensors_event_t _temp; // Used as placeholder for now in the next function, even if you don't need temperature values.
// 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.
mpu.getEvent(&_a, &_g, &_temp); // Reads in your values.
// 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
_x_accel = _a.acceleration.x; // acceleration in x.
_y_accel = _a.acceleration.y; // acceleration in y.
// 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.
// Basic example for use //
_calc = _y_accel * 12.5; // Calculate postion if you want to.
if(_calc > 100) // reduce values if you only want values from (-100) - 100 for example
{
_calc = 100;
}
else if(_calc < (-100))
{
_calc = (-100);
}
// 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.
// 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. //
}
// 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.
}
// 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.
// 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.
// 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.