Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana caranya membuat sebuah alat yang dapat mengukur kecepatan angin dan suhu secara realtime menggunakan arduino, jadi alat ini terdapat dua buah sensor yaitu sensor anemometer dan sensor suhu ds18b20 dengan output lcd 16x2, selain itu juga terdapat indikator berupa led dan buzzer. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. Sensor Kecepatan Angin
c. Sensor Suhu ds18b20
d. LCD 16x2
e. Program Arduino IDE
#include <OneWire.h>
#include <Wire.h> // i2C Conection Library
#include <LiquidCrystal_I2C.h> //i2C LCD Library
//LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
OneWire ds(4); // on pin 10 (a 4.7K resistor is necessary)
volatile byte half_revolutions; //variabel tipe data byte
unsigned int rpmku; //variabel tipe data integer
unsigned long timeold; //variabel tipe data long
int kalibrasi; //variabel tipe data integer
int ledsuhum = A2;
int ledsuhuk = A1;
int ledsuhuh = A0;
int ledrpmm = 5;
int ledrpmk = 6;
int ledrpmh = 7;
int buzp = 8;
int buzm = 11;
void setup() {
pinMode(ledsuhum,OUTPUT);
pinMode(ledsuhuk,OUTPUT);
pinMode(ledsuhuh,OUTPUT);
pinMode(ledrpmm,OUTPUT);
pinMode(ledrpmk,OUTPUT);
pinMode(ledrpmh,OUTPUT);
pinMode(buzp,OUTPUT);
pinMode(buzm,OUTPUT);
attachInterrupt(0, rpm_fun, RISING); //mengambil sinyal high pada pin 2
half_revolutions = 0; //memberikan nilai 0 pada viariabel
rpmku = 0;
timeold = 0;
kalibrasi = 0;
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
for( i = 0; i < 8; i++) {
}
if (OneWire::crc8(addr, 7) != addr[7]) {
return;
}
switch (addr[0]) {
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
default:
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
rpmku = 30*1000/(millis() - timeold)*half_revolutions; //mengaktifkan counter millis
timeold = millis(); //hasil counter dimasukkan ke variabel timeold
half_revolutions = 0; //reset variabel
kalibrasi = (rpmku - 150)/109; //rumus kalibrasi
if(kalibrasi == 599){
kalibrasi = 0;
}
if(kalibrasi == 600){
kalibrasi = 0;
}
lcd.setCursor(0, 0);
lcd.print("Suhu=");
lcd.print(celsius);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Kec=");
lcd.print(kalibrasi);
lcd.print(" m/s ");
if(celsius > 35){
digitalWrite(ledsuhum,LOW);
digitalWrite(ledsuhuk,HIGH);
digitalWrite(ledsuhuh,HIGH);
lcd.setCursor(12, 0);
lcd.print("HIGH ");
}
if((celsius >= 30)&&(celsius <= 35)){
digitalWrite(ledsuhum,HIGH);
digitalWrite(ledsuhuk,LOW);
digitalWrite(ledsuhuh,HIGH);
lcd.setCursor(12, 0);
lcd.print("LOW ");
}
if(celsius < 30){
digitalWrite(ledsuhum,HIGH);
digitalWrite(ledsuhuk,HIGH);
digitalWrite(ledsuhuh,LOW);
lcd.setCursor(12, 0);
lcd.print("LOW ");
}
if(kalibrasi > 20){
digitalWrite(ledrpmm,LOW);
digitalWrite(ledrpmk,HIGH);
digitalWrite(ledrpmh,HIGH);
digitalWrite(buzp,HIGH);
digitalWrite(buzm,LOW);
lcd.setCursor(12, 1);
lcd.print("HIGH");
}
if((kalibrasi >= 10)&&(kalibrasi <= 20)){
digitalWrite(ledrpmm,HIGH);
digitalWrite(ledrpmk,LOW);
digitalWrite(ledrpmh,HIGH);
digitalWrite(buzp,LOW);
digitalWrite(buzm,LOW);
lcd.setCursor(12, 1);
lcd.print("LOW ");
}
if(kalibrasi < 10){
digitalWrite(ledrpmm,HIGH);
digitalWrite(ledrpmk,HIGH);
digitalWrite(ledrpmh,LOW);
digitalWrite(buzp,LOW);
digitalWrite(buzm,LOW);
lcd.setCursor(12, 1);
lcd.print("LOW ");
}
}
void rpm_fun(){
half_revolutions++; //counter interupt
}
#include <Wire.h> // i2C Conection Library
#include <LiquidCrystal_I2C.h> //i2C LCD Library
//LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
OneWire ds(4); // on pin 10 (a 4.7K resistor is necessary)
volatile byte half_revolutions; //variabel tipe data byte
unsigned int rpmku; //variabel tipe data integer
unsigned long timeold; //variabel tipe data long
int kalibrasi; //variabel tipe data integer
int ledsuhum = A2;
int ledsuhuk = A1;
int ledsuhuh = A0;
int ledrpmm = 5;
int ledrpmk = 6;
int ledrpmh = 7;
int buzp = 8;
int buzm = 11;
void setup() {
pinMode(ledsuhum,OUTPUT);
pinMode(ledsuhuk,OUTPUT);
pinMode(ledsuhuh,OUTPUT);
pinMode(ledrpmm,OUTPUT);
pinMode(ledrpmk,OUTPUT);
pinMode(ledrpmh,OUTPUT);
pinMode(buzp,OUTPUT);
pinMode(buzm,OUTPUT);
attachInterrupt(0, rpm_fun, RISING); //mengambil sinyal high pada pin 2
half_revolutions = 0; //memberikan nilai 0 pada viariabel
rpmku = 0;
timeold = 0;
kalibrasi = 0;
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
for( i = 0; i < 8; i++) {
}
if (OneWire::crc8(addr, 7) != addr[7]) {
return;
}
switch (addr[0]) {
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
default:
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
rpmku = 30*1000/(millis() - timeold)*half_revolutions; //mengaktifkan counter millis
timeold = millis(); //hasil counter dimasukkan ke variabel timeold
half_revolutions = 0; //reset variabel
kalibrasi = (rpmku - 150)/109; //rumus kalibrasi
if(kalibrasi == 599){
kalibrasi = 0;
}
if(kalibrasi == 600){
kalibrasi = 0;
}
lcd.setCursor(0, 0);
lcd.print("Suhu=");
lcd.print(celsius);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Kec=");
lcd.print(kalibrasi);
lcd.print(" m/s ");
if(celsius > 35){
digitalWrite(ledsuhum,LOW);
digitalWrite(ledsuhuk,HIGH);
digitalWrite(ledsuhuh,HIGH);
lcd.setCursor(12, 0);
lcd.print("HIGH ");
}
if((celsius >= 30)&&(celsius <= 35)){
digitalWrite(ledsuhum,HIGH);
digitalWrite(ledsuhuk,LOW);
digitalWrite(ledsuhuh,HIGH);
lcd.setCursor(12, 0);
lcd.print("LOW ");
}
if(celsius < 30){
digitalWrite(ledsuhum,HIGH);
digitalWrite(ledsuhuk,HIGH);
digitalWrite(ledsuhuh,LOW);
lcd.setCursor(12, 0);
lcd.print("LOW ");
}
if(kalibrasi > 20){
digitalWrite(ledrpmm,LOW);
digitalWrite(ledrpmk,HIGH);
digitalWrite(ledrpmh,HIGH);
digitalWrite(buzp,HIGH);
digitalWrite(buzm,LOW);
lcd.setCursor(12, 1);
lcd.print("HIGH");
}
if((kalibrasi >= 10)&&(kalibrasi <= 20)){
digitalWrite(ledrpmm,HIGH);
digitalWrite(ledrpmk,LOW);
digitalWrite(ledrpmh,HIGH);
digitalWrite(buzp,LOW);
digitalWrite(buzm,LOW);
lcd.setCursor(12, 1);
lcd.print("LOW ");
}
if(kalibrasi < 10){
digitalWrite(ledrpmm,HIGH);
digitalWrite(ledrpmk,HIGH);
digitalWrite(ledrpmh,LOW);
digitalWrite(buzp,LOW);
digitalWrite(buzm,LOW);
lcd.setCursor(12, 1);
lcd.print("LOW ");
}
}
void rpm_fun(){
half_revolutions++; //counter interupt
}
f. VIDEO HASILNYA
No comments:
Post a Comment