MPU6050 with Netduino - Project Showcase - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

MPU6050 with Netduino

MPU6050 Sensor c# I2C

  • Please log in to reply
2 replies to this topic

#1 Heroduino

Heroduino

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationGermany

Posted 05 July 2013 - 05:29 PM

Hallo Netduino Community,

 

I think it is time to post my example here in the Netduino Forum. My first post was in German, but I see many webcalls come from around the world and the google translater is not quite good at understanding my descriptions. Well, my English is very rusty. So, I am sorry for any mistakes I might (and will) make.

 

Last year, I bought the sensor MPU-6050 and at first I couldn't run it. I saw the code library in C++ and read how to function the sensor. It was not very helpful for me. But it helped me to understand the code from Jeff Rowbergs Librarys. The code is awesome, but unfortunately not for a quick understanding.

 

First half of the year, I succeeded to run my prorgam with the sensor and posted it on my blog for sharing. Later I wrote a short Version for arduino. It fitted on a page side ;)

 

Ok, what does my program do.

public class MPU6050Sensor{    // I²C Class for connecting to the sensor    private I2CDevice _i2CDevice;    // The Contructor initialized the connecting and the sensor    public MPU6050Sensor()    {        // I²C connecting:        // The first HEX value 0x68 is the address from the sensor and        // the value 100 is for the transmission speed.        // Well you can use 400kHz,        // if you have a clean cable connection.        _i2CDevice = new I2CDevice(new I2CDevice.Configuration(0x68, 100));        // Sensor init        // Sleep und Reset execute:        // The HEX value 0x6B is for the Power Management 1        // The secound Hex value includes the 'Bit' for reset.        StatusMessage(Write(new byte[] { 0x6B, 0x80 }));        Thread.Sleep(10);        // Stop sleep and Clock setup:        // The HEX value 0x6B is for the Power Management 1        // The secound Hex value 0x00 set the 'Clock Select'        // to 'Internal 8MHz oscillator'        // If you don't need the temperature sens,        // you can set the HEX value 0x08.        StatusMessage(Write(new byte[] { 0x6B, 0x00 }));        // Configuration setup:        // This Configuration activates the Low Pass Filter (DLPF).        // Setting => Acc=5Hz, Delay=19.0ms, Gyro=5Hz, Delay=18.6ms, Fs=1kHz        StatusMessage(Write(new byte[] { 0x1A, 0x06 }));    }    // Send a byte array to the sensor    private int Write(byte[] buffer)    {        I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[]        {            I2CDevice.CreateWriteTransaction(buffer)        };        return _i2CDevice.Execute(transactions, 1000);    }    // Read a byte array the sensor results    private int Read(byte[] buffer)    {        I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[]        {            I2CDevice.CreateReadTransaction(buffer)        };        return _i2CDevice.Execute(transactions, 1000);    }    // Give a status message of result    private void StatusMessage(int result)    {        if (result == 0)        {            Debug.Print("Status: Fehler beim Senden oder Empfangen");        }        else        {            Debug.Print("Status: OK");        }    }    // Call the object SensorData with the sensor result    public SensorData GetSensorData()    {        // Byte Array for reading        byte[] buffer = new byte[14];        buffer[0] = 0x3B;        // The HEX Value set the first byte        // of the acceleration axis X        Write(new byte[] { 0x3B });        // Read from the sensor        Read(buffer);        // Give the byte array result to the object,         // and convert it to usable values        return new SensorData(buffer);    }}

The last method shows the SensorData object class and how it Looks.

public class SensorData{    public uint Acceleration_X = 0;    public uint Acceleration_Y = 0;    public uint Acceleration_Z = 0;    public uint Temperatur = 0;    public uint Gyroscope_X = 0;    public uint Gyroscope_Y = 0;    public uint Gyroscope_Z = 0;    public SensorData(byte[] buffer)    {        // Result of the acceleration axis        Acceleration_X = (((uint)buffer[0]) << 8) | buffer[1];        Acceleration_Y = (((uint)buffer[2]) << 8) | buffer[3];        Acceleration_Z = (((uint)buffer[4]) << 8) | buffer[5];        // Restult of temperature        Temperatur = (((uint)buffer[6]) << 8) | buffer[7];        // Result of the gyroscope axis        Gyroscope_X = (((uint)buffer[8]) << 8) | buffer[9];        Gyroscope_Y = (((uint)buffer[10]) << 8) | buffer[11];        Gyroscope_Z = (((uint)buffer[12]) << 8) | buffer[13];    }}

The next part is to connect the hardware, before you can deploy. Netduino to Sensor 3.3V >> VCC GND  >> GND AnalogIn 4 >> SDA AnalogIn 5 >> SCL

At last, for the Main() Methode

public static void Main(){    MPU6050Sensor mpu = new MPU6050Sensor();    SensorData data = mpu.GetSensorData();    while (true)    {        data = mpu.GetSensorData();        Debug.Print(            "Acc X: " + data.Acceleration_X + " " +            "Acc Y: " + data.Acceleration_Y + " " +            "Acc Z: " + data.Acceleration_Z + " " +            "Gyro X: " + data.Gyroscope_X + " " +            "Gyro Y: " + data.Gyroscope_Y + " " +            "Gyro X: " + data.Gyroscope_Z + " " +            "Temperatur: " + data.Temperatur);    }}

Well, that is all and I hpoe you have fun :)

Greetings from Germany, have a nice weekend!!

 

 

 

Edit:

My reposting on my blog (Language German)

http://meineweltinme...en-mit-dem.html

Attached Files


Edited by Heroduino, 07 July 2013 - 07:45 PM.

  • alejamp likes this

#2 alejamp

alejamp

    New Member

  • Members
  • Pip
  • 1 posts

Posted 31 December 2013 - 12:00 PM

Heroduino, thank you for share. I was stuck using third party drivers for MPU-6050, your code works fine, it gave me a solid ground for debugging more complicated MPU-60x0 drivers. 



#3 gt5193

gt5193

    New Member

  • Members
  • Pip
  • 3 posts

Posted 22 September 2014 - 02:32 PM

Hello,

 

I ran the example above and everything was working great. I was going through the code just to understand it better. In the Config for the I2CDevice I changed the clock rate to 1 or 10...something like that. I ran it and it didn't work so I changed it back to the default 100. After that everything stopped working. Now I am getting nothing but zeros from the MPU-6050. My question is did I fry my gyroscope? Thanks!







Also tagged with one or more of these keywords: MPU6050, Sensor, c#, I2C

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.