Monday, February 14, 2011

ADXL335 Accelerometer

Accelerometer is a sensor that measure linear acceleration. Today we will use ADXL335 accelerometer, we will write a program to display the measured values of acceleration of all 3 axes.
ADXL335 on a board(Manufactured by Sparkfun.com)
You can see axis scheme on the board, the x and y axises directions are clear, and the z axis direction is point out of the board. Remember this sensor has a sensing range of +/-3g, so you can't measure a higher accelerations :). But it is not a big disadvantage, because the calculation shows that more than 100 km/h we can get during 1 second of 3g acceleration. If you want to check it but you are lazy to do, you can use http://www.wolframalpha.com(3 * gravitational acceleration * 1 second in km/h).
The interface of this device is analog: The voltages on X(Y,Z) port depends on the linear acceleration throughout X(Y,Z) axis. The Arduino board has 6 analog-in ports, we can use 3 of them(0,1,2) to connect to X,Y,Z ports of ADXL335.
Analog ports

So lets open the Arduino IDE and write a simple program, which will periodically read the values on analog ports and send trough the Serial port.
const int xpin = 0;
const int ypin = 1;
const int zpin = 2;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("BEG");
Serial.print("X"); Serial.print(analogRead(xpin));
Serial.print("Y"); Serial.print(analogRead(ypin));
Serial.print("Z"); Serial.print(analogRead(zpin));
Serial.println();
delay(50);
}
view raw 1.pde hosted with ❤ by GitHub

In this program analogRead function read the voltage on the pin and return the integer value between 0 and 1023.
On the computer side we will read the information coming through Serial port an show the values on 3 horizontal progress bars.

This is C# code of program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace ADXL335
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SerialPort serial_port_;
private Thread thread_;
private void refresh_button_Click(object sender, EventArgs e)
{
portlist_comboBox.Items.Clear();
foreach (string s in SerialPort.GetPortNames())
{
portlist_comboBox.Items.Add(s);
}
}
private void connect_button_Click(object sender, EventArgs e)
{
if ("Connect" == connect_button.Text)
{
if (-1 == portlist_comboBox.SelectedIndex)
{
MessageBox.Show("Select com port");
return;
}
string port_name = portlist_comboBox.SelectedItem.ToString();
serial_port_ = new SerialPort(port_name, 9600);
serial_port_.Open();
thread_ = new Thread(read_serial_port);
thread_.Start();
connect_button.Text = "Disconnect";
}
else
{
try
{
thread_.Abort();
serial_port_.Close();
}
catch
{
}
connect_button.Text = "Connect";
}
}
private void read_serial_port()
{
while (serial_port_.IsOpen)
{
try
{
string str = serial_port_.ReadLine();
set_axis(str);
}
catch (TimeoutException) { }
}
}
delegate void SetTextCallback(string newText);
private string str_;
private void set_axis(string text)
{
if (this.X_progressBar.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(set_axis);
this.Invoke(d, new object[] { text });
}
else
{
str_ += text;
int i1 = str_.IndexOf("BEG");
int i2 = str_.IndexOf("BEG", 1 + i1);
if ((-1 != i1) && (-1 != i2))
{
string current = str_.Substring(0, i2);
current = current.Substring(current.IndexOf("BEG"));
str_ = str_.Substring(i2);
int beg_index = current.IndexOf("BEG");
int x_index = current.IndexOf("X");
int y_index = current.IndexOf("Y");
int z_index = current.IndexOf("Z");
if ((0 != beg_index) || (3 != x_index) || (-1 == y_index) || (-1 == z_index))
{
MessageBox.Show("Error " + current + ", beg_index = " + beg_index.ToString() + ", x_index = " + x_index.ToString() + ", y_index = " + y_index.ToString() + ", z_index = " + z_index.ToString());
return;
}
int x = Convert.ToInt32(current.Substring(x_index + 1, y_index - x_index - 1));
int y = Convert.ToInt32(current.Substring(y_index + 1, z_index - y_index - 1));
int z = Convert.ToInt32(current.Substring(z_index + 1));
if (x > X_progressBar.Maximum) x = X_progressBar.Maximum;
if (y > Y_progressBar.Maximum) y = Y_progressBar.Maximum;
if (z > Z_progressBar.Maximum) z = Z_progressBar.Maximum;
if (x < X_progressBar.Minimum) x = X_progressBar.Minimum;
if (y < Y_progressBar.Minimum) y = Y_progressBar.Minimum;
if (z < Z_progressBar.Minimum) z = Z_progressBar.Minimum;
X_progressBar.Value = x;
Y_progressBar.Value = y;
Z_progressBar.Value = z;
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
serial_port_.Close();
}
catch
{
}
}
}
}
view raw 1.cs hosted with ❤ by GitHub

Be careful, connect ADXL335 to the Arduino board, before connecting them to the PC, otherwise the board will hang and only solution is reconnecting.

This is a short video how it runs.

1 comment:

  1. can u sent n me the entire code to yohannan,basil@gmail.com plz plz

    ReplyDelete