Arduino 12V Digital Inputs using PC817

Arduino or other microcontroller platforms are very popular among hobbyist and students these days. One of the main reasons of this popularity is the simplicity of these platforms. However, most of these platforms have limited capabilities to their I/O pins. Most Arduino or other microcontroller platforms have 5V or 3.3V input output pins. Also, these microcontrollers do not have any type of isolation to avoid any damage may cause from over voltage inputs. This is one of the main drawbacks in these platforms. As there are lots of industrial sensors that can be used for variety of tasks. But almost every industrial sensor is designed to operate at higher voltages than 5V. Commonly most industrial sensors are using 12V DC or 24V DC for operation and for the output signals as well.

How to connect 12V outputs to 5V input pins ?

Let’s see some of the common available methods to connect a 12V output to a 5V input pin.

  • Using a resistor divider – This is the most basic and most simple method to connect a 12V or 24V output signal to a 5V input pin. In this method two resistors are used in the ratio of the voltages to divide. The drawbacks of this method is if the resistor divider is designed to output 5V when the input is 12V, if it is supplied more than 12V output is also going to exceed 5V and damage the microcontroller’s input pins.
  • Using a transistor level shifter circuit – This method incudes 2 transistors with few resistors and they will output 5V from a 12V input. Also this method does not have the drawback of the resistor divider. Usually this method can tolerate around 30V more than it is designed and it will still output the 5V output signal. However, if a high voltage spike or a surge come across the input this may damage the circuit and the microcontroller input pin as well.
  • Using a optocoupler – This is the most safest and most suitable method to convert a 12V signal to a 5V signal. Usually optocouplers can tolerate few volts and even if a surge come across the circuit it will only damage the optocoupler without harming the microcontroller. So, we just have to replace the optocoupler. PC817 is one of the most versatile optocouplers in the electronics world.


What is PC817 ?

PC817 is an optocoupler, in simple terms PC817 is a device that isolate a circuit electrically using optical signal conversion methods. PC817 has two main parts. First one is the light emitting diode (LED) and the second part is the photosensor. When an input signal is given to PC817 it will be converted in to an optical signal by the light emitting diode (LED) then this optical signal is caught by the photosensor and it will be converted to an electrical signal. So, the input signal will be emitted from the output without any change but without any electrical connection between the input and output. Another advantage of optocouplers like PC817 is we can supply two voltage levels to the input of the optocoupler and output of the optocoupler. In other words, we can convert a signal of one voltage level to another voltage level. You can understand the operation of the optocoupler by studying the symbol of PC817 shown below.

Figure 1. – Symbol of optocoupler

Pinout of PC817

PC817 comes in a small 4 pin package. You can see the physical appearance of the PC817 optocoupler given in the below image.

Figure 2. – Physical appearance of PC817

Let’s find out the pin configuration of the PC817 optocoupler. Below image is showing the pinout and pin labels of the PC817 optocoupler. Functions of each pin are list out below.

Figure 3. – Pinout of PC817

  • Pin 1 : Anode – This is the positive pin of the light emitting diode (LED). This is the input pin of the PC817. Input voltage to this pin should not exceed 1.2V with respect to the ground and it only can handle up to 50mA current.
  • Pin 2 : Cathode – This is the ground or negative pin of the LED. This act as the ground pin of the input side of the PC817
  • Pin 3 : Emitter – This is the emitter of the photosensor inside the PC817 optocoupler.
  • Pin 4 : Collector – This is the collector  of the photosensor inside the PC817 optocoupler.


Main components

Let’s identify what are the main components we going to need for this project other than PC817.

  • 470Ω resistors
  • 10kΩ resistors


Circuit diagram.

You can see the circuit diagram to connect a 12V output to 5V input is given in below image.

Figure 4. – Circuit diagram

Let’s see how this circuit works. R1 470Ω resistor is place in series with the LED of the PC817 with 12V input signal. 470Ω is calculated using voltage divider principle. Assuming 1.2V should be across the LED of the PC817 to work without damaging it. In essence R1 is the current limiting resistor for the LED inside the PC817 optocoupler. R2 is acting as a pull-down resistor to the emitter pin of the PC817. When the photosensor of the PC817 is not active point of the R2 resistor where Arduino input pin is connected will be equal to GND as there is no voltage difference across the R2. When the photosensor of the PC817 is active this point will be around 5V. This circuit is only showing how to connect 12V output to one Arduino pin you can repeat this to any number of pins as you need.

Leave a Reply

Your email address will not be published. Required fields are marked *