Go Down

Topic: Gyroscope Library Review Request (Read 1 time) previous topic - next topic

Hello Arduino community! I have been working with the arduino for almost a year now, and recently decided to get more in depth and write my own library. After much research I finally made a library. It doesn't work, at all. That is why I am asking those whom have the greater knowledge than I to review my code.

Code: [Select]
//gyro header

#ifndef gyro_h
#define gyro_h

#include <Wire.h>

class  gyro
void begin();
void xValue(int x);
void yValue(int y);
void zValue(int z);

void readI2C();
void writeI2C();



Code: [Select]
//This is a library for reading and reporting the axis
//states of the sensor.
//Anonymous Parallax
// and Nicolas Guerra-Contreras
#include <Wire.h>
#include "gyro.h"

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define Addr = 105;

#define int x
#define int y
#define int z


void gyro::begin() //start the gyro
writeI2C(CTRL_REG1, 0x1F);    // Turn on all axes, disable power down
    writeI2C(CTRL_REG3, 0x08);    // Enable control ready signal
    writeI2C(CTRL_REG4, 0x80);    // Set scale (500 deg/sec)
    delay(100);                   // Wait to synchronize

void gyro::xValue() //gets x value
byte MSB, LSB; //byte stores an 8-bit unsigned number(MSB,LSB) from 0 to 255.

MSB = readI2C(0x29); //reads x memreg MSB
LSB = readI2C(0x28); //reads x memreg LSB
x = ((MSB << 8) | LSB); //moves MSB out of memo

void gyro::yValue()
byte MSB, LSB;

MSB = readI2C(0x2B);
LSB = readI2C(0x2A);
y = ((MSB << 8) | LSB);

void gyro::zValue()
byte MSB, LSB;

MSB = readI2C(0x2D);
LSB = readI2C(0x2C);
z = ((MSB << *) | LSB);

void gyro::readI2C(byte regAddr)
Wire.write(regAddr);                // Register address to read
Wire.endTransmission();             // Terminate request
Wire.requestFrom(Addr, 1);          // Read a byte
while(!Wire.available()) { };       // Wait for receipt
return(Wire.read()); // Get result

void gyro::writeI2C(byte regAddr, byte val)


We recently purchased a l3gd20 board from Pololu which came with a library and example. It took around 10 minutes on a UNO to get the gyro working. Have you had a look at the Pololu library?

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131