Arduino Projects

Photodiode, Phototransistor and IR Sensor with Arduino


Photodiode, phototransistor, and IR Sensor with Arduino– A photodiode is a device that, when it is excited by light, produces in the circuit a proportional current flow (and measurable). In this way, they can be made serve as light sensors, although, while it is It is true that there are photodiodes especially sensitive to visible light, the vast majority are especially in infrared light.

It must be taken into account that, despite having a behavior in similar appearance to LDRs, a very important difference from these (in addition sensitivity to other wavelengths) is the response time to changes from darkness to illumination, and vice versa, which in photodiodes is much lower.

Like standard diodes, photodiodes have an anode and a cathode, But be careful, in order for it to work as we wish, a photodiode must always be connect to the circuit in reverse polarity. Of course, the same as with diodes common, normally the anode is longer than the cathode (if they are equal length, the cathode should be marked in some way).

Its internal operation is as follows: when the photodiode is polarized in direct, the light that falls on it does not have an appreciable effect and therefore both the device behaves like a common diode. When polarized in reverse and no light radiation reaches it, it also behaves like a diode normal since the electrons flowing through the circuit do not have enough energy to go through it, so the circuit remains open. But at the moment that the photodiode receives light radiation within a length range of waveform, the electrons receive enough energy to be able to “jump” the reverse photodiode barrier and continue on your way.

Example1: how to check the behavior of photodiode

To test its behavior, we can use the below circuit. This circuit is identical to the one we saw with the LDRs, replacing these with a photodiode (which is identified by a new symbol that we had not seen until now). The value of your voltage divider will depend on the amount of light (infrared) present in the environment: higher resistances improve the sensitivity when there is only one light source and lower resistances improve it when there are many (the sun itself or the lamps are sources of infrared); a value 100 KΩ may be fine to start with. Let us also note that it is the cathode of the photodiode (the shortest terminal, remember) the one that connects to the power supply.

The operation of this circuit is as follows: as long as the photodiode does not detect infrared light, through the analog input of the Arduino board (in this case the number 0) a voltage of 0V will be measured because the circuit will act as a circuit opened. As the light intensity on the photodiode increases will increase the number of electrons passing through it (that is, the intensity of current). This implies that, as the “pull-down” resistance is fixed, by Ohm’s Law the voltage measured at the analog input pin will also increase, up to a moment in which when receiving a lot of light the photodiode does not cause hardly any resistance to the passage of the electrons and therefore the Arduino board reads a maximum voltage of 5V.

Photodiode, phototransistor, IR Sensor

We have added an LED connected to PWM output pin # 5 such as we did when we saw the LDRs to have a visible way (pun intended) of detect the incidence of infrared light on the photodiode. As you can Observe in the code used (shown below), we have made the LED brightness intensity of the amount of infrared light detected by the photodiode: the more infrared radiation received, the brighter the LED will be.


Programming explanation:

First of all For receiving the photodiode value I define integer type variable

Then I define integer type variable for led value “Value sent to the LED”

In void loop function first is I receive the value using analogRead function and store these value in photodiode_value variable and then use serial.println to  print values on serial monitor

 The brightness of the LED is proportional to the amount of infrared light received


Another type of light sensors in addition to photodiodes are called phototransistors, that is, light-sensitive transistors (also normally infrared). Its operation is as follows: when light falls on its base, it is generates a current that brings the transistor to a conducting state. Therefore, a phototransistor is equal to a common transistor with the only difference that the base current Ib is dependent on the received light. In fact, there are phototransistors that can work in both ways: either as phototransistors or as common transistors with a given specific base current Ib.

The phototransistor is much more sensitive than the photodiode (due to the effect of gain of the transistor itself), since the currents that can be obtained with a photodiode are really limited. In fact, you can understand a phototransistor as a combination of photodiode and amplifier, so actually if we would like to build a homemade phototransistor, it would be enough to add to a transistor common a photodiode, connecting the cathode of the photodiode to the collector of the transistor and the anode to the base. In this configuration, the current delivered by the photodiode (which would circulate towards the base of the transistor) would be amplified β times.

In many circuits we can find a phototransistor a short distance from an infrared emitting LED of a compatible wavelength. This couple of components is useful for detecting the interposition of an obstacle between them (due to the interruption of the light beam) and therefore act as switches opticians. They can be used in a multitude of applications, such as in detectors of the passing of a credit card (at an ATM) or the introduction of the paper (in a printer) or as tachometers, among many others. A tachometer is a device that counts the turns per minute made by an obstacle subject to a wheel or blade that rotates (usually due to the operation of a motor); it is that is, it is used to measure the speed of rotation of an object.

phototransistor  consists of two terminals corresponding to the anode and cathode of the LED, and two terminals corresponding to the collector and emitter of an NPN phototransistor. In general, we will want to connect the terminals of the LED to a closed circuit continuously powered (anode to source, cathode to ground), the collector terminal of the photo switch to a power source and the emitter terminal of the photo switch to a digital input of our Arduino board, to be able to detect thus the appearance of current when lighting is received. On the other hand, both this Arduino board input as the emitter should be grounded to through the same pull-down resistor, to obtain more stable readings (a typical value of 10 KΩ may work, but depending on the circuit it may be need higher values).

We can also find the infrared LED plus phototransistor pair in some components called “optocouplers” or “optoisolator”. The schematic representation is usually like this:

Photodiode, phototransistor, IR Sensor

Broadly speaking, an optocoupler acts as a closed circuit when Light comes from the LED to the base of the transistor and open when the LED is off. Its main function is to control and at the same time isolate two parts of a circuit that they normally work at different voltages (just like a common transistor would, but in a somewhat safer way). Physically they are usually chips that offer as at least four pins (same as the photo switches): two corresponding to the terminals of the LED and two corresponding to the collector and emitter of the phototransistor (although they may have one more pin corresponding to the base if allowed control the intensity that flows through it also as standard). Examples of optocouplers are the 4N35 or the CNY75, manufactured by various companies.

