Arduino akan digunakan untuk membaca data RTC (Real Time Clock) data yang diperoleh berupa jam, menit, detik, hari ke-, tanggal, bulan dan tahun.
Pada project kali ini kita membuat jam digital dengan tampilan seven segment 6 digit dengan sistem scanning data 7 segment. Angka yang ditampilkan pada masing masing digit ditampilkan secara bergantian dari digit 1 sampai digit 6 dengan jeda penampilan 3mS/digit sehingga seolah olah angkanya tampil secara bersamaan. RTC yang digunakan DS1307 sebagai sumber clock-nya. Sedangkan untuk seting jam, menit dan detik menggunakan 3 tombol push ON yang terhubung dengan pin Arduino.
Kebutuhan Hardware
- Modul Jam Digital display 7 segment 6 digit
- Modul RTC DS1307
- 3 Tombol push ON (tack switch)
- Modul Arduino UNO
- Power supply +5 Volt
Push Button | Source
Diagram Blok
Schematic
7-Segment CA Wiring | Source
Koneksi Arduino UNO dengan 7-Segment
Koneksi Arduino - RTC
Koneksi Arduino - Button
Source Code/Sketch
#include <Wire.h>
#define DS1307_ADDRESS 0x68
byte zero = 0x00;
byte nilai, i, f_kpd, menu;
byte second ,minute,hour, weekDay;
byte monthDay,month,year;
word kedip;
long lastButton = 0;
long delayAntiBouncing = 50;
byte seven_seg_digits[10][7] = { { ,0,0,0,0,0,1 }, // = 0
{ 1,0,0,1,1,1,1 }, // = 1
{ 0,0,1,0,0,1,0 }, // = 2
{ 0,0,0,0,1,1,0 }, // = 3
{ 1,0,0,1,1,0,0 }, // = 4
{ 0,1,0,0,1,0,0 }, // = 5
{ 0,1,0,0,0,0,0 }, // = 6
{ 0,0,0,1,1,1,1 }, // = 7
{ 0,0,0,0,0,0,0 }, // = 8
{ 0,0,0,0,1,0,0 } // = 9
};
void setup()
{
pinMode(2, OUTPUT); //a
pinMode(3, OUTPUT);//b
pinMode(4, OUTPUT);//c
pinMode(5, OUTPUT);//d
pinMode(6, OUTPUT);//e
pinMode(7, OUTPUT);//f
pinMode(8, OUTPUT);//g
pinMode(9, OUTPUT);//dig 1
pinMode(10, OUTPUT);//dig 2
pinMode(11, OUTPUT);//dig 3
pinMode(12, OUTPUT);//dig 4
pinMode(13, OUTPUT);//dig 5
pinMode(A0, OUTPUT);//dig 6
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);
digitalWrite(A3,HIGH);
Wire.begin();
}
void loop(){
bacaRTC();
tampilJam();
tampilMenit();
tampilDetik();
cekTombol();
}
void tampilJam(){
digitalWrite(9,LOW); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(hour/10);
delay(3);
digitalWrite(9,HIGH); digitalWrite(10,LOW);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(hour%10);
delay(3);
}
void tampilMenit(){
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,LOW); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(minute/10);
delay(3);
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,LOW);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(minute%10);
delay(3);
}
void tampilDetik(){
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,LOW); digitalWrite(A0,HIGH);
sevenSegWrite(second/10);
delay(3);
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,LOW);
sevenSegWrite(second%10);
delay(3);
}
void sevenSegWrite(byte segment) {
byte pin = 2;
for (byte segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_seg_digits[segment][segCount]);
++pin;
}
}
byte decToBcd(byte val){
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}
void cekTombol(){
do{
//------------------------SET
if (digitalRead(A1)==0){
if ((millis() - lastButton) > delayAntiBouncing){
f_kpd=1;
menu++;
}
lastButton = millis();
}
//---------------------------up
else if(digitalRead(A2)==0){
if ((millis() - lastButton) > delayAntiBouncing){
if (menu==1){
++hour;
if (hour==24){
hour=0;
}
}
else if(menu==2){
++minute;
if (minute==60){
minute=0;
}
}
else if(menu==3){
++second;
if (second==60){
second=0;
}
}
}
lastButton = millis();
}
//----------------------------DOWN
else if(digitalRead(A3)==0){
if ((millis() - lastButton) > delayAntiBouncing){
if (menu==1){
--hour;
if (hour==255){
hour=23;
}
}
else if(menu==2){
--minute;
if (minute==255){
minute=59;
}
}
else if(menu==3){
--second;
if (second==255){
second=59;
}
}
}
lastButton = millis();
}
if (menu==1 && kedip<30){
tampilMenit();
tampilDetik();
}
else if (menu==2 && kedip<30){
tampilJam();
tampilDetik();
}
else if (menu==3 && kedip<30){
tampilJam();
tampilMenit();
}
else{
tampilJam();
tampilMenit();
tampilDetik();
if(kedip>60)kedip=0;
}
++kedip;
if (menu==4){
setingRTC();
f_kpd=0;
}
}
while(f_kpd);
menu=0;
}
void bacaRTC(){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111);
weekDay = bcdToDec(Wire.read());
monthDay = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void setingRTC(){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop RTC
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
#define DS1307_ADDRESS 0x68
byte zero = 0x00;
byte nilai, i, f_kpd, menu;
byte second ,minute,hour, weekDay;
byte monthDay,month,year;
word kedip;
long lastButton = 0;
long delayAntiBouncing = 50;
byte seven_seg_digits[10][7] = { { ,0,0,0,0,0,1 }, // = 0
{ 1,0,0,1,1,1,1 }, // = 1
{ 0,0,1,0,0,1,0 }, // = 2
{ 0,0,0,0,1,1,0 }, // = 3
{ 1,0,0,1,1,0,0 }, // = 4
{ 0,1,0,0,1,0,0 }, // = 5
{ 0,1,0,0,0,0,0 }, // = 6
{ 0,0,0,1,1,1,1 }, // = 7
{ 0,0,0,0,0,0,0 }, // = 8
{ 0,0,0,0,1,0,0 } // = 9
};
void setup()
{
pinMode(2, OUTPUT); //a
pinMode(3, OUTPUT);//b
pinMode(4, OUTPUT);//c
pinMode(5, OUTPUT);//d
pinMode(6, OUTPUT);//e
pinMode(7, OUTPUT);//f
pinMode(8, OUTPUT);//g
pinMode(9, OUTPUT);//dig 1
pinMode(10, OUTPUT);//dig 2
pinMode(11, OUTPUT);//dig 3
pinMode(12, OUTPUT);//dig 4
pinMode(13, OUTPUT);//dig 5
pinMode(A0, OUTPUT);//dig 6
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);
digitalWrite(A3,HIGH);
Wire.begin();
}
void loop(){
bacaRTC();
tampilJam();
tampilMenit();
tampilDetik();
cekTombol();
}
void tampilJam(){
digitalWrite(9,LOW); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(hour/10);
delay(3);
digitalWrite(9,HIGH); digitalWrite(10,LOW);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(hour%10);
delay(3);
}
void tampilMenit(){
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,LOW); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(minute/10);
delay(3);
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,LOW);
digitalWrite(13,HIGH); digitalWrite(A0,HIGH);
sevenSegWrite(minute%10);
delay(3);
}
void tampilDetik(){
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,LOW); digitalWrite(A0,HIGH);
sevenSegWrite(second/10);
delay(3);
digitalWrite(9,HIGH); digitalWrite(10,HIGH);
digitalWrite(11,HIGH); digitalWrite(12,HIGH);
digitalWrite(13,HIGH); digitalWrite(A0,LOW);
sevenSegWrite(second%10);
delay(3);
}
void sevenSegWrite(byte segment) {
byte pin = 2;
for (byte segCount = 0; segCount < 7; ++segCount) {
digitalWrite(pin, seven_seg_digits[segment][segCount]);
++pin;
}
}
byte decToBcd(byte val){
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}
void cekTombol(){
do{
//------------------------SET
if (digitalRead(A1)==0){
if ((millis() - lastButton) > delayAntiBouncing){
f_kpd=1;
menu++;
}
lastButton = millis();
}
//---------------------------up
else if(digitalRead(A2)==0){
if ((millis() - lastButton) > delayAntiBouncing){
if (menu==1){
++hour;
if (hour==24){
hour=0;
}
}
else if(menu==2){
++minute;
if (minute==60){
minute=0;
}
}
else if(menu==3){
++second;
if (second==60){
second=0;
}
}
}
lastButton = millis();
}
//----------------------------DOWN
else if(digitalRead(A3)==0){
if ((millis() - lastButton) > delayAntiBouncing){
if (menu==1){
--hour;
if (hour==255){
hour=23;
}
}
else if(menu==2){
--minute;
if (minute==255){
minute=59;
}
}
else if(menu==3){
--second;
if (second==255){
second=59;
}
}
}
lastButton = millis();
}
if (menu==1 && kedip<30){
tampilMenit();
tampilDetik();
}
else if (menu==2 && kedip<30){
tampilJam();
tampilDetik();
}
else if (menu==3 && kedip<30){
tampilJam();
tampilMenit();
}
else{
tampilJam();
tampilMenit();
tampilDetik();
if(kedip>60)kedip=0;
}
++kedip;
if (menu==4){
setingRTC();
f_kpd=0;
}
}
while(f_kpd);
menu=0;
}
void bacaRTC(){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111);
weekDay = bcdToDec(Wire.read());
monthDay = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void setingRTC(){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop RTC
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
Jalannya Alat
1. Tampilan pertama seven segment langsung menampilkan jam sesuai setingan RTC
2. Untuk merubah jam pertamakali tekan tombol “SET”, sehingga pada digit jam berkedip,
kemudian tekan tombol UP untuk menambah jam atau tekan tombol DOWN untuk mengurangi
jam
UP/DOWN untuk menyesuaikan menit:
4. Selanjutnya tekan tombol “SET” lagi untuk seting detik:
5. Terakhir tekan tombol “SET”, sehingga seting jam selesai dan seven segment menampilkan
setingan jam terakhir:
Video untuk Project I - 9. Jam Digital 6 Digit 7-Segment dengan Pengatur Tombol berbasis Arduino
KONTAK KAMI 085743320570 (adi sanjaya)
gan, sketch d atas bs mnggunakan rtc ds3231 ga?
ReplyDeleteBisa
Deletemaaf gan, menampilkan tgl, bln, thn bagaimana gan
ReplyDeleteMantap gan
ReplyDeleteiya gan ,,, mantap
DeleteTambahin dengan alarm
ReplyDeleteKok gk bisa gan
ReplyDeleteKok gk bisa gan
ReplyDeleteKok error' terus ya gan, error : expected '}' at end of input
ReplyDelete