Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat mengukur intensitas cahaya atau luxmeter dengan menggunakan Arduino dan sensor BH1750, selain itu juga terdapat setting waktu pemakaian sehingga bisa terlihat berapa waktu yang telah dipakai oleh alat. setting waktu menggunakan 4 buah tombol. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. LCD + I2C
c. Sensor Intensitas Cahaya (LUX) bh1750
d. RTC DS1307
e. Program Arduino IDE
#include "Wire.h"
#include <LiquidCrystal_I2C.h>
#define DS3231_I2C_ADDRESS 0x68
#include <BH1750FVI.h>
LiquidCrystal_I2C lcd(0x3F,16,2);
int btup = 3;
int btdown = 4;
int btok = 5;
int btset = 6;
int savejam;
int jam;
int btupx = 0;
int btdownx = 0;
int btokx = 0;
int btsetx = 0;
int buzzer = 2;
BH1750FVI LightSensor;
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() {
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
pinMode(btup,INPUT_PULLUP);
pinMode(btdown,INPUT_PULLUP);
pinMode(btok,INPUT_PULLUP);
pinMode(btset,INPUT_PULLUP);
pinMode(buzzer,OUTPUT);
digitalWrite(buzzer,HIGH);
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(0,0,0,5,10,4,18);
LightSensor.begin();
LightSensor.SetAddress(Device_Address_H);
LightSensor.SetMode(Continuous_H_resolution_Mode);
}
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() {
lux = LightSensor.GetLightIntensity();// Get Lux value
lcd.setCursor(0,0);
lcd.print("TEKAN TOMBOL");
lcd.setCursor(0,1);
lcd.print("L:");
lcd.print(lux);
lcd.print(" ");
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
if(btsetx == 0){
delay(200);
lcd.clear();
setting();
delay(1000);
lcd.clear();
setDS3231time(0,0,0,5,10,4,18);
mulai();
}
}
void setting(){
lcd.setCursor(0,0);
lcd.print("SET JAM");
lcd.setCursor(0,1);
lcd.print(jam);
lcd.print(" ");
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
btokx = digitalRead(btok);
btsetx = digitalRead(btset);
if(btupx == 0){
delay(200);
jam++;
}
if(jam > 6){
jam = 0;
}
if(btdownx == 0){
delay(200);
jam--;
}
if(jam < 0){
jam = 0;
}
if(btokx == 0){
delay(2000);
savejam = jam;
lcd.clear();
return;
}
setting();
}
void mulai(){
displayTime();
delay(1000);
lux = LightSensor.GetLightIntensity();// Get Lux value
lcd.setCursor(0,1);
lcd.print("L:");
lcd.print(lux);
lcd.print(" ");
lcd.print("Jam:");
lcd.print(savejam);
lcd.print(" ");
if(savejam == hour){
digitalWrite(buzzer,LOW);
}
mulai();
}
f. VIDEO HASILNYA
#include <LiquidCrystal_I2C.h>
#define DS3231_I2C_ADDRESS 0x68
#include <BH1750FVI.h>
LiquidCrystal_I2C lcd(0x3F,16,2);
int btup = 3;
int btdown = 4;
int btok = 5;
int btset = 6;
int savejam;
int jam;
int btupx = 0;
int btdownx = 0;
int btokx = 0;
int btsetx = 0;
int buzzer = 2;
BH1750FVI LightSensor;
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() {
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
pinMode(btup,INPUT_PULLUP);
pinMode(btdown,INPUT_PULLUP);
pinMode(btok,INPUT_PULLUP);
pinMode(btset,INPUT_PULLUP);
pinMode(buzzer,OUTPUT);
digitalWrite(buzzer,HIGH);
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(0,0,0,5,10,4,18);
LightSensor.begin();
LightSensor.SetAddress(Device_Address_H);
LightSensor.SetMode(Continuous_H_resolution_Mode);
}
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() {
lux = LightSensor.GetLightIntensity();// Get Lux value
lcd.setCursor(0,0);
lcd.print("TEKAN TOMBOL");
lcd.setCursor(0,1);
lcd.print("L:");
lcd.print(lux);
lcd.print(" ");
btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
if(btsetx == 0){
delay(200);
lcd.clear();
setting();
delay(1000);
lcd.clear();
setDS3231time(0,0,0,5,10,4,18);
mulai();
}
}
void setting(){
lcd.setCursor(0,0);
lcd.print("SET JAM");
lcd.setCursor(0,1);
lcd.print(jam);
lcd.print(" ");
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
btokx = digitalRead(btok);
btsetx = digitalRead(btset);
if(btupx == 0){
delay(200);
jam++;
}
if(jam > 6){
jam = 0;
}
if(btdownx == 0){
delay(200);
jam--;
}
if(jam < 0){
jam = 0;
}
if(btokx == 0){
delay(2000);
savejam = jam;
lcd.clear();
return;
}
setting();
}
void mulai(){
displayTime();
delay(1000);
lux = LightSensor.GetLightIntensity();// Get Lux value
lcd.setCursor(0,1);
lcd.print("L:");
lcd.print(lux);
lcd.print(" ");
lcd.print("Jam:");
lcd.print(savejam);
lcd.print(" ");
if(savejam == hour){
digitalWrite(buzzer,LOW);
}
mulai();
}
f. VIDEO HASILNYA
No comments:
Post a Comment