Arduino Nano: Accelerometer Gyroscope Compass MPU9250 I2C Sensor With Visuino

30,754

47

39

Posted

Introduction: Arduino Nano: Accelerometer Gyroscope Compass MPU9250 I2C Sensor With Visuino

MPU9250 is one of the most advanced combined Accelerometer, Gyroscope and Compass small size sensors currently available. They have many advanced features, including low pass filtering, motion detection, and even a programmable specialized processor. Having nearly 130 registers however, with many settings, they are also very difficult to work with from code.

Couple of weeks ago GearBest were nice enough to donate a MPU9250 module to sponsor adding support for it in Visuino. It took 2 weeks of hard work, but at the end I not only had the support for MPU9250 implemented, but I also added Acceleration To Angle converter, Complementary(First and Second order), and Kalman filters that can be used with it to improve precision.

This is the first Instructable on the new MPU9250 support in Visuino, and it shows how easy it is to use it with Visuino. In the following Instructables I will show you how you can use the Acceleration To Angle converter, the Complementary and Kalman filters and get really good results from your sensor module.

Step 1: Components

  1. One Arduino compatible board (I use Arduino Nano, because I have one, but any other will be just fine)
  2. One MPU9250 Sensor Module (in my case generously donated by GearBest)
  3. 4 Female-Female jumper wires

Step 2: Connect the MPU9250 Accelerometer Gyroscope Compass to Arduino

  1. Connect 5V VCC Power(Red wire), Ground(Black wire), SDA(Green wire), and SCL(Yellow wire), to the MPU9250 Module (Picture 1)
  2. Connect the other end of the Ground wire(Black wire) to Ground pin of the Arduino board (Picture 2)
  3. Connect the other end of the 5V VCC Power wire(Red wire) to the 5V power pin of the Arduino board (Picture 2)
  4. Connect the other end of the SDA wire(Green wire) to SDA/Analog pin 4 of the Arduino Nano board (Picture 2)
  5. Connect the other end of the SCL wire(Yellow wire) to SCL/Analog pin 5 of the Arduino Nano board (Picture 2)
  6. Picture 3 shows where are the Ground, 5V Power, SDA/Analog pin 4, and SCL/Analog pin 5, pins of the Arduino Nano

Step 3: Start Visuino, and Select the Arduino Board Type

To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/.

Make sure that you install 1.6.7 or higher, otherwise this Instructable will not work!

The Visuino: https://www.visuino.com also needs to be installed.

  1. Start Visuino as shown in the first picture
  2. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino
  3. When the dialog appears, select Arduino Nano as shown in Picture 2

Step 4: In Visuino: Add and Connect MPU9250 Component

  1. Type "mpu" in the Filter box of the Component Toolbox then select the "Accelerometer Gyroscope Compass MPU9250 I2C" component (Picture 1), and drop it in the design area (Picture 2)
  2. Connect the "Out" pin of the AccelerometerGyroscopeCompass1 component to the to the "In" pin of the I2C channel of the Arduino component (Picture 3)

Step 5: In Visuino: Add and Connect Packet Component

To send all the channels data over serial port from Arduino we can use the Packet component to packet the channels together, and display them in the Scope and Gauges in Visuino:

  1. Type "pack" in the Filter box of the Component Toolbox then select the "Packet" component (Picture 1), and drop it in the design area
  2. Connect the "Out" output pin of the Packet1 component to the "In" input pin of the "Serial[ 0 ]" channel of the "Arduino"component (Picture 2)

Step 6: In Visuino: Add 7 Binary Analog Elements to the Packet Component and Specify Their Names

  1. Click on the "Tools" button of the Packet1 component (Picture 1)
  2. In the "Elements" editor select the “Binary Analog” element, and then click on the "+" button (Picture 2) to add Analog element
  3. In the Object Inspector set the "Name" property of the Analog Element to "Compass(X)" (Picture 3)
  4. In the "Elements" editor select the “Binary Analog” element on the right, and then click on the "+" button on the left to add another Analog element
  5. In the Object Inspector set the "Name" property of the new Analog Element to "Compass(Y)" (Picture 4)
  6. In the "Elements" editor select the “Binary Analog” element on the right, and then click on the "+" button on the left to add another Analog element
  7. In the Object Inspector set the "Name" property of the new Analog Element to "Compass(Z)" (Picture 5)
  8. Repeat the same steps to add 7 more Binary Analog elements named "Accelerometer(X)", "Accelerometer(Y)", "Accelerometer(Z)", "Gyroscope(X)", "Gyroscope(Y)", "Gyroscope(Z)" and "Thermometer" (Picture 6)

Step 7: In Visuino: Configure the Visualization of the Packet Element for the Thermometer

