Translate

Membuat Alat Monitor Daya, Tegangan 220v ZMPT101b dan Arus AC ACS712 / KWH Meter / Token Pulsa Listrik Menggunakan ATMega CV AVR dan Interface VB 6

Membuat Alat Monitor Daya, Tegangan 220v ZMPT101b dan Arus AC ACS712  / KWH Meter / Token Pulsa Listrik Menggunakan ATMega CV AVR dan Interface VB 6


         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang bisa digunakan untuk monitoring tegangan dan arus AC 220v 50Hz kemudian dicari daya-nya sehingga bisa dibuat KWH meter, alat ini menggunakan mikrokontroller ATMega32 dan bahasa CV AVR, prinsip kerja alat ini yaitu hanya mengambil data tegangan dan arus kemudian dicari nilai daya dengan mengkalikan nilai arus dan tegangan, selain itu alat ini juga terdapat fitur memasukkan nilai menggunakan keypad matrik3x4 sesuai kebutuhan daya yang dipakai, jika beban yang terdeteksi melebihi daya maka relay akan aktif dan mematikan sumber tegangan pada beban seperti halnya fuse, namun jika daya yang kita inputkan masih diatas dengan daya yang termonitoring maka output masih menyala. alat ini menggunakan interface VB 6 dan LCD sebagai penampilnya. untuk lebih jelasnya berikut adalah skema dan program keseluruhannya.




a. Minimum System ATMega32





b. Keypad matrik 3x4





c. RTC DS1307





d. LCD 16x2






e. Sensor Tegangan ZMPT101B





f. Sensor Arus ACS712





g. Modul Relay






h. Program CodeVision AVR

/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional

Chip type               : ATmega32
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 512
*****************************************************/

#include <mega32.h>

#include <delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// I2C Bus functions
#asm
   .equ __i2c_port=0x15 ;PORTC
   .equ __sda_bit=1
   .equ __scl_bit=0
#endasm
#include <i2c.h>

// DS1307 Real Time Clock functions
#include <ds1307.h>

// Alphanumeric LCD Module functions
#include <alcd.h>


#define ADC_VREF_TYPE 0x40

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// Declare your global variables here
char buf[33];
unsigned char dd;
unsigned char mm;
unsigned char yy;
unsigned char detik;
unsigned char menit;
unsigned char jam;

int biaya;
int dataadc;
char temp[10];
char temp1[10];
char temp2[10];
char temp3[10];
char temp4[10];
int z,zm,zk,zs;
int i,k,m,s;
int values[300];
float vrms;

float p;
char temp5[10];

int dataadcx;
char tempx[10];
char temp1x[10];
char temp2x[10];
char temp3x[10];
char temp4x[10];
int zx,zmx,zkx,zsx;
int ix,kx,mx,sx;
int valuesx[300];
float irms;

void simpan_dlm_1variabel();
void scanning_keypad();
void tampil_lcd();
void inputdaya();

float nilai = 0;
unsigned char tempo[12];
unsigned char array[10];
int n=0;


void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB = 0XFF;
DDRB=0XF0;

// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=In Func0=In
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=T State0=T
PORTC=0x00;
DDRC=0xFC;

// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// I2C Bus initialization
i2c_init();

// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: Off
// SQW/OUT pin state: 0
rtc_init(0,0,0);

// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 2
// RD - PORTD Bit 3
// EN - PORTD Bit 4
// D4 - PORTD Bit 5
// D5 - PORTD Bit 6
// D6 - PORTD Bit 7
// D7 - PORTC Bit 2
// Characters/line: 16
lcd_init(16);

//rtc_set_time(22,30,00);
//rtc_set_date(8,8,17);

