I2C to onboard gyro

Discussion in 'UDOO NEO' started by knick, Nov 11, 2016.

Tags:
  1. knick

    knick New Member

    Joined:
    Nov 1, 2016
    Messages:
    29
    Likes Received:
    8
    I thought I would have a go at rolling my own interface for the gyro. Unfortunately I'm having trouble getting the I2C working. Here's a snippet from the code;

    #include <Wire.h>

    #define WHO_AM_I 0x0C
    #define I2C_ADDRESS ((uint8_t)0x20)

    byte Gyro::readReg(byte reg)
    {
    byte value;

    Wire1.beginTransmission(I2C_ADDRESS);
    Wire1.write(reg);
    Wire1.endTransmission();
    Wire1.requestFrom(I2C_ADDRESS, (uint8_t)1);
    value = Wire1.read();
    Wire1.endTransmission();

    return value;
    }

    void Gyro::test()
    {
    byte val = readReg(WHO_AM_I);
    Serial.println(val);
    }

    When I invoke the test() function I would expect to see 0xD7 printed to the screen (or the decimal equivalent). I am getting '0'.

    Any ideas why this isn't working? I've essentially just copied the readReg function from the FXAS21002C.cpp library.
     
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
  3. knick

    knick New Member

    Joined:
    Nov 1, 2016
    Messages:
    29
    Likes Received:
    8
  4. knick

    knick New Member

    Joined:
    Nov 1, 2016
    Messages:
    29
    Likes Received:
    8
    I've been into the Device Tree editor, right clicked on the I2C-4 pins in the right panel and removed them. Then I saved the config and rebooted.

    No joy. My code is still outputting '0'.
     
  5. knick

    knick New Member

    Joined:
    Nov 1, 2016
    Messages:
    29
    Likes Received:
    8
    I had to change a line in the readReg function to get it working;

    Before: Wire1.endTransmission();
    After: Wire1.endTransmission(false);

    With this change it is working :)
     
    waltervl and Andrea Rovai like this.
  6. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    Good news!
     

Share This Page