Shop OBEX P1 Docs P2 Docs Learn Events
BS2 & HMC5883L Code — Parallax Forums

BS2 & HMC5883L Code

sylveeskisylveeski Posts: 17
edited 2014-03-11 14:55 in BASIC Stamp
Hi,
I am trying to calibrate an HMC5883L compass with my BS2. I uploaded the sample Kickstarter code and am getting a constant unchanging output. No matter which way I tilt the compass, my debug window shows:
X= - 4096
Y= - 4096
Z = - 4096

I don't know what it could be. The only wiring I did was connecting the Vin, Gnd, SDA & SDL so I don't know what it could be.

Any ideas or suggestions?

Thanks!
«1

Comments

  • davejamesdavejames Posts: 4,047
    edited 2014-03-03 23:05
    Hello sylveeski - welcome to the Forum.

    To assist Forum members to formulate an answer, we'll need you to:
    1 - post the code you're using
    2 - post a schematic or image of the connections between the BS2 and compass
  • GenetixGenetix Posts: 1,754
    edited 2014-03-03 23:39
    Try rewiring it again and reloading the BS2 program.

    The Compass Module is also very sensitive to magnetic fields such as electrical wiring in the wall. If you still get the same reading try moving it away from anything electrical or electronic.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-08 13:49
    So I tried rewiring, put in new wires and am now getting x=-1, y=-1, z=-1

    This is what it looks like connected to my basic stamp:

    Compass-4.png

    And the code is here:
    http://learn.parallax.com/KickStart/29133


    I tried using longer wires to get the compass farther away from my breadboard. I walked around my whole house with it trying to get it to say something else but the readings now are always -1,-1,-1Sylvee
  • sylveeskisylveeski Posts: 17
    edited 2014-03-08 14:07
    I did it again and am getting the -4096 readings again....
  • Hal AlbachHal Albach Posts: 747
    edited 2014-03-08 14:35
    Can you post the actual code you are programming into the BS2 as well as a picture of your wiring. What you have posted is how it should look, we can better help you if you show us how at actually is​ hooked up and programmed.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-08 15:00
    BS2Pic.jpg
    Here is a picture of the basic stamp 2 wiring.
    640 x 480 - 168K
  • sylveeskisylveeski Posts: 17
    edited 2014-03-08 15:02
    Here's the actual code:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    SDA PIN 0 ' SDA of gyro to pin P0
    SCL PIN 1 ' SCL of gyro to pin P1


    WRITE_Data CON $3C ' Requests Write operation
    READ_Data CON $3D ' Requests Read operation
    MODE CON $02 ' Mode setting register
    X_MSB CON $03 ' X MSB data output register


    X VAR Word
    Y VAR Word
    Z VAR Word
    rawl VAR Word
    rawh VAR Word


    ' Variables for I2C communications
    I2C_DATA VAR Byte
    I2C_LSB VAR Bit
    I2C_REG VAR Byte
    I2C_VAL VAR Byte




    PAUSE 100 ' Power up delay
    I2C_REG = MODE ' Set operating mode to continuous
    I2C_VAL = $0
    GOSUB I2C_Write_Reg


    DO
    GOSUB GetRawReading ' Get raw Compass reading
    DEBUG HOME, "X = ",11, SDEC x, CR ' Print values
    DEBUG "Y = ",11, SDEC y, CR
    DEBUG "Z = ",11, SDEC z, CR
    DEBUG CR
    LOOP


    GetRawReading:
    PAUSE 400 ' Wait for new data


    ' Send request to X MSB register
    GOSUB I2C_Start
    I2C_DATA = WRITE_Data
    GOSUB I2C_Write
    I2C_DATA = X_MSB
    GOSUB I2C_Write
    GOSUB I2C_Stop


    'Get data from register (6 bytes total, 2 bytes per axis)
    GOSUB I2C_Start
    I2C_DATA = READ_Data
    GOSUB I2C_Write


    ' Get X
    GOSUB I2C_Read
    rawH = I2C_Data
    GOSUB I2C_ACK
    GOSUB I2C_Read
    rawL = I2C_Data
    GOSUB I2C_ACK
    X = (rawH << 8) | rawL


    ' Get Z
    GOSUB I2C_Read
    rawH = I2C_Data
    GOSUB I2C_ACK
    GOSUB I2C_Read
    rawL = I2C_Data
    GOSUB I2C_ACK
    Z = (rawH << 8) | rawL


    ' Get Y
    GOSUB I2C_Read
    rawH = I2C_Data
    GOSUB I2C_ACK
    GOSUB I2C_Read
    rawL = I2C_Data
    GOSUB I2C_NACK
    Y = (rawH << 8) | rawL


    GOSUB I2C_Stop


    RETURN




    '
    I2C functions
    ' Set I2C_REG & I2C_VAL before calling this
    I2C_Write_Reg:
    GOSUB I2C_Start
    I2C_DATA = WRITE_DATA
    GOSUB I2C_Write
    I2C_DATA = I2C_REG
    GOSUB I2C_Write
    I2C_DATA = I2C_VAL
    GOSUB I2C_Write
    GOSUB I2C_Stop
    RETURN


    ' Set I2C_REG before calling this, I2C_DATA will have result
    I2C_Read_Reg:
    GOSUB I2C_Start
    I2C_DATA = WRITE_DATA
    GOSUB I2C_Write
    I2C_DATA = I2C_REG
    GOSUB I2C_Write
    GOSUB I2C_Stop
    GOSUB I2C_Start
    I2C_DATA = READ_DATA
    GOSUB I2C_Write
    GOSUB I2C_Read
    GOSUB I2C_NACK
    GOSUB I2C_Stop
    RETURN


    I2C_Start:
    LOW SDA
    LOW SCL
    RETURN


    I2C_Stop:
    LOW SDA
    INPUT SCL
    INPUT SDA
    RETURN


    I2C_ACK:
    LOW SDA
    INPUT SCL
    LOW SCL
    INPUT SDA
    RETURN


    I2C_NACK:
    INPUT SDA
    INPUT SCL
    LOW SCL
    RETURN


    I2C_Read:
    SHIFTIN SDA, SCL, MSBPRE, [I2C_DATA]
    RETURN


    I2C_Write:
    I2C_LSB = I2C_DATA.BIT0
    I2C_DATA = I2C_DATA / 2
    SHIFTOUT SDA, SCL, MSBFIRST, [I2C_DATA\7]
    IF I2C_LSB THEN INPUT SDA ELSE LOW SDA
    INPUT SCL
    LOW SCL
    INPUT SDA
    INPUT SCL
    LOW SCL
    RETURN
  • sylveeskisylveeski Posts: 17
    edited 2014-03-08 15:08
    BS2HD.jpg
    And here's a picture of the 4 wires coming from the compass.
    320 x 240 - 23K
  • FranklinFranklin Posts: 4,747
    edited 2014-03-08 16:54
    Try plugging the compass directly into the breadboard and use short jumpers like in the original diagram.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-03-08 17:28
    How long are those wires between the module and the Stamp board? It's probably a trick of the lighting, but the module shows blue wires and the stamp board shows green. With all the wires the same color you need to be very certain that the each wire connects to the proper point. Reversing power leads usually becomes self-evident, all the magic smoke leaves. Reversed I2C connections just won't work (unless you reverse the pairs at both ends). Also, I2C was never meant to go far, your extension wires may just be too much of a load and is deteriorating the signals.Try doing what Franklin above has said and plug the module into breadboard and use short​ wires. Even with a lot of nearby magnetic interference your readings should be different. It all points to a communication problem.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-08 19:22
    Hal and Franklin,

    I did those things and here's a picture of what my wiring looks like now.
    BS2HD2.jpg


    I also went to Fry's and bought another compass and it's doing the exact same thing. The readings are always X=-4096, Y=-4096, Z=-4096.
    Any ideas anyone? I really need this to work so I can use the compass for my project.

    Thanks,
    Sylvee
    640 x 480 - 131K
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-03-08 20:12
    That sure looks like it should work.

    Are you sure your battery is good?

    What about changing the IO pins you're using? Is is possible the Stamp is damaged?

    One last thing. Move the compass to a different area of the breadboard and use different wires (it's worked for me more than once).
  • Hal AlbachHal Albach Posts: 747
    edited 2014-03-09 09:39
    Can you measure the voltage levels of SDA, SCL, and +5 with reference to ground on the compass module and then with ground on the board? With the program running all three pins should be very close to +5. If SDA and/or SCL are very low in the readings disconnect SDA and SCL from the module and see if both pins hover near +5 on the module. I just want to make sure the pull ups on the module are working and that the module has proper power.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-09 11:08
    New battery = SUCCESS!!!!! Finally! I totally should have caught this earlier. My original battery was measuring 4.7V. Got a new one at 8.8V and now the compass works!

    I appreciate your help!
  • Hal AlbachHal Albach Posts: 747
    edited 2014-03-09 11:30
    Duane said:
    That sure looks like it should work.

    Are you sure your battery is good?

    Good call, Duane! Maybe that should be our #1 question for anyone using a board that can take 9 volt batteries.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-09 17:39
    Ok so now I'm trying to figure out how to calibrate this compass. Basically, I want to be able to rotate a lazy Susan and have the compass sense where a point on the edge of the lazy Susan is pointing. See picture:
    BS2CompassSusan.jpg


    So basically, I don't care about the z-axis, but want to use x and y to determine a degree between 0 and 360. I tried using the ATN command in basic stamp using the X and Y raw values, but that doesn't seem to correlate with anything.

    Some info: The x and y raw values I am getting are between ~ -250 to +250

    I want to be able to pick up this whole contraption and have the compass give the same degree readings at different points along the lazy Susan. Should I add a magnet or something onto the contraption so it is calibrated to that? If it's calibrated to the Earth's magnetic field, I will have to re-orient the lazy Susan everytime I move it.

    I read the application notes for the HNC5883L, but am still not fully understanding which equations to use to calibrate the compass. For your info, I'm in the Cupertino, CA so do I use the known Earth coordinates of this location when calibrating?

    Any suggestions would be greatly appreciated!

    Sylvee
    640 x 480 - 138K
  • FranklinFranklin Posts: 4,747
    edited 2014-03-09 18:17
    I want to be able to pick up this whole contraption and have the compass give the same degree readings at different points along the lazy Susan.
    Does that mean you don't want this to be an earth compass always sensing north?
  • sylveeskisylveeski Posts: 17
    edited 2014-03-09 21:03
    Yes, I don't want this to be an earth compass always sensing north.

    If that's not possible, that's fine. I can make sure that I always set the contraption the same way and align it facing earth north. I can use my iphone compass or something to make sure it's facing the right way.

    But I would prefer for it to just be calibrated to the contraption instead of earth.... What do you think?
  • FranklinFranklin Posts: 4,747
    edited 2014-03-09 22:11
    Could you post a drawing of what you want it to do? From what I understand a compass is not what you want but I don't understand well enough to make a suggestion
  • Hal AlbachHal Albach Posts: 747
    edited 2014-03-10 07:05
    Like Franklin, I don't understand what your goals are. The following statements seem to be at odds.
    In post 17 you first state that you want the "contraption" to sense where a point on the turntable is pointing (at). Then further on you state "I want to be able to pick up this whole contraption and have the compass give the same degree readings at different points along the lazy Susan."
    To me, the second statement sounds like you want the compass to report the same readings no matter which way it is pointing. If that is correct, what is the compass for?

    Many years ago I helped a friend move a boat he purchased from Florida to Ohio. When I got on board the owner told the magnetic compass was not working. It always pointed somewhere North-East. One quick look at his console told me why. He had installed a weatherproof loudspeaker about an inch from the compass! That compass and the big magnet on the speaker had a very intimate relationship.
    The point is, placing a magnet on the lazy Susan which also holds the compass will perform the same job as my friend's compass.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-10 10:12
    Capture.JPG


    I want the compass to sense where the arrow is pointing along the turntable. Then, eventually I will blow the fans to move the arrow to a desired location along the circle. Does this make sense?
    465 x 471 - 22K
  • FranklinFranklin Posts: 4,747
    edited 2014-03-10 10:40
    You should be looking for a pot or a rotary encoder like the AS5043 or this http://www.melexis.com/Asset/MLX91204-Datasheet-DownloadLink-5741.aspx rather than a compass.
  • PublisonPublison Posts: 12,366
    edited 2014-03-10 10:47
    Franklin wrote: »
    You should be looking for a pot or a rotary encoder like the AS5043 or this http://www.melexis.com/Asset/MLX91204-Datasheet-DownloadLink-5741.aspx rather than a compass.

    +1.

    I was just going to post that solution.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-10 11:03
    Hmmm.... Well my teacher listed some examples for what we could do and here's what he wrote:

    There are many examples of simple 1st and 2nd order systems that can be combined together in actuator-plant-sensor combinations in order to obtain fourth order (or higher) systems. Note that for mechanical motion, position dynamics are generally 2nd order and speed control is generally 1st order. A 4th order system can be built by having a second order actuator control a 2nd order plant, or perhaps controlling a 2nd order plant with a first order sensor and actuator. You will be shown videos of previous projects in order to get a sense of typical systems for this assignment. Example systems include: positioning a cart (a 2nd order mass) along a beam in order to balance the beam (a 2nd order inertia); positioning a cart to balance an inverted pendulum (another 2nd order and unstable plant); positioning a motor in order to tilt a beam and therefore position a ball on that beam; speed controlling fans to rotate a lazy susan platform to a specific position and using a slow compass to determine position; and so on. Note that you are responsible for 4 orders of control; if you buy a hobby servo that already has built-in position control (such that you don't need to design/tune it), then you do not get credit for implementing those two orders - I will only give you cridt for 1 order for such an actuator. Each project will be reviewed by the instructor to assess the true level of student-implemented system order. There are many other great ideas, so brainstorm with the instructor and your partner.

    So what do you think he meant by usage of a compass then?
  • PublisonPublison Posts: 12,366
    edited 2014-03-10 11:13
    No disrespect, but your last post should have been in the first post so we knew where you were going with this.

    The first post sounded as you where using the compass in a normal operation.

    What is a slow compass?
  • sylveeskisylveeski Posts: 17
    edited 2014-03-10 12:16
    A "slow compass" is like one of those old school ones with the needle that took a while to settle.
  • sylveeskisylveeski Posts: 17
    edited 2014-03-10 13:55
    So now what are your thoughts? How can I incorporate the HMC5883L? Suggestions?
  • PublisonPublison Posts: 12,366
    edited 2014-03-10 14:47
    sylveeski wrote: »
    So now what are your thoughts? How can I incorporate the HMC5883L? Suggestions?

    No suggestions here. You are using the device in a situation against from it's intended use.
  • FranklinFranklin Posts: 4,747
    edited 2014-03-10 19:56
    and using a slow compass to determine position;
    I think they are referring to a position relative to earth magnetic. Otherwise I also have no suggestions.
  • GenetixGenetix Posts: 1,754
    edited 2014-03-11 14:55
    Why not just use a stepper motor since the position only depends on the number of steps sent to it.
Sign In or Register to comment.