while (1)
      {
          
      rtc_get_time(&jam,&menit,&detik);
      rtc_get_date(&dd,&mm,&yy);
      lcd_gotoxy(0,0);
      sprintf(buf,"%02u:%02u:%02u",jam,menit,detik);
      lcd_puts(buf);
      //printf("Tanggal: %02u:%02u:%02u \r",dd,mm,yy);
      //printf("Waktu  : %02u:%02u:%02u \r",h,m,s);  
          
              
      for(i=0;i<300;i++) {
        values[i] = read_adc(0);            

      if (values[i] >= z) {
        z = values[i];
      }        
    
      } 
    
      for(k=0;k<300;k++) {
        values[k] = read_adc(0);            

      if (values[k] >= zk) {
        zk = values[k];
      }        
    
      }
    
      for(m=0;m<300;m++) {
        values[m] = read_adc(0);            

      if (values[m] >= zm) {
        zm = values[m];
      }        
    
      }
    
      for(s=0;s<300;s++) {
        values[s] = read_adc(0);            

      if (values[s] >= zs) {
        zs = values[s];
      }        
    
      }


      if((z >= zk)&&(z >= zm)&&(z >= zs)){  
         vrms = (float)(z - 518.1) / 0.413;
         if(vrms < 100){
         vrms = 0;
         }
         ftoa(vrms,1,temp);
      }
      if((zk >= z)&&(zk >= zm)&&(zk >= zs)){  
         vrms = (float)(zk - 518.1) / 0.413;
         if(vrms < 100){
         vrms = 0;
         }
         ftoa(vrms,1,temp);
      }
      if((zm >= z)&&(zm >= zk)&&(zm >= zs)){   
         vrms = (float)(zm - 518.1) / 0.413;
         if(vrms < 100){
         vrms = 0;
         }
         ftoa(vrms,1,temp);
      }
      if((zs >= z)&&(zs >= zk)&&(zs >= zm)){
         vrms = (float)(zs - 518.1) / 0.413;
         if(vrms < 100){
         vrms = 0;
         }
         ftoa(vrms,1,temp);
      }

       
      
   
      z = 0;
      zk = 0;
      zm = 0;
      zs = 0;
     
     
     
      //======================================================
     
      for(ix=0;ix<300;ix++) {
        valuesx[ix] = read_adc(1);            

      if (valuesx[ix] >= zx) {
        zx = valuesx[ix];
      }        
    
      }  
    
      for(kx=0;kx<300;kx++) {
        valuesx[kx] = read_adc(1);            

      if (valuesx[kx] >= zkx) {
        zkx = valuesx[kx];
      }        
    
      }
    
      for(mx=0;mx<300;mx++) {
        valuesx[mx] = read_adc(1);            

      if (valuesx[mx] >= zmx) {
        zmx = valuesx[mx];
      }        
    
      }
    
      for(sx=0;sx<300;sx++) {
        valuesx[sx] = read_adc(1);            

      if (valuesx[sx] >= zsx) {
        zsx = valuesx[sx];
      }        
    
      }


      if((zx >= zkx)&&(zx >= zmx)&&(zx >= zsx)){
         irms = (float)(zx * (5.0/1023.0));
         irms = (float)(irms - 2.50) / 0.1;
         if(irms < 0){
         irms = 0;
         }
         ftoa(irms,2,tempx);
      }
      if((zkx >= zx)&&(zkx >= zmx)&&(zkx >= zsx)){
         irms = (float)(zkx * (5.0/1023.0));
         irms = (float)(irms - 2.50) / 0.1;
         if(irms < 0){
         irms = 0;
         }
         ftoa(irms,2,tempx);
      }
      if((zmx >= zx)&&(zmx >= zkx)&&(zmx >= zsx)){
         irms = (float)(zmx * (5.0/1023.0));
         irms = (float)(irms - 2.50) / 0.1; 
         if(irms < 0){
         irms = 0;
         }
         ftoa(irms,2,tempx);
      }
      if((zsx >= zx)&&(zsx >= zkx)&&(zsx >= zmx)){
         irms = (float)(zsx * (5.0/1023.0));
         irms = (float)(irms - 2.50) / 0.1;  
         if(irms < 0){
         irms = 0;
         }
         ftoa(irms,2,tempx);
      }
       
     
        
        PORTB = 0b11011111;
        delay_ms(20);
        if(PINB.2 == 0){
        delay_ms(200);
               
        lcd_gotoxy(0,1);
        lcd_putsf("V=");
        lcd_puts(temp);
        lcd_putsf("   ");
      
        lcd_gotoxy(8,1);
        lcd_putsf("I=");
        lcd_puts(tempx);
        lcd_putsf("   ");
       
        }
       
        if(PINB.2 == 1){
        delay_ms(200);
        lcd_gotoxy(0,1);      
        lcd_putsf("Pmax= ");
        lcd_puts(tempo);
        lcd_putsf(" watt            ");            
        }
       
       
        p = (float)(vrms * irms);
       
        ftoa(p,1,temp5);
       
        lcd_gotoxy(9,0);
        lcd_putsf("P=");
        lcd_puts(temp5);
        lcd_putsf(" ");
       
      delay_ms(200);
    
      zx = 0;
      zkx = 0;
      zmx = 0;
      zsx = 0;
     
       PORTB = 0b10111111;
       delay_ms(20);
       if(PINB.3 == 0){
       delay_ms(2000);
        i=0;
        nilai=0;
       lcd_clear();
       inputdaya();
      
       } 
      
      
       if(p > nilai){
       //off
        PORTC.7 = 0;
       }
       if(p < nilai){
       //on
        PORTC.7 = 1;
       }
      
        if((p > nilai - 20)&&(p > 0)){
       //off
        PORTC.6 = 0;
        delay_ms(200);
        PORTC.6 = 1;
        delay_ms(200);
       }
       if(p < nilai - 20){
       //on
        PORTC.6 = 0;
       }
      
       biaya = p * 450;
      
       printf("%0.1f",vrms);
       putchar('|');
       printf("%0.1f",irms);
       putchar('|');
       printf("%0.1f",p);
       putchar('|');
       printf("%i",biaya);
       putchar('|');                        
      } 
     
}


