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

Gyroscope Module L3G4200D help


  • Please log in to reply
4 replies to this topic

#1 jsnmc

jsnmc

    New Member

  • Members
  • Pip
  • 7 posts

Posted 22 December 2011 - 07:22 AM

Hello, I have been following for a while, but never posted - great site btw!

This is the first time that I have attempted I2C. I am trying to get a Parallax gyro ( L3G4200D ) working with my Netduino and I don't seem to be getting good numbers for the x/y/z ( I get initial numbers and they never change with movement, nor do they look correct).

I lifted some of the I2C code from LoveElectronics.Sensors.Accelerometers. I tried to use this: "Arduino example with the L3G4200D" and this: "Parallax kickstart"as other points of reference.

I have posted a very simple version of my code below. I don't feel comfortable with what I am passing to the Configuration constructor, but I am not sure.

Any help here would be appreciated.


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using LoveElectronics.Sensors.Accelerometers;
using LoveElectronics.Resources;


namespace NetduinoApplication1
{
    public class gyroTest : I2CDevice
    {
        private int m_Timeout = 1000;
        // int Addr = 105; // 0x69 Adress for L3G4200D
        private ushort m_Address;

        public gyroTest()
            : this(0x69)
        { }

        public gyroTest(ushort address)
            : base(new Configuration(address, 100))
        {
            m_Address = address;
        }

        public byte[] Read(byte memoryAddress, int responseLength = 1)
        {
            var buffer = new byte[responseLength];
            I2CDevice.I2CTransaction[] transaction;
            transaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(new byte[] { memoryAddress }),
                I2CDevice.CreateReadTransaction(buffer)
            };

            int result = Execute(transaction, m_Timeout);
            return buffer;
        }

        public void Write(byte memoryAddress, byte value)
        {
            I2CTransaction[] transaction;
            transaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(new byte[] { memoryAddress, value })
            };
            int result = Execute(transaction, m_Timeout);
        }
    }
    public class Program
    {

        public static void Main()
        {
            gyroTest t = new gyroTest();
            t.Write(0x1F, 0x20);   // Turn on XYZ axes
            t.Write(0x08, 0x22);   // control 
            t.Write(0x80, 0x23);   // Scale to 500 deg/sec            
            Thread.Sleep(1000);

            int timerPeriod = 50;
            Timer timer = new Timer(delegate(object o)
            {

                byte[] MSBx = t.Read(0x29, 1);
                byte[] LSBx = t.Read(0x28, 1);

                int x = ((MSBx[0] << 8) | LSBx[0]);

                byte[] MSBy = t.Read(0x2B, 1);
                byte[] LSBy = t.Read(0x2A, 1);

                int y = ((MSBy[0] << 8) | LSBy[0]);

                byte[] MSBz = t.Read(0x2D, 1);
                byte[] LSBz = t.Read(0x2C, 1);

                int z = ((MSBz[0] << 8) | LSBz[0]);

                Debug.Print("X: " + x + " Y: " + y + " Z: " + z);

            }, null, 0, timerPeriod);

            // Make sure the device runs the timer and doesnt end execution.
            while (true)
            {
                Thread.Sleep(Timeout.Infinite);
            }
        }
    }
}



#2 gbreder

gbreder

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts
  • LocationGermany

Posted 02 January 2012 - 05:29 PM

Hi jsnmc,
when I look into your code I see a write transaction immediately followed by a read transaction. I guess the write transaction is used to set some parameters in the gyro and the read transaction is the real operation to get some values from the sensor. Such a contruct needs IMHO an I2C repeated-start-condition. The I2C repeated-start-condition is only supported on Netduino Firmware 4.1.1 and 4.2.x. The RTM firmware 4.0.x doesn't support it.
Please look in the specs of your gyro sensor if two transaction without stop-condition between them are needed. If so update to 4.2 RC3 Firmware. Please erase your netduino completly and reflash it with help of the SAMBA tool. Otherwise it will be really unstable.
Here are two link to successfully update the firmware on the first try: :)
Erase Netduino
Update firmware

And here is the page how to use repeated start bit: :)
How to code repeated start bit

Regards
Guido

#3 StanDeMan

StanDeMan

    Member

  • Members
  • PipPip
  • 14 posts

Posted 03 January 2012 - 08:45 PM

Hi jsnmc, swap address and value to this: t.Write(0x20, 0x0F); // Turn on XYZ axes t.Write(0x22, 0x08); // control t.Write(0x23, 0x30); // Scale to 2000 deg/sec Hopefully this helps. Let me know, please. :) Regards, StanDeMan

#4 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 04 January 2012 - 03:58 PM

Please look in the specs of your gyro sensor if two transaction without stop-condition between them are needed.


Judging from the arduino sample code, it isn't needed.
I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#5 jsnmc

jsnmc

    New Member

  • Members
  • Pip
  • 7 posts

Posted 07 January 2012 - 03:13 AM

Hopefully this helps. Let me know, please. :)


StanDeMan - that fixed it. Oh boy rookie mistake :huh: . Thanks for the good eyes!

jsnmc




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.