Connection diagram of a photoresistor to Arduino. Arduino connection of a photoresistor. Light sensor for arduino. Light sensor circuit using a photoresistor and relay

Sensors are completely different. They differ in the principle of action, the logic of their work and the physical phenomena and quantities to which they are capable of reacting. Light sensors are used not only in automatic lighting control equipment, they are used in a huge number of devices, ranging from power supplies to alarms and security systems.

Main types of photoelectronic devices. General information

A photodetector in a general sense is an electronic device that responds to changes in the light flux incident on its sensitive part. They may differ both in their structure and operating principle. Let's look at them.

Photoresistors - change resistance when illuminated

A photoresistor is a photodevice that changes conductivity (resistance) depending on the amount of light incident on its surface. The more intense the sensitive area, the less resistance. Here is a schematic representation of it.

It consists of two metal electrodes, between which there is a semiconductor material. When light hits a semiconductor, charge carriers are released in it, which promotes the passage of current between the metal electrodes.

The energy of the light flux is spent on electrons overcoming the band gap and their transition to the conduction band. As a semiconductor for photoresistors, materials such as: Cadmium Sulfide, Lead Sulfide, Cadmium Selenite and others are used. The spectral characteristics of the photoresistor depend on the type of material.

Interesting:

The spectral characteristic contains information about which wavelengths (colors) of the light flux the photoresistor is most sensitive to. For some specimens, it is necessary to carefully select a light emitter of the appropriate wavelength in order to achieve the greatest sensitivity and operating efficiency.

The photoresistor is not intended to accurately measure illumination, but rather to determine the presence of light; according to its readings, one can determine whether the environment has become lighter or darker. The current-voltage characteristic of a photoresistor is as follows.

It shows the dependence of current on voltage at different values ​​of luminous flux: F is darkness, and F3 is bright light. It's linear. Another important characteristic is sensitivity, it is measured in mA (μA)/(Lm*V). Which reflects how much current flows through the resistor, given a certain luminous flux and applied voltage.

Dark resistance is an active resistance in the complete absence of lighting, denoted Rt, and the characteristic Rt/Rsv is the factor of change in resistance from the state of the photoresistor in the complete absence of lighting to the maximum illuminated state and the minimum possible resistance, respectively.

Photoresistors have a significant drawback - their cutoff frequency. This value describes the maximum frequency of the sinusoidal signal with which you model the light flux, at which the sensitivity decreases by 1.41 times. In reference books this is reflected either by the frequency value or through the time constant. It reflects the speed of the devices, which usually takes tens of microseconds - 10^(-5) s. This does not allow it to be used where high performance is needed.

Photodiode - converts light into electrical charge

A photodiode is an element that converts light falling on a sensitive area into an electrical charge. This happens because during irradiation, various processes associated with the movement of charge carriers occur in the p-n junction.

If the conductivity of the photoresistor changes due to the movement of charge carriers in the semiconductor, then a charge is formed at the boundary of the p-n junction. It can operate in photoconverter and photogenerator mode.

Its structure is the same as a regular diode, but its body has a window for light to pass through. Externally, they come in various designs.

Photodiodes with a black body perceive only infrared radiation. The black coating is something similar to tinting. Filters the IR spectrum to exclude the possibility of triggering radiation of other spectra.

Photodiodes, like photoresistors, have a cutoff frequency, only here it is orders of magnitude higher and reaches 10 MHz, which allows for good performance. P-i-N photodiodes have high speed - 100 MHz-1 GHz, like diodes based on the Schottky barrier. Avalanche diodes have a cutoff frequency of about 1-10 GHz.

In photoconverter mode, such a diode works as a light-controlled switch; for this, it is connected to the circuit in forward bias. That is, the cathode is to a point with a more positive potential (towards plus), and the anode is to a more negative potential (towards minus).

When the diode is not illuminated by light, only reverse dark current Irev flows in the circuit (units and tens of μA), and when the diode is illuminated, a photocurrent is added to it, which depends only on the degree of illumination (tens of mA). The more light, the greater the current.

Photocurrent Iф is equal to:

where Sint is the integral sensitivity, Ф is the luminous flux.

Typical circuit for switching on a photodiode in photoconverter mode. Pay attention to how it is connected - in the opposite direction to the power source.

Another mode is generator. When light hits a photodiode, a voltage is generated at its terminals, and the short circuit currents in this mode are equal to tens of amperes. This resembles, but has low power.

Phototransistors - open depending on the amount of incident light

A phototransistor is essentially one in which, instead of a base output, there is a window in the body for light to enter. The operating principle and reasons for this effect are similar to previous devices. Bipolar transistors are controlled by the amount of current flowing through the base, and phototransistors are similarly controlled by the amount of light.

Sometimes the UGO also displays the output of the base. In general, the voltage is applied to the phototransistor in the same way as to a regular one, and the second connection option is with a floating base, when the base pin remains unused.

Phototransistors are included in the circuit in a similar way.

Or swap the transistor and resistor, depending on what exactly you need. In the absence of light, a dark current flows through the transistor, which is formed from the base current, which you can set yourself.

Having set the required base current, you can set the sensitivity of the phototransistor by selecting its base resistor. This way, even the dimmest light can be captured.

In Soviet times, radio amateurs made phototransistors with their own hands - they made a window for light by cutting off part of the body of an ordinary transistor. Transistors like MP14-MP42 are excellent for this.

From the current-voltage characteristic, the dependence of the photocurrent on illumination is visible, while it is practically independent of the collector-emitter voltage.

In addition to bipolar phototransistors, there are also field-effect ones. Bipolar ones operate at frequencies of 10-100 kHz, while field ones are more sensitive. Their sensitivity reaches several Amps per Lumen, and the “faster” ones - up to 100 MHz. Field-effect transistors have an interesting feature: at maximum luminous flux values, the gate voltage has almost no effect on the drain current.

Application areas of photoelectronic devices

First of all, you should consider more familiar options for their use, for example, automatically turning on the light.

The circuit shown above is the simplest device for turning a load on and off at a certain light level. Photodiode FD320 When light hits it, it opens and a certain voltage drops across R1, when its value is sufficient to open transistor VT1 - it opens and opens another transistor - VT2. These two transistors are a two-stage current amplifier, necessary to power the relay coil K1.

Diode VD2 is needed to dampen the EMF self-induction that is formed when the coil is switched. One of the wires from the load is connected to the supply contact of the relay, the top one in the diagram (for alternating current - phase or zero).

We have normally closed and open contacts; they are needed either to select the circuit to be turned on, or to select whether to turn on or turn off the load from the network when the required illumination is achieved. Potentiometer R1 is needed to adjust the device to operate with the required amount of light. The greater the resistance, the less light is needed to turn on the circuit.

Variations of this circuit are used in most similar devices, adding a certain set of functions if necessary.

In addition to switching on the light load, such photodetectors are used in various control systems, for example, on metro turnstiles, photoresistors are often used to detect unauthorized (hare) crossing of the turnstile.

In a printing house, when a strip of paper breaks, the light hits the photodetector and thereby gives a signal to the operator about this. The emitter is on one side of the paper, and the photodetector is on the reverse side. When the paper is torn, light from the emitter reaches the photodetector.

In some types of alarms, an emitter and a photodetector are used as sensors for entering a room, while IR devices are used to prevent the radiation from being visible.

Regarding the IR spectrum, there is no mention of the TV receiver, which receives signals from the IR LED in the remote control when you change channels. The information is encoded in a special way and the TV understands what you need.

Information was previously transmitted in this way through the infrared ports of mobile phones. The transmission speed is limited both by the serial transmission method and by the operating principle of the device itself.

Computer mice also use technology related to photoelectronic devices.

Applications for signal transmission in electronic circuits

Optoelectronic devices are devices that combine an emitter and a photodetector in one housing, such as those described above. They are needed to connect two circuits of an electrical circuit.

