How MEMS Accelerometer Gyroscope Magnetometer Work & Arduino Tutorial



hello dan the dog here from how to mechatronics comm in this tutorial we will learn how the MEMS accelerometer gyroscope and magnetometer work and how to use them with your Dino board also with the processing development environment we will make some practical applications using the sensors first let's briefly explain how each of these micro electromechanical systems or MEMS sensor work we will start with the accelerometer it measures acceleration by measuring change in capacitance its microstructure looks something like this it has a mass attached to a screen which is confined to move along one direction and fixed outer plates so when an acceleration in the particular direction will be applied the mass will move and the capacitance between the plates and the mass will change this change in capacitance will be measured process and it will correspond to a particular acceleration value next is the gyroscope which measures angular rate using the Coriolis effect when a mass is moving in a particular direction with a particular velocity and when an external angular rate will be applied as shown with the green arrow a force will occur as shown with the blue arrow which will cause perpendicular displacement of the mass so similar to the accelerometer this displacement will cause change in capacitance which will be measured process and it will correspond to a particular angular rate the microstructure of the gyroscope looks something like this a mass that is constantly moving or oscillating and when an external angular rate will be applied a flexible part of the mass would move and make the perpendicular displacement okay now let's explain how the magnetometer works it measures the earth magnetic field by using Hall FA or magnet or resistive effect actually almost 90 percent of the sensors on the market use the Hall effect and here's how it works if we have a conductive plate like this and we set current to flow to it the electrons would flow straight from one to the other side of the plate now if we bring some magnetic field near the play we will disturb the straight flow and the electrons will deflect to one side of the plate and positive poles to the other side of the plate this means that if we put a mirror now between these two sides we will get some voltage which depends from the magnetic field strength and its direction the other ten percents of the sensors on the market use the magnet or resistive effect these sensors use materials that are sensitive to magnetic field usually composed of iron and nickel so when these materials are exposed to magnetic field they change their resistance okay now let's connect these scissors to the Arduino board and make some use of them as an example I will use the G Yad breakout board which has the following sensors a DXL 345 Q axis accelerometer LG g4 200 D 3-axis gyroscope MC 588 3 L 3 axis magnetometer and also a barometer and a thermometer which we won't use in this tutorial first let's hook up the board to the Arduino this board used the I Square C communication protocol which means that we can use all the sensors with just 2 wires so in order to make the communication between the Arduino and the sensors we need to know their unique device addresses and their internal register addresses for getting the data out of them these addresses can be found from the datasheet of the sensor and I have attached links to each of them on my website for more details how this communication works you can check my I square C communication tutorial ok now let's see the code for getting the data from the sensors we will start with the accelerometer first we need to include the arduino wire library and define the registers addresses of the sensor in the setup section we need to initiate the wire library and start the serial communication as we will use the serial monitor for showing the results also here we need to activate the sensor or enable the measurement by sending appropriate bye to the power control register and here's how we do that using the wire dot begin transmission function we leg two which sensor we will talk the three axis accelerometer in this case then using the wire dot write function we tale to which internal register we will talk after this we will send the appropriate by for enabling the measurement using the wire dot in transmission function we will end the transmission and that will transmit the data to the registers now in the loop section we need to read the data for each axis we will start with the x-axis so first we will select which registers we will talk the two x-axis internal registers in this case then using the wire dot request from function we will request the transmitted data or the two bytes from the two registers the wire dot available function will return the number of bytes available for retrieval and if that number match with our requested bytes in our case two bytes using the wired dot read function we will read the bytes from the two registers of the x axis the output data from the two registers is two's complement with x0 as the least significant byte and x1 as the most significant byte using this expressions here we can convert these bytes in to float values from minus one to plus one depending on the direction of the x axis relative to the earth acceleration or the gravity we will repeat this procedure for the two other axis and at the end we will print these values on the serial monitor here is how they appear okay now for getting the data from the gyroscope we will have a similar code as the previous one so first we have to define the register addresses and sign variables for the data in the setup section we have to wake up and put the sensor in normal mode using the control register one and also select the sensitivity of the sensor for this example I will select the 2000 DPS or degrees per second sensitivity mode in the loop section similar to the accelerometer we will read the data for the x y&z axis now the raw data has to be converted into angle values from the data sheet of the sensor we can see that for the 2000 DPS or degrees per second sensitivity mode corresponds a 70 mil it agrees per second or digit unit this means that we have to multiply the row output data by zero point zero seven in order to get the angular rate in degrees per second now if we multiply the angular rate by time it will give us the angle value so we need to calculate the time interval of each loop section and we can do that by using the moolies function at the top and the bottom of the loop section and we will store it value into this DT variable so for each executed loop we will calculate the angle and add it to the final angle value we will do the same for the two other axes and at the end we will print the results on the serial monitor here is how they appear the pen shows the x-axis of the sensor so if I rotate the sensor around that axis we can see the change of the angle on the serial monitor keep in mind that the gyro result are not stable because they drift as the time goes so in order to get accurate angles we have to combine it with the accelerometer here's an example of that combining the gyro and the accelerometer data as well as the complimentary filter for smoothing the results so the data from the sensors and the Arduino board are sent to the processing development and they are used for controlling the orientation of the 3d object you can find more details and the source codes for this example on my website what's left now is to see how to get the data from the magnetometer again we will use a similar technique to the previous one first we need to define the registers addresses and in the setup section set the sensor in continuous measurement mode in the loop section we will get the raw data for each axis with the same method as for the previous sensors now we need to convert the raw data into magnetic field value or gauss units from the data sheet of the sensor we can see that the default sensitivity mode is 0.92 milli Gauss per digit this means that we need to multiply the row data by zero point zero zero zero 92 in order to get the earth magnetic field in Gauss units at the end we will print the values on the serial monitor here I have marked the North Pole on the paper and pen shows the y-axis of the sensor now if I point the y-axis to the North Pole and fill the sensor around 45 degrees I will get the maximum value of around zero point six cows the tilt angle depends on your location for example if you are at the equator you don't have to tilt the sensor because the inclination at that location is zero or if you are at the North Pole you will have to tell the sensor 90 degrees here is a cool looking application of the sensor a digital compost made using the processing developing environment you can find more details and the source code of this example on my website thanks for watching and for more tutorials visit my official website how to mechatronics calm

36 Comments

  1. Victor Dominguez said:

    NICE VIDEO best explanation for advance data obtaining! Just a question, where Power_Register comes? Data sheet? what it means? thanks a lot!

    June 27, 2019
    Reply
  2. Vicky Dzulfikar said:

    sughhooooi

    June 27, 2019
    Reply
  3. evans yomu said:

    Can I measure angle of the 3 axis?

    June 27, 2019
    Reply
  4. Андрей Миронов said:

    Ландан из зы кэпитал оф грейт бритэн, браток! Акцент выдаёт капитально!

    June 27, 2019
    Reply
  5. Jayur Patel said:

    hey guys i need some help. anyone knows how to convert binary elctromagnetic wave into massages through which controller or ic or microprocessore .

    June 27, 2019
    Reply
  6. Somali unity said:

    How Gyroscope works? = 7:52

    June 27, 2019
    Reply
  7. William Burkhouse said:

    Nice video thanks

    June 27, 2019
    Reply
  8. Kuriocity said:

    I want to do the same in RPie

    June 27, 2019
    Reply
  9. Julio Gutierrez said:

    great video, simple and very useful.

    June 27, 2019
    Reply
  10. P. B. said:

    Springs is your love – acceleration your h8th

    June 27, 2019
    Reply
  11. P. B. said:

    God mode

    June 27, 2019
    Reply
  12. P. B. said:

    Squid Plus

    June 27, 2019
    Reply
  13. MekaciRobotic said:

    You have mentioned that we need to combine accelerometer values on gyro meter values to get best result for gyro; but how to do it ? thank you.

    June 27, 2019
    Reply
  14. Venkos pro said:

    Дружище, ты русскоговорящий по любому.

    June 27, 2019
    Reply
  15. SierraSlim1 said:

    Awesome job!

    June 27, 2019
    Reply
  16. SolidWork Tutorial said:

    thank you

    June 27, 2019
    Reply
  17. Diggy Dude said:

    Hence the old saying, "Drifty as a ten cent gyro."

    June 27, 2019
    Reply
  18. R1S8K LLTP said:

    I'm really lost between the three sensors! Are they similar or what?! Do they share similar functionality or they are completely different?

    I learned some of the HMC5883L and got the atan2(x,y) to get the heading which is I guess in radian. So I got the 360 degrees heading, but what does the z-axis do?

    And then what does the accelerometer has to offer other than the HMC5883L? And how's the gyroscope is different than the other two?

    June 27, 2019
    Reply
  19. ffoska said:

    My experience with magnetometers is, that they are pretty unreliable. Can't seem to get valid readings near motors or large metal objects.Any thoughts on improving that? Also it would be nice to have a more in dept video, about correcting the gyro drift.

    June 27, 2019
    Reply
  20. Karras Bastomi said:

    I'm reading the title wrong. I read MEME sensor. ROFL

    June 27, 2019
    Reply
  21. Taha Tartoussi said:

    Awesome explanation for everything! I followed you and I will always be following you!
    But I have one question: Did we use any kind of filter to reduce the noises? And if not, shouldn't we use one?

    June 27, 2019
    Reply
  22. Cindonator Mahluko said:

    Interesting Video, What's the name of the Sound Track?

    June 27, 2019
    Reply
  23. bexpi said:

    Interestingly enough I'm pretty sure the HMC5883L is a magneto-resistive sensor. Kinda cool to find this video right now as I'm working on using the gy-88 board for a robot.

    June 27, 2019
    Reply
  24. Chris Cain said:

    This is fascinating thank you so interesting

    June 27, 2019
    Reply
  25. Fernando Hood said:

    When there is no library adruino users struggle becuase a lot of stuff is abstracted.

    June 27, 2019
    Reply
  26. Energy Nut said:

    I need to capture the direction change of the x axis when it goes from rising to falling then when it goes from falling to rising. need it for a pendulum that triggers an event when it is at maximum potential or mass is weightless. Anybody know how I can do this. My sketches used on other types of sensors do not work with a gyro .

    June 27, 2019
    Reply
  27. Vijay Tikhe said:

    great video …..

    June 27, 2019
    Reply
  28. C-Dog Truffles said:

    I was just like to ask a simple question if I may?
    If the LHC has a higher magnetism that the Earth it's self how do we know the readings are correct? LHC is the greatest scientific machine humans have built. I'm not getting into any other point in that matter, would you be willing to assist each other in finding if this is actually true?..
    Thanks take care.

    June 27, 2019
    Reply
  29. Gonzalo Meade Aranda said:

    Could you tell me the name of the program you use to make the code?

    June 27, 2019
    Reply
  30. my one said:

    I wld like to ask how fast we can move the umm that blue box but still accurate? Thats matter of quality of sensor board or another arduino with faster cpu or what?

    June 27, 2019
    Reply
  31. Жавхаа Ганбаатар said:

    Holy shit how do u know this many things
    Im just curious how old are you and what is your degree of electronic?

    June 27, 2019
    Reply
  32. HiWiFi Drones said:

    Hi! Have anybody ever connect it to a Naze32 or any other Flight Controller? Please, send me the specs, ok? Thanks

    June 27, 2019
    Reply
  33. upendra chaudhari said:

    wow amazing !!

    June 27, 2019
    Reply
  34. Lou Costello said:

    Wow. First-rate tutorial!

    June 27, 2019
    Reply
  35. Khanh Trần Ngọc said:

    Tuyệt vời! Cảm ơn anh rất nhiều!!! Video giúp cho mình nhiều kiến thức! <3

    June 27, 2019
    Reply
  36. Mario Kim said:

    great great video, but i have a question can you help me?
    I am trying to put the arduino sensor in my room door and want to calculate the angular velocity of the door accordingly to the force applied by my arms or something else. Is this simply possible with this simple arduino sensor? is it the matter of coding to get the angular velocity or should i look for another sensor to get the values of it? any helps please?

    June 27, 2019
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *