Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitor daya yang berupa arus dan tegangan dari sumber DC, selain itu alat ini juga bisa mengukur suhu menggunakan sensor LM35 dan mengukur intensitas cahaya atau LUXMETER dengan sensor BH1750. alat ini dilengkapi dengan penyimpanan secara berkala setiap 1 menit sekali pada SD card atau datalogger. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. Sensor Tegangan DC
c. Sensor Arus ACS712
d. LCD 16x2 + I2C
e. Sensor LUX BH1750
f. Sensor Suhu LM35
g. RTC DS1307
h. Program Arduino IDE
#include "Wire.h"
#include <LiquidCrystal_I2C.h>
#include <BH1750FVI.h>
#define DS3231_I2C_ADDRESS 0x68
#include <SPI.h>
#include <SD.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
BH1750FVI LightSensor;
//mosi - 11
//miso - 12
//clk - 13
//cs - 4
int cacah = 0;
File myFile;
int adcteg;
int adcarus;
float tegangan;
float arus;
float v;
int adcsuhu;
int suhu;
uint16_t lux;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup()
{
lcd.begin();
lcd.clear();
lcd.noCursor();
Wire.begin();
Serial.begin(9600);
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(0,35,8,5,10,4,18);
LightSensor.begin();
/*
Set the address for this sensor
you can use 2 different address
Device_Address_H "0x5C"
Device_Address_L "0x23"
you must connect Addr pin to A3 .
*/
LightSensor.SetAddress(Device_Address_H);//Address 0x5C
// To adjust the slave on other address , uncomment this line
// lightMeter.SetAddress(Device_Address_L); //Address 0x5C
//-----------------------------------------------
/*
set the Working Mode for this sensor
Select the following Mode:
Continuous_H_resolution_Mode
Continuous_H_resolution_Mode2
Continuous_L_resolution_Mode
OneTime_H_resolution_Mode
OneTime_H_resolution_Mode2
OneTime_L_resolution_Mode
The data sheet recommanded To use Continuous_H_resolution_Mode
*/
LightSensor.SetMode(Continuous_H_resolution_Mode);
//Serial.println("Running...");
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
lcd.setCursor(0,0);
// send it to the serial monitor
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
}
void loop()
{
displayTime();
delay(1000);
lux = LightSensor.GetLightIntensity();// Get Lux value
adcteg = analogRead(A0);
tegangan = adcteg * (5.0 / 1023.0) * 4.8;
adcarus = analogRead(A1);
v = adcarus * (5.0 / 1023.0);
arus = (v - 2.5)/0.1;
adcsuhu = analogRead(A2);
suhu = adcsuhu * (5.0 / 1023.0) * 100;
if(arus < 0){
arus = 0;
}
if(tegangan < 0){
tegangan = 0;
}
lcd.setCursor(9,0);
lcd.print("L:");
lcd.print(lux);
lcd.setCursor(0,1);
lcd.print(tegangan);
lcd.print("/");
lcd.print(arus);
lcd.print("/");
lcd.print(suhu,1);
lcd.print(" ");
cacah++;
if(cacah >= 60){
lcd.clear();
simpan();
cacah = 0;
lcd.clear();
}
}
void simpan(){
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.print("Tanggal:");
myFile.print(dayOfMonth);
myFile.print("/");
myFile.print(month);
myFile.print("/");
myFile.println(year);
myFile.print("Jam:");
myFile.print(hour);
myFile.print(":");
myFile.print(minute);
myFile.print(":");
myFile.println(second);
myFile.print("V= ");
myFile.println(tegangan);
myFile.print("I= ");
myFile.println(arus);
myFile.print("P= ");
myFile.println(tegangan*arus);
myFile.print("T= ");
myFile.println(suhu);
myFile.print("Lux= ");
myFile.println(lux);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
i. VIDEO HASILNYA
No comments:
Post a Comment