Robot Senses: Sharp GP2Y0A21YK IR Distance Sensor. Infrared proximity sensor IR proximity sensor

Robots, like death, all people really need sense organs to navigate in space. The Sharp GP2Y0A21YK infrared rangefinder is very suitable for this role if you need to avoid collisions with obstacles or know approximately where this obstacle is located.

By the way, you may already have one of the robots at home that uses similar sensors. These are almost all sane Chinese robot vacuum cleaners and, I believe, many Roomba models. And probably many others.

And if these sensors have a place in more or less serious technology, then we will find a use for them, right?

In order not to prevaricate, I will say right away: I ordered these sensors not just to play around with. On the contrary, from the very beginning I knew that they would be useful to me to make an interactive lamp that changes the intensity of the glow depending on the position of the palm above it.

Of course, reality made its own adjustments in the end. In other words, it now has five modes: night light, dimmable light, thermometer, manually adjustable northern lights, and automatic northern lights.

And in addition - a couple of service functions: turning on and off the background and overhead lighting in the room.

Here's how it works:

Well, now is the time to talk in more detail about the sensor, thanks to which everything happened.

As I said at the very beginning, the Sharp GP2Y0A21YK is an infrared rangefinder. This means that it is equipped with an IR emitter and an IR receiver: the first serves as the source of the beam, the reflection of which is caught by the second. At the same time, the IR rays of the sensor are invisible to the human eye (although you can discern a red flicker if you look into the sensor) and at this intensity are harmless.

They also have no effect on domestic animals.

According to the characteristics:

  • Supply voltage: 5V
  • Maximum current consumption: 40 mA (typical - 30 mA)
  • Operating range: 10 cm - 80 cm
As for competitors, compared to ultrasonic sensors, for example, the super popular HC-SR04, this sensor has both advantages and disadvantages. The advantages include everything that is said above, i.e. neutrality and harmlessness.

The disadvantages are a shorter range (the HC-SR04 has about 4 m) and dependence on external interference, including some types of lighting. For example, I have come across mentions that sunlight may affect sensor readings.

The sensor is supplied in a spartan kit, i.e. the sensor itself and a cable with a connector for connecting to the sensor. On the other side there are simply tinned wires, which is not very convenient for use with Arduino Uno, but is quite suitable for controllers without soldered connectors. Since I planned to use the sensor with Arduino Pro Mini, it was quite possible suitable option- I simply soldered the wires into the breadboard.

The wires differ in color: yellow - signal, black - ground, red - power plus (+5V).

The sensor output is analog (although for some reason the datasheet says digital). That is, the voltage on it is proportional to the distance to the obstacle. However, as in the case of ultrasound, for the sensor there is a difference between different types obstacles.

In this regard, in the datasheet, Sharp provides data using Kodak reference cards with a reflectivity of 90% as reflectors. Judging by it, at 20 cm the sensor produces 1.3V.

Let's compare with my experimental data:

Let me remind you that the Arduino analog input operates in the range 0V - 5V and has 1024 steps, hence the calculation: (5/1024)*(sensor readings). So if you take into account the fact that everything is done with your own (shaky) hands, then the readings fit well into the characteristics of the sensor. And at the same time you can see that the black surface makes its own adjustments.

So it shines

At the same time, as the attentive reader has noticed, there are specifics. The point is that when the obstacle is closer to the lower limit of the range (10 cm), the sensor begins to consider that the obstacle, on the contrary, is moving away (when I covered it with my hand, the readings were fixed at 345).

It looks something like this:

Hence the conclusion: although the datasheet is quite adequate for many purposes, sometimes it makes sense to conduct experiments so that later it will not be excruciatingly painful. And this is especially true if the sensor is somewhat recessed (or covered with IR-transparent material), which means it can receive reflections from walls or other elements of the housing.

For example, I was faced with the fact that Evlampia, having been installed in its regular place after successful “desktop” tests, began to go crazy. At first I thought that power supply interference was to blame and even installed a couple of capacitors (10 µF and 0.1 µF) in parallel with the sensor power supply, pulled the Arduino analog input to zero through a 10 kOhm resistor and even bought a surge protector socket.