This is necessary for galvanic isolation, fast signal transmission, as well as for connecting DC and AC circuits, as in the case of controlling a triac in a 220 V 5 V circuit with a signal from a microcontroller.

They have a conventional graphic designation that contains information about the type of elements used inside the optocoupler.

Let's look at a couple of examples of using such devices.

If you are designing a thyristor or triac converter you will encounter a problem. Firstly, if the transition at the control output breaks, a high potential will hit and the latter will fail. For this purpose, special drivers have been developed with an element called an optosimistor, for example MOC3041.

Switching stabilized power supplies require feedback. If we exclude galvanic isolation in this circuit, then if some components in the OS circuit fail, a high potential will arise on the output circuit and the connected equipment will fail, I’m not talking about the fact that you can get an electric shock.

In a specific example, you see the implementation of such an OS from the output circuit to the feedback (control) winding of the transistor using an optocoupler with the serial designation U1.

conclusions

Photo- and optoelectronics are very important sections in electronics, which have significantly improved the quality of equipment, its cost and reliability. Using an optocoupler, it is possible to eliminate the use of an isolating transformer in such circuits, which reduces weight and size parameters. In addition, some devices simply cannot be implemented without such elements.

New articles

● Project 13: Photoresistor. We process illumination by lighting or extinguishing LEDs

In this experiment we will get acquainted with an analog sensor for measuring illumination - a photoresistor (Fig. 13.1).

Required components:

A common use of a photoresistor is to measure illuminance. In the dark its resistance is quite high. When light hits a photoresistor, the resistance drops in proportion to the illumination. The connection diagram of the photoresistor to Arduino is shown in Fig. 13.2. To measure illumination, it is necessary to assemble a voltage divider, in which the upper arm will be represented by a photoresistor, the lower arm by an ordinary resistor of a sufficiently large value. We will use a 10 kOhm resistor. We connect the middle arm of the divider to the analog input A0 of the Arduino.

Rice. 13.2. Connection diagram for photoresistor to Arduino

Let's write a sketch for reading analog data and sending it to the serial port. The contents of the sketch are shown in Listing 13.1.

Int light; // variable for storing photoresistor data void setup()( Serial.begin(9600 ); ) void loop()( light = analogRead(0); Serial.println(light); delay(100); )
Connection order:

1. Connect the photoresistor according to the diagram in Fig. 13.2.
2. Load the sketch from Listing 13.1 onto the Arduino board.
3. We adjust the illumination of the photoresistor by hand and observe the output of changing values ​​​​to the serial port, remember the readings when the room is fully illuminated and when the light flux is completely blocked.

Now let's create a light indicator using an LED row of 8 LEDs. The number of LEDs lit is proportional to the current illumination. We assemble the LEDs according to the diagram in Fig. 13.3, using limiting resistors with a nominal value of 220 Ohms.

Rice. 13.3. Connection diagram for photoresistor and LEDs to Arduino


The contents of the sketch for displaying the current illumination on a line of LEDs are shown in Listing 13.2.

// Contact for connecting LEDs const int leds=(3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ); const int LIGHT=A0; // Pin A0 for photoresistor input const int MIN_LIGHT=200 ; // Lower illumination threshold const int MAX_LIGHT=900 ; // upper illumination threshold // Variable for storing photoresistor data int val = 0 ; void setup(){ // Configure LED pins as output for (int i=0 ;i<8 ;i++) pinMode(leds[i],OUTPUT); } void loop()( val = analogRead(LIGHT); // Read the photoresistor readings // Using the map() function val = map(val, MIN_LIGHT, MAX_LIGHT, 8, 0); // limit so that it does not exceed the limits val = constrain(val, 0 , 8 ); // light up the number of LEDs proportional to the illumination, // put out the rest for (int i=1 ;i<9 ;i++) { if (i>=val) // light up the LEDs digitalWrite(leds,HIGH); else // turn off the LEDs digitalWrite(leds,LOW); ) delay(1000); // pause before next measurement }
Connection order:

1. Connect the photoresistor and LEDs according to the diagram in Fig. 13.3.
2. Load the sketch from Listing 13.2 onto the Arduino board.
3. We adjust the illumination of the photoresistor by hand and determine the current illumination level by the number of lit LEDs (Fig. 13.3).

We take the lower and upper illumination limits from the remembered values ​​when conducting the experiment using the previous sketch (Listing 13.1). We scale the intermediate illumination value by 8 values ​​(8 LEDs) and light the number of LEDs proportional to the value between the lower and upper limits.

Program listings

An example of connecting a photoresistor to control an LED

This example demonstrates connecting a photoresistor to control an LED to create an analogue of a night light. The resistance of the photoresistor depends on the intensity of the light incident on it, so when the light decreases, the LED will burn brightly, and when there is bright light, it will turn off.

Required Components

  • Bread board;
  • Resistor on 220 Ohm;
  • Resistor on 10 kOhm;
  • Photoresistor with nominal resistance 200kOhm;
  • One red LED;
  • Jumper wires;

Scheme

Connect 9 -th digital pin of Arduino with one of the pins 220 Ohm th resistor, connect the other terminal of this resistor to the anode of the LED (long leg), and the cathode of the LED to ground (contact GND on the Arduino board). Contact 5V connect the Arduino board to one of the photoresistor pins, and connect the other pin to 0 th analog pin of the Arduino and with one of the pins 10kOhm th resistor, connect the other terminal of the resistor to ground (contact GND on the Arduino board).

A resistive voltage divider consists of two resistors; the output voltage depends on the ratio of resistances. In this example, one of the resistors is variable (photoresistor, with a rated resistance of 200kOhm, that is, in complete darkness, the resistance of the photoresistor will be equal to the nominal value, and in bright light it will drop almost to zero), so we can get a change in voltage. Another resistor determines the sensitivity. If you use a trim resistor, you can make an adjustable sensitivity.

The scale and accuracy of the readings depends on where the photoresistor is located and the value of the constant resistor in the voltage divider circuit. Change the circuit and look through the port monitor (for this you can download the code from the section "Code for adjusting parameters", located below) as the readings change.

In the port monitor, in the first and second cases, you will see that you will not get the entire range of values ​​​​(from 0 to 1023), because the resistance of the photoresistor will never be zero. But you can determine the minimum (MIN_LIGHT) and maximum (MAX_LIGHT) illumination values ​​(the values ​​depend on the lighting conditions, the value of the constant resistor and the characteristics of the photoresistor) to build our “night light”.

Code

Upload the sketch shown below to your Arduino board.

    #define RLED 9 //Connect the red LED to the 9th digital pin with PWM support

  1. int val = 0 ; //Variable for storing the value read from the sensor

  2. void setup()

    pinMode(RLED, OUTPUT) ; //Set pin 9 as output

  3. void loop()

    val = analogRead(LIGHT) ; //read the value from the analog input

    val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0) ; //convert the range of read values

    val = constrain(val, 0 , 255 ) ; //"define" the range of acceptable values

    analogWrite(RLED, val) ; //control the LED

  • Note: This example uses a photoresistor with a nominal resistance of 200kOhm. If you have a photoresistor of a different value, you may have to change the minimum (MIN_LIGHT) and maximum (MAX_LIGHT) illumination values.

Code for adjusting parameters

If you have a photoresistor with a different rating, you may have to adjust the minimum (MIN_LIGHT) and maximum (MAX_LIGHT) illumination values. To do this, add two lines of code (highlighted). And determine the minimum (MIN_LIGHT) and maximum (MAX_LIGHT) illumination values ​​by blocking (and vice versa opening) access to light for the photoresistor with your hand and observing changes in values ​​​​using a serial port monitor. After adjustments, you can comment out the added lines of code.

    #define RLED 9 //Connect the red LED to the 9th digital pin with PWM support

    #define LIGHT 0 //Connect the photoresistor to the 0th analog pin

    #define MIN_LIGHT 200 //Minimum light value

    #define MAX_LIGHT 900 //Maximum light value