void inputdaya(){

        PORTC.6 = 0;
       
        scanning_keypad();
        tampil_lcd();
       
        PORTB = 0b11101111;
        delay_ms(20);
        if(PINB.3 == 0){
        delay_ms(200);
        lcd_clear();
        return;
        } //*

        delay_ms(200);
        inputdaya();
}

void scanning_keypad()
{

 int waktu = 300;

 PORTB = 0b10111111;
 delay_ms(20);
 if(PINB.2 == 0){n++; array[n]=9; simpan_dlm_1variabel();delay_ms(waktu);}
 if(PINB.1 == 0){n++; array[n]=6; simpan_dlm_1variabel();delay_ms(waktu);}
 if(PINB.0 == 0){n++; array[n]=3; simpan_dlm_1variabel();delay_ms(waktu);}

 PORTB = 0b11011111;
 delay_ms(20);
 if(PINB.3 == 0){n++; array[n]=0; simpan_dlm_1variabel();delay_ms(waktu);}
 if(PINB.2 == 0){n++; array[n]=8; simpan_dlm_1variabel();delay_ms(waktu);}
 if(PINB.1 == 0){n++; array[n]=5; simpan_dlm_1variabel();delay_ms(waktu);}
 if(PINB.0 == 0){n++; array[n]=2; simpan_dlm_1variabel();delay_ms(waktu);}

 PORTB = 0b11101111;
 delay_ms(20);
 if(PINB.2 == 0){n++; array[n]=7; simpan_dlm_1variabel();delay_ms(waktu);}
 if(PINB.1 == 0){n++; array[n]=4; simpan_dlm_1variabel();delay_ms(waktu);}
 if(PINB.0 == 0){n++; array[n]=1; simpan_dlm_1variabel();delay_ms(waktu);}


}

void simpan_dlm_1variabel()
{
    if ( n == 1 ){nilai = array[n];}
    if ( n >= 2 &&  n <= 8 )
    {
        nilai = (nilai*10)+array[n];
    }
   
}

void tampil_lcd()
{
 lcd_gotoxy(0,0);
 lcd_putsf("Input Daya");
 if(nilai>0)
 {
    ftoa(nilai,0,tempo);
    lcd_gotoxy(0,1);
    lcd_puts(tempo);
 }        

}






i. Program Interface VB 6


Option Explicit
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 30
Dim sHari As String
Dim aHari



Private Sub berhenti_Click()
  start.Enabled = True
    berhenti.Enabled = False
    TimerBaca.Enabled = False
End Sub


Private Sub Command2_Click()
Unload Me
End Sub



Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
    ComboCOM.AddItem (i)
Next i
 aHari = Array("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu")

End Sub



Private Sub MSComm2_OnComm()
Dim buffer As String
Dim temp As String
Dim pisah() As String
Dim i As Integer

If MSComm2.CommEvent = comEvReceive Then
    buffer = MSComm2.Input
    'pisah = Split(buffer, Chr$(13))
    pisah = Split(buffer, "|")

On Error Resume Next
If buffer <> " " Then
With Text1
    .SelStart = Len(.Text)
    .SelText = buffer
End With
For i = 0 To 4
    If 0 < 5 Then
        Text2(i).Text = CStr(pisah(i))
        End If
        Next i
    Else
    buffer = " "
    End If
End If
End Sub

Private Sub start_Click()
Dim u As Integer

    On Error GoTo ada_eror
   
    MSComm2.CommPort = ComboCOM.Text
    MSComm2.Settings = "9600,N,8,1"
    MSComm2.RThreshold = 20
    MSComm2.InputLen = 20
    MSComm2.InputMode = comInputModeText
    MSComm2.PortOpen = True ' buka port
   
    ReDim arrdata(1 To MAKSBACA + 1)
    For u = 1 To MAKSBACA
        arrdata(u) = 0
    Next
    TotalBaca = 0
    start.Enabled = False
    berhenti.Enabled = True
    TimerBaca.Enabled = True
ada_eror:
 If ComboCOM.ListIndex < 1 Then
    MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
    start.Enabled = True
    berhenti.Enabled = True
    ComboCOM.Text = "Pilih COM"
End If
End Sub

Private Sub Timer1_Timer()
 sHari = aHari(Abs(Weekday(Date) - 1))
 Text3.Text = "" & sHari & "," & Format(Date, "dd mmmm yyyy")
 Text4.Text = Format(Time, "hh:mm:ss")
End Sub

Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
    strInput = MSComm2.Input
        strPotong = strInput
        'Text1.Text = Text1.Text + strPotong + vbCrLf
        'singleInput = Val(strPotong)
        'Label1.Caption = singleInput
        If TotalBaca >= MAKSBACA Then
            TimerBaca.Enabled = False
            If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
            Call berhenti_Click
        End If
 
End Sub






j. VIDEO HASILNYA











No comments:

Post a Comment