Author Archives: pccontrol

Dasar pemrograman I2C pada Arduino

[DRAFT]

Sebelum memprogram I2C pada arduino ada baiknya  Anda mengetahui dasar komunikasi i2c  .Dasar  Cara kerja I2C  bisa dibaca disini

Contoh  device yang sering dikoneksi ke arduino  melalui komunikasi i2c antara lain : io expander  , memory eeprom external , LCD , dsb.

berikut ini contoh koneksi Arduino (sebagai master )  dengan beberapa  device slave berikut ini : ic RTC ds1307,  ic EEPROM At24C32(ada di modul RTC)  dan ic io expander PCF8574  (yg dipakai di LCD matrix.) .adapun koneksinya adalah sbb:

RTC_LCD_I2C

..

i2ccircuit2

 

library  utama yg digunakan untuk  komunikasi i2c adalah wire.h  yang anda bisa dicek  di library manager . ( jika hanya cek library  apakah sdh terinstal atau blm  internet anda tdk perlu  online. Untuk intsall library baru melalui library manager internet harus online)

menulibrarymanager
setelah muncul menu library manager  , ketik wire pada kolom pencarian.

wirelib.jpg

pada gambar diatas terlihat bahwa library wire telah terinstall (INSTALLED

selain itu dibutuhkan juga library khusus device slave yg kita gunakan antara lain

  1. untuk RTC DS1307 :  DS1307RTC.h
  2. untuk AT24C32       :  At24CXX.h
  3. untuk LCD i2c         : LiquidCrystal_I2C.H

(anda bisa menggunakan library selain yg diatas, banyak pilihan ada di menu library manager  ).  saat menginstall library melalui library manager pastikan internet anda aktif.

Langkah langkah memprogram device I2C

  1. cek alamat device   dengan sketch scan device yg terkoneksi ke arduino
  2. instal library device tsb
  3. buat sketch untuk read/write device i2c
  4. upload sketch ke arduino

 

Berikut ini contoh sketch untuk scan alamat device slave  i2c yang terkoneksi dengan master i2c (arduino).

#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial);
Serial.println(“\nI2C Scanner”);
}

 

void loop()
{
byte kode_eror, alamat;
int nDevices;
Serial.println(“Scanning…cari chip i2c pd alamat 1 – 127”);
nDevices = 0;
for(alamat = 1; alamat < 127; alamat++ )
{
Wire.beginTransmission(alamat);
kode_eror = Wire.endTransmission();
if (kode_eror == 0)
{
Serial.print(“I2C device alamat: 0x”);
Serial.println(alamat,HEX);
nDevices++;
}
else if (kode_eror==4)
{
Serial.print(“ada device eror”);
}
}
if (nDevices == 0)
Serial.println(“tak terdeteksi device i2c \n”);
else
Serial.println(“selesai\n”);
delay(5000);
}

upload sketch diatas  dan buka menu monitor serial  contoh hasil scan :

scanSlavei2c

alamat 0x27  adalah alamat  IO expander  pcf8574 yg digunakan pada LCD matrix

alamat 0x50 adalah alamat  EEProm At24C32

alamat 0x68 adalah alamat RTC DS1307

contoh Kode sketch untuk  LCD i2c dan DS2307 bisa dilihat di artikel sebelumnya .

Berikut ini contoh kode sketch  menulis dan membaca memori i2c 24X32

#include <Wire.h>

void write(unsigned int address, byte data);
byte _pageSize=32;
byte _id=0x50; // id /alamat  chip 24C32
byte _b[8];

 

void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}

unsigned int alamatMemori=0;
void loop() {
// put your main code here, to run repeatedly:
writeInt(8,999);
delay(1000);
Serial.print(“alamat=”);
Serial.println(alamatMemori);
Serial.print(“data=”);
Serial.println(readInt(alamatMemori++));
delay(2000);
if(alamatMemori==32)alamatMemori=0; //reset alamat counter
}

/**
* Write byte
*/
void write(unsigned int address, byte data)
{
Wire.beginTransmission(_id);
if(Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
Wire.write(data);
Wire.endTransmission();
delay(20);
}
}

//=======================

/**
* Write sequence of n bytes
*/
void write(unsigned int address, byte *data, int n)
{
// status quo
int c = n; // bytes left to write
int offD = 0; // current offset in data pointer
int offP; // current offset in page
int nc = 0; // next n bytes to write

// write alle bytes in multiple steps
while (c > 0) {
// calc offset in page
offP = address % _pageSize;
// maximal 30 bytes to write
nc = min(min(c, 30), _pageSize – offP);
write(address, data, offD, nc);
c-=nc;
offD+=nc;
address+=nc;
}
}

/**
* Write sequence of n bytes from offset
*/
void write(unsigned int address, byte *data, int offset, int n)
{
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
byte *adr = data+offset;
Wire.write(adr, n);
Wire.endTransmission();
delay(20);
}
}
/**
* Read byte
*/
byte read(unsigned int address) {
byte b = 0;
int r = 0;
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
Wire.requestFrom(_id, 1);
while (Wire.available() > 0 && r<1) {
b = (byte)Wire.read();
r++;
}
}
}
return b;
}

 