But when this did not help, he returned to the table again, where he twisted the sensor different sides and saw that in fact, even if the distance to the nearest obstacle is more than 80 cm, the sensor readings change noticeably. So if your charges are inadequate, check the actual readings in real conditions.

Here, for example, is an elementary sketch that, firstly, displays sensor readings at half-second intervals, and, secondly, lights up the Arduino LED if the readings fall in the range from 100 to 200:

// Yellow - A0, Black - ground, Red - +5V unsigned int l; void setup() ( Serial.begin(9600); pinMode(A0, INPUT); pinMode(13, OUTPUT); l = 0; ) void loop() ( l = analogRead(A0); Serial.println(l); delay(1000); if (l > 100 && l< 200) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); } }

To sum it up, the sensor, although a little finicky, is very easy to use and relatively cheap.

It can be used in robots, as well as for intersection control doorways, in some interactive devices controlled by gestures and in something else that your imagination suggests.

I'm planning to buy +33 Add to favorites I liked the review +38 +67

Today's device will be an infrared proximity sensor. The sensor is assembled on an inexpensive Attiny13 microcontroller, is easy to manufacture and does not require any adjustment.

Video of the sensor working:

How does such a sensor differ from, say, factory-made motion sensors (which, by the way, have become very accessible and inexpensive)?

The main difference is the scope. Ready-made sensors are still more focused on large rooms and motion control. In our case, the sensor is compact and more designed for proximity control functions and is intended for integration into ready-made projects.

The scope of use can be extensive:

- reaction of objects to the approach of a hand (for example, interactive toys, automatic devices);
- opening cabinets, doors, etc. when a hand approaches;
— turning on the light when passing the “checkpoint”;
— robot orientation in space (control of walls and obstacles);
— hand movement control systems;
- alarm;
— …

1 Operating principle.

The sensor works very simply. The device sends pulses with a certain period using an IR LED. Infrared rays reflected from an object are received by the TSOP infrared receiver. There is an object - there is a signal, there is no object - there is no signal. To avoid false alarms from household remote controls, interference, or pulses when the lights are turned on, the device transmits a certain sequence of pulses and when decoding TSOP, everything that does not match this sequence is discarded. On household appliances(controlled using IR remote controls) the device has no effect, since the signal is relatively weak and modulated by a sequence that is not used anywhere.

2 Scheme, board.

Structurally, the sensor is already assembled. The scarf has proven itself well in various projects, so it was decided to make this project using it as well.
A minor design change is the installation of a variable resistor to adjust the sensitivity of the sensor. There are no more changes. The components used in the design are not critical to the ratings - values ​​close to them can be used.

3 Microcontroller firmware.

To flash the microcontroller firmware (in the board), you need to connect the programmer to the corresponding pins:

Reminder: For Algorithm Builder and UniProf, check the boxes as in the picture.
For PonyProg, AVR Studio, SinaProg, the checkboxes are checked inversely.
Fuse bytes: Low=$7A, High=$FF
Read how to program microcontrollers in

4 Design features.

One of the disadvantages of the operation of the circuit is the dependence of the sensitivity of the sensor on the general illumination. This occurs due to automatic sensitivity correction by the TSOP itself (so that extraneous illumination does not bring the receiver into the non-working area).

This effect can be reduced in several ways:

— In order for less extraneous light to fall on the receiver, you need to place it in an opaque tube (I used black heat shrink, having previously shrunk it to obtain thicker walls) and close the tube on one side with an opaque stopper (I filled it with black hot glue) and on the other, make it dark -red light filter. This design protects as much as possible from indirect light, while sensitivity does not suffer since the red filter is highly transparent for IR rays. It is advisable to place the IR LED in the tube - this will reduce the lateral reflections of infrared rays - which can give false alarms.

— Another way to solve this problem is to use illumination correction, for example, the simplest is to use a photoresistor in the sensitivity adjustment circuit (in series with a variable sensitivity resistor). With brighter lighting, the current through the photoresistor increases, which leads to an increase in sensitivity and vice versa.

Another recommendation, this time for installing the sensor. Since the principle of the sensor is based on receiving reflected radiation, when an object is close to a reflecting plane (for example, a wall in a corridor), reflections from the plane will produce an additional background that will reduce the overall sensitivity. In this case, try to place the sensor at an angle to the plane - this will direct the reflected rays to the side (for the most part).

