Pada kesempatan ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang bisa digunakan untuk mengukur berat atau massa suatu benda, selain mengukur berat atau massa alat ini juga bisa menampilkan nilai tersebut ke komputer dengan software Visual Basic 6 (VB 6). alat ini menggunakan Arduino Uno sebagai kontrollernya dan juga ampli yang digunakan adalah HX711, untuk tampilan ke user menggunakan LCD 16x2. loadcell yang digunakan maks 3kg. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. Modul HX711 + Loadcell 3kg
c. LCD 16x2
d. Program Arduino IDE
//library HX711 bisa di download di link berikut
//https://github.com/bogde/HX711
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
#include "HX711.h"
// HX711.DOUT - pin #2
// HX711.PD_SCK - pin #3
HX711 scale(2, 3); // parameter "gain" is ommited; the default value 128 is used by the library
float tera = 0;
int berat;
float fix;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
Serial.begin(9600);
/*
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
*/
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
/*
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
*/
//lcd.setCursor(0, 0);
//lcd.print("WELLCOME");
// delay(20000);
// lcd.clear();
}
void loop() {
berat = scale.get_units(10) * -1;
fix = ((berat + 1.523) / 0.223) - tera ;
if (fix < 0 ) {
fix = 0;
}
Serial.println(fix);
// Serial.print("one reading:\t");
// Serial.print(scale.get_units(), 1);
// Serial.print("\t| average:\t");
// Serial.println(scale.get_units(10), 1);
lcd.setCursor(0, 0);
lcd.print("Berat= ");
lcd.print(fix);
lcd.print(" gr ");
scale.power_down();
delay(1000);
scale.power_up();
Serial.println(fix);
}
//https://github.com/bogde/HX711
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
#include "HX711.h"
// HX711.DOUT - pin #2
// HX711.PD_SCK - pin #3
HX711 scale(2, 3); // parameter "gain" is ommited; the default value 128 is used by the library
float tera = 0;
int berat;
float fix;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.noCursor();
Serial.begin(9600);
/*
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
*/
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
/*
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
*/
//lcd.setCursor(0, 0);
//lcd.print("WELLCOME");
// delay(20000);
// lcd.clear();
}
void loop() {
berat = scale.get_units(10) * -1;
fix = ((berat + 1.523) / 0.223) - tera ;
if (fix < 0 ) {
fix = 0;
}
Serial.println(fix);
// Serial.print("one reading:\t");
// Serial.print(scale.get_units(), 1);
// Serial.print("\t| average:\t");
// Serial.println(scale.get_units(10), 1);
lcd.setCursor(0, 0);
lcd.print("Berat= ");
lcd.print(fix);
lcd.print(" gr ");
scale.power_down();
delay(1000);
scale.power_up();
Serial.println(fix);
}
e. Interface VB 6.0
Option Explicit
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 10
Dim sHari As String
Dim aHari
Private Sub berhenti_Click()
start.Enabled = True
berhenti.Enabled = False
TimerBaca.Enabled = False
End Sub
Private Sub Command1_Click()
Open "C:\Users\yanuar\Desktop" & "\project.txt" For Append As #1
Print #1, "======================"
Print #1, " Data dalam satu menit "
Print #1, "======================"
Print #1, "Tanggal = "; lbltanggal.Caption
Print #1, " Jam = "; lbljam.Caption
Print #1, " NIlai = "; Label1.Caption
Print #1,
Close #1
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command3_Click()
Text1.Text = " "
Label1.Caption = " "
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
ComboCOM.AddItem (i)
Next i
aHari = Array("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu")
End Sub
Private Sub start_Click()
Dim u As Integer
On Error GoTo ada_eror
MSComm2.CommPort = ComboCOM.Text
MSComm2.Settings = "9600,N,8,1"
MSComm2.RThreshold = 15
MSComm2.InputLen = 15
MSComm2.InputMode = comInputModeText
MSComm2.PortOpen = True ' buka port
ReDim arrdata(1 To MAKSBACA + 1)
For u = 1 To MAKSBACA
arrdata(u) = 0
Next
TotalBaca = 0
start.Enabled = False
berhenti.Enabled = True
TimerBaca.Enabled = True
ada_eror:
If ComboCOM.ListIndex < 1 Then
MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
start.Enabled = True
berhenti.Enabled = True
ComboCOM.Text = "Pilih COM"
End If
End Sub
Private Sub Timer1_Timer()
sHari = aHari(Abs(Weekday(Date) - 1))
lbltanggal.Caption = "" & sHari & "," & Format(Date, "dd mmmm yyyy")
lbljam.Caption = Format(Time, "hh:mm:ss")
End Sub
Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
strInput = MSComm2.Input
strPotong = strInput
Text1.Text = Text1.Text + strPotong + vbCrLf
singleInput = Val(strPotong)
Label1.Caption = singleInput
If TotalBaca >= MAKSBACA Then
TimerBaca.Enabled = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
Call berhenti_Click
End If
End Sub
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 10
Dim sHari As String
Dim aHari
Private Sub berhenti_Click()
start.Enabled = True
berhenti.Enabled = False
TimerBaca.Enabled = False
End Sub
Private Sub Command1_Click()
Open "C:\Users\yanuar\Desktop" & "\project.txt" For Append As #1
Print #1, "======================"
Print #1, " Data dalam satu menit "
Print #1, "======================"
Print #1, "Tanggal = "; lbltanggal.Caption
Print #1, " Jam = "; lbljam.Caption
Print #1, " NIlai = "; Label1.Caption
Print #1,
Close #1
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command3_Click()
Text1.Text = " "
Label1.Caption = " "
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
ComboCOM.AddItem (i)
Next i
aHari = Array("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu")
End Sub
Private Sub start_Click()
Dim u As Integer
On Error GoTo ada_eror
MSComm2.CommPort = ComboCOM.Text
MSComm2.Settings = "9600,N,8,1"
MSComm2.RThreshold = 15
MSComm2.InputLen = 15
MSComm2.InputMode = comInputModeText
MSComm2.PortOpen = True ' buka port
ReDim arrdata(1 To MAKSBACA + 1)
For u = 1 To MAKSBACA
arrdata(u) = 0
Next
TotalBaca = 0
start.Enabled = False
berhenti.Enabled = True
TimerBaca.Enabled = True
ada_eror:
If ComboCOM.ListIndex < 1 Then
MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
start.Enabled = True
berhenti.Enabled = True
ComboCOM.Text = "Pilih COM"
End If
End Sub
Private Sub Timer1_Timer()
sHari = aHari(Abs(Weekday(Date) - 1))
lbltanggal.Caption = "" & sHari & "," & Format(Date, "dd mmmm yyyy")
lbljam.Caption = Format(Time, "hh:mm:ss")
End Sub
Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
strInput = MSComm2.Input
strPotong = strInput
Text1.Text = Text1.Text + strPotong + vbCrLf
singleInput = Val(strPotong)
Label1.Caption = singleInput
If TotalBaca >= MAKSBACA Then
TimerBaca.Enabled = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
Call berhenti_Click
End If
End Sub
f. VIDEO HASILNYA
No comments:
Post a Comment