Thursday, June 16, 2011

Accelerometer ADXL345 + Arduino Severino + VB6

Accelerometer ADXL345 memiliki 3 sumbu, X-Axis, Y-Axis dan Z-Axis, pada video ini menunjukkan respon perangkat ini dari berbagai arah gerakan, selanjutnya diproses pada Arduino Severino dan mengirimkannya ke komputer melalui serial RS232 dan ditampilkan pada monitor dalam bentuk Grafik (pemrograman menggunakan VB6)


Schematic Diagram:



List Program

#include <Wire.h>

#define DEVICE (0x53)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
#define TRIGGER 16  // pin Analog2 connect to CS ADXL345,active HIGH

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
char str[64];          //string buffer to transform data before sending it to the serial port

void setup()
{ pinMode(TRIGGER,OUTPUT);
  digitalWrite(TRIGGER,HIGH);
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  
  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);      
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
}

void loop()
{
  int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  int x, y, z;
  digitalWrite(TRIGGER,LOW);
  delay(15);
  digitalWrite(TRIGGER,HIGH);
  //
  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
  
   //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
   //thus we are converting both bytes in to one int
  x = (((int)buff[1]) << 8) | buff[0];   
  y = (((int)buff[3])<< 8) | buff[2];
  z = (((int)buff[5]) << 8) | buff[4];
  
  //we send the x y z values as a string to the serial port
  sprintf(str, "x=%4d y=%4d z=%4d", x, y, z);  
  Serial.print(str);
  Serial.print(10, BYTE);
  
  //It appears that delay is needed in order not to clog the port
  delay(20);
}

//---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
   Wire.beginTransmission(device); //start transmission to device 
   Wire.send(address);        // send register address
   Wire.send(val);        // send value to write
   Wire.endTransmission(); //end transmission
}

//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
  Wire.beginTransmission(device); //start transmission to device 
  Wire.send(address);        //sends address to read from
  Wire.endTransmission(); //end transmission
  
  Wire.beginTransmission(device); //start transmission to device
  Wire.requestFrom(device, num);    // request 6 bytes from device
  
  int i = 0;
  while(Wire.available())    //device may send less than requested (abnormal)
  { 
    buff[i] = Wire.receive(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}

Program pada Komputer (VB6)
 
 

List Program

Dim Y1, Y2, Y3, y1old, y2old, y3old, x, sg1, sg2, sg3 As Integer
Dim lebar, tinggi As Long

Public Sub init_gbr()
lebar = Form1.Width
tinggi = Form1.Height
Label1.Top = tinggi - 1800
Label2.Top = tinggi - 1800
Label3.Top = tinggi - 1800
Label4.Top = tinggi - 1200
Label5.Top = tinggi - 1200
Text1.Top = tinggi - 1800
Text2.Top = tinggi - 1800
Text3.Top = tinggi - 1800
sg1 = tinggi \ 4
sg2 = sg1 * 2
sg3 = sg1 * 3
Y1 = sg1
Y2 = sg2
Y3 = sg3
y1old = Y1
y2old = Y2
y3old = Y3
x = 0
Form1.Cls
Line (0, sg1)-(lebar, sg1), RGB(255, 0, 0)
Line (0, sg2)-(lebar, sg2), RGB(0, 128, 0)
Line (0, sg3)-(lebar, sg3), RGB(0, 0, 255)
End Sub

Private Sub Form_Load()
MSComm1.CommPort = 1 ' port COM1
MSComm1.PortOpen = True
init_gbr
End Sub

Private Sub Form_Resize()
init_gbr
End Sub

Private Sub Timer1_Timer()
rx = MSComm1.Input
If (Len(rx) > 0 And Val(rx) <> 0) Then
   sbx = Val(Left(rx, 4)) - 5: ' 5 kalibrasi
                  sby = Val(Mid(rx, 6, 4))
                  sbz = Val(Mid(rx, 11, 4)) - 172: ' 172 kalibrasi
                  Text1.Text = sbx
                  Text2.Text = sby
                  Text3.Text = sbz
                  Y1 = sg1 - sbx * 3
                  Y2 = sg2 - sby * 3
                  Y3 = sg3 - sbz * 3
                  PSet (x, y1old), RGB(255, 0, 0)
                  Line (x, y1old)-(x + 50, Y1), RGB(255, 0, 0)
                  PSet (x, y2old), RGB(0, 255, 0)
                  Line (x, y2old)-(x + 50, Y2), RGB(0, 128, 0)
                  PSet (x, y3old), RGB(0, 0, 255)
                  Line (x, y3old)-(x + 50, Y3), RGB(0, 0, 255)
                  y1old = Y1
                  y2old = Y2
                  y3old = Y3
                  x = x + 50
                 If x >= lebar - 50 Then
                     init_gbr
                  End If
End If
End Sub

9 comments:

Syafruddin said...

Dimana ya bisa belajar atau kursus mengenai Arduino ini.

Metin Kahraman said...

can you share more datail for this project

Aan Darmawan H said...

OK, i'll upload a schematic and list of program soon....

Aan Darmawan H said...

Done

Unknown said...

boleh anda kongsikn cara untuk keluarkn graph??software atau application arduino...tq

Unknown said...

boleh saya tau bagaimana cara membuat program VB nya?

dan apakah sama jika menggunakan ADXL 335 ?

Terimakasih

Anonymous said...

Hello,

I have been able to get the accelerometer data to a RTB in VB , but am not sure on how to display it in a real time graph. Are you willing to send me the code in VB? my email address is dicecontroller4@gmail.com.

Thanks....

Aan Darmawan H said...

@Hardyanto and @Ed Duda :
I have already upload list of program in VB, I hope you can try .... :-)

Unknown said...

kalo mau menggunakan matlab dengan metode fft secara realtime gimana ya?