{"id":682,"date":"2025-10-18T10:24:15","date_gmt":"2025-10-18T08:24:15","guid":{"rendered":"https:\/\/avlos.gr\/wpress\/?p=682"},"modified":"2025-10-18T10:24:15","modified_gmt":"2025-10-18T08:24:15","slug":"motion-sensor","status":"publish","type":"post","link":"https:\/\/avlos.gr\/wpress\/en\/motion-sensor\/","title":{"rendered":"Motion Sensor"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Motion Sensor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Motion is the constant change of the position of an object in space.&nbsp;Here I will introduce you in simple words the 9-axis sensor that monitors the change of its position in the three-dimensional space, the MPU 9250.<br>Then I will present you a simple construction with the MPU 9250 sensor based on the&nbsp;<a href=\"https:\/\/www.youtube.com\/watch?v=BCxv9VGqhQk\" target=\"_blank\" rel=\"noreferrer noopener\">construction&nbsp;<\/a>I made with the servomotors.<br>The position sensor can be used in aircraft (various drones and quadcopters), smartphones and robotics, in various operations and in devices related to 3D control and motion recognition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The MPU-9250 is a System in Package (SiP) that combines two chips: the three-axis MPU-6500 gyroscope, which is also a three-axis accelerometer, and the AK8963, which is a three-axis magnetometer.&nbsp;So the MPU 9250 performs the functions of several sensors at the same time: it is a gyroscope, accelerometer and magnetometer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us dwell a little on each of them.<br>The gyroscope is a sensor that responds to changes in the orientation angles in space.&nbsp;In the drone, it is used to stabilize the position of the device in the air and protect it from the wind.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The accelerometer compares the acceleration projection of an object to the acceleration of gravity and is able to measure the linear velocity of the object and, together with the gyroscope, its position in space.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A magnetometer is a device for measuring the intensity of the nearest magnetic field acting on an object (the name of the sensor speaks for itself).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">According to some sources, the MPU 9250 is the smallest nine-axis sensor in the world.&nbsp;This indicates the high performance of the chip, which is provided using CMOS MEMS (&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Microelectromechanical_systems\" target=\"_blank\" rel=\"noreferrer noopener\">microelectromechanical systems<\/a>&nbsp;) technology.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>The case of the unit consists of two smaller crystals, one of which is responsible for the gyroscope and accelerometer and the other for the magnetometer.&nbsp;The data from these crystals are processed by the integrated signal processor DMP (Data Management Platform) using Motion Fusion algorithms and transmitted via the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/I%C2%B2C\" target=\"_blank\" rel=\"noreferrer noopener\">I&nbsp;<sup>2<\/sup>&nbsp;C<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Serial_Peripheral_Interface\" target=\"_blank\" rel=\"noreferrer noopener\">SPI interfaces<\/a>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to high performance, the unit is quite popular among electrical engineers and those who love remote control devices, while also having low power consumption and cost.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"383\" height=\"637\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/mpu_9250.jpg\" alt=\"MPU 9250\" class=\"wp-image-678\" style=\"width:222px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/mpu_9250.jpg 383w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/mpu_9250-180x300.jpg 180w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><figcaption class=\"wp-element-caption\">MPU 9250<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Principle of operation of the accelerometer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An accelerometer measures the rate at which the speed of an object changes over time, also known as acceleration.&nbsp;With an accelerometer, you can also understand the angle of inclination of the sensor relative to the ground.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An accelerometer has tiny crystals that are pressurized when vibrations occur.&nbsp;From this pressure, a voltage is generated that gives an indication of any acceleration.&nbsp;The unit of measurement for acceleration is the measure per second squared (m \/ s ^ 2).&nbsp;But as the accelerometer sensors express measurements in &#8220;g&#8221;, a &#8220;g&#8221; is the value of the earth&#8217;s gravity equal to 9.8 meters per second squared.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"485\" height=\"415\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/Acceler_1.jpg\" alt=\"Accelerometer\" class=\"wp-image-679\" style=\"width:299px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/Acceler_1.jpg 485w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/Acceler_1-300x257.jpg 300w\" sizes=\"auto, (max-width: 485px) 100vw, 485px\" \/><figcaption class=\"wp-element-caption\">Accelerometer<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For example, in a 3-axis accelerometer such as the MPU9250 accelerometer sensor, when mounted horizontally with the Z axis up, the output of the sensor Z axis = 1 g or 9.8m \/ s&nbsp;<sup>2<\/sup>&nbsp;while X and Y = 0 This is due to the force of gravity perpendicular to the X and Y axes and thus does not affect them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From this data, with simple trigonometric mathematics, one can calculate the angle of the sensor and consequently the construction on which the sensor is attached.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Principle of gyroscope operation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The gyroscope measures the speed of rotation or the rate of change of angular position over time.&nbsp;Its operation is based on<a href=\"https:\/\/en.wikipedia.org\/wiki\/Coriolis_force\" target=\"_blank\" rel=\"noreferrer noopener\">&nbsp;the Coriolis effect<\/a><a href=\"https:\/\/translate.google.com\/website?sl=el&amp;tl=en&amp;nui=1&amp;u=https:\/\/el.wikipedia.org\/wiki\/%CE%94%CF%8D%CE%BD%CE%B1%CE%BC%CE%B7_%CE%9A%CE%BF%CF%81%CE%B9%CF%8C%CE%BB%CE%B9%CF%82\" target=\"_blank\" rel=\"noreferrer noopener\">.<\/a><br>When a mass moves in a certain direction at a certain speed and an external angular rotation rate is applied, then a force will be detected which the sensor will detect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The gyroscope outputs are in degrees per second<br>If we know the initial angle of the IMU, we can add the value given to us by the gyroscope to know the new angle at any time.&nbsp;Suppose we start the IMU from 0 \u00b0.&nbsp;If the instrument gives us a measurement every second and marks 3 on the X axis, we will have the angle with this simple formula:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AngleX = PreviousAngleX + GyroDataX * elapsedTime<br>AngleY \u200b\u200b= PreviousAngleY \u200b\u200b+ GyroDataY * elapsedTime<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Where elapsedTime is the time elapsed each time this formula is calculated within the loop, PreviousAngle is the angle calculated the last time this formula was called and GyroData is the y or X angle reading of the gyroscope.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The magnetometer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The MPU-9250 chip includes the AK8963 which is the magnetometer.&nbsp;This may know the orientation relative to the magnetic north, similar to how a hand compass works.&nbsp;Built-in 16-bit ADCs simultaneously sample the 3 drive axles (X, Y, Z).<br>The magnetometer can be calibrated automatically using the magcalMPU9250 function (float * dest1, float * dest2) and calibrates the magnetometer as you move the sensor in figure eight.&nbsp;Saves the maximum and minimum readings and takes the average.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Construction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The construction is a continuation of the previous one I did with the&nbsp;<a href=\"https:\/\/www.youtube.com\/watch?v=BCxv9VGqhQk\" target=\"_blank\" rel=\"noreferrer noopener\">two servomotors&nbsp;.<\/a>&nbsp;To this we will add the MPU-9250 sensor.&nbsp;The most important thing for the construction is the software that we will write so that the movement in the servomotors is now controlled by the sensor and not by the potentiometers.<br>The following figure shows the connection circuit of the sensor with the Arduino UNO.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"580\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/MPU9250_2.jpg\" alt=\"Connecting the MPU9250\" class=\"wp-image-680\" style=\"width:349px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/MPU9250_2.jpg 576w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/MPU9250_2-298x300.jpg 298w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/MPU9250_2-150x150.jpg 150w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><figcaption class=\"wp-element-caption\">Connecting the MPU9250<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The connection is made with the I2C (Inter-Integrated Circuit) bus, a communication protocol with two conductors that the sensor has.<br>For the sake of simplicity I will use three of the nine axes of the sensor.&nbsp;I will only use the accelerometer for the x, y and z axes.&nbsp;The drive will be in two levels perpendicular to each other and will be provided by the two servomotors.&nbsp;As you can see on the sensor board the manufacturer has marked the axes.&nbsp;The axes are parallel to the planes of motion, each with a plane of motion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The purpose of the construction is to keep the sensor in a horizontal plane as well as to turn the support level of the construction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The sensor is mounted on a stand that is horizontal at rest.&nbsp;We place it so that it is parallel to the support base.&nbsp;Then as I rotate the stand I will notice how the servomotors bring the sensor to a horizontal position.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From the basket with my grandchildren&#8217;s toys I took a smurf, Spirtoulis, and put it on the platform with the sensor.&nbsp;Spirtoulis&#8217; upright posture immediately indicates that the sensor is horizontal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The photo below shows the construction and the video I give you at the end of the article shows the operation of the construction.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"616\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/servo_9.jpg\" alt=\"Construction\" class=\"wp-image-688\" style=\"width:204px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/servo_9.jpg 378w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/servo_9-184x300.jpg 184w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><figcaption class=\"wp-element-caption\">Construction<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Programming<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><br>My advice is that the libraries you use for the Arduino IDE are in the folder of the project you are working on.&nbsp;The library I used for the MPU9250 is the Bolder_Flight_Systems_MPU9250 which if you do not have it installed is easy to download from the internet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then I quote the code I wrote which you can download from\u00a0<a href=\"https:\/\/avlos.gr\/wpress\/download\/servo_mpu9250\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\/\/Author : Manolis Aristovoulidis\n\/\/This program uses an MPU-9250 and two servo motors\n\/\/with the program running the servo motors always keep the sensor in a\n\/\/horizontal position\n\n\n#include &lt;Wire.h&gt;\n#include &lt;MPU9250.h&gt;\n#include &lt;Servo.h&gt;\n\n\nServo servo_x;  \/\/ creating a servo object to control the servo\nServo servo_y; \n\nint val_x = 95;     \/\/ variable for the movement angle of servo_x\nint val_y = 100;    \/\/ variable for the movement angle of servo_y\n\n \/\/ an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68\n MPU9250 IMU(Wire,0x68);\n \n\nint status;\n\n \/\/Acc Variables\n\nfloat rad_to_deg = 180\/3.141592654;           \/\/This value is for pasing from radians to degrees values\nfloat Acc_rawX, Acc_rawY, Acc_rawZ;           \/\/Here we store the raw data read \nfloat Acc_angle_x, Acc_angle_y;               \/\/Here we store the angle value obtained with Acc data\n\nfloat Total_angle_x, Total_angle_y;           \/\/Here we store the final total angle\n\nint x_tilt = 0;\nint y_tilt = 0;\nint i;\n\n\nvoid setup() {\n  \/\/ serial to display data\n  Serial.begin(115200);\n  while(!Serial) {}\n\n  servo_x.attach(4);    \/\/attaches the servo to pin 4 of the servo object\n  servo_y.attach(5);    \/\/attaches the servo to pin 5 of the servo object\n  \n  \/\/rotation of servo motors\n  servo_x.write(val_x);   \/\/After experimentation this is the\n  servo_y.write(val_y);   \/horizontal position for the MPU9250 base\n\n  delay(1000);\n\n  \/\/ start communication with IMU\n  status = IMU.begin();\n  if (status &lt; 0) {\n    Serial.println(\"IMU initialization unsuccessful\");\n    Serial.println(\"Check IMU wiring or try cycling power\");\n    Serial.print(\"Status: \");\n    Serial.println(status);\n    while(1) {}\n  }\n  \/* Default values of MPU9250\n   * accel range to 16G as default \n   * gyro range to 2000DPS as default\n   * bandwidth to 184Hz as default\n   * sample rate divider to 0 as default\n   *\n   *\/\n   \/\/ \u03b1\u03bd\u03b1\u03bc\u03bf\u03bd\u03ae \u03b3\u03b9\u03b1 \u03c4\u03bf calibration \u03c0\u03bf\u03c5 \u03c0\u03c1\u03bf\u03ba\u03b1\u03bb\u03b5\u03b9 \u03b7 IMU.begin()\n  delay(3000);\n}\n\nvoid loop() {\n   \n  IMU.readSensor();\n  Acc_rawX = IMU.getAccelX_mss();\n  Acc_rawY = IMU.getAccelY_mss();\n  Acc_rawZ = IMU.getAccelZ_mss();\n  \n  \n  \/*---Y---*\/\n  Acc_angle_x = (atan((Acc_rawY)\/sqrt(pow((Acc_rawX),2) + pow((Acc_rawZ),2)))*rad_to_deg) ;\n  \/*---Y---*\/\n  Acc_angle_y = (atan(-1*(Acc_rawX)\/sqrt(pow((Acc_rawY),2) + pow((Acc_rawZ),2)))*rad_to_deg) ;  \n\n  \n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Tilt angle and filter \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n\n  Total_angle_x = 0.94 *Total_angle_x  + 0.06*Acc_angle_x;\n  Total_angle_y = 0.94 *Total_angle_y  + 0.06*Acc_angle_y;\n  \n  x_tilt = (int)Total_angle_x ;\n  y_tilt = (int)Total_angle_y ;\n\n\/\/********************* Calculating return movement on the X axis **************************************\n  if(x_tilt &gt; 1){\n    val_x--;\n  }\n  if(x_tilt &lt; -1){\n    val_x++;\n  }\n  if(val_x &gt; 180){val_x = 180;}\n  if(val_x &lt; 0){val_x = 0;}\n\/\/********************* Calculating return movement on the Y axis ****************************************\n  if(y_tilt &gt; 1){\n    val_y++;\n  }\n  if(y_tilt &lt; -1){\n    val_y--;\n  }\n  if(val_y &gt; 180){val_y = 180;}\n  if(val_y &lt; 0){val_y = 0;}\n\n\/\/******************* Servo motor movement at an angle between 0-180 degrees******************************************\n\n  servo_x.write(val_x);   \/\/ sets the servo position to the scale value\n  servo_y.write(val_y);   \/\/ sets the servo position to the scale value\n\n  delay(50);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By using two servomotors, the platform that supports the MPU9250 motion sensor is always kept in a horizontal position.&nbsp;No matter how I turn the base on which the whole mechanism rests, the platform returns to a horizontal position.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the image below you can see the values \u200b\u200bgiven by the accelerometer of the sensor while moving, while trying to balance the value to zero for the x-axis.&nbsp;The red line shows the sensor values \u200b\u200band the green line shows the values \u200b\u200bgiven by the smoothing filter on line 166 of the program and which values \u200b\u200bdrive the servomotor x.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"633\" height=\"345\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/servo_10.jpg\" alt=\"Sensor value chart\" class=\"wp-image-693\" style=\"width:339px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/servo_10.jpg 633w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/09\/servo_10-300x164.jpg 300w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><figcaption class=\"wp-element-caption\">Sensor value chart<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Construction in operation is shown\u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=uT_tmZekWdc\" target=\"_blank\" rel=\"noreferrer noopener\">here\u00a0<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Motion Sensor Motion is the constant change of the position of an object in space.&nbsp;Here I will introduce you in simple words the 9-axis sensor that monitors the change of its position in the three-dimensional space, the MPU 9250.Then I&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,75],"tags":[73],"class_list":["post-682","post","type-post","status-publish","format-standard","hentry","category-home","category-robotics","tag-robotics"],"_links":{"self":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":10,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"predecessor-version":[{"id":695,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/682\/revisions\/695"}],"wp:attachment":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}