Tuesday, May 19, 2015

Color Sensor for PLC using RGB Color Sensor and Arduino




Color Sensor for PLC using RGB Color Sensor and Arduino Color sensor for PLC use for color detection on an object. For this application using RGB color sensor and Arduino for transfer data from color sensor module to PLC via Modbus communication.
Examples of applications for color sensors on industrial:
1. Detecting the color of the product
2. Color card to access the machine
3. Detecting the color of waste, if waste is not good then the machine stop.
4. Detecting color on the Workwear Uniform of employees, who will run the machine

Color Sensor for PLC using RGB Color Sensor and Arduino

Video about Color Sensor for PLC:




Preparation of the necessary Hardware:
1. RGB Color Sensor Module: TCS3200 Color Sensor Recognition Module
2. Arduino UNO R3
3. DC 9V Power Plug Adapter for Arduino UNO R3
4. Male Connector RS232 to TTL Serial Port Converter Module
5. 2 pieces push button switch for: black and white color calibration
6. Color Cards for calibration and testing.
7. Siemens S7-200 PLC, or other PLC with Modbus Support
8. PLC Cable : RS232 PPI Multi Master Cable

Hardware of Color Sensor for PLC using RGB Color Sensor and Arduino


Hardware Connections of RGB Color Sensor for PLC

Hardware Connections of RGB Color Sensor for PLC


Download Project File for RGB Color Sensor:
1. Arduino Libraries for this project, click here
Copy-Paste folder: SimpleModbusMaster, EEPROMEx, FreqCount and MD_TCS230 into folder C:\Program Files (x86)\Arduino\libraries
2. Arduino Project File for this application, click here
3. PLC Ladder Programming for RGB Color Sensor, click here

PLC Ladder Programming Setting:
Setting of Red Value, Green Value and Blue Value in PLC Ladder Programming:
Placing objects in color sensor, check Red Value in VW804, Green Value in VW804 and Blue Value in VW808. And then look for the minimum and maximum values.

Setting of Red Value, Green Value and Blue Value in PLC Ladder Programming


Arduino Code for the Color Application on PLC:
//MODBUS
#include <SimpleModbusMaster.h>
//Arduino Serial Port Connect to Port 0 of Siemens PLC S7 200
//for more info program-plc.blogspot.com
#define slaveAddr 1
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 0
#define TxEnablePin 2 
enum
{
  PACKET1,
  PACKET2,
  TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];
unsigned int readRegs[1];
unsigned int writeRegs[5];

//SAVE BLACK WHITE COLOR SETTING
#include <EEPROMex.h>
int CalValueInput[6];
int CalValueOutput[6];
int CalBR = 0;
int CalBG = 0;
int CalBB = 0;
int CalWR = 0;
int CalWG = 0;
int CalWB = 0;

//COLOR SENSOR
#include <MD_TCS230.h>
#include <FreqCount.h>
//Pin definitions
//OUT Pin 5
#define  S2  6
#define  S3  7

MD_TCS230 ColorSensor(S2, S3);
word OrderInc=0;
bool Calibrate=true;


void setup(){
EEPROM.readBlock(0, CalValueOutput,6);  
CalValueInput[0] = CalBR = CalValueOutput[0];
CalValueInput[1] = CalBG = CalValueOutput[1];
CalValueInput[2] = CalBB = CalValueOutput[2];
CalValueInput[3] = CalWR = CalValueOutput[3];
CalValueInput[4] = CalWG = CalValueOutput[4];
CalValueInput[5] = CalWB = CalValueOutput[5];

ColorSensor.begin();  
modbus_construct(packet1, slaveAddr, READ_HOLDING_REGISTERS, 0, 1, readRegs);
modbus_construct(packet2, slaveAddr, PRESET_MULTIPLE_REGISTERS, 1, 5, writeRegs);
modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);  
ColorSensor.read();
}

void loop(){
modbus_update();
word check = (word)readRegs[0];//in PLC VW800  
         
if (ColorSensor.available()) 
{ 
OrderInc++;
if(OrderInc>=32760)OrderInc=10;
writeRegs[0]=(word)OrderInc;//in PLC VW802

sensorData sd;
colorData rgb;
if(OrderInc==1 && CalBR>0 && CalBG>0 && CalBB>0)check=3;
if(OrderInc==2 && CalWR>0 && CalWG>0 && CalWB>0)check=4;

  switch(check)
  {
   case 0:// Read RGB Color  
       ColorSensor.getRGB(&rgb);
       writeRegs[1]= (word)rgb.value[TCS230_RGB_R];//in PLC VW804
       writeRegs[2]= (word)rgb.value[TCS230_RGB_G];//in PLC VW806
       writeRegs[3]= (word)rgb.value[TCS230_RGB_B];//in PLC VW808
       writeRegs[4]=(word)0;//in PLC VW810
       Calibrate=true;
       break;
       
   case 1:// Calibrate Black from Black Card
      if(Calibrate){
    ColorSensor.getRaw(&sd);
 ColorSensor.setDarkCal(&sd);
        CalValueInput[0] = CalBR = (int)sd.value[TCS230_RGB_R];
        CalValueInput[1] = CalBG = (int)sd.value[TCS230_RGB_G];
        CalValueInput[2] = CalBB = (int)sd.value[TCS230_RGB_B];
        if(CalBR>0 && CalBG>0 && CalBB>0){        
          EEPROM.writeBlock(0, CalValueInput,6);
          writeRegs[4]=(word)1;//in PLC VW810
          Calibrate=false;
        }else{
          writeRegs[4]=(word)255;//in PLC VW810=255 for Error
        }
      } 
        break;
        
   case 2:// Calibrate White from White Card
     if(Calibrate){
    ColorSensor.getRaw(&sd); 
 ColorSensor.setWhiteCal(&sd);
        CalValueInput[3] = CalWR = (int)sd.value[TCS230_RGB_R];
        CalValueInput[4] = CalWG = (int)sd.value[TCS230_RGB_G];
        CalValueInput[5] = CalWB = (int)sd.value[TCS230_RGB_B];
        if(CalWR>0 && CalWG>0 && CalWB>0){       
          EEPROM.writeBlock(0, CalValueInput,6);   
          writeRegs[4]=(word)2;//in PLC VW810
          Calibrate=false;
        }else{
          writeRegs[4]=(word)255;//in PLC VW810=255 for Error
        }
     }
        break; 
        
   case 3:// Calibrate Black from EEPROM DATA
        sd.value[TCS230_RGB_R]=CalBR;
        sd.value[TCS230_RGB_G]=CalBG;
        sd.value[TCS230_RGB_B]=CalBB; 
 ColorSensor.setDarkCal(&sd);
        writeRegs[4]=(word)3;//in PLC VW810
        break;
        
   case 4:// Calibrate White from EEPROM DATA
        sd.value[TCS230_RGB_R]=CalWR;
        sd.value[TCS230_RGB_G]=CalWG;
        sd.value[TCS230_RGB_B]=CalWB;        
 ColorSensor.setWhiteCal(&sd);
        writeRegs[4]=(word)4;//in PLC VW810
        break;
        
   default:// Read RGB Color  
       ColorSensor.getRGB(&rgb);
       writeRegs[1]= (word)rgb.value[TCS230_RGB_R];//in PLC VW804
       writeRegs[2]= (word)rgb.value[TCS230_RGB_G];//in PLC VW806
       writeRegs[3]= (word)rgb.value[TCS230_RGB_B];//in PLC VW808
       writeRegs[4]=(word)0;//in PLC VW810
       Calibrate=true;
       break;
  } 
}
}




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 !