5 Sensor operation.

After assembling the sensor, we put it into operation. To begin with, we set the sensitivity to the middle, turn on the sensor, point it in the desired direction, and use the sensitivity to set up a reliable response to the object we need.

If control from a household remote control will be used when operating the sensor, you need to go through the procedure of learning the button (command) of the remote control. The device uses only one button - resetting the trigger value. To study the button, you need to de-energize the device, “press” the TSOP output pin (in the diagram, the “Out” pin) to ground, turn on the device, release the “Out” pin and press the selected remote control button. The sensor will now start working normally.

When several sensors are turned on at a close distance from each other (for example, to control the direction of movement of an object), the sensors will interfere with each other's work, since their signals are not synchronized. To eliminate this problem, the infrared prohibition output “LED-Ban” is used. On all devices except one, this pin must be “pressed” to ground. In this case, all sensors will operate from the bottom source of infrared signal. If one emitting LED is not enough, then you can connect IR LEDs in parallel to the output of the emitting device (not forgetting about ballast resistors).

In the case of parallel operation of several sensors, all of them must be trained on the same remote control button or all of them must not be trained.

6 Conclusions.

The operation of the scheme has both advantages and disadvantages.

First off, the disadvantages:
— Dependence of device operation (sensitivity) on lighting brightness. This is being solved, to some extent, but the problem is there;
— Low resolution (small objects will “work” poorly);
— Short response range (the presence of reflective walls and ceilings reduces the range, since they do not allow increasing sensitivity - false alarms from reflections appear).

And for dessert, the advantages:
— Simplicity of design (and if you have already assembled a scarf before, you don’t need to do anything at all!);
— Absence of scarce and expensive elements;
- Does not require adjustment.