The Visuino by default can display the analog elements from the packet component in gauges. It is nice however to display the temperature in Thermometer. Visuino allows customization of the way the analog elements are displayed.

  1. In the Elements editor, select the last Analog element named "Thermometer" (Picture 1)
  2. In the Object Inspector select the "Instrument" property and click on the "Arrow Down" button next to its value (Picture 1)
  3. From the Drop Down box select "Thermometer" (Picture 2)
  4. In the Object Inspector expand the "Instrument" property, then the "Scale" sub-property (Picture 3)
  5. In the Object Inspector set the value of the "Auto" sub-property of the "Scale" to False (Picture 3) This will disable the auto scaling for the thermometer
  6. In the Object Inspector set the "Max" sub-property of the "Scale" to 100 (Picture 4) This will configure the thermometer to have a scale from 0 to 100

Step 8: In Visuino: Specify Unique Packet Header

To make sure that Visuino will find the starting point of the packet, we need to have a unique header. The Packet component uses special algorithm to ensure that the header marker does not appear in the data.

  1. Select the Packet1 component (Picture 1)
  2. In the Object Inspector expand the "Head Marker"property (Picture 1)
  3. In the Object Inspector click on the "..." button (Picture 1)
  4. In the Bytes editor type some numbers, as example 55 55 (Picture 2)
  5. Click on the OK button to confirm and close the editor

Step 9: In Visuino: Connect the MPU9250 Component to the Elements of the Packet Component

  1. Click in the "Out" box containing the pins of the "Compass" of the AccelerometerGyroscopeCompass1 component to start connecting all the Out pins at once (Picture 1)
  2. Move the mouse over the "In" pin of the "Elements.Compass(X)" element of the Packet1 component. The Visuino will automatically spread the wires so they will connect correctly to the rest of the pins (Picture 1)
  3. Click in the "Out" box containing the pins of the "Accelerometer" of the AccelerometerGyroscopeCompass1 component to start connecting all the Out pins at once (Picture 2)
  4. Move the mouse over the "In" pin of the "Elements.Accelerometer(X)" element of the Packet1 component. The Visuino will automatically spread the wires so they will connect correctly to the rest of the pins (Picture 2)
  5. Click in the "Out" box containing the pins of the "Gyroscope" of the AccelerometerGyroscopeCompass1 component to start connecting all the Out pins at once (Picture 3)
  6. Move the mouse over the "In" pin of the "Elements.Gyroscope(X)" element of the Packet1 component. The Visuino will automatically spread the wires so they will connect correctly to the rest of the pins (Picture 3)
  7. Connect the "Out" pin of the "Thermometer" of the AccelerometerGyroscopeCompass1 component to the "In" pin of the "Elements.Thermometer" input pin of the Packet1 component (Picture 4)

Step 10: Generate, Compile, and Upload the Arduino Code

  1. In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
  2. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)

Step 11: And Play...

  1. In Visuino select the ComPort, and then click on the "Format:" drop down box, and select Packet1(Picture 1)
  2. Click on the "Connect" button(Picture 1)
  3. If you select the "Scope" tab, you will see the the Scope plotting the X,Y,Z values from the Accelerometer, Gyroscope, and Compass, as well as the Temperature over time(Picture 2)
  4. If you select the "Instruments" tab, you will see the Thermometer and the Gauges showing the same information (Picture 3)

You can see the connected and running MPU9250 Accelerometer, Gyroscope and Compass sensor on Picture 4.

Congratulations! You have created a MPU9250 Accelerometer, Gyroscope and Compass measuring project in Arduino, with Visual Instrumentation.

On Picture 5 you can see the complete Visuino diagram.

Also attached is the Visuino project, that I created for this Instructable. You can download and open it in Visuino: https://www.visuino.com

Share

Recommendations

  • Microcontroller Contest

    Microcontroller Contest
  • Science of Cooking

    Science of Cooking
  • Pocket-Sized Contest

    Pocket-Sized Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

33 Comments

Hello BoianM,

I have the exact same problem as UmerHasan, I have tested with other Arduino sketches and the sensor works fine, but no output from com port using the visuino generated sketch. Did you find a solution ?

I have downloaded your code and uploaded to my arduino but when I connect using visuino it connects without error but doesn't show anything. The green and red lights are both off. what could be the problem. My sensor is ok because another program is running fine.

You will see some garbage symbols arriving in the Serial terminal, but at least you will know that it is sending data. I will try to contact you directly to give you my Skype so we can investigate ;-)

Can you connect using the Arduino serial terminal?

and also what about calibration? none of the sensors here are calibrated, what shall we do about this?

is it possible to save the data or even show it in serial.print in arduino for example

can we somehow use the dmp and extract the pedometer information too?

Yes, again, I have made number of tutorials some showing how you can record the data on MicroSD as example. Visuino also has the option to log the serial communication to file for later processing. Serial print is shown in most of my tutorials ;-)

Thank you for this thorough explanation, i really really appreciate your work. btw i had 2 questions:
1)what shall we do if we want to use SPI protocol(if possible)?

2)how can we read another sensor beside this module and show the results(for example a force sensor?)

Please take a look at my other tutorials. With Visuino you can add as many sensors as you want, and run simultaneously, until you run out of pins, get address conflict on I2C, or run out of memory ;-)

What is the difference between the mpu 6050 and the mpu9250 ?

MPU 9250 has an MPU 6500 in it instead of a MPU 6050.