Automation of lighting supply in an apartment, house or street is achieved through the use of photo relays. If configured correctly, it will turn on the light when it gets dark and turn off during daylight hours. Modern devices contain a setting that allows you to set the trigger depending on the light level. They are an integral part of the “smart home” system, taking on a significant part of the responsibilities of the owners. The photo relay circuit first of all contains a resistor that changes the resistance under the influence of light. It is easy to assemble and configure with your own hands.

Operating principle

The connection diagram for a photo relay includes a sensor, an amplifier and a photoconductor PR1 changes resistance under the influence of light. At the same time, the magnitude of the electric current passing through it changes. The signal is amplified by a composite transistor VT1, VT2 (Darlington circuit), and from it goes to the actuator, which is K1.

In the dark, the resistance of the photosensor is several mOhms. Under the influence of light it decreases to several kOhms. In this case, transistors VT1, VT2 open, turning on relay K1, which controls the load circuit through contact K1.1. Diode VD1 does not allow self-induction current to pass when the relay is turned off.

Despite its simplicity, the photo relay circuit is highly sensitive. To set it to the required level, resistor R1 is used.

The supply voltage is selected according to the relay parameters and is 5-15 V. The winding current does not exceed 50 mA. If it is necessary to increase it, more powerful transistors and relays can be used. The sensitivity of the photo relay increases with increasing supply voltage.

Instead of a photoresistor, you can install a photodiode. If a sensor with increased sensitivity is needed, circuits with phototransistors are used. Their use is advisable in order to save electricity, since the minimum response limit of a conventional device is 5 lux, when surrounding objects are still distinguishable. The threshold of 2 lux corresponds to deep twilight, after which darkness sets in 10 minutes later.

It is advisable to use a photo relay even with manual lighting control, since you can forget to turn off the light, and the sensor will “take care” of this on its own. It is easy to install and the price is quite affordable.

Characteristics of photocells

The choice of photo relay is determined by the following factors:

  • photocell sensitivity;
  • supply voltage;
  • switching power;
  • external environment.

Sensitivity is characterized as the ratio of the resulting photocurrent to the external light flux and is measured in μA/lm. It depends on frequency (spectral) and light intensity (integral). To control lighting in everyday life, the last characteristic is important, depending on the total luminous flux.

The rated voltage can be found on the device body or in the accompanying document. Foreign-made devices may have different supply voltage standards.

The load on its contacts depends on the power of the lamps to which the photo relay is connected. Lighting photo relay circuits can provide for direct switching of lamps through sensor contacts or through starters when the load is high.

Outdoors, the twilight switch is placed under a sealed transparent cover. It provides protection from moisture and precipitation. When working in cold periods, heating is used.

Factory made models

Previously, the photo relay circuit was assembled by hand. Now this is not necessary, since devices have become cheaper and functionality has expanded. They are used not only for external or internal lighting, but also for controlling plant watering, ventilation systems, etc.

1. Photo relay FR-2

Factory-made models are widely used in automation devices, for example, to control street lighting. You can often see lights burning during the day that you forgot to turn off. With photo sensors, there is no need for manual lighting control.

The industrially manufactured photo relay circuit fr-2 is used for automatic control of street lighting. Relay K1 is also here. The FSK-G1 photoresistor with resistors R4 and R5 are connected to the base of transistor VT1.

Power is supplied from a single-phase 220 V network. When the illumination is low, the resistance of FSK-G1 is large and the signal based on VT1 is not enough to open it. Accordingly, transistor VT2 is also closed. Relay K1 is energized and its operating contacts are closed, keeping the lamps lit.

When the illumination increases to the operating threshold, the resistance of the photoresistor decreases and opens, after which relay K1 turns off, opening the power supply circuit for the lamps.

2. Types of photo relay

The choice of models is large enough so that you can choose the right one:

  • with a remote sensor located outside the product body, to which 2 wires are connected;
  • Lux 2 - a device with high reliability and quality level;
  • photo relay with 12 V power supply and load no higher;
  • module with a timer mounted on a DIN rail;
  • IEC devices from a domestic manufacturer with high quality and functionality;
  • AZ 112 - automatic machine with high sensitivity;
  • ABB, LPX are reliable manufacturers of European quality devices.

Methods for connecting a photo relay

Before purchasing a sensor, you need to calculate the power consumed by the lamps and take it with a margin of 20%. With a significant load, the circuit of a street photo relay provides for the additional installation of an electromagnetic starter, the winding of which must be switched on through the contacts of the photo relay, and switch the load with power contacts.

This method is rarely used at home.

Before installation, the supply voltage of ~220 V is checked. The connection is made from a circuit breaker. The photo sensor is installed in such a way that the light from the flashlight does not fall on it.

The device uses terminals to connect wires, which makes installation easier. If they are missing, a junction box is used.

Thanks to the use of microprocessors, the connection diagram of the photo relay with other elements has acquired new functions. A timer and a motion sensor were added to the action algorithm.

It is convenient when the lamps automatically turn on when a person passes along a landing or along a garden path. Moreover, operation occurs only in the dark. Due to the use of a timer, the photo relay does not react to headlights from passing cars.

The simplest connection diagram for a timer with a motion sensor is serial. For expensive models, special programmable circuits have been developed that take into account various operating conditions.

Photo relay for street lighting

To connect the photo relay, the circuit is applied to its body. It can be found in the documentation for the device.

Three wires come out of the device.

  1. Neutral conductor - common for lamps and photo relays (red).
  2. Phase - connected to the device input (brown).
  3. Potential conductor for supplying voltage from the photo relay to the lamps (blue).

The device operates on the principle of phase interruption or phase switching. Color markings may vary from manufacturer to manufacturer. If there is a ground conductor in the network, it is not connected to the device.

In models with a built-in sensor, which is located inside a transparent case, the street lighting operates autonomously. You just need to supply power to it.

Options with remote sensors are used when the electronic content of the photo relay is conveniently placed in the control panel with other devices. Then there is no need for stand-alone installation, power wiring and maintenance at height. The electronic unit is placed indoors, and the sensor is taken outside.

Features of photo relay for street lighting: diagram

When installing a photo relay outdoors, you need to take into account some factors.

  1. Availability of supply voltage and matching power of contacts and load.
  2. Installation of devices near flammable materials and in an aggressive environment is not allowed.
  3. The base of the device is located at the bottom.
  4. There should be no moving objects in front of the sensor, such as tree branches.

The wires are connected through an outdoor junction box. It is fixed next to the photo relay.

Selecting a photo relay

  1. The ability to adjust the response threshold allows you to adjust the sensitivity of the sensor depending on the time of year or in cloudy weather. The result is energy savings.
  2. A minimum of labor costs is required when installing a photo relay with a built-in sensitive element. This does not require any special skills.
  3. The timer relay is well programmable for its needs and operation in the set mode. You can set the device to turn off at night. Indication on the device body and push-button control make settings easy.

Conclusion

The use of a photo relay allows you to automatically control the period of lamp switching on. Now there is no longer any need to become a lamplighter. The photo relay circuit, without human intervention, turns on the lights on the streets in the evenings and turns them off in the morning. The devices can control the lighting system, which increases its resource and makes operation easier.

Photoresistor
IMHO an endangered species. The last time I saw him was when I was a child. Usually it is a round piece of metal with a glass window in which you can see something like this. When illuminated, its resistance drops, albeit slightly, by a factor of three to four.

