Shop OBEX P1 Docs P2 Docs Learn Events
L3G4200D Gyroscope Module — Parallax Forums

L3G4200D Gyroscope Module

MoZak18MoZak18 Posts: 26
edited 2012-04-18 20:32 in Accessories
Hello,

I am trying to use the example spin code for the L3G4200D gyroscope to yield the raw data output....however, I am getting 0's for the X, Y, and Z axes. I have the SDA and SCL pins connected and assigned appropriately to the P8X32A microcontroller. I have modified the code slightly to provide an output using "parallax serial terminal" (which turns out to work better for me) but it provides the same output if I use "full duplex serial", so this is not the problem. If anyone can help it would be greatly appreciated, I have a feeling something obvious is likely the problem. My code is below. Thanks!



CON


_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000


SCLpin = 17
SDApin = 16

'****Registers****


WRITE = $D2
READ = $D3

CTRL_REG1 = $20 'SUB $A0
CTRL_REG3 = $22
CTRL_REG4 = $23
STATUS_REG = $27
OUT_X_INC = $A8




x_idx = 0
y_idx = 1
z_idx = 2

VAR


long x
long y
long z


long cx
long cy
long cz


long ff_x
long ff_y
long ff_z


long multiBYTE[3]


OBJ
pst : "parallax serial terminal"

PUB Main | last_ticks


''Main routine for example program - Shows RAW X,Y,Z data and example of calculated data for degrees


pst.start(38400) 'start a pstinal Object (rxpin, txpin, mode, baud rate)


Wrt_1B(CTRL_REG3, $08) 'set up data ready signal
Wrt_1B(CTRL_REG4, $80) 'set up "block data update" mode (to avoid bad reads when the values would get updated while we are reading)
Wrt_1B(CTRL_REG1, $1F) 'write a byte to control register one (enable all axis, 100Hz update rate)


pst.str(string("initialized", 13))

Calibrate


pst.str(string("calibrated", 13))


last_ticks := cnt


pst.str(string("about to enter loop", 13))


repeat
pst.str(string("in the loop", 13))

WaitForDataReady
Read_MultiB(OUT_X_INC) 'Read out multiple bytes starting at "output X low byte"
pst.str(string("ran some stuff", 13))

x := x - cx 'subtract calibration out
y := y - cy
z := z - cz

' at 250 dps setting, 1 unit = 0.00875 degrees,
' that means about 114.28 units = 1 degree
' this gets us close
x := x / 114
y := y / 114
z := z / 114

RawXYZ 'Print the Raw data output of X,Y and Z
waitcnt(80_000_000 + cnt)


PUB RawXYZ


''Display Raw X,Y,Z data


pst.str(string("RAW X ",11))
pst.dec(x)

pst.str(string(13, "RAW Y ",11))
pst.dec(y)

pst.str(string(13, "RAW Z ",11))
pst.dec(z)
pst.str(string(13))


PUB Calibrate
cx := 0
cy := 0
cz := 0


repeat 25
WaitForDataReady
Read_MultiB(OUT_X_INC) ' read the 3 axis values and accumulate
cx += x
cy += y
cz += z


cx /= 25 ' calculate the average
cy /= 25
cz /= 25



PUB WaitForDataReady | status
repeat
status := Read_1B(STATUS_REG) ' read the ZYXZDA bit of the status register (looping until the bit is on)
if (status & $08) == $08
quit

PUB Wrt_1B(SUB1, data)


''Write single byte to Gyroscope.

start
send(WRITE) 'device address as write command
'slave ACK
send(SUB1) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
send(data) 'data you want to send
'slave ACK
stop

PUB Wrt_MultiB(SUB2, data, data2)


''Write multiple bytes to Gyroscope.


start
send(WRITE) 'device address as write command
'slave ACK
send(SUB2) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
send(data) 'data you want to send
'slave ACK
send(data2) 'data you want to send
'slave ACK
stop


PUB Read_1B(SUB3) | rxd


''Read single byte from Gyroscope


start
send(WRITE) 'device address as write command
'slave ACK
send(SUB3) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
stop
start 'SR condition
send(READ) 'device address as read command
'slave ACK
rxd := receive(false) 'recieve the byte and put in variable rxd
stop

result := rxd


PUB Read_MultiB(SUB3)


''Read multiple bytes from Gyroscope


start
send(WRITE) 'device address as write command
'slave ACK
send(SUB3) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
stop
start 'SR condition
send(READ) 'device address as read command
'slave ACK
multiBYTE[x_idx] := (receive(true)) | (receive(true)) << 8 'Receives high and low bytes of Raw data
multiBYTE[y_idx] := (receive(true)) | (receive(true)) << 8
multiBYTE[z_idx] := (receive(true)) | (receive(false)) << 8
stop

x := ~~multiBYTE[x_idx]
y := ~~multiBYTE[y_idx]
z := ~~multiBYTE[z_idx]


PRI send(value) ' I²C Send data - 4 Stack Longs


value := ((!value) >< 8)


repeat 8
dira[SDApin] := value
dira[SCLpin] := false
dira[SCLpin] := true
value >>= 1


dira[SDApin] := false
dira[SCLpin] := false
result := not(ina[SDApin])
dira[SCLpin] := true
dira[SDApin] := true


PRI receive(aknowledge) ' I²C receive data - 4 Stack Longs


dira[SDApin] := false


repeat 8
result <<= 1
dira[SCLpin] := false
result |= ina[SDApin]
dira[SCLpin] := true


dira[SDApin] := (aknowledge)
dira[SCLpin] := false
dira[SCLpin] := true
dira[SDApin] := true


PRI start ' 3 Stack Longs


outa[SDApin] := false
outa[SCLpin] := false
dira[SDApin] := true
dira[SCLpin] := true


PRI stop ' 3 Stack Longs


dira[SCLpin] := false
dira[SDApin] := false