void writeInt(unsigned int address, unsigned int data)
{
write(address, (byte*)&data, 2);
}

 

/**
* Read integer
*/
unsigned readInt(unsigned int address) {
read(address, _b, 2);
return *(unsigned int*)&_b[0];
}

 

/**
* Read sequence of n bytes
*/
void read(unsigned int address, byte *data, int n) {
int c = n;
int offD = 0;
// read until are n bytes read
while (c > 0) {
// read maximal 32 bytes
int nc = c;
if (nc > 32)
nc = 32;
read(address, data, offD, nc);
address+=nc;
offD+=nc;
c-=nc;
}
}

 

/**
* Read sequence of n bytes to offset
*/
void read(unsigned int address, byte *data, int offset, int n) {
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
int r = 0;
Wire.requestFrom(_id, n);
while (Wire.available() > 0 && r<n) {
data[offset+r] = (byte)Wire.read();
r++;
}
}
}
}

 

 

 

Advertisements

Contoh Code Sketch untuk LCD matrix I2C dan RTC1307 I2C dipakai bersama .

RTC_LCD_I2C

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
tmElements_t tm;
unsigned int counter=0;
void setup() {

lcd.init(); // lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0,0); // Set LCD cursor position (column, row)
lcd.print(“DS1307 + LCD-I2C”);
lcd.setCursor(0, 1); // Set LCD cursor position (column, row)
}

void loop()
{
digitalClockDisplay();
}

void digitalClockDisplay()
{
//tmElements_t tm;
if (RTC.read(tm))
{
lcd.setCursor(0, 1); // Set LCD cursor position (column, row)
print2digits(tm.Hour);
lcd.print(“:”);
print2digits(tm.Minute);
lcd.print(“:”);
print2digits(tm.Second);
//lcd.setCursor(4, 2);
print2digits(tm.Day);
lcd.print(“/”);
print2digits(tm.Month);
lcd.print(“/”);
print2digits(tm.Year+1970-2000);
//lcd.setCursor(4, 3);
//lcd.print(tm.Wday);
}

}
//=================
void print2digits(int number)
{
if (number >= 0 && number < 10) {
lcd.print(‘0’);
}
lcd.print(number);
}

/*
void TimeCheck()
{
int tem[17];
for(int i=11;i<17;i++)
{
tem[i]=EEPROM.read(i);
}
if(Hor == tem[11] && Min == tem[12] && off==1)
{
add=11;
lcd.clear();
lcd.print(“alarm………..”);
}
}
*/

Contoh Code Parsing Data string Serial Arduino

 

String readString; //main captured String

String data1; //data String
String data2;
String data3;
String data4;
String data5;

//format data diterima   dari pc atau arduino lainya : 123a123b123c123d123*

void setup()
{
Serial1.begin(9600);

}

void loop()
{
TerimaData();
}//loop