Phototransistor
Lately I keep coming across them; an inexhaustible source of phototransistors is five-inch disk drives. The last time, for the price of dirt, I got 5 pieces of disc converter scarves at a radio flea market, where the light transistors are located opposite the holes for controlling the recording and rotation of the floppy disk. There is also a dual phototransistor (and maybe a photodiode, depending on your luck) in an ordinary ball mouse.
It looks like a regular LED, only the body is transparent. However, LEDs are also the same, so it’s hard to confuse which one is which. But it doesn’t matter, the partisan can be easily calculated with a regular multimeter. It is enough to turn on the ohmmeter between its emitter and collector (it does not have a base) and shine a light on it, and its resistance will collapse simply catastrophically - from tens of kilo-ohms to just a few ohms. The one that I have in the gear rotation detector in the robot changes its resistance from 100 kOhm to 30 Ohm. A phototransistor works like a regular one - it holds current, but the control action here is not the base current, but the luminous flux.

Photodiode
Externally, it is no different from a phototransistor or a regular LED in a transparent housing. Sometimes you can also find ancient photodiodes in metal cases. Usually these are Soviet devices, FD-cheto brands there. It's a metal cylinder with a window at the end and wires sticking out of the back.

Unlike a phototransistor, it can operate in two different modes. In photovoltaic and photodiode.
In the first, photovoltaic, version, the photodiode behaves like a solar battery, that is, if you shine light on it, a weak voltage appears at the terminals. It can be strengthened and applied =). But it is much easier to work in photodiode mode. Here we apply reverse voltage to the photodiode. Since, although it is a photo, it is a diode, the voltage will not go in the opposite direction, which means its resistance will be close to a break, but if it is illuminated, the diode will begin to erode very strongly and its resistance will drop sharply. And sharply, by a couple of orders of magnitude, like a phototransistor.

Range
In addition to the type of device, it also has a working spectrum. For example, a photodetector focused on the infrared spectrum (and most of them) practically does not react to the light of a green or blue LED. It reacts poorly to a fluorescent lamp, but responds well to an incandescent lamp and a red LED, and there’s nothing to say about infrared. So don’t be surprised if your photo sensor doesn’t react well to light, maybe you made a mistake with the spectrum.

Connection
Now it's time to show how to connect it to the microcontroller. With a photoresistor everything is clear, there are no problems here - you take it and hook it up as per the diagram.
It’s more complicated with a photodiode and phototransistor. It is necessary to determine where its anode/cathode or emitter/collector is. This is done simply. You take a multimeter, put it in diode testing mode and hook it to your sensor. The multimeter in this mode shows the voltage drop across the diode/transistor, and the voltage drop here mainly depends on its resistance U=I*R. You take it and illuminate the sensor, monitoring the readings. If the number decreases sharply, then you guessed right and the red wire is on the cathode/collector, and the black wire is on the anode/emitter. If it doesn't change, swap the pins. If it doesn’t help, then either the detector is dead, or you are trying to get a reaction from the LED (by the way, LEDs can also serve as light detectors, but not everything is so simple. However, when I have time, I will show you this technological perversion).


Now about the operation of the circuit, everything is elementary here. In the darkened state, the photodiode does not pass current in the opposite direction, the phototransistor is also closed, and the photoresistor has a very high resistance. The input resistance is close to infinity, which means the input will have full supply voltage aka logical unit. As soon as you now illuminate the diode/transistor/resistor, the resistance drops sharply, and the terminal turns out to be firmly planted on the ground, or very close to the ground. In any case, the resistance will be much lower than the 10 kOhm resistor, which means the voltage will drop sharply and will be somewhere at the level of logical zero. In AVR and PIC you don’t even need to install a resistor; an internal pull-up will be enough. So DDRx=0 PORTx=1 and you will be happy. Well, turn it around like a regular button. The only difficulty that may arise with a photoresistor is that its resistance does not drop so sharply, so it may not reach zero. But here you can play with the size of the pull-up resistor and make sure that the change in resistance is enough to transition through the logic level.

If you just need to measure illumination, and not stupidly catch light/dark, then you will need to hook everything up to the ADC and make the pull-up resistor variable to adjust the parameters.

There is also an advanced type of photo sensors - TSOP there is a built-in frequency detector and amplifier, but I will write about it a little later.

Z.Y.
I have some problems here, so the site will be very slow with the update, I think it will be until the end of the month. Then I hope to return to the previous rhythm.