.NET Gadgeteer Gyro Module from Seeed Studio

Here’s an example that graphically displays data from the Seeed Gyro module for .NET Gadgeteer.  The GHI Electronics Display_T35 module displays the output.

Plug the Gyro module into Socket 3.  Connect the display module and a button in the .NET Gadgeteer designer, but do not connect the optional touch-screen cable to Socket 10.  Apparently the touch screen is incompatible with the Seeed Gyro and Accelerometer modules.  (This example is easily adaptable to the Accelerometer, and I found it instructive to try both modules in this application.)

The following code block is the complete Program.cs file.  It sets a measurement interval for the Gyro module at 100 ms.  When the program starts, it calibrates the Gyro to the position it occupies at startup.  Make sure it is not moving during calibration.  A delegate is assigned to handle the MeasurementComplete event.  Everything of interest happens in the MeasurementComplete handler.  A button starts and stops the measurements by calling the methods StartContinuousMeasurements and StopContinuousMeasurements on the Gyro.

using Microsoft.SPOT;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace Gyro
    public partial class Program

        void ProgramStarted()
            gyro.ContinuousMeasurementInterval = new System.TimeSpan(0, 0, 0, 0, 100);

            gyro.MeasurementComplete +=
                new GTM.Seeed.Gyro.MeasurementCompleteEventHandler(gyro_MeasurementComplete);

            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);

            Debug.Print("Program Started");

        void button_ButtonPressed(Button sender, Button.ButtonState state)
            if (button.IsLedOn)

        void gyro_MeasurementComplete(GTM.Seeed.Gyro sender, GTM.Seeed.Gyro.SensorData sensorData)
            if (sensorData.X > 0 && sensorData.Y > 0)
                display.SimpleGraphics.DisplayEllipse(Gadgeteer.Color.Orange, 150, 125, (uint)(sensorData.X * 10), (uint)(sensorData.Y * 10));
                display.SimpleGraphics.DisplayText("Gyro sensor X: " + sensorData.X.ToString(),
                    Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Purple, 25, 75);
                    display.SimpleGraphics.DisplayText("Gyro sensor Y: " + sensorData.Y.ToString(),
                    Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Purple, 25, 100);

The code in the gyro_MeasurementComplete handler draws an ellipse on the Display_T35 when there is rotation in a positive direction along the X and Y axes.  The horizontal radius of the ellipse corresponds to the rotation on the X axis of the Gyro and the vertical radius corresponds to the rotation on the Y axis.  It isn’t possible to draw an ellipse with a negative radius when rotation is negative on either axis, so the code simply prints the data to the screen.

I found a little experimentation with this data display to be helpful in understanding how the Gyro works.


, ,

  1. #1 by zazkapulsk on May 16, 2012 - 5:54 AM


    I’m trying some things with the Hydra and a gyro module (http://www.ghielectronics.com/catalog/product/331), integrating over time to see the attitude angles.
    There are two funny things:
    1. It seems X and Z axis are interchanged.
    2. the up-down axis (yaw, marked X, believed to be Z) is showing almost no response as I yaw the module (both other axes are OK).

    This is the (pseudo-)code I am using. It’s initial, but it does the trick for testing the module. All variables are doubles:


    attitudeGyro.rollX = 0;
    attitudeGyro.pitchY = 0;
    attitudeGyro.yawZ = 0;

    gyro.ContinuousMeasurementInterval = new System.TimeSpan(0, 0, 0, 0, 10);
    gyro.MeasurementComplete +=
    new Gyro.MeasurementCompleteEventHandler(gyro_MeasurementComplete);

    void gyro_MeasurementComplete(GTM.Seeed.Gyro sender, GTM.Seeed.Gyro.SensorData sensorData)
    changeGyro.rollX = (sensorData.X + lastGyroAngularVelocity.rollX * 180 / System.Math.PI) / 2 * sender.ContinuousMeasurementInterval.Milliseconds / 1000 * System.Math.PI / 180; // in rad
    changeGyro.pitchY = (sensorData.Y + lastGyroAngularVelocity.pitchY * 180 / System.Math.PI) / 2 * sender.ContinuousMeasurementInterval.Milliseconds / 1000 * System.Math.PI / 180; // in rad
    changeGyro.yawZ = (sensorData.Z + lastGyroAngularVelocity.yawZ * 180 / System.Math.PI) / 2 * sender.ContinuousMeasurementInterval.Milliseconds / 1000 * System.Math.PI / 180; // in rad

    lastGyroAngularVelocity.rollX = sensorData.X * System.Math.PI / 180; // in rad/sec
    lastGyroAngularVelocity.pitchY = sensorData.Y * System.Math.PI / 180; // in rad/sec
    lastGyroAngularVelocity.yawZ = sensorData.Z * System.Math.PI / 180; // in rad/sec

    attitudeGyro.rollX += changeGyro.rollX;
    attitudeGyro.pitchY += changeGyro.pitchY;
    attitudeGyro.yawZ = changeGyro.yawZ;

    Debug.Print(“Roll X: ” + attitudeGyro.rollX * 180 / System.Math.PI + ” – Pitch Y: ” + attitudeGyro.pitchY * 180 / System.Math.PI + ” – Yaw Z: ” + attitudeGyro.yawZ * 180 / System.Math.PI);


    Help please: Is the module faulty or am I?


  2. #2 by Michael Dodaro on May 16, 2012 - 7:06 AM

    In theory the axes depend on the position of the module when calling the Calibrate method. It’s been a while since I tested this module, and this week I am pressed due impending Maker Faire. I’ll try my module when I get some time, but I suspect your code is not the problem.

    • #3 by zazkapulsk on May 16, 2012 - 7:07 AM


      • #4 by Michael Dodaro on May 25, 2012 - 11:07 PM

        I tried the gyro module again today in a new scenario and it worked well enough that I could find the Y axis to get my device to react to a door opening. The calibration function seems usable, though I’m still not certain the labels are correct on the PCB. The new example is here: http://wp.me/p1TEdE-nN

      • #5 by zazkapulsk on May 26, 2012 - 1:18 AM

        Thanks, Michael.
        The thing is there are these two symptoms that were confirmed in the GHI forum: “I tried your code with my hardware and I have the same issue. In additional I tried with different hardware (FEZ-Spider) and found the same behaviour. It seems a bug in the driver”.

  3. #6 by Michael Dodaro on May 26, 2012 - 8:13 AM

    I’ll take your word for it. I cannot access the discussion or anything on the GHI Electronics or Tiny CLR domains. I keep hearing that they are working on this problem, but it has been three weeks. Yesterday I couldn’t link to products used in the examples so users can buy them–very strange security.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: