Monday, September 1, 2014

PLC Real Time Clock Using Arduino




PLC real time clock using arduino and real time clock module. for real time module use DS3231 RTC Board. The way it works is: Arduino reads data from the DS3231, this data is sent directly to the PLC memory. and for setting the date and time via the DS3124 and computer by using visual basic net (VB.Net)

PLC real time clock process:
1. Read date and time from DS3231 RTC Board with Arduino via I2C communication
2. Send data to PLC with Arduino via RS232 communication and PLC Protocol
3. Receive data in PLC data memory
Flow process of the PLC Real TIme Clock:
Flow process of the PLC Real TIme Clock

Hardware requirements:
1. DS3231 RTC Board Module
DS3231 RTC Board Module

2. Arduino Nano
Arduino Nano Ver3

3. Multi Master Cable S7-200 RS-232
Multi Master Cable S7-200 RS-232

4. Siemens PLC S7-200 (not real time clock)
Siemens PLC S7-200 CPU 226

5. DB9 Male crossover cable
DB9 Male crossover cable

6. RS232 Serial Port To TTL Converter Module
RS232 Serial Port To TTL Converter Module


Wiring of DS3231 Module to Arduino
Wiring of DS3231 Module to Arduino

Upload Program to Arduino
1. Download Arduino Project File, click here
2. Open Project file and Upload to Arduino

Date Time Setting in DS3231 Module using VB.NET:
1. Connect USB cable from Arduino to computer
2. Download VB Project File, click here
3. Open VB Project File and RUN
See Video for DS3231 Setting:

Download Ladder PLC Programming to PLC
1. Download file, click here
2. And Download Ladder programming to PLC

Wiring of DS3231,Arduino,PLC Cable, RS232 Cross and PLC
For a description of the image numbers, can be seen in the hardware requirements
Wiring of PLC Real Time Clock Using Arduino

Clock in PLC memory

VB0=SECOND : 0 to 59
VB1=MINUTE : 0 to 59
VB2=HOUR OF DAY : 0 to 23
VB3= DATE : 1 to 28/29/30//31
VB4= MONTH : 1 to 12
VB5= DAY OF WEEK : 1-SUNDAY, 2-MONDAY, 3-TUESDAY, 4-WEDNESDAY, 5-THURSDAY, 6-FRIDAY, 7-SATURDAY
VB6= YEAR: 0 to 99

Arduino Source Code
#include <Wire.h>;
const int DS1307 = 0x68;
byte second = 0;
byte minute = 0;
byte hourofday = 0;
byte dayofweek = 0;
byte date = 0;
byte month = 0;
byte year = 0;
byte set[7];
const char delimiter = ',';
String  message;
byte i;
int y;

void setup() {
  Wire.begin();
  Serial.begin(9600,SERIAL_8E1);//9600,8,Even,1
  Serial.setTimeout(800);
}

void loop() {
  sendTime();  
  message = Serial.readString();  //= "year,month,date,week,hour,minute,second";
  incomeTime(); 
}
byte decToBcd(byte val) {
  return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
  return ((val/16*10) + (val%16));
}

void incomeTime() { 
  i=0;
  y=-1;  
  do
  {
      y = message.indexOf(delimiter);
      if(y != -1)
      {
          if(i<=5)set[i]=message.substring(0,y).toInt();
          message = message.substring(y+1, message.length());
          i++;
      }
      else
      {
         if(message.length() > 0 && i==6)
           set[i]=message.toInt();         
      }
   }
   while(y >=0);
   
   if (i==6){
     if(set[0]>=0 && set[0]<=99){
       if(set[1]>=1 && set[1]<=12){
         if(set[2]>=1 && set[2]<=31){
           if(set[3]>=1 && set[3]<=7){
             if(set[4]>=0 && set[4]<=23){
               if(set[5]>=0 && set[5]<=59){
                 if(set[6]>=0 && set[6]<=59){
                   setTime();
                 }
               }
             }
           }
         }
       }
     }
   }

}
void setTime() { 
  year =set[0]; 
  month =set[1]; 
  date =set[2]; 
  dayofweek =set[3]; 
  hourofday =set[4]; 
  minute =set[5]; 
  second = set[6];
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hourofday));
  Wire.write(decToBcd(dayofweek));
  Wire.write(decToBcd(date));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(byte(0));
  Wire.endTransmission();
}

void sendTime() {
  readTime();
  int VB0 = second;// VB0=SECOND : 0 to 59
  int VB1 = minute;//VB1=MINUTE : 0 to 59
  int VB2 = hourofday;//VB2=HOUR OF DAY : 0 to 23
  int VB3 = date;//VB3= DATE : 1 to 31
  int VB4 = month;//VB4= MONTH : 1 to 12
  int VB5 = dayofweek;//VB5= DAY OF WEEK : 1=SUNDAY, 2=MONDAY, 3=TUESDAY, 4=WEDNESDAY, 5=THURSDAY, 6=FRIDAY, 7=SATURDAY
  int VB6 = year;//VW6= YEAR 00 to 99

  int VWstart = 0; //Start from VW0
  int VWcount = 4; 

  byte str_write[45];
  long Temp_FCS=0;
  int i;
                    
  str_write[1] = (byte)((VWcount * 2) + 31);
  str_write[2] = (byte)str_write[1];
  str_write[24] = (byte)(VWcount * 2);
  str_write[16] = (byte)(str_write[24] + 4);
  str_write[33] = (byte)((VWcount * 16) / 256);
  str_write[34] = (byte)((VWcount * 16) % 256);

  str_write[29] = (byte)((VWstart * 8) / 256);
  str_write[30] = (byte)((VWstart * 8) % 256);

  str_write[0] = (byte)0x68;//H68
  str_write[3] = (byte)0x68;//H68
  str_write[4] = (byte)0x02;//H2
  str_write[5] = (byte)0x00;//H0
  str_write[6] = (byte)0x7C;//H7C
  str_write[7] = (byte)0x32;//H32
  str_write[8] = (byte)0x01;//H1
  str_write[9] = (byte)0x00;//H0
  str_write[10] = (byte)0x0;//H0
  str_write[11] = (byte)0x43;//H43
  str_write[12] = (byte)0x01;//H1
  str_write[13] = (byte)0x00;//H0
  str_write[14] = (byte)0x0E;//HE
  str_write[15] = (byte)0x00;//H0

  str_write[17] = (byte)0x05;//H5
  str_write[18] = (byte)0x01;//H1
  str_write[19] = (byte)0x12;//H12
  str_write[20] = (byte)0x0A;//HA
  str_write[21] = (byte)0x10;//H10
  str_write[22] = (byte)0x02;//H2
  str_write[23] = (byte)0x00;//H0

  str_write[25] = (byte)0x00;//H0
  str_write[26] = (byte)0x01;//H1  
  str_write[27] = (byte)0x84;//H84  
  str_write[28] = (byte)0x00;//H0

  str_write[31] = (byte)0x00;//H0
  str_write[32] = (byte)0x04;//H4


  str_write[35] = (byte)VB0;
  str_write[36] = (byte)VB1;

  str_write[37] = (byte)VB2;
  str_write[38] = (byte)VB3;

  str_write[39] = (byte)VB4;
  str_write[40] = (byte)VB5;

  str_write[41] = (byte)VB6;
  str_write[42] = (byte)0;

  for (i = 4; i <= 42; i++)
  {
      Temp_FCS = Temp_FCS + str_write[i];
  }
  str_write[43] = (byte)(Temp_FCS % 256);
  str_write[44] = (byte)0x16;//H16

  Serial.write(str_write, sizeof(str_write));

  delay(100);
    byte str_val[6];
    str_val[0] = (byte)0x10;//H10; 
    str_val[1] = (byte)0x02;//H2;
    str_val[2] = (byte)0x00;//H0;
    str_val[3] = (byte)0x5C;//H5C;
    str_val[4] = (byte)0x5E;//H5E;
    str_val[5] = (byte)0x16;//H16;
    Serial.write(str_val, sizeof(str_val));
  delay(100);
}


void readTime() {
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 7);
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hourofday = bcdToDec(Wire.read());
  dayofweek = bcdToDec(Wire.read());
  date = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}



Labels:



Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to program-plc.blogspot.com




We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !

Your email is stored safely on Google FeedBurner