As you can see from the video, the sensor reacts quite reliably to the hand within half a meter. It works reliably from the remote control and does not interfere with the nearby TV. The current consumption is within 10mA. The sensor can be powered from sources with a voltage of 3 to 6 volts (some TSOPs cannot operate below 5 volts - this must be taken into account).

  • pbvf

    Let me describe the situation in more detail: there is a room with two entrances. When you enter from any side, the lamp should turn on (there is a motion sensor and it slows down firmly). When you leave the room, it turns off immediately.
    If an object is in a given room and someone else crosses any of the entrances, the light is still on, and will go out only if there are no people in this room... no matter how simple it is

  • GetChiper Posted by

    This sensor is not able to determine the direction of its intersection (that is, it does not know whether they entered or left).
    It is necessary either to install sensors that control the direction of intersection, or to monitor the presence of people in the passage (for example, with a PIR sensor)

  • pbvf

    it's all wrong. I’ll try to describe the algorithm of all the work, since you know how to write programs, unlike me :-). So there is a room with two or three entrances (and or exits). each input/exit is controlled by an IR block like yours, and the entire room is controlled by a PIR sensor - the light will turn on after someone is in it, and will turn off only after the PIR sensor commands that all objects have left the given one room based on a signal from any of the IR blocks. All this is processed by the microcontroller (not necessarily Tinka13, but better than AVR Thank you!

  • GetChiper Posted by

    They explained it somehow confusingly. I misunderstood. If there is a PIR sensor that monitors people in the room, why control the entrance separately? Or entering a room - this is long corridors?

  • pbvf

    all PIR sensors work for some time after the object leaves the control zone, or turn off when there is still a person inside the room. Again, the sensitivity is not very good, and one sensor cannot fully scan the room, and there is a long delay when entering the control zone. There are circuits on the counters, but this is for one input/output. ok, I have one idea here... today I’ll assemble your sensors (I made the boards yesterday), and I’ll stir things up. Thanks for participating. And if anything needs to be changed in the firmware, I hope you won’t refuse

  • Anastas

    Hello! Can I please have the source code of the program? What language was the firmware written in?

  • GetChiper Posted by

    The source is at the end of the article.
    Written at http://algrom.net/russian.html

  • Anastas
  • vitalik.dog1958

    Thank you! Probably I didn’t look through all the modifications.

  • vitalik.dog1958

    Good afternoon GetChiper! I looked through your links and there is only talk about the third contact.
    What about pins? 3 And 7 with memorization and only from the remote control no and I didn’t find it? And please do it in the same firmware for 10 seconds. off delay on the 5th leg.
    Sincerely. Thank you.

  • GetChiper Posted by
  • The sensor is designed to control electrical equipment or to work with a security system. It reacts to the approach of a person or any object in it. Depending on the sensitivity set by the trimming resistor, the operating range can be from several meters to several centimeters.

    The circuit is based on the LM567 chip, which is a tone decoder. Since the setting to the decoding frequency depends on the frequency of the built-in generator, and is actually equal to it, this frequency can be used as a pulse source for modulating infrared radiation.

    The frequency of the chip's built-in oscillator depends on the RC circuit R7-C2. In this case, pulses can be removed from pin 5 of the microcircuit. Which is what has been done here. Pulses from pin 5 A1 through circuit R4-C3 are supplied to the input of the amplifier using transistors VT1 and VT2, at the output of which (in the collector circuit VT1) the infrared LED HL1 is turned on.

    Thus, HL1 serves as an IR signal emitter, and phototransistor VT3 serves as a receiver.

    HL1 and VT3 are mutually located so that there is no direct optical connection between them. They are directed in one direction - in that direction, and between them there is an opaque partition, which can be, for example, a table top (for example, HL1 is on the table, and VT3 is under the table).

    If a person or some object appears in front of the sensor consisting of HL1 and VT3, the IR beam emitted by the HL1 LED is reflected from its surface and hits the VT3 phototransistor. Since the beam was modulated by pulses from the generator of the A1 microcircuit, photocurrent pulses of the same frequency are formed at the VT3 emitter. They, through the tuning resistor R6, which regulates the sensitivity, and the capacitor C1, are supplied to the input of the decoder of the A1 chip. Since their frequency coincides with the frequency of the generator on R7 and C2, and it cannot be otherwise, the switch at the output of microcircuit A1 opens, it goes out as a collector to its pin 8. This creates a current based on transistor VT4. It opens and the voltage on its collector rises to the supply voltage.

    The nominal supply voltage for the LM567CN chip is 5V, and the entire circuit here is powered by 12V. Therefore, the supply voltage of the microcircuit is reduced and stabilized at the 5U level by the parametric stabilizer VD2-R11.

    The domestically produced AL123A IR LED can be replaced with almost any IR LED designed for remote control systems.

    The ratings R7 and C2 may differ significantly from those indicated in the diagram. This will have virtually no effect on the operation of the sensor, because the same circuit R7-C2 works both in the reference frequency generator for the phase detector of the decoder of the A1 chip, and in the generator for modulating the IR radiation of the LED. That is, the transmission and reception frequencies coincide in any case, because they are generated by the same generator.

    All capacitors used must be designed for a maximum voltage not lower than the supply voltage.

    The sensitivity of the sensor (response range) can be adjusted in two ways. In the first case, this is a tuning resistor R6, which regulates the sensitivity of the decoder. In the second case, this is the selection of the resistance of resistor R5, which limits the current through the infrared LED. You should not choose this resistor less than 3-4 Ohms.

    Literature:

    1. “Two automatic lighting control systems.” and. Radio, 2008, No. 3, p. 37.

    Gorchuk N.V.

    In order to attract clients or customers, you can make an automated advertising stand or display window in which the lighting will turn on when a person approaches it. An attempt to use standard motion sensors for this was unsuccessful because they respond to movement, not presence.

    IR proximity sensor

    Yes, when a person approaches, the motion sensor will turn on the advertisement, but if the person stops and stands looking at the advertising stand or display window, the advertisement will turn off because there will be no movement. We need a sensor that reacts not to movement, but to the fact that a person is standing in front of it. For example, an IR reflectance sensor, the diagram of which is given here. The sensor consists of an "optical pair" from the system remote control TV, infrared LED HL1 and resonant photodetector HF1, tuned to a frequency of 36 kHz.

    The LED and photodetector are directed in one direction to the place in front of the advertising stand or display case. They must be located so that the light from the NI does not directly hit HF1, but only when reflected from an obstacle located in front of the sensor. That is, there should be an opaque partition between them.

    The multivibrator on elements D1.3 and D1.4 generates pulses with a frequency of 36 kHz (exactly this frequency is set by selecting resistance R7). These pulses arrive at the base of the switch on transistor VT3. An infrared LED NI is included in its collector circuit. Light-emitting diode
    emits flashes of IR light, repeating at a frequency of 36 kHz, and the luminous intensity of these flashes depends on the current through the LED, the value of which is set by selecting the resistance of resistor R5.

    If a person stands in front of the sensor, flashes of light emitted by the NI LED are reflected from it and fall on the HF1 photodetector. In this case, the output switch of the photodetector opens and its output (pin 3) will be logical zero. Transistor VT1 opens and charges capacitor C2. The voltage on it is a logical unit. The output D1.2 is also a logical one.

    Transistor VT2 opens and relay K1 with its contacts (not shown in the diagram) turns on the lighting of the stand or display case. When a person steps aside, the light does not turn off immediately, but after 23 seconds (time for discharge C2 through R3). This is necessary so that the light does not blink when a person moves near the advertising stand or display case. The sensitivity of the sensor (range to a person) depends on the resistance R5.

    The “Tyap-Blyap” channel presented for consideration a kit set for self-made from finished parts infrared proximity sensor. According to the channel's host, this is an irreplaceable thing in the house. The board is painted, the details are indicated. There are instructions with a diagram. Unfortunately, there is no description in Russian. The main thing is that the elements are signed.
    You can buy it in this Chinese store.

    This sensor reacts when an object approaches a certain distance. The relay will operate and turn the circuit on or off. The wizard will place the elements on the board, perform soldering and check the proximity sensor in operation. Before starting, check the resistor values. A convenient device is used for this.

    Almost the elements are inserted onto the board. All that remains is to solder the microcircuit and you can start testing. All is ready. All that remains is to wash the board.
    Device characteristics. The supply voltage is 12 volts, the load can be connected from 250 volts, 10 amperes. Everything is ready for testing. Everything is connected. A 12 volt LED light bulb will be used as a load. It is powered from a separate lead battery. The board's idle mode consumption is only 26 milliamps. When an obstacle appears, the light comes on. The time relay works for some time and can withstand the load. Then it turns off. The operating time is regulated by a trimming resistor. Let's try to unscrew it clockwise. Now the load is switched off almost simultaneously with the removal of the obstacle. Let's try, on the contrary, to increase the operating time. You can set the time much longer than shown in the test.

    Regarding the operating distance. The infrared sensor reacts to the hand when approaching at a distance of approximately 10 centimeters.
    If we take a thicker object, for example a piece of plywood. The device was triggered when approaching 16 centimeters. The question arises: what affects the distance? The volume of the object, its thickness? A piece of paper was triggered at a distance of 12 centimeters.
    The aluminum sheet reacted when approaching 30 centimeters. Let's try with a mirror. The mirror worked at 50 cm. What if you take it further away and try to move objects? The sensing distance has increased by another decimeter.

    Source: youtu.be/ASsk3xXDMuU

    Infrared sensor


    The figure above is a diagram of a simple infrared sensor that allows you to signal when something is approaching it.
    The operating range of the infrared sensor is about a meter, this distance depends on design feature infrared transceiver part of the device, which is made in the form of a HOA1405 module. This is a module with an infrared LED and a phototransistor built inside, the design of the module is shown in the figure below.

    The emitted infrared light bounces off something and hits a phototransistor, which is connected to the legendary and ubiquitous NE555 timer, which operates in monostable trigger mode. When a certain resistance of the phototransistor is reached, which depends on the intensity of the received reflected infrared signal, the trigger on the NE555 changes its state and a sound is heard from the tweeter, and the LED also lights up for two minutes. The alarm time depends on elements R4 and C2. It is permissible to use any other module as a transceiver module, or to install the LED and phototransistor separately, however, if used separately, it is necessary to provide such a design so that the phototransistor is not illuminated by the LED. The scheme is simple, easy to repeat and does not require configuration. You can even use it for compactness wall-mounted installation. Such a sensor can be used, for example, in burglar alarm, in systems for contactless activation of something, etc., it is a matter of imagination and the needs of the radio amateur.

    Share