ADX335 based Gy-61 3 axis accelerometer

The Gy-61 module contains a 3 axis accelerometer as well as a voltage regulator.

Below is an example sketch that turns this module into a spirit level, also using our 20X4 LCD module.

 

/* 
//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the ADXL335, prints calculated orientation via serial
//////////////////////////////////////////////////////////////////


Modified 2014-08-09 by ChilliTronix Ltd to demonstrate the GY61 
accellerometer and 20 X 4 LCD


 
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 10
* LCD D5 pin to digital pin 9
* LCD D6 pin to digital pin 8
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
LCD VDD to 5V
LCD VSS to GND


*/

//Set up LCD
//include the library code:
#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

//Analog read pins
const int xPin = 0;
const int yPin = 1;
const int zPin = 2;

//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 265;
int maxVal = 402;


//to hold the caculated values
double x;
double y;
double z;
int xRead;
int yRead;
int zRead;
int xAng;
int yAng;
int zAng;



void setup()
{
  lcd.begin(20, 4); //Set LCD to 20X4)
  lcd.clear();
  Serial.begin(9600);           // sets the serial port to 9600
  lcd.setCursor(0,0);
  lcd.print("ChilliTronix Accellerometer");
}
void loop()
{

  //read the analog values from the accelerometer
  xRead = analogRead(xPin);
  yRead = analogRead(yPin);
  zRead = analogRead(zPin);

  //convert read values to degrees -90 to 90 - Needed for atan2
  xAng = map(xRead, minVal, maxVal, -90, 90);
  yAng = map(yRead, minVal, maxVal, -90, 90);
  zAng = map(zRead, minVal, maxVal, -90, 90);

  //convert read values to degrees -90 to 90 - Needed for atan2
  xAng = map(xRead, minVal, maxVal, -90, 90);
  yAng = map(yRead, minVal, maxVal, -90, 90);
  zAng = map(zRead, minVal, maxVal, -90, 90);

  //Caculate 360deg values like so: atan2(-yAng, -zAng)
  //atan2 outputs the value of -π to π (radians)
  //We are then converting the radians to degrees
  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

  //Output the caculations
  Serial.print("x: ");
  Serial.print(x);
  Serial.print(" | y: ");
  Serial.print(y);
  Serial.print(" | z: ");
  Serial.println(z);

  lcd.setCursor(0,1);
  lcd.print("X Degrees:");
  lcd.print(x);
  lcd.setCursor(0,2);
  lcd.print("Y Degrees:");
  lcd.print(y);
  lcd.setCursor(0,3);
  lcd.print("Z Degrees:");
  lcd.print(z);
  delay(10);              // wait 100ms for next reading
}