The LED-phototransistor pair is also useful for detecting objects located at small distances from it. We will study this in the section corresponding to distance sensors.

How to make Remote control using IR sensor:

An immediate practical utility of an infrared emitter-receiver pair (such as an LED and a photodiode / phototransistor) located at a certain distance is the sending “messages” between them. That is, since infrared light is not visible (and therefore, it does not “annoy”), pulses of a certain duration can be emitted and / or frequency that can be received and processed several meters away without let it “be noticed.” The device that receives them must then be programmed to perform different actions depending on the type of pulse read.

In fact, any device that works with a “remote control” It works in a similar way because in its front part I have to have a sensor infrared sensors (also called “IR” sensors, from the English “infra-red”) that receive the infrared signals emitted by the remote. And what’s inside this is basically an LED that emits pulses of infrared light following a certain pattern that signals to the device the order to be carried out: there is a blink code to turn on the TV, another to change channels, etc.

above we talked about “IR sensors” and not about photodiodes / phototransistors because the former are somewhat more sophisticated. Specifically, IR sensors do not detect any infrared light, but only that that (thanks to the incorporation of an internal band-pass filter and a demodulator) is modulated by a carrier wave with a frequency of 38 KHz + 3 KHz. This basically means only the signals whose information is carried by a 38 KHz waveform will be read. This is to prevent IR sensors go “crazy” when they receive the infrared light that exists coming from all sides (sun, electric light … in this way they only respond to very concrete already standardized.

Another difference with photodiodes / phototransistors is that IR sensors offer a binary response: if they detect an IR signal of 38 KHz the value that is you can read from them in most cases is LOW (0 V), and if they detect nothing, your reading gives a HIGH value (5 V). This behavior is what is usually called “Active low, or” low-active “.

Examples of IR sensors can be  TSOP32838  or the GP1UX311QS. As features The most prominent ones have that their sensitivity range is between wavelengths of 800nm ​​to 1100nm with a maximum response at 940nm and needing around 5V and 3 mA to operate.

The TSOP32838 chip offers three pins: facing its back hemispherical, the leftmost pin is the digital output provided by the sensor, the middle pin must be grounded and the rightmost pin must be VCC. connect to the power supply (between 2.5 V and 5.5 V).


how to turn on led with remote control using Arduino and, TSOP32838  IR Sensor:

To test its operation, we could design a circuit like the one following. The voltage divider for the LED can be between 200 and 1000 ohms.

Photodiode, phototransistor, IR Sensor

The idea is to briefly turn on the LED when the IR sensor detect an infrared signal. But be careful, not just any infrared signal is valid, but only that modulated at 38 KHz. Therefore, to test this circuit, do not We can use any infrared LED: we must use a control knob remote that we have on hand (from a television, a DVD player, a computer, etc.). Once loaded on the Arduino board the sketch presented to Next, if we point that remote control at the IR sensor and press some of its buttons, we should see the LED light up. In this way, we will be using the IR sensor as if it were a switch, which illuminates the LED while detecting that signal and turns it off when it is no longer detected.

the sensor output is connected to the digital input  pin2 of the Arduino board and the LED is connected to its digital output pin12):


Programming explanation:

First I define integer type variable for IR sensor and led

in setup function I set IR sensor as input and led as output

Since the signal emitted by the sensor is normally HIGH, when the button of a remote control is pressed, it changes to LOW. What the pulseIn() function is to pause the sketch until a signal is detected LOW, whose duration does not really interest us but logically it will always be greater than zero. Therefore, if the condition of the if means that a button on a remote control was pressed

It is necessary to wait a certain time (which will depend on the specific remote control model) after the detection of the first LOW signal because each button press produces multiple oscillations between HIGH and LOW values. Although physically he has no nothing to see, we can understand this waiting as if it were a way to avoid a “bounce” (phenomenon studied when we treat the push buttons). Once this waiting time has elapsed, the signal from the sensor should have returned to its resting state (HIGH value).

We keep the LED on for a few milliseconds. During this time the sketch will not be able to detect other keystrokes coming from the remote control. We could also have sent a message to the “Serial monitor” notifying the pulsation.


how to receive the remote control command on the serial monitor using TSOP32838 IR Sensor with Arduino:

First of all download the required library for IR sensor

Arduino IRremote library 

Circuit diagram:

Photodiode, phototransistor, IR Sensor


Programming explanation:

First, I import the required library

Then I define digital input pin for receiver

Then  I create an object called “irrecv” of type IRrecv

Then I declare a variable of a special type, “decode_results”.

Then I Start the receiver in void step function

Then in void loop I look to see if any modulated IR pattern has been detected. If so, I read it and I keep it entirely in the special variable “results”, in hexadecimal number form

 Then I look at what kind of trading pattern it is, if it is from one recognized by the library

And then I show the received pattern (in hexadecimal format) on the serial channel

Once the pattern has been decoded, reactivate the listens to detect the next possible pattern


how to make remote control using TSOP32838 IR Sensor with Arduino:

First of all download the required library for IR sensor

Arduino IRremote library 

Use the same circuit as I used in the previous project

Circuit diagram:

Photodiode, phototransistor, IR Sensor


If you enjoyed this article, be sure to check out my other pieces for more great content!

Related Articles


  1. you should fix this part:
    The TSOP32838 chip offers three pins: facing its back hemispherical, the leftmost pin is the digital output provided by the sensor, the The middle pin must be grounded and the rightmost pin must be grounded. connect to the power supply (between 2.5 V and 5.5 V).

Leave a Reply

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

Back to top button