Tuesday, January 22, 2013

AVR Atmega L3G4200D gyroscope library 01

The L3G4200D is a low-power three-axis angular rate sensor able to provide unprecedented stablility of zero rate level and sensitivity over temperature and time.


This library implements a way to get values from l3g4200d.
Calibration is suggested to get more accurate parameters.

Setup parameters are stored in file l3g4200d.h

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.


Code


Notes
  • read risk disclaimer
  • excuse my bad english

26 comments:

  1. hey! This is great work for L3G4200 gyroscope. So i need to use this library on my AVR-5 compiler. How can use this? Please give me a tip. (Actually I tried to work on AVR compiler, didn't work. Only just stopped whole my programm)

    ReplyDelete
    Replies
    1. To me the simpler way of working with atmega is Eclipse + AVR plugin for Eclipse + avrgcc as compiler, or AVR studio. But i prefer Eclipse.
      If you use other than avrgcc compiler i suppose you should port this code to your compiler.
      Anyway, using Eclipse for AVR is really simple, you can just google around here and you will find a lot of tutorial on how to do it.
      I suggest you to start from a simple "hello world" blinking led, just to see if your compiler works ;)

      Delete
    2. Yeah! My atmel processor's compiler is AVR studio. Recently I installed Eclipse+ Avrplugin. I couldn't find out how to import L3G4200 gyroscope's C code, header file to my eclipse C project. Could you give a all completed L3G4200 gyroscope C projecd based on Eclipse?

      Delete
    3. Make a src directory.
      Import all code files. Compile. It should be as simple.
      Anyway, just send me an email, and i will reply you with my eclipse directory for this project.

      Delete
  2. hello, sorry for the inconvenience, but I have a problem with i2c_readNak and i2c_readAck twimaster.c library functions. The problem is that they do not return the call to the function i2c_readAck and i2c_readNak, ie remain in the whille (!(...)); and never leaves donot how to fix it, I could help? Please. Thank you.

    ReplyDelete
    Replies
    1. Hello, it could be hardware or software problem. Double check your wiring. Check that the hardware (your board), works on other software, like an arduino board. Also check that you write the code and compile it with the same frequency FCPU settings. If you have logic analyzer, you could also try to inspect the I2C line. Try to define a SCL_CLOCK at 10000L or 50000L if you are working at 1Mhz. Check that you have selected the correct device I2C address "(0x68<<1)" or "(0x69<<1)" depending on the SDO pin. As datasheet states, to enable the I2C line, CS has to tied high.

      Delete
  3. I'm using avr studio 4.15 and WinAVR avr_gcc

    ReplyDelete
  4. HI good library. How do you convert the data to angles?

    ReplyDelete
    Replies
    1. Raw data it is converted to deg/sec, angular velocity. Not to angles. If you look at the datasheet you will find the Sensitivity parameter for this IC. You can use this parameter to convert raw values to deg/sec. Look at the l3g4200d_getdata function to see the conversion forumla.

      Delete
    2. Hi davide, how do I know that the raw data are correct?

      Delete
    3. Looking at the datasheet, if your IC is not damaged, you should have +-2% error. Also, if you calibrate the gyroscope, you can reduce this error.

      Delete
    4. Hi I get these values without using offset and calibration
      X: -8192 Y: 17376 Z: -8192
      X: -7864 Y: 17367 Z: -7864
      X: -8192 Y: 17376 Z: -8192

      And this when I roll it on the X-axi
      X: -20644 Y: -15228 Z: -20644
      X: -20644 Y: -15228 Z: -20644
      X: -20644 Y: -15228 Z: -20644
      Can it be correct?

      Delete
    5. Shouldnt it be 0 when its still?, the data are jumping all over the place

      Delete
    6. Consider that, even if you have setup the sensitivity to 250 dps,
      a raw value of of 8192 it is converted to: 8192*0.00875
      = deg/sec, more than 50 deg/sec, which is too much for a still device.
      Check with a logic analizer what's going on the bus, and compare the reading with the value you get.
      Check that the IC is well initialized. Double check your wiring. Check that you F_CPU it is well setted up. Check you IC with other hardware, try it on Arduino as example. Just to prove that you IC works.

      Delete
    7. Shouldnt it be 0 when its still?, the data are jumping all over the place

      Delete
  5. I want to ask a question,
    what differrent about getrawdata and getdata?

    ReplyDelete
    Replies
    1. Hello, the getrawdata return raw data (not converted, as it is read by the data registers), get data return converted to deg/sec data. Hope this help.

      Delete
  6. hi I have a question, do I connect SDA (gyro) to SDA (atmega) and then both to VCC through 4,7k resistor?? Same with SCL and do I have to connect SDO somehow?

    ReplyDelete
    Replies
    1. Hello, you are right SDA to SDA of your micro, SCL to SCL of your micro, then one pullup 4k7 resistor for each bus to VCC. SDO unconnected.

      Delete
  7. I compiled your code and it has no errors :) although nothing happened after uploading it to my ATmega8, I'm using USB-UART FTDI converter and Realterm Terminal to watch data on PC and nothing, I have also this avrdude: warning: cannot set sck period. please check for usbasp firmware update, but I've searched for solutions online and ppl said to ignore it basically. Dunno if to invest in some analyzer or try sending some text via your program to PC.

    ReplyDelete
    Replies
    1. Hello, at first just use the uart library to check if the code complies and output a simple message to your serial. Something like this should emit a message to your serial output
      for(;;) { uart_puts("Test\r\n"); _delay_ms(500); }

      Delete
  8. Uart worked! :) I changed baud rate to 9600, bcs i remembered it as more "bulletproof" when I built my tachometer, but nothing happens if I "uncomment" l3g4200d_init() and I'm wondering what now. I see on your schematics that AREF of ATmega is connected to VCC, is it really necessary?, also do I have to connect VIN of my gyro to VCC? I believe that your code is perfect after reading it, but what do you mean by typing: "adapt the SCL and SDA port and pin definitions and eventually the delay routine in the module i2master.s? (why ".s" though), do I have to find some info in my datasheet and implement in your code in i2cmaster.h? And finally do I have to uncomment int main (void) in this header? Sorry for my amateurishness xd

    ReplyDelete
    Replies
    1. Hello. AREF is not needed for this project, you can leave it unconnected. I do not know your module wiring, what's the VIN pin? "adapt..." that is Peter Fleury, cause the i2c library if from him. I suppose he mean to change SCL and SDA port and pin when you are using the .s module (which is the assembler versione of the i2c libary I'm using). Do not uncomment the main, that is a library example. I suppose you do not have a logic analyzer, so you have to debut if by uart, test it putting debug poit in the init function. One think it may be is a wrong L3G4200D_ADDR.

      Delete
    2. Hmmm is there a chance that I broke the gyro by connecting VCC to 5 [V]? do i need some pull-up resistor?

      Delete
    3. If your module does not support 5V, yes. I mean, if your module did not have a 3.3V voltage regulator you may have burn the main gyro chip, which works from 2.4v to 3.6v. No pull-up resistor is needed on the VCC input.

      Delete