void TerimaData(void)
{

if( Serial1.available())
{

//=================
char c = Serial1.read();           //gets one byte from serial buffer
if (c == ‘*’)
{
ind1 = readString.indexOf(‘a’); //finds location of first ,
data1 = readString.substring(0, ind1); //captures first data String

ind2 = readString.indexOf(‘b’, ind1+1 ); //finds location of second ,
data2 = readString.substring(ind1+1, ind2+1); //captures second data String

ind3 = readString.indexOf(‘c’, ind2+1 );
data3 = readString.substring(ind2+1, ind3+1);

ind4 = readString.indexOf(‘d’, ind3+1 );
baris4 = readString.substring(ind3+1); //captures remain part of data after last ,

ind5 = readString.indexOf(‘*’, ind4+1 );
data5 = readString.substring(ind4+1); //captures remain part of data after last ,

readString=””; //clears variable for new input

data1=””;
data2=””;
data3=””;
data4=””;
data5=””;
}
else
{
readString += c;     //  data serial yg dibaca gabungkan
}

}

Pengetahuan Dasar Arduino: Dasar RTC DS1307 Menampilkan Jam dan Tanggal pada LCD

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h> // library ini berisi class  RTCDS1307
#include <LiquidCrystal.h>
LiquidCrystal lcd( 5,6,7,8,9,10);//(rs, en,d0, d1, d2, d3)    pin rw di hubungkan dg GND
tmElements_t  waktu;     //membuat objeck dari class  DS1307RTC

void setup() {
lcd.begin(20, 4);
lcd.setCursor(0, 0); // Set LCD cursor position (column, row)
lcd.print(“TEST RTC DS1307”);
lcd.setCursor(0, 1); // Set LCD cursor position (column, row)
lcd.print(“JAM:”);
//lcd.setCursor(0, 2); // Set LCD cursor position (column, row)
//lcd.print(“TGL:”);
//lcd.setCursor(0, 3); // Set LCD cursor position (column, row)
//lcd.print(“Day:”);

}

void loop()
{
digitalClockDisplay();
delay(1000);
}

void digitalClockDisplay()
{

if (RTC.read(waktu))
{
lcd.setCursor(4, 1); // Set LCD cursor position (column, row)
print2digits(waktu.Hour);
lcd.print(“:”);
print2digits(waktu.Minute);
lcd.print(“:”);
print2digits(waktu.Second);

//lcd.setCursor(4, 2);
//print2digits(waktu.Day);
//lcd.print(“/”);
//print2digits(waktu.Month);
//lcd.print(“/”);
// lcd.print(tmYearToCalendar(waktu.Year));  //atau lcd.print(waktu.year+1970);
//lcd.setCursor(4, 3);
//lcd.print(waktu.Wday);
}

}
//=================
void print2digits(int number)
{
if (number >= 0 && number < 10) {
lcd.print(‘0’);
}
lcd.print(number);
}

 

Video Tutorial Dasar Arduino Bag-4: Built in Function

Video Tutorial Dasar Arduino Bag-4: built in function

Video Tutorial Dasar Arduino Bag-3: Sofware Arduino

Video Tutorial Dasar Arduino Bag-3: Sofware Arduino

Video Tutorial Dasar Arduino Bag-2: hardware Arduino

Video Tutorial Dasar Arduino Bag-2: hardware Arduino

Video Tutorial Dasar Arduino Bag-1: Mengenal Arduino

Tutorial Dasar Arduino Bag-1: Mengenal Arduino

 

Ebook Pemrograman Android Berbahasa Indonesia Resmi dari Google

https://chromplex.com/unduh-ebook-pemrograman-android-berbahasa-indonesia-resmi-dari-google/

Pengetahuan Dasar Membuat Aplikasi VB.Net untuk Monitoring Counter Produksi via LAN

sumber  :  www.counterproduksi.com

Panduan teknis  untuk membaca/menulis  data  ke  mikro counter dan Andon 7 segment maupun Andon matrik p10.

vbcounter

Untuk  mengakses data counter menggunakan aplikasi VB.Net atau C#  anda akan diberikan class library untuk setiap pembelian counter produksi. Dengan class library ini pemrograman VB.net atau C# untuk mengakses data counter menjadi sangat mudah  dilakukan.

vbcounter

Step by step Cara Mengakses (baca/tulis)  Data Counter dengan VB.Net  via LAN (TCP)

1. Menambahkan library counter*  ke dalam projet VB.net

Melalui menu Project->Add reference
Lalu pada code tambahkan:
Imports CounterTCP
——————————————————————————–
2. Membuat Obect counter

Private CounterLineA As CounterTCP.Client
Private CounterLineB As CounterTCP.Client

——————————————————————————–
3. Membuat koneksi ke Counter

Try
‘ Create new client Counter  and add event functions
CounterLineA = New Client(txtAlamatIPLineA..Text, NoPort)

AddHandler CounterLineA.OnResponseData, New CounterTCP.Client.ResponseData(AddressOf CounterLineA_OnResponseData)

Catch [error] As SystemException
‘MessageBox.Show([error].Message)
End Try
——————————————————————————–
4. Membaca Counter

// Membaca data Counter Line A
Try
CounterLineA.ReadDataCounter( 0, 15)  ‘baca data  0 – 15
Catch [error] As SystemException
MessageBox.Show(“READ EROR”)
End Try

// Membaca data Counter Line B
Try
CounterLineB.ReadDataCounter( 0, 15)  ‘baca data  0 – 15
Catch [error] As SystemException
MessageBox.Show(“READ EROR”)
End Try

——————————————————————————– 
5. Menampilkan Data Counter ke Form

Private Sub CounterLineA_OnResponseData()

Dim data As Byte()

data = values
TextBox1.Text = (data(0) * 256 + data(1)).ToString()   ‘ data  ke-1  plan
TextBox2.Text = (data(2) * 256 + data(3)).ToString()  ‘data ke-2    actual
TextBox3.Text = (data(4) * 256 + data(5)).ToString()   ‘data ke-3   target
TextBox4.Text = (data(6) * 256 + data(7)).ToString()    ‘data ke- 4… dst

End Sub
——————————————————————————–

6. Menulis data ke   Counter

Private Sub Button9_Click()
Dim data(1) As Integer
Dim Address As UShort

Address = Convert.ToUInt16(txtAdress.Text)
data(0) = Convert.ToInt16(txtData.Text)

Try
CounterLineA.WriteDataCounter(Address, data)
Catch [error] As SystemException
MessageBox.Show([error].ToString)
End Try

End Sub

——————————————————————————–
7. Memutus koneksi aplikasi  dgn  Counter

CounterLineA.disconnect()

——————————————————————————–

Contoh Form menampilkan data Counter

form display counter

*Class Library Counter untuk VB.net / C# disertakan pada setiap pembelian counter produksi  . www.counterproduksi.com

iklan