Help understanding accelermoter code :)

There part I’m specifically confused about is the getAcceleration function. Why are we dividing the ADC_REF with the ADC_Amplitude ? I’m guessing in the spark core it would be 3.3V/4096 ? What is the ZERO_X, ZERO_Y and ZERO_Z. and what role does the sensitivity play ?

/*****************************************************************************/
//	Function:    Cpp file for ADXL335 
//  Hardware:    Grove - 3-Axis Analog Accelerometer
//	Arduino IDE: Arduino-1.0
//	Author:	 FrankieChu		
//	Date: 	 Jan 10,2013
//	Version: v1.0
//	by www.seeedstudio.com
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
/*******************************************************************************/
#include <Arduino.h>
#include "ADXL335.h"

void ADXL335::pinsInit()
{
	pinMode(X_AXIS_PIN, INPUT);
	pinMode(Y_AXIS_PIN, INPUT);
	pinMode(Z_AXIS_PIN, INPUT);
}
void ADXL335::begin()
{
	pinsInit();
	scale = (float)SENSITIVITY*ADC_AMPLITUDE/ADC_REF;
}
void ADXL335::getXYZ(int16_t *x,int16_t *y,int16_t *z)
{
	*x = analogRead(X_AXIS_PIN);
	*y= analogRead(Y_AXIS_PIN);
	*z = analogRead(Z_AXIS_PIN);
}
void ADXL335::getAcceleration(float *ax,float *ay,float *az)
{
	int x,y,z;
	float xvoltage,yvoltage,zvoltage;
	getXYZ(&x,&y,&z);
	xvoltage = (float)x*ADC_REF/ADC_AMPLITUDE;
	yvoltage = (float)y*ADC_REF/ADC_AMPLITUDE;
	zvoltage = (float)z*ADC_REF/ADC_AMPLITUDE;
	Serial.println("voltage:");
	Serial.println(xvoltage);
	Serial.println(yvoltage);
	Serial.println(zvoltage);
	*ax = (xvoltage - ZERO_X)/SENSITIVITY;
	*ay = (yvoltage - ZERO_Y)/SENSITIVITY;
	*az = (zvoltage - ZERO_Z)/SENSITIVITY;
	
}
</pre>

<pre> /*****************************************************************************/
//	Function:    Header file for ADXL335 
//  Hardware:    Grove - 3-Axis Analog Accelerometer
//	Arduino IDE: Arduino-1.0
//	Author:	 FrankieChu		
//	Date: 	 Jan 10,2013
//	Version: v1.0
//	by www.seeedstudio.com
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
/*******************************************************************************/
#ifndef __ADXL335_H__
#define __ADXL335_H__

#include <Arduino.h>
/*macro definitions of Analog read pins*/
#define X_AXIS_PIN A0
#define Y_AXIS_PIN A1
#define Z_AXIS_PIN A2

#define ADC_AMPLITUDE 1024//amplitude of the 10bit-ADC of Arduino is 1024LSB
#define ADC_REF	5	//ADC reference is 5v
#define ZERO_X	1.22 //accleration of X-AXIS is 0g, the voltage of X-AXIS is 1.22v
#define ZERO_Y	1.22 //
#define ZERO_Z	1.25 //
#define SENSITIVITY 0.25//sensitivity of X/Y/Z axis is 0.25v/g

class ADXL335
{
private:
	void pinsInit();
	float scale;
public:
	void begin();
	void getXYZ(int16_t *x,int16_t *y,int16_t *z);
	void getAcceleration(float *ax,float *ay,float *az);
};

#endif

The sensor can measure +/- 3 g so the zero g point cannot be zero volts if you want to measure negative g accelerations. That values for the zero point are biased up to “make room” for negative g measurements while maintaining a positive voltage output. The datasheet for the ADXL335 gives the approximate values used in the code.

ADC_AMPLITUDE should be 4096 and ADC_REF should be 3.3 on Spark as you guessed.

Have fun with it!

2 Likes