HOW TO INTERFACE AN ACCELEROMETER WITH ARDUINO

 

 

 

In this project we are going to control an led by Accelerometer sensor. Accelerometer is a sensor which calculates the three axis i.e. x,y, and z  axis in 3D space w.r.t a fixed origin. We can easily print value of each axis on Serial monitor of Arduino IDE.

LIST OF COMPONENTS REQUIRED

  1. Arduino uno
  2. Arduino software
  3. Breadboard
  4. Led
  5. Accelerometer
  6. Jumper wires

 

CIRCUIT DIAGRAM

 

 

CIRCUIT DIAGRAM

 

 

CONNECTIONS

The circuit:

  1. Gnd of the sensor to A4( analog input pin 4 of the arduino)
  2. VCC of the sensor A5 (analog input pin 5)
  3. Xaxis to A3 (analog input pin 3)
  4. Yaxis to A2 (analog input pin 2)
  5. Zaxis to A1 (analog input pin 1)
  6. An led to pin 11 of arduino

 

 

CODE

const int groundpin = 18; // analog input pin 4 — ground

const int powerpin = 19; // analog input pin 5 — voltage

const int xpin = A3; // x-axis to pin A3

const int ypin = A2; // y-axis to pin A2

const int zpin = A1; // z-axis to pin A1

void setup()

{

Serial.begin(9600);

pinMode(groundpin, OUTPUT);

pinMode(powerpin, OUTPUT);

digitalWrite(groundpin, LOW); //ground pin is given 0V

digitalWrite(powerpin, HIGH); //power pin is given +5V

pinMode(11,OUTPUT);

}

void loop()

{

int xval=analogRead(xpin); //reads the x axis displacement of accelerometer

int yval=analogRead(ypin); //reads the y axis displacement of accelerometer

int zval=analogRead(zpin); //reads the y axis displacement of accelerometer

// print the sensor values:

Serial.print(analogRead(xpin));

// print a tab between values:

Serial.print(“\t”);

Serial.print(analogRead(ypin));

// print a tab between values:

Serial.print(“\t”);

Serial.print(analogRead(zpin));

Serial.println();

// delay before next reading:

delay(100);

if((xval>265 &&xval< 280 )&&(yval>310 &&yval<340)&&(zval>290 &&zval<340)) //when tilted on right side these readings were obtained

digitalWrite(11,HIGH);

//pin number 11 of arduino uno will +5V to led

if((xval>340 &&xval< 395 )&&(yval>310 &&yval<350)&&(zval>270 &&zval<290))

//when tilted on left side these readings were obtained

digitalWrite(11,HIGH);

if((xval>330 &&xval< 335 )&&(yval>330&&yval<335)&&(zval>268 &&zval<280))

//when kept horizontally i.e. tilted in neither direction than these readings were obtained

digitalWrite(11,LOW);

}

 

 

UNDERSTANDING CODE:

In the void loop() function we are trying to take the readings of x, y, and z axis of the accelerometer and perform the desired task i.e. to switch the led  ON and OFF. We first replace the following code in void loop() of above code in order to take readings from the accelerometer. Than upload the code in arduino and turn serial monitor on of arduino software. Tilt the accelerometer in either direction the serial monitor will show the readings.

 

 

WORKING:

An accelerometer is a device that detects motion. It is available in two types

2 axis accelerometer and 3 axis accelerometer

Axis means the x, y and z direction in which the accelerometer is positioned.

This device gives the readings of x, y and z direcition on serial monitor option available in arduino software when positioned at different angles. We can thus note down the readings at different positions of accelerometer eg. tilted rightwards, tilted leftwards  and many other positions. Thus we can program it in a way that if accelerometer is tilted leftwards than led will be ON. Similarly we can control different devices using accelerometer.

 

 

When Accelerometer is tilted left, LED is ON

 

 

 

 

 

 

 

When accelerometer is placed horizontally, LED is OFF

 

 

 

 

 

 

 

When Accelerometer is tilted Right, LED is ON
5 Comments

5 Replies to “HOW TO INTERFACE AN ACCELEROMETER WITH ARDUINO”

  1. I believe this is one of the such a lot vital information for me. And i’m glad studying your article. However should statement on some common things, The web site style is wonderful, the articles is actually great : D. Excellent activity, cheers

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: