PLC (Programmable Logic Controller) with Microchip Pic Microcontroller

Summary of PLC (Programmable Logic Controller) with Microchip Pic Microcontroller


One year ago I built a 16-input, 16-output PLC using a PIC16F877A, programmed with ladder logic via LDMicro. Inputs are opto-isolated; outputs use 16 relays driven by ULN2803. The core board runs at 20 MHz, powered from +12 V with an LM7805 for +5 V. A built-in PICkit 2 ICSP programmer (with DIP switches) programs the PIC. A separate status LCD board (89C51) uses four 74LS574 latches and a 40×4 LCD to display I/O states. Project code and PCB designs were created and shared.

Parts used in the PLC project:

  • PIC16F877A microcontroller
  • 20 MHz crystal
  • Optocouplers (for 16 inputs)
  • Diodes for input reverse polarity protection
  • Pull-up resistors for input pins
  • 16 relays (12 V rated)
  • ULN2803 Darlington driver IC
  • LM7805 voltage regulator
  • +12 V DC power supply
  • PICkit 2 programmer (on-board, ICSP interface)
  • DIP switches (to connect/disconnect PICkit 2 to PIC)
  • 89C51 microcontroller (status LCD board)
  • Four 74LS574 8-bit latches
  • 40×4 character LCD
  • PCB (core PLC board and status board)
An year ago i made a plc (programmable logic controller) using microchip pic microcontroller. Its a 16 bit digital input and output programmable logic controller. Plc programming is done using a standard ladder logic language. The software that is used for plc programming and ladder logic design is an open source compiler written by Jonathan Westhues called LDMicro. Microchip pic microcontroller that i used in the project is an 8-bit pic16f877a.  

Core PLC (Programmable Logic Controller) board

Core plc board is comprised of 16 digital inputs and 16 digital outputs. Inputs are opto isolated. Optocouplers are used to protect the circuit/processor from heavy loads damage. Diodes are used at inputs from reverse polarity protection. At output side i used 16 relays for switching heavy loads. Relays are rated as +12v. They are derived from Uln2803 Ic.Uln2803 is an 8-channel darling-ton array high output current ic. Uln2803 can sink 500mA of current from a 50V power supply. It has built in fly-back diodes for driving coils and prevents from back efm damage. Relay coils are directly connected to the uln2803 output and does not need the fly back diodes, since they are build inside ic. Core plc board processor is pic16f877a working at 20MHz. Whole board is powered using a +12v dc supply. LM7805 regulator is used to convert the +12v to +5v for power supply to microcontroller, opto couplers and uln2803 relay driver. 

For programming the board/microcontroller/pic16f877A pickit 2 programmer is made on the board. Pickit 2 is a programmer by microchip used to program its pic microcontrollers. Pickit 2 uses icsp interface to program the target microcontroller. Icsp interface of pickit 2 programmer is connected with the pic16f877a microcontroller using DIP switches. When ever a new logic is desired to be downloaded in the processor, switch on the dip switches. Dip switches are installed to prevent the controller pins from interference by the pickit 2 programmer circuit. I have a separate tutorial on pickit 2 programmer made at home. Visit the link for more information 
Microchip Pickit 2 Programmer made at home – Diy Project

The Resistors on the front side of the pcb that are not aligned are added after the pcb printing. These resistors are added as pull-ups to the input pins. While testing the circuit i found that the input pins of pic16f877 floats so i added the pull-up resistors. On pcb back i added some extra tin/lead on the traces to remove any resistance created in them during the etching process.  

Core PLC (Programmable Logic Controller) board

Circuit/Block diagram and Pcb of Core PLC board.

Circuit Block diagram and Pcb of Core PLC board.

PLC status board

Status lcd board purpose is to show the status of the plc inputs and outputs. Four 74ls574 8-bit latches on status board are used to store the input and output status of plc. Two latches store input status and the remaining two store the output status. A 40×4 character lcd on status board is used to show the plc status visually. Active pin is shown as up arrow and the non active pin is shown as down arrow. Status lcd core processor is 89c51 microcontroller. Port-0 of 89c51 has a common 8-bit bus, shared with all the latches. Latches are activated sequentially to obtain the stored result and prevent from data collusion. Latches data is processed and then displayed on the 40×4 character lcd. Lcd is connected to port-1 of 89c51 microcontroller. Lcd is interfaced with 89c51 in 8-bit mode. Status board works on +12v dc.

Some tutorials that will help you in understanding the interfaces and working of the status board.

Pic16f877 microcontroller PLC (Programmable Logic Controller) status board PCB back

Plc Status board Circuit

Below pictures shows the status of a process that is controlled using the plc microcontroller processor. Circuit of status board is made in proteaus. Pcb of the status board is also designed in proteaus. Download the status board microcontroller code from the links given at the bottom of the post. Code is written using keil uvision-4.
 
Plc Status board Circuit

Ladder logic design and processor selection

Below is a simple ladder logic program written in ldmicro. Microcontroller for which the ladder design is made is shown below with the clock frequency on which the microcontroller is operating. I used an external 20Mhz crystal in core plc board. So i selected the crystal frequency of 20Mhz in ldmicro. On the left side a simple code flow is shown. We write code in ldmicro, test the logic. Compile the logic and extract the hex file. This hex file is then loaded to the pic16f877a using pikit-2 programmer. 
 
Ladder logic design and processor selection
Download the project code written in c++ using keil uvision-4. All the project keil project files are included in the folder. Please give us your feed back on the project.
 

Quick Solutions to Questions related to PLC project:

  • What microcontroller was used in the core PLC board?
    The core PLC uses a PIC16F877A microcontroller.
  • How many digital inputs and outputs does the PLC have?
    The PLC has 16 digital inputs and 16 digital outputs.
  • How are the PLC inputs protected?
    Inputs are opto-isolated and diodes are used for reverse polarity protection.
  • What drives the relay coils on the output side?
    The relay coils are driven by a ULN2803 driver IC and are 12 V relays.
  • How is the PIC microcontroller powered?
    The board is powered from +12 V DC and an LM7805 regulator provides +5 V to the microcontroller and other components.
  • Which software is used to design ladder logic for the PLC?
    Ladder logic is designed using the open source LDMicro compiler by Jonathan Westhues.
  • How is the PIC programmed on the board?
    The PIC is programmed using an on-board PICkit 2 via the ICSP interface connected through DIP switches.
  • What displays the PLC input/output status?
    A status board with an 89C51 microcontroller, four 74LS574 latches, and a 40×4 character LCD displays I/O status.
  • Why were pull-up resistors added to input pins?
    Pull-up resistors were added because during testing the PIC16F877A input pins were floating.
  • At what clock frequency does the PLC microcontroller run?
    The PIC16F877A runs with an external 20 MHz crystal.

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.