Waterproof Ultrasonic Distance Sensors - JSN-SR04T & A02YYUW ๐งโ

Science Central
21 followers
3 Video ViewsยทOct 31, 2022
Waterproof Ultrasonic Distance Sensors - JSN-SR04T & A02YYUW ๐งโ
Today in the workshop we're working with waterproof ultrasonic distance sensors.
We'll see how these devices work and how to use them with an Arduino.
We'll also test them outside and even underwater.
We're going the distance today, so welcome to the workshop.
Well hello and welcome to the workshop and today we are going to be working
with a couple of ultrasonic distance sensors.
Now of course we've used an ultrasonic distance sensor before in our robotics
and other projects and we've used it many times.
And that's our good friend the HC-SR04.
The HC-SR04 is a great little sensor.
It's relatively accurate.
It can sense over a reasonable distance.
It's very inexpensive and it's pretty easy to use.
However I have an application right now in which it is not suitable.
Specifically a robot I'm building that is meant to operate outdoors.
And the HC-SR04 is not exactly a great sensor to use outdoors.
There are a number of reasons for that.
But one of the principal reasons is that you can't get it wet.
So I'm evaluating a couple of ultrasonic sensors that you can use in a damp and outdoor environment.
And I want to show them to you both today.
So let's first of all take a quick look at how these sensors work.
And then I'll show you the sensors that we're going to be using instead of the HC-SR04.
Ultrasonic sensors use 40 kilohertz sound pulses to measure distance.
These devices consist of a transmitter and receiver.
Although those can be combined into one transducer.
The transmitter sends out pulses of ultrasonic sound.
If a surface reflects this signal,
we can use the time delay between the transmission and the reception to calculate the distance.
If the distance is shorter the time delay will be shorter.
Conversely if it's longer the time delay will be longer.
The HC-SR04 is a very popular ultrasonic distance sensor.
It works on 5 volts only.
However you can use an RCWL1601 or a US100 if you want a 3.3 volt equivalent.
This sensor can run in a 2-wire or 1-wire mode.
It's reasonably accurate and it's very inexpensive.
But the HC-SR04 isn't perfect.
It's not suitable for outdoor use.
It's quite fragile and susceptible to dirt.
And most importantly it is not water resistant.
However there are ultrasonic distance sensors that are waterproof.
And we'll be looking at two of them today.
The JSN-SR04T and the A02YYUW.
Let's take a quick look at how these sensors performance compares to the HC-SR04.
The A02YYUW and the HC-SR04 both have about the same range.
The JSN-SR04T however has a longer maximum range but also a longer minimum range.
The accuracy of all of these sensors is quite good.
Certainly suitable for robotics and intruder detection applications.
None of them however would be good enough for surveying.
The JSN-SR04T and the A02YYUW can work with both 3.3 and 5 volt logic.
The HC-SR04 however is just a 5 volt device.
Both the JSN-SR04T and the HC-SR04 support raw data.
In other words they deliver back the length of the received pulse
and leave it to the microcontroller attached to calculate the distance.
The two sensors we are looking at today support serial data.
With serial data the distance is calculated by the sensor
and is just sent as a serial data packet.
In terms of cost however both of these sensors are much more expensive than the HC-SR04.
However they're not extremely expensive
and if you need a sensor that is rugged and capable of working outdoors
they're probably good choices.
So let's go take a closer look at these sensors now.
Now I've got the sensors that we're going to be using in our experiments today over here.
I've also got an HC-SR04 sensor just so you can sort of compare everything.
And the one over here is the A02YYUW sensor.
And as you can see it looks a lot like the HC-SR04
in that it's got a separate transmit and receive transducer
but it is substantially larger than that unit.
Now over here in the middle these two pieces are actually one sensor.
This is the JSN-SR04T and it comes as a separate sensor
that plugs into a board over here.
And this board is what you wire everything up to of course at the electronics.
And as you can see the board is similar in size to the HC-SR04.
A little bit taller than it and of course the sensor is completely different.
And it has a fairly long cable I think that's a couple of meters long
so you can mount it away and keep your electronics nice and safe and dry.
And so these two are the units that we are going to be using today in our experiments.
So let's get going.
Now the first sensor that we're going to check out is a JSN-SR04T sensor.
This is the one that had a separate circuit board and sensor unit.
And another way that this one differs from the other ultrasonic sensors we've seen
is that the sensor unit just uses one transducer.
It's using it as both the transmitter and the receiver.
And I believe that this is the reason why the minimum range of this sensor
is down around 20 centimeters.
It just simply can't flip itself back and forth in time
in order to resolve anything shorter than that.
But other than that this sensor does seem to get a bit of a bad reputation on the internet.
And I don't think it's very deserved quite frankly.
There's even a YouTube video out there that specifically tells you not to buy it.
And I saw that video after I bought a couple and thought
did I make a big mistake?
But what I've noticed about these videos is that they all use this sensor in an emulation mode
where it emulates an HC-SR04 sensor.
And there are two aspects about that.
First of all, I have found by experimenting that if you change the timing on it slightly
it will work fine.
And it's actually within specs of the HC-SR04.
Another thing I've noticed is that everyone who tests it tested in this emulation mode.
Whereas this sensor actually has six operating modes
including modes in which it calculates the distance all on its own.
So I thought I'd give it a proper test by testing it in a couple of different modes.
So let's go and take a look at this sensor right now
and then I'll show you some code that we can use to test it in a couple of different modes
and see how it works.
The JSN-SR04T is a separate sensor and printed circuit board.
The sensor is attached to a two meter cable which attaches to the board with a two pin connector.
The sensor itself is a single transducer design.
This transducer acts as both the transmitter and receiver.
The JSN-SR04T has six different modes of operation.
In mode 0 which is the default mode this sensor will emulate an HC-SR04.
It has the same pinout and can be used in the same circuits as the HC-SR04.
Mode 1 is activated by shorting the two M1 pads.
In mode 1 the sensor calculates the distance itself
and sends it back as serial data and it sends this data constantly.
Mode 2 is activated by shorting the M2 pads.
In this mode you need to request the serial data by sending it the hexadecimal character 55.
It will then return one block of data.
In this way you can just request a distance measurement
rather than having it continually sent to you.
Modes 3 through 5 are activated by placing a resistor across the mode pins.
The value of the resistor will determine what mode you are in.
In mode 3 which uses a 200k resistor the sensor is in automatic trigger mode.
This is like an HC-SR04 without a trigger pin.
The trigger is fired every 200 milliseconds and you can read the results
and then calculate the distance yourself.
Mode 4 which is activated with a 360k resistor across the mode pins
is the low power automatic trigger mode.
It's the same as mode 3 except the sensor consumes very little power
consuming only 70 micro amps on idle.
It does this by powering down certain sections like its internal watchdog timer.
In mode 5 which is activated by placing a 470k resistor across the mode pads
it doesn't act as a sensor but acts as a 1.5 meter switch.
It will stay high as long as the distance is less than 1.5 meters otherwise it will go low.
This is used for things like automatic backup detector alarms.
The serial data from the JSN-SR04T is sent at 9600 baud and consists of four bytes.
The first byte is the header and it is always hexadecimal FF.
The next two bytes are the actual sensor data
with the high byte being first and the low byte being second.
This is the distance in millimeters that has been measured by the sensor.
The final byte is a checksum. It is the addition of the previous three bytes.
Note the checksum only holds the bottom eight bits of this calculation.
Upon receiving the data you should perform the same mathematics
and determine that the checksums match.
If they don't then the data has been scrambled.
So now that we know a little bit about it let's go and hook up our JSN-SR04T.
Now this is the JSN-04T which as we've seen before is a sensor that comes in actually two parts.
There's a sensor unit itself that's on the cable and a circuit board over here.
Now if we look at the head of the sensor itself you can see this is a completely sealed unit
and it just has a transducer on the front over here.
There's a little opening beside the transducer but I believe it's sealed behind that as well
and that's probably just to allow the sound to get in a bit better.
Now let's take a look at the board that comes with this and if we take a good look at the board.
Now this is the version 3 board and the big difference between version 3
and the previous versions is how you set the mode.
On version 3 up in the corner over here you'll see a couple of jumpers.
There's one that says M1, one above it that says M2, and a couple below that say mode.
And the M1 and M2 ones can be shorted out to put the device into mode M1 or M2 respectively.
If you want to go for some of the other modes you actually need to put a small
across this one that says mode and the value of that resistor will determine what mode you have.
If you don't have anything on it as you see right now then this is defaulting to mode 0
which means it emulates an HCSR04 and you will notice the pin outs on the bottom over here have
four pins. In fact if I flip this around so you can see the writing on that if you can see that
you'll see it says 5 volt trigger echo and ground and those are indeed the pins that you use in an
HCSR04 so you can pop this in as a direct replacement for it if you wish. Of course
the trigger and echo pins are also transmit and receive so they can be used for the other modes
as well. But otherwise it's a fairly easy unit to use it has a really long cable over here
that you can use to separate the sensor from the board and so this would be ideal for an
application you're using outdoors let's say in a vehicle or something where you have this mounted
on the bumper to detect distance and you have the circuit board somewhere else within the vehicle.
So it's a very inexpensive and as you're going to see relatively easy to use sensor.
For our first experiment we'll require an Arduino Uno and a JSN SR04T ultrasonic distance sensor
module. We'll begin with this distance sensor module in its default mode of mode 0. We start
by hooking the ground from the Arduino to the ground pin on the JSN SR04T. The 5 volt output
of our Arduino will be connected to the VCC pin on our ultrasonic sensor. Pin 11 of the Arduino
will be connected to the echo or TX pin on the JSN SR04T and pin 10 from the Arduino will be
connected to the remaining pin which is the trigger or RX pin and this completes our wiring.
Now here's a sketch that we're going to use for the mode 0 demo for the JSN SR04T
and if you've used the HCSR04 before you're going to recognize this sketch because it is
virtually identical to the sketch that I first demonstrated the HCSR04 with. In fact there's
only one real difference and it has to do with timing and I'll show you that in a moment.
Now we're going to start off the sketch by defining the connections that we made
to the sensor and then we define a couple of floats. One that represents the duration,
the amount of time it takes the signal to come back and one that we have to hold the actual
distance that we're calculating and we're going to bring our distance back in millimeters.
And then we go into our setup. We set up our serial monitor over here and we define our two
pins as inputs and outputs respectively and go into the loop. Now the loop again is very simple.
This is how we manually use an HCSR04 and it's how we're going to use this sensor in mode 0.
So we start off by setting the trigger pin low for 2 microseconds and then we set it high and
here is where I've changed the timing. In the original sketch that I used for the HCSR04,
I used 10 microsecond pulses and when you read the spec sheet on the HCSR04,
this is the minimum amount that you can use but you can use a longer pulse.
And I found that with this sensor when I used a 10 microsecond pulse,
I occasionally got intermittent results and I think this may be the result
of some people saying that this sensor doesn't work. It's simply because
I think they weren't giving it a long enough pulse. So I increased it to 20 microseconds
and everything seemed to work pretty fine. So we're going to set it high for 20 microseconds
and then bring it low and so this will have sent out our ultrasonic pulses. Now we're going to
measure the width of the incoming pulse. So we use a pulse in to measure the amount of time
that the pulse goes high coming in and then we determine the distance by that width over here.
Now we're using 343 meters per second as the speed of sound. We're also dividing by a thousand
as we want millimeters. So this is what we're using over here and of course we have to take
the duration by two because the signal is going out and being reflected back. So we only want
half of it. And then finally we print our results to the serial monitor and add a delay before we
repeat. This gives everything time to settle and then we do it all over again. So it's a very
simple sketch. Let's load it up and take a look at it in action. And so here we have our mode zero
demo and as you can see from the serial monitor it's reading out readings pretty well of 226
millimeters which of course are wrong. But remember this sensor has a minimum range
of 30 centimeters and that's about a foot that's about 300 millimeters. And so when it gets below
that it'll give a false reading and I notice with this particular one that I have that seems to be
the false reading. So if I lift the sensor up a little bit we should start getting some more
accurate readings. And this is the distance basically between me and the wall over here.
So if I get something in the way we should start seeing some readings over here. And now we're
approaching that 300 millimeters. If I go back a little bit over here it goes up. So the sensor
does indeed seem to work although of course we're not really accurately measuring anything right now.
I don't know if my 381 millimeters over here is precise although it's probably in the ballpark.
And we will test this a little bit later for accuracy but at least we know that our circuit
is working and that we are enabled to measure distance with this ultrasonic sensor albeit
distances over 30 centimeters. Here's the sketch that we're going to use for our sensor in mode
one where it sends back data in a serial fashion. Now we're going to start off by including the
software serial library because the Arduino Uno only has one serial port which is being used for
the USB connection. And then we define our connections to the sensor over here. So the
two pins that we've got it connected to. And we create an array to store the incoming serial data.
Now if you recall the data is in a format of four consecutive bytes. So we're going to set up an
array with four elements inside it. And we also need an integer to represent our distance. That's
over here. And we also have a character value to represent our checksum to make certain that our
data has integrity. We'll set up an object to represent the serial port called my serial and
attach it to two different pins and then go into the setup. In the setup all we do is set up our
serial monitor and also set up our software serial port at 9600 baud which is the rate that the
sensor gives data back at. Then we go into the loop. We check first of all to see if we actually
have data available. If there is we add a small delay. And then we check for the packet header.
And the packet header has hexadecimal FF in it. So we look at every character coming in until we see
that. And we know we've got the packet header. Once we know we have the header we'll insert that
into the first element of our array. Then the remaining three characters of data will be
inserted into the next three consecutive elements of the array which as you recall only has four
elements. Then we'll compute the checksum by adding up the data in the first three elements
and seeing the results. If that result equals what we have in the last element of the array then the
checksum is correct and our data has integrity. And then we'll get the actual distance. And the
distance is represented by the two bytes in the middle of our array. Numbers one and number two.
So we'll put those two together and print everything out to the serial monitor. And then go back up
and do it all over again. So it's actually a pretty simple sketch. Let's load it up and see
how it works with our sensor in mode one. And so we're going to repeat the same test we did on mode
zero with our mode one sensor. And once again you'll notice that in idle it is reading about 230
millimeters. Again the minimum range of this is 300 so you'll get false readings when it is at
minimum range. I'll lift it up again so that we can actually get a reading. Back to my wall and I'll put
this in front of it, move it, and it does indeed seem to be responding. Once again we aren't
verifying the accuracy of these measurements just simply the fact that it does measure something.
And we will take it out later and give it a proper accuracy demonstration. But it does seem to work
in mode one where the sensor itself is doing all the calculations and just passing the data back
to the microcontroller. Now the A02YYUW looks a little bit like the HCSR04 in that you can see
that it has both a separate transmitter and receiver unlike the last sensor that we just
looked at. And it is completely encased in rubber. It's a very nice little unit and I wanted to see
if I could perhaps get better performance than I did from the previous sensor using this sensor.
Now this sensor only operates in a serial mode. It has no emulation mode for the HCSR04 whatsoever.
And so we are going to be using it in that serial mode. Now one interesting thing about that serial
mode is that it uses the exact same data format that our previous sensor used in mode one. So we
don't even need to write any new code this time. We can just simply use the last sketch that we used
with this sensor and see how it compares. So let's take a look at the sensor and then we'll go and
see how it works. The A02YYUW is a fully waterproof ultrasonic distance sensor.
The sensor works on either 3.3 or 5 volts. It operates on a temperature range of negative 15
to 60 degrees celsius. This sensor outputs serial data at 9600 baud and the format of the data is
identical to that of the serial mode of the JSN SR04T. The sensor has four connections as follows.
Pin 1 is the VCC which is the power of either 3.3 or 5 volts. Pin 2 is the ground. Pin 3 is the
output selection. If you pull it high or just leave it alone the data will come back every 300
milliseconds. If you bring this pin low the data will come back every 100 milliseconds.
The 300 millisecond data is considered to be more stable so unless you need data at a rate
of 100 milliseconds you should just leave this pin alone. And finally pin 4 is the UART output
which contains the serial data from the sensor. So now let's go hook up the A02YYUW sensor.
Now here's just a quick look at the A02YYUW and there's not really that much to look at. I've got
the sensor over here and I've also got an adapter cable and I purchased this from DF Robot and it's
under their part number SEN0311 and they included this little adapter cable which adapts the JST
connector on the sensor to four separate wires that you can use for experimenting purposes. So
that's going to be very handy. So let's just take a quick look at the sensor itself. Again really not
much to see. It's a completely sealed unit. You can see some resemblance to the HC-SR04 in that
it's got a separate transmit and receive transducer. Sealed away with two mounting holes on each end
over here. If I flip it over to the back over here you'll see it's also completely sealed. It's got
kind of a rubberized feel to it and it's got a bit of weight to it. It feels like a very solid product
and so I'd feel quite comfortable mounting this outdoors using it on my outdoor robot etc.
So let's go and hook it up and see what it can do. The wiring for our next sensor is just as
simple as the last one was. Again we'll require an Arduino Uno and an A02YYUW ultrasonic sensor unit.
We'll begin by connecting the five volts from the Arduino to the VCC of the ultrasonic sensor.
This is the red wire. The Arduino's ground will be connected to the sensor's ground which is the
black wire. We'll connect pin 11 of our Arduino to the receive or blue wire on the sensor and the
sensor's remaining green wire which is the transmit lead will be connected to Arduino pin 10 and this
completes our wiring. Now here I have my A02YYUW sensor and once again I'm going to try to measure
distances with it. Now you can come closer than 30 millimeters with this one so I can come in
a little bit more 30 centimeters excuse me. I can actually come in quite close right now
and here I am down to about 50 millimeters and of course I can move it back and get a reading as we
would expect. And so this does seem to work just fine using the code that we used for the other
sensor and once again we'll have to actually take it out and give it a proper test to see
how accurate it is. So we've seen how our sensors work but we haven't really given them a proper
test. Here on my workbench I just simply don't have the space to check out their range and our
first sensor wouldn't even work at anything underneath 22 centimeters. And so what we need
to do is bring these things outside and that's exactly what I'm going to do right now. I'm going
to take both sensors outside give them a test and see how they work and I'll also even test out
their waterproof capabilities. So let's go ahead and take a look at that. So I took the experiment
outside into my tiny backyard along with a computer and I set everything up so that I could
read the reflection off of my wall. I measured everything as best that I could but of course the
accuracy of these measurements is going to be a little bit off just because of my measurement
process. I then went and took measurements for both sensors at a number of different distances.
I also tried wetting the sensors and was pleasantly surprised to see that it really had no effect on
the reading. By the way those of you who saw my video on comparing IR sensors and ultrasonic may
notice that board in the background. Unfortunately it was unusable for these tests. Here are the
results that I got and as you can see both sensors were pretty accurate. 2460 millimeters was the
furthest that I could measure in my tiny yard and as you can see at the one meter mark they're both
very close on. I would say that either of these sensors would work properly in your application
although if you need distances under 300 millimeters you're probably best going with
the A02YYUW as the JSNSR04T will not measure low distances accurately. Also another thing to note
was that the ambient temperature was rather cool. It was only 14 degrees celsius and of course that
will affect the speed of sound. I made no effort to compensate. Again wetting the sensors didn't
change the readings so I don't have a separate set of readings for that but I was pleasantly
surprised to see that because this means that these sensors will indeed work in an outdoor
environment. Now I do most of the filming for the DroneBot Workshop channel here in my workshop but
as you saw from the last scene I've also taken it occasionally outside. We've occasionally run a
robot on the floor outside the room but there's a lot of my house you haven't seen yet and there
was one area of my house that I never thought I would be showing you yet I'm about to show it to
you right now and that is my bathroom and the reason for that is I want to give these sensors
a test and see if they will actually work underwater and as I don't own a swimming pool
the largest body of water that I have in my home is my bathtub. Now I am not expecting accurate
results I'm not even sure if I'm expecting any results from these because of course the speed
of sound in water is different than the speed of sound in air but I would like to see if they are
actually waterproof and if they will work underwater so let's go and take a look and see
if they will. So I took the experiment up to the bathtub and very carefully immersed the sensors
underwater. I had to be particularly careful with the A02YYUW because it has a very short cord
and I didn't want to drag my Arduino into the tub. The results however were very disappointing. I got
basically the same reading from both of the sensors at least each sensor gave a consistent
reading it wasn't the same reading between the two of them and so they were completely useless
inside the tub. However one thing I did note is when I pulled them out of the bathtub they
continued to work just fine so immersing them completely in water didn't affect them a bit
they worked properly when they were out of the water so they're indeed waterproof
but you can't use them in a bathtub. Well that wraps up our look at outdoor ultrasonic sensors
and I hope that you found it useful. I know that I found it useful because I think I can determine
which sensor I'm going to use for my outdoor robot project. A project that I will be showing you very
soon by the way here on the DroneBot Workshop YouTube channel. So if you're not a channel
subscriber please do that. All you need to do is click on the little subscribe button
and if you also click on the bell notification you will get notified every time that I make a
new video and that of course is assuming that you've got notifications enabled on your YouTube
account. Now if you need the code that I used today or if you want a bit more information about
the two sensors we looked at head over to the DroneBotWorkshop.com website where you will find
an article that accompanies this video and there's a link to that article right below the video and
if you'd like to discuss this well the best place of course is the DroneBot Workshop forums. We've
got a lot of great people on the forum who can help you out with your projects or you could
perhaps help them out as well. It's free to join so head over to the forum where you will find a
thread that discusses this video specifically. And finally if you want more information about
what's going on in the workshop or more info about that outdoor robot please consider subscribing to
my newsletter. Again it's absolutely free it's not a sales letter just my way of occasionally keeping
in touch with you to let you know what's going on here in the workshop. So until we meet the next
time please take good care of yourself, please stay safe out there, and we will see you again
very soon here in the DroneBot Workshop. Goodbye for now.
