0% found this document useful (0 votes)
48 views

Kode Pemrograman Alat Berbasis Mikrokontroler Arduino

The document contains code for 3 Arduino projects: 1. An ultrasonic distance measurement program that displays the distance on an LCD screen. 2. A digital scale program that calibrates a load cell sensor and displays the weight reading. 3. An automatic height measurement program that uses an ultrasonic sensor to measure a person's height and display it on an LCD screen.

Uploaded by

Trimon Mikael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Kode Pemrograman Alat Berbasis Mikrokontroler Arduino

The document contains code for 3 Arduino projects: 1. An ultrasonic distance measurement program that displays the distance on an LCD screen. 2. A digital scale program that calibrates a load cell sensor and displays the weight reading. 3. An automatic height measurement program that uses an ultrasonic sensor to measure a person's height and display it on an LCD screen.

Uploaded by

Trimon Mikael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Coding alat

1. Kode program penggaris digital


#include <LiquidCrystal_I2C.h>
#include <Wire.h> //library I2C
LiquidCrystal_I2C lcd(0x27,20,4);
const float TRIG_PIN = 5;
const float ECHO_PIN = 4;
float s;
float P;
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
s = pulseIn(ECHO_PIN,HIGH);
P = s * 0.034 / 2;
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd. Print(“ jarak”);
lcd.setCursor(4,1);
lcd.print(P);
lcd.print(" cm ");
delayMicroseconds(10);
}
2. Kode program timbangan digital
a. Kalibrasi sensor loat cell
#include <HX711_ADC.h>
#include <EEPROM.h>
const int HX711_dout = 12;
const int HX711_sck = 13;
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
long u;

void setup() {
Serial.begin(9600); delay(10);
Serial.println();
Serial.println("Starting...");
LoadCell.begin();
long stabilizingtime = 2000;
boolean _tare = false;
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag() || LoadCell.getSignalTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(1.0);
Serial.println("Startup is complete");
}
while (!LoadCell.update());
calibrate();
}

void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0;
if (LoadCell.update()) newDataReady = true;
if (newDataReady) {
if (millis() > u + serialPrintInterval) {
float i = LoadCell.getData();
Serial.print("Load_cell output val: ");
Serial.println(i);
newDataReady = 0;
u = millis();
}
}
if (Serial.available() > 0) {
float i;
char inByte = Serial.read();
if (inByte == 'u') LoadCell.tareNoDelay();
else if (inByte == 'r') calibrate();
else if (inByte == 'c') changeSavedCalFactor();
}
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
}
}

void calibrate() {
Serial.println("***");
Serial.println("Start calibration:");
Serial.println("Place the load cell an a level stable surface.");
Serial.println("Remove any load applied to the load cell.");
Serial.println("Send 'u' from serial monitor to set the tare offset.");

boolean _resume = false;


while (_resume == false) {
LoadCell.update();
if (Serial.available() > 0) {
if (Serial.available() > 0) {
float i;
char inByte = Serial.read();
if (inByte == 'u') LoadCell.tareNoDelay();
}
}
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
_resume = true;
}
}

Serial.println("Now, place your known mass on the loadcell.");


Serial.println("Then send the weight of this mass (i.e. 100.0) from serial monitor.");

float known_mass = 0;
_resume = false;
while (_resume == false) {
LoadCell.update();
if (Serial.available() > 0) {
known_mass = Serial.parseFloat();
if (known_mass != 0) {
Serial.print("Known mass is: ");
Serial.println(known_mass);
_resume = true;
}
}
}

LoadCell.refreshDataSet();
float newCalibrationValue = LoadCell.getNewCalibration(known_mass);
Serial.print("New calibration value has been set to: ");
Serial.print(newCalibrationValue);
Serial.println(", use this as calibration value (calFactor) in your project sketch.");
Serial.print("Save this value to EEPROM adress ");
Serial.print(calVal_eepromAdress);
Serial.println("? y/n");
_resume = false;
while (_resume == false) {
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 'y') {
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(512);
#endif
EEPROM.put(calVal_eepromAdress, newCalibrationValue);
#if defined(ESP8266)|| defined(ESP32)
EEPROM.commit();
#endif
EEPROM.get(calVal_eepromAdress, newCalibrationValue);
Serial.print("Value ");
Serial.print(newCalibrationValue);
Serial.print(" saved to EEPROM address: ");
Serial.println(calVal_eepromAdress);
_resume = true;

}
else if (inByte == 'n') {
Serial.println("Value not saved to EEPROM");
_resume = true;
}
}
}

