Wednesday, June 22, 2011

RTC (Real Time Clock) DS1307 utk Arduino atau ATMEGA 8/16/32

Modul  RTC (Real Time Clock) menggunakan IC DS1307 mencatat Tanggal dan waktu (jam). RTC ini dapat digunakan pada Arduino atau ATMEGA 8/16/32 melalui komunikasi I2C


Aplikasi RTC pada arduino severino +LCD 16x2 sebagai calendar:

Diagram Koneksi RTC pada Arduino (Severino):

Agar program berfungsi , harus menggunakan librari RTClib.h yang dapat diunduh di  Ladyada (modul kit RTC ini compatible dengan RTC produk Ladyada)

List Program untuk Test :

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
} 

Friday, June 17, 2011

Aplikasi Accelerometer ADXL345 mengendalikan 2 motorservo

Video ini memperlihatkan aplikasi Accelerometer ADXL345 untuk mengatur gerakan 2 buah servo motor (sumbu x dan sumbu y)
 

Schematic:


List Program

// ADXL345 control 2 Servo Motors
// Modified by: Aan Darmawan
// valfa.blogspot.com
// June 2011
//
#include <Wire.h>
#include <Servo.h> 
Servo myservox; // servo for x axis
Servo myservoy; // servo for y axis
#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 (16) connect to CS Adxl345, active high
byte buff[TO_READ] ;  //6 bytes buffer for saving data read from the device
int i;

void setup()
{
  pinMode(TRIGGER,OUTPUT);
  digitalWrite(TRIGGER,HIGH);
  Wire.begin();        // join i2c bus (address optional for master)
  myservox.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservoy.attach(10); // attaches the servo on pin 10 to the servo object 
  myservox.write(90);  // tell servo x to go to position 90 deg
  delay(25);           // makesure servo move
  myservoy.write(90);  // tell servo y to go to position 90 deg
  delay(25);          // makesure servo move
  //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(10);
  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
  //read 5 times and average them
  x=0;
  y=0;
  z=0;
  for(i=1;i<=5;i++){
    x += (((int)buff[1]) << 8) | buff[0];   
    y += (((int)buff[3])<< 8) | buff[2];
    z += (((int)buff[5]) << 8) | buff[4];
    delay(10);
  }
  x/=5;
  y/=5;
  z/=5;
  if(x<-255)x= -255; else if (x>255)x=255;
  if(y<-255)y= -255; else if (y>255)y=255;
  x=map(x, -255, 255, 0, 180);  // map range y
  y=map(y, -255, 255, 0, 180);  // map range y
  myservox.write(x);            // tell servo to go to position in variable x
  delay(25); 
  myservoy.write(180-y);            // tell servo to go to position in variable y 
  delay(25); 
   //It appears that delay is needed in order not to clog the port
  delay(200);
}

//---------------- 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
}


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

Saturday, March 26, 2011

Vibration Sensor using Piezoelectric on Arduino (Sensor Getar dengan Piezoelectric pd Arduino)

Piezoelectric adalah komponen yang dapat menghasilkan tegangan listrik sebagai respon dari suatu perubahan tekanan mekanik. Dalam proyek ini Piezoelectric digunakan sebagai sensor tekanan mekanik (yang diperoleh dari getaran) dan hasil keluarannya yang berupa tegangan listrik dibaca melalui input analog arduino dan hasilnya dikirim ke komputer melalui serial RS-232, pada komputer data-data ini ditampilkan dalam bentuk grafik sinyal.

Video Demo

Skema rangkaian
 List Program (Arduino)
// Program Deteksi getar dengan Piezoelectric
// Oleh : Aan Darmawan
// valfa.blogspot.com
// maret 2011

/* Keterangan skema:
 * Sambungkan Output Piezo ke pin A0 (Analog input pin 0) Arduino
 * Pin 8 output ke relay , jika nilai getaran mencapai 800, Relay ON
 */

// deklarasi variabel
int mgetar;
int getarPin = 0;

void setup() {
  pinMode(8,OUTPUT);
  // aktifkan serial port
  Serial.begin(9600);
}

