Shop OBEX P1 Docs P2 Docs Learn Events
Having difficulty figuring out I2C. Where do I start? — Parallax Forums

Having difficulty figuring out I2C. Where do I start?

tosjduenfstosjduenfs Posts: 37
edited 2015-02-05 02:26 in Propeller 1
I'm trying to build a balance bot with stepper motors and the pololu AltIMU-10 v3 https://www.pololu.com/product/2469. I got my parts a few nights ago, I've got the stepper motors going using a square wave generator (since all I want to do is control the rotation rate of the motors as part of my PID scheme). I've used analog gyros and accelerometers before with other projects but this time I thought I'd skip the ADC and try using an I2C sensor. When I ordered it thought "how hard could it be?" now here I am after two nights of trying to figure out how to talk to this thing and I don't feel like I am much closer than when I started. I've gone through tutorials (mostly for other Microprocessors) and the datasheets and I read through them but I am still having trouble on where to start with getting data from this thing.

My goal right now is to simply read the raw data from the gyro and the accelerometer and display it into the parallax serial terminal. If I can get there I'll have just about everything I need to start. Can someone please point me in the right direction?

Regards,

Mike

Comments

  • trangertranger Posts: 179
    edited 2015-02-02 09:22
    Spin / Pasm or C or ?

    There is a spin file here: http://obex.parallax.com/object/335 for a similar gyro. May be a place to start. At first glance, registers seem to be the same.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-02 09:34
    You might want to practice with EEPROM.

    Here's a link to the PEK tutorial.

    http://forums.parallax.com/showthread.php/97625-PE-Kit-Lab-Applications-%96-EEPROM-Datalogging-and-I2C?p=678271#post678271

    The Propeller's EEPROM is an I2C device. You can see how the code works to communicate with the EEPROM and apply the techniques to your sensor. One of the things which will be different is the I2C address.

    Some I2C addresses are listed as 7-bit addresses and other include the read/write bit and use an 8-bit address. You want to make sure you know which type you're using.
  • tosjduenfstosjduenfs Posts: 37
    edited 2015-02-02 09:34
    Sorry I didn't specify. Spin
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-02-02 09:51
    The short story about I2C is this.

    1. It is serial communication, not parallel
    2. It is synchronous, so it needs a clock line to coordinate the serial signal. (RS-232 is asynchonous, no clock)
    3. It is a fancier synchonous serial than SPI.
    4. It usually requires pullup resistors on some or all of its lines.
    5. Actual specifics of I2C depend on the device that requires its use... so you have to read the IMU documentation in detail.
  • tritoniumtritonium Posts: 543
    edited 2015-02-02 09:53
    You might find the following link has useful information

    http://forums.parallax.com/showthread.php/159885-help-I2C-in-SPIN

    Dave
  • tonyp12tonyp12 Posts: 1,951
    edited 2015-02-02 09:59
    Are you trying to roll your own i2c driver?

    Data is bi-directional so you have to switch DIRA often.
    Clk is always provided by master, but slave can keep it low if it use clock stretching to signal it's not ready for more data etc
    So even clk pin have to change DIRA if you want to support clock stretching

    I2C - Bus Master - Step 1
    https://www.youtube.com/watch?v=skkyudHHSWY

    But there are many already made available in obex
  • tosjduenfstosjduenfs Posts: 37
    edited 2015-02-02 11:12
    tritonium wrote: »
    You might find the following link has useful information

    http://forums.parallax.com/showthread.php/159885-help-I2C-in-SPIN

    Dave

    Thanks you Dave I will check this out.

    tonyP12

    No I'm not trying to make my own driver. I've download pretty much all of the I2C drivers from the OBEX and looked through them hoping something would click in my head but I'm not there yet. I've slept on it now so I'm going to try reading through some of the drivers again to see if I can get anywhere. Mostly I'm confused about the the initial start of the sensors although looking through the data sheets it looks like the default settings will work for what I need. I'm also confused about reading multiple 16bit values (x,y,z) from a single request, the data comes in 8bit chunks and then I need to combine the bytes into the respective values for x y and z.
  • ChrisGaddChrisGadd Posts: 310
    edited 2015-02-02 12:46
    I have a few drivers written in Spin and PASM, with plenty of demos located in the obex here: http://obex.parallax.com/object/700
    I tried to write the drivers to be as easy to use as possible. For 16-bit values, you need to determine how the data is stored, whether a value of $0123 is stored as $01, $23 (big-endian) or $23, $01 (little-endian).
    For big-endian values, my drivers have a read_word method, for little-endian a page_read can be used.
  • tosjduenfstosjduenfs Posts: 37
    edited 2015-02-02 16:10
    ChrisGadd wrote: »
    I have a few drivers written in Spin and PASM, with plenty of demos located in the obex here: http://obex.parallax.com/object/700
    I tried to write the drivers to be as easy to use as possible. For 16-bit values, you need to determine how the data is stored, whether a value of $0123 is stored as $01, $23 (big-endian) or $23, $01 (little-endian).
    For big-endian values, my drivers have a read_word method, for little-endian a page_read can be used.

    This is it! I was looking at the data sheet for the L3G4200D and it has essentially the same registers as the L3GD20H i'm trying to use so your gyroscope demo is working for me. All I had to do was change the device address and I am getting data in the serial terminal. Thank you so much. I am going to study your examples. Now that I can actually see some output I think I can work from here. Thanks everyone for your input.

    Mike
  • tosjduenfstosjduenfs Posts: 37
    edited 2015-02-05 02:26
    Just wanted to say thanks again for the quick help. In the past few days I've figured out how to configure my gyro and accelerometer and I've successfully used them to code an AHRS for the balance bot I'm working on. This thing will be rolling in no time.

    Mike
Sign In or Register to comment.