Serial.println("End calibration");
Serial.println("***");
Serial.println("To re-calibrate, send 'r' from serial monitor.");
Serial.println("For manual edit of the calibration value, send 'c' from serial monitor.");
Serial.println("***");
}

void changeSavedCalFactor() {
float oldCalibrationValue = LoadCell.getCalFactor();
boolean _resume = false;
Serial.println("***");
Serial.print("Current value is: ");
Serial.println(oldCalibrationValue);
Serial.println("Now, send the new value from serial monitor, i.e. 696.0");
float newCalibrationValue;
while (_resume == false) {
if (Serial.available() > 0) {
newCalibrationValue = Serial.parseFloat();
if (newCalibrationValue != 0) {
Serial.print("New calibration value is: ");
Serial.println(newCalibrationValue);
LoadCell.setCalFactor(newCalibrationValue);
_resume = true;
}
}
}
_resume = false;
Serial.print("Save this value to EEPROM adress ");
Serial.print(calVal_eepromAdress);
Serial.println("? y/n");
while (_resume == false) {
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 'y') {
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(512);
#endif
EEPROM.put(calVal_eepromAdress, newCalibrationValue);
#if defined(ESP8266)|| defined(ESP32)
EEPROM.commit();
#endif
EEPROM.get(calVal_eepromAdress, newCalibrationValue);
Serial.print("Value ");
Serial.print(newCalibrationValue);
Serial.print(" saved to EEPROM address: ");
Serial.println(calVal_eepromAdress);
_resume = true;
}
else if (inByte == 'n') {
Serial.println("Value not saved to EEPROM");
_resume = true;
}
}
}
Serial.println("End change calibration value");
Serial.println("***");
}
b. Uji coba sensor loat cell
#include<HX711_ADC.h>
#include<EEPROM.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
const int HX711_dout = 12;
const int HX711_sck = 13;

HX711_ADC LoadCell(HX711_dout, HX711_sck);

const int calVal_eepromAdress = 0;


float u;

void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(HX711_dout, INPUT);
delay(10);
lcd.init();
LoadCell.begin();
lcd.setCursor(12,0);
lcd.print("0");
lcd.setCursor(14,0);
lcd.print("gr");
float calibrationValue;
calibrationValue = 696.0;
EEPROM.get(calVal_eepromAdress, calibrationValue);
long stabilizingtime = 2000;
boolean _tare = true;
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, cek kabel MCU>HX711 pastikan sudah tepat");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue);
Serial.println("Startup selesai");
}
}

void loop() {
static boolean newDataReady = 0;
const int serialPrintInterval = 0;
if (LoadCell.update()) newDataReady = true;
if (newDataReady) {
if (millis() > u + serialPrintInterval) {
int i = LoadCell.getData();
if(i<0){
i=0;
}
tampil(i);
newDataReady = 0;
u = millis();
}
}
if(Serial.available() > 0){
float i;
char inByte = Serial.read();
if (inByte == 'u') LoadCell.tareNoDelay();
}
if (LoadCell.getTareStatus() == true) {
Serial.println("Tara selesai");
}
}

void tampil(int j){


lcd.setCursor(4,0);
lcd.print(" ");
if(j<10){
lcd.setCursor(12,0);
}else if(j<100 && j>=10){
lcd.setCursor(11,0);
}else if(j<1000 && j>=100){
lcd.setCursor(10,0);
}else if(j<10000 && j>=1000){
lcd.setCursor(9,0);
}else if(j<100000 && j>=10000){
lcd.setCursor(8,0);
}else if(j<1000000 && j>=100000){
lcd.setCursor(7,0);
}else if(j<10000000 && j>=1000000){
lcd.setCursor(6,0);
}else if(j<100000000 && j>=10000000){
lcd.setCursor(5,0);
}else{
lcd.setCursor(4,0);
}
lcd.print(j);
}
3. Kode program pengukur tinggi badan otomatis
#include <LiquidCrystal_I2C.h>
#include <Wire.h> //library I2C
LiquidCrystal_I2C lcd(0x27,20,4);
const float TRIG_PIN = 5;
const float ECHO_PIN = 4;
float s;
float P;
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
s = pulseIn(ECHO_PIN,HIGH);
P = s * 0.034 / 2;
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd. Print(“ TINGGI BADAN ANDA”);
lcd.setCursor(4,1);
lcd.print(P);
lcd.print(" cm ");
delayMicroseconds(10);
}

You might also like