Sharp 2Y0A21 Distance Sensor

The objective of this project is simply to test the Sharp 2Y0A21 infra-red distance sensor, to see if it can be used to better detect occupancy in my Smart Shower.

In this application, I need a ceiling mounted sensor with a very narrow field of view. It basically needs to be able to accurately detect if a person is standing underneath it. Because the shower head is wall mounted, the water exiting from it may also be picked up by the sensor (it isn't by the ultrasonic sensor) but, this is not a problem.

I can't use a PIR sensor because they have too wide a field of view and would pick up people walking past the shower.

I'm currently using the JSN-SR04T ultrasonic distance sensor but it isn't as reliable as I'd like for detecting people standing underneath it. People don't reflect ultrasonic sound pulses very well and the hair on your head is quite absorbant.

Whilst this sensor is waterproof and has a longer range, that's not much use for this application if it can't detect people reliably.

The Sharp 2Y0A21 has a shorter range (up to 80cm claimed) but this is enough in my smart shower application. Mounted on the ceiling, this would detect people over 1.4m tall.

Design & Build

I created this 3D design in Autodesk Fusion 360, to 3D print. It is 37mm in diameter and if I cut the mounting lugs off the 2Y0A21, then it will fit inside this enclosure and the transmitter and receiver will protrude through the face.

I plan to 3D print a second part to essentially cover the back of this part and allow it to be mounted into the existing 18mm hole in the ceiling. It will sit a bit further off the ceiling than I would like but, if it works more reliably then this is a price worth paying.

The Sharp 2Y0A21 sensor has a transmitter and a receiver 20mm apart. Whilst they look like an 5mm LED and phototransistor, these are actually just lenses.

To enable my enclosure to be more compact, I'm going to cut off the two mounting lugs. This then results in a sensor that will fit inside a 31mm diameter circle.

If you look at the back of the sensor, there is a small screw to detect the circuit board. My thinking was that I could use this in a brand new housing but as you can see from the next picture, that is not really an option.

The actual sensors mount to the PCB but use two lenses in the case. This means that I'm really going to have to reuse the lenses and case the PCB is mounted in.

I then 3D printed my design to test it fits. Once I'm happy with it, I will smooth it and paint it white to make it moisture resistant.

The sensor fits inside the enclosure perfectly. I could resin encapsulate it too, to make it more waterproof.

I can now start testing the sensor in situ.

Arduino Shield

As part of my smart home building blocks approach to the smart home, I have developed my own Smartisant Arduino Shield SHLD1, to enable me to prototype and build new smart home services and features quickly and reliably. This is described in a lot more detail here.

This exposes 6 analogue inputs via 4-way Molex connector and includes 5V dc power. The 2Y0A21 sensor has an analogue output, so it will just plug in but it is recommended that a capacitor (10µF or more) is mounted between Vcc and GND.

Testing

The output from the sensor varies based on distance and the range is a lot better than I expected, at more than 3m. Typical values coming back from the analogRead() function with a white sheet of cardboard in front of it at various distances are:

With nothing in front of the sensor, the readings are about 330 to 370. This means anything over 135 and less than 320 means there is a person detected. More importantly, if I use the top of my head as the reflector it still works :-)

The main challenge now is going to be stopping the shower tray being detected as a person but it would be easy to limit the range of accepted values (e.g. 135 to 220) and hence the detection range.

Once installed, I did some more testing to see what kind of values were coming back from the sensor. Installed above my shower, the sensor could then be calibrated to accurately detect occupancy. The in-situ testing showed it to be much more relaible and sensitive to detecting people :-) This is a much better solution.

Installation

Before I can install this sensor, I need to design the lid/mount part in Autodesk Fusion 360 and 3D print it. This is a back cover with a 21.5mm diameter mounting tube. The tube diameter could be anything I want really but I have designed this part to fit through the existing ceiling hole.

This is the 3D printed mount in place. My next job is to smooth and spray paint the sensor enclosure.

This is the spray painted sensor.

Summary

The Sharp 2Y0A21 distance sensor works really well in situations where you want an occupancy. If you need to detect if person is below or in front of the sensor, then it seems well suited to this task. In addition it can also provide an indication of how far away they are if required. The only downside that I can see is that it is not a pretty sensor and is also not a nice form factor. Enclosing it in a bespoke 3D printed enclosure helps but it is still fairly bulky. If I was prepared to cut a larger hole in the ceiling, then it could be flush mounted.

With a little bit more work, the enclosure design could easily be modified to house a temperature sensor and humidity sensor as well.

Note:  I am using this sensor in my smart shower project and it is working incerdibly reliably.