void loop() {
  // baca getaran dari A0
  mgetar =analogRead(getarPin);
  //kirim ke serial
  Serial.println(mgetar);
  if(mgetar>=800) //jika getaran cukup keras
  {
    digitalWrite(8,HIGH); // aktifkan relay
    delay(2000); // delay 2 detik
  }
  else digitalWrite(8,LOW);
  delay(30); // berhenti beberapa milidetik
 
List Program Visual Basic 6
 
 
Public x1, y1, x2, y2 As Integer
Private Sub Command1_Click()
  If Command1.Caption = "START" Then
    MSComm1.PortOpen = True
    Timer1.Enabled = True
    Command1.Caption = "STOP"
  Else
    MSComm1.PortOpen = False
    Timer1.Enabled = False
    Command1.Caption = "START"
  End If
  
End Sub

Private Sub Form_Load()
  Timer1.Enabled = False
Timer1.Interval = 30
  x1 = 2
  y1 = 450
  x2 = 2
  y2 = 450
End Sub

Private Sub Form_Paint()
  Form1.DrawWidth = 2
  Form1.PSet (x1, y1), RGB(255, 0, 0)
  Form1.Line (1, 70)-(1, 452), RGB(255, 0, 0)
  Form1.Line (1, 452)-(620, 452), RGB(255, 0, 0)
  For i = 6 To 616 Step 10
    Form1.Line (i, 450)-(i, 455), RGB(255, 0, 0)
  Next i
  For i = 452 To 72 Step -10
    Form1.Line (0, i)-(5, i), RGB(255, 0, 0)
  Next i
  Form1.PSet (x1, y1), RGB(0, 0, 255)
End Sub

Private Sub Timer1_Timer()
  hasil = MSComm1.Input
  If Len(hasil) > 0 Then
    n = Val(hasil)
    If n > 50 Then n = n / 2.273 Else If n > 5 Then n = n * 2
    Text1.Text = n
    y2 = 450 - n
    If y2 <= 186 Then
      y2 = 186
      Form1.Line (x1, y1)-(x2, y2), RGB(255, 0, 0)
    Else
      Form1.Line (x1, y1)-(x2, y2), RGB(0, 0, 255)
    End If
    x1 = x2
    y1 = y2
    x2 = x2 + 1
    If x2 >= 620 Then
      x1 = 2
      x2 = 2
      Form1.Cls
      Form1.PSet (x1, y1), RGB(255, 0, 0)
      Form1.Line (1, 70)-(1, 452), RGB(255, 0, 0)
      Form1.Line (1, 452)-(620, 452), RGB(255, 0, 0)
      For i = 6 To 616 Step 10
        Form1.Line (i, 450)-(i, 455), RGB(255, 0, 0)
      Next i
      For i = 452 To 72 Step -10
        Form1.Line (0, i)-(5, i), RGB(255, 0, 0)
      Next i
      Form1.PSet (x1, y1), RGB(0, 0, 255)
    End If
  End If
End Sub

Friday, March 18, 2011

Temperature Sensor using LM35 and LCD Display on Arduino (Sensor suhu dengan LM35 dan LCD Display pada Arduino)

LM35 adalah IC sensor suhu yang presisi, keluarannya berupa tegangan yang proposional linier terhadap perubahan suhu dalam derajat Celsius. Dalam video ini memperlihatkan aplikasi sensor suhu IC LM35 untuk mengatur Nyala/Padam Kipas, hasil pengukuran dari sensor suhu ditampilkan pada LCD Display 16x2. Jika suhu lebih besar sama dengan 30 derajat Celsius Kipas akan nyala, sebaliknya apabila suhu lebih kecil dari 30 derajat Celsius kipas akan padam.

Demo Video hasil:




Skema Rangkaian:



List Program:

/* Keterangan skema:
 * Sambungkan LCD RS pin ke pin 12 Arduino
 * Sambungkan LCD enable pin ke pin 11 Arduino
 * Sambungkan LCD pins D4 s.d D7 ke pin 5 s.d 2 Arduino
 * Sambungkan LCD +5 dan ground ke pin +5V dan ground Arduino
 * Sambungkan LCD Vo pin ke potensiometer. Guna potensiometer ini adalah untuk mengatur kontras LCD
 * Sambungkan IC LM35 bagian output (Kaki tengah) ke pin A0 (Analog input pin 0) Arduino
 * Pin 13 arduino sebagai output, jika temp >=30 derajat Pin 13 logik 1 
 */

// Program demo LCD

// Gunakan library LCD
#include "LiquidCrystal.h";

// Inisialisasi LCD dan menentukan pin yang dipakai
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// deklarasi variabel
float tempC;
int tempPin = 0;

void setup() {
  pinMode(13,OUTPUT);
  // Serial.begin(9600);
  // Set jumlah kolom dan baris LCD
  lcd.begin(16, 2);
  // Tulis Temperatur di LCD
  lcd.print("Temperatur:");
}

void loop() {
  // Set cursor ke kolom 0 dan baris 1
  // Catatan: Baris dan kolom diawali dengan 0
  lcd.setCursor(0, 1);
  // baca data dari sensor
  tempC = analogRead(tempPin);
  // konversi analog ke suhu
  tempC = (5.0 * tempC * 100.0)/1024.0;
  // tampilkan ke LCD
  lcd.print(tempC);
  // Serial.println(tempC);
  if(tempC>=30) //jika temperatur >=30 derajat
    digitalWrite(13,HIGH);
  else digitalWrite(13,LOW);
  delay(2000); // berhenti 2 detik untuk menunggu perubahan temperatur
 
 
 
Perlu income tambahan ? coba masuk sini