Shop OBEX P1 Docs P2 Docs Learn Events
Accelerometer Help — Parallax Forums

Accelerometer Help

Tom PTom P Posts: 97
edited 2007-10-29 15:59 in General Discussion
Need some help with the 3-axis accelerometer. I would like to modify the demo code so that I can set off an alarm i.e. an LED when the vertical axis force is over .5g. Observation: It appears that the when the sensor is laying flat, the vertical g force component is = 1, so the I have to place the sensor vertical to produce a zero g at rest! HELP - HELP


' H48C_3-Axis.BS2
' Hitachi H48C 3-Axis Accelerometer Demonstration
' Author..... Copyright (c) 2005-2006 Parallax, Inc.
' {$STAMP BS2}
' {$PBASIC 2.5}
'

'
' axis - vref 3.3
' G =
x
' 4095 0.3663
'
' For use in the program the forumla can be simplified to:
'
' G = (axis - vref) x 0.0022
'
' To allow the display of fractional g-force in the integer system of the
' BASIC Stamp we multiply 0.0022 by 100 -- this will allow us to display
' g-force in 0.01g units.



Dio PIN 15 ' data to/from module
Clk PIN 14 ' clock output
CS PIN 13 ' active-low chip select

XAxis CON 0 ' adc channels
YAxis CON 1
ZAxis CON 2
VRef CON 3

Cnt2Mv CON $CE4C ' counts to millivolts
' 0.80586 with **
GfCnv CON $3852 ' g-force conversion
' 0.22 with **

axis VAR Nib ' axis selection
rvCount VAR Word ' ref voltage adc counts
axCount VAR Word ' axis voltage adc counts
mVolts VAR Word ' millivolts
gForce VAR Word ' axis g-force

dValue VAR Word ' display value
dPad VAR Nib ' display pad


Reset:
HIGH CS ' deselect module
DEBUG CLS, ' paint display
"=========================", CR,
"H48C 3-Axis Accelerometer", CR,
"=========================", CR,
CR,
" Count Volts G ", CR,
"


", CR,
"VRef ", CR,
" X ", CR,
" Y ", CR,
" Z "


Main:
FOR axis = XAxis TO ZAxis ' loop through each axis
GOSUB Get_H48C ' read vRef & axis counts

dValue = rvCount ' display vRef count
DEBUG CRSRXY, 6, 6
GOSUB RJ_Print

dValue = axCount ' display axis count
DEBUG CRSRXY, 6, (7 + axis)
GOSUB RJ_Print

mVolts = rvCount ** Cnt2Mv ' convert vref to mv
DEBUG CRSRXY, 13, 6, ' display
DEC (mVolts / 1000), ".",
DEC3 mVolts

mVolts = axCount ** Cnt2Mv ' convert axis to mv
DEBUG CRSRXY, 13, (7 + axis),
DEC (mVolts / 1000), ".",
DEC3 mVolts

' calculate g-force
' -- "gForce" is signed word

IF (axCount >= rvCount) THEN
gForce = (axCount - rvCount) ** GfCnv ' positive g-force
ELSE
gForce = -((rvCount - axCount) ** GfCnv) ' negative g-force
ENDIF
DEBUG CRSRXY, 20, (7 + axis), ' display g-force
" " + (gForce.BIT15 * 13),
DEC1 (ABS(gForce) / 100), ".",
DEC2 ABS(gForce)
NEXT
PAUSE 200
GOTO Main


' Reads VRef and selected H48C axis through an MCP3204 ADC
' -- pass axis (0 - 2) in "axis"
' -- returns reference voltage counts in "rvCount"
' -- returns axis voltage counts in "axCounts"

Get_H48C:
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, VRef\3] ' select vref register
SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]rvCount\13] ' read ref voltage counts
HIGH CS
PAUSE 1
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, axis\3] ' select axis
SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13] ' read axis voltage counts
HIGH CS
RETURN

RJ_Print:
LOOKDOWN dValue, >=[noparse][[/noparse]10000, 1000, 100, 10, 0], dPad
DEBUG REP " "\dPad, DEC dValue
RETURN

Comments

  • Dennis FerronDennis Ferron Posts: 480
    edited 2007-10-29 15:29
    I believe your sensor is working just as it should.· Think about it; an acceleration of 1 g:· you're measuring gravity!· Imagine a 1 gram weight setting on a scale; if the scale is horizontal it will measure one gram.··You would have to put the scale sideways to measure 0 grams.· (Just like the way your accelerometer is working.)· I've only ever used the 2-axis version so I don't know for sure, but I'm pretty sure your accelerometer is working as it should be.

    I'll hazard a guess and say that you probably have been asked to do this for a class project, right?· I've had a lot of clueless professors come up with arbitrary tasks that can't be done because they didn't themselves understand the physics involved in the experiment.· (Sorry, I'll try not to rant too much.)· Anyhow, what you really want is not a .5 g total, but a change in .5 g.· That would be 1.5 g if you're accelerating up, or .5 g if you're falling down.· Not sure which way you need it.· Anyhow, the key is that you want to arbitrarily "define zero" to be 1.0 g, and any difference from there is what you're really measuring.
  • Tom PTom P Posts: 97
    edited 2007-10-29 15:59
    I believe the sensor is working Ok, as well.
    Yes, this is for a class
    I understand what you are saying about the change in acceleration.
    My experiment is in trying to accelerate up. I see what you are saying about defining 1g as zero, but I do not understand exactly on how to proceed with setting up the code to register an alarm when acceleration reaches .5g. Still need more help on this. Thanks
Sign In or Register to comment.