Comments

  • prof_brainoprof_braino Posts: 4,313
    edited 2012-03-14 18:25
    You need to put the [ code ] and [ / code ] tags around your code to start with, so we can make better sense of what you posted.

    Did you try with the original example code, and what were the results?
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-14 18:40
    Yes, the results were the same. Zeros for all 3 axes.
  • prof_brainoprof_braino Posts: 4,313
    edited 2012-03-14 18:57
    I would say you should check how you hooked it up. When the example code doesn;t work, it usually means you didn't hook something up correctly.

    Once the example works, then you can start changing things.

    In your original post, find the EDIT POST button on the bottom to add the code tags. This will allow you to format the code as it looks in the propeller tool, and formating is important, as it controls aspects of compilation.
  • vanmunchvanmunch Posts: 568
    edited 2012-03-16 14:34
    I think you need pull-up resistors connected to SDL and SCL. I know you do with the one I sell and not having those will give you an error.
  • cessnapilotcessnapilot Posts: 182
    edited 2012-03-21 23:29
    MoZak18: This is a rate gyroscope. Rotate it, it should work.

    Istvan
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-24 09:36
    Cessnapilot, I was hoping that would work...unfortunately that's not the case. The values are stuck at 0, however, it looks like the serial terminal is constantly updating the values since they all flicker, but they are staying at 0 even if i move it around like a lunatic nonetheless. Pull up resistors haven't helped, I tried 600-700, 10k, and 20k Ohm resistors with the same result. I have a feeling this is either something simple, or I have a defective part. I have ordered numerous things from Parallax recently, and out of all the parts I ordered, I had one defective Prop Plug (I realized this since I ordered 2 of them and the other worked), so I suppose it being defective is not outside the realm of possibility. Any other helpful suggestions would be greatly appreciated. I am using the example code off the product page, and really want to get this thing working! Thanks again.
  • ratronicratronic Posts: 1,451
    edited 2012-03-24 18:53
    MoZak18 indentation is really important, all I did was copy and paste your posted code and inserted proper indentation and it works for me (spits out #'s other than 0 when gyro moved). Indentation is the only thing I changed to get your posted code to work. So give this a try.
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-24 21:21
    I just re-copied and pasted what I put in the window.....I can see why it wouldn't work since none of the repeat loops or if statements could function. I suppose the indentations were lost when I pasted the code. Thanks for the effort ratronic, but that isn't my problem. In the future I will just attach the file itself instead of pasting the code. But since you have the sensor on hand....could you try the example code with only the SDA and SCL pins connected....and try different pins on the prop by changing the pin assignment within the code? This will probably work for you just fine if my modified code worked.....I'm beginning to get the feeling I have a defective sensor.
  • ratronicratronic Posts: 1,451
    edited 2012-03-25 07:30
    I have used the SDA and SCL pins on several different Propeller port pins in the past and have also used the SPI connections. I hooked it up to P17(SCL), P16(SDA), gnd, and +3.3volts and ran the corrected program I posted and I get back #'s anywhere from 0 to over 100 when I move the gyro. I am posting a modified version of the above program so it doesn't keep scrolling the display and updates the display faster so you can quickly see any movement of the gyro. If you don't get any response with this program then maybe you can post a picture of your setup?
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-25 20:10
    Thanks a lot ratronic, at the moment I do not have access to any of the parts since I am not in the lab....but tomorrow I'll be there and I'll try the code. I'll let you know what happens and get a picture of my setup if it doesn't work. Thanks again.
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-26 12:04
    I tried the code....to no avail (all zeros). However, I did notice something strange regarding the sensor and the output. While leaving either an SDA or SDO line connected, but by disconnecting the SCL line and touching it....erroneous readings were given. They would randomly change very quickly. I thought this might be indicative of the need for a pull up resistor, but I tried several ranging from 500 to 5k to no avail. A picture of the setup is provided. It's not the most neat since I'm in a rush, but the gyro is the furthest left on the picture. I'm not using a startup kit or anything, just the microcontroller wired up on the breadboard. The SDA and SCL pins are connected to pins 9 and 8 respectively, power and ground is also being fed to the sensor. I don't think my connections are a problem, unless you can suggest a configuration of pull-up resistors that I may have not tried yet? Let me know if you have any questions regarding the picture, i'll understand if it's difficult to comprehend.

    Thanks
    1024 x 768 - 92K
  • ratronicratronic Posts: 1,451
    edited 2012-03-26 12:39
    I have not used pullup resistors with this module on a BS2BOE or the Propeller professional development board. Edit See next post. As far as the pullup resistors they are not needed per this page in the manual.
    1024 x 623 - 81K
  • ratronicratronic Posts: 1,451
    edited 2012-03-26 14:35
    MoZak18 on closer look at your picture you have SCL wired to P9 and SDA wired to P8 contradicting the way you stated it is wired in post#12. Maybe they are reversed in the program definition?
  • MoZak18MoZak18 Posts: 26
    edited 2012-03-26 22:58
    Ratronic,

    I may have left them that way and forgot to switch them around before I took the picture. I have tried every combination of connecting them with given pin assignments in the code, I literally made a table to try just about every combination out of the chance that the datasheet may have been incorrect (I've seen it happen several times before with certain components) or that there was some glitch in the code. That was probably why, but that wouldn't be my problem.
  • ratronicratronic Posts: 1,451
    edited 2012-03-27 07:58
    Perhaps you do have a bad module I think Parallax tests all modules before they are put in stock. But if your willing to give it one more shot run the program thats set for how it is wired now and when it doesn't work, at that point take a picture of your setup and post the code you used to try it with that setup along with the picture..
  • MoZak18MoZak18 Posts: 26
    edited 2012-04-04 12:50
    Ratronic,

    I ordered two more gyros and found out the one I had was indeed defective. This is the 3rd defective part from Parallax that I have purchased, so I'm not too happy about that, especially since I'm short on time for this project. Thanks for all the help and replies, I greatly appreciate it. The new gyro works great.
  • Bulslayer85Bulslayer85 Posts: 34
    edited 2012-04-18 20:32
    Lol had this issue with development board and boe bot, found out i kept crossing the I/O pins so it would work but return false values.
Sign In or Register to comment.