Waterproof Ultrasonic Sensor JSN SR04T/AJ-SR04M with Arduino for distance measurement
Table of Contents
Waterproof Ultrasonic Sensor:
Waterproof Ultrasonic Sensor JSN SR04T/AJ-SR04M with Arduino for distance measurement- In today’s article, you will learn how to measure the distance using Arduino Nano, the Waterproof Ultrasonic Sensor JSN SR04T, and an I2C-supported SSD1306 Oled display module.
Don’t worry if you see AJ-SR04M printed on the module. JSN SR04T and AJ-SR04M are exactly the same the only difference is that the AJ-SR04M has an empty location for a resistor and the JSN SR04T doesn’t have it. The amazing thing about this Ultrasonic Sensor is that, it comes with a long wire due to which it can be easily installed.
Instead of using the Arduino Nano, you can also use Arduino Uno, Arduino Mega, Arduino Pro micro, or any other Arduino board. You can also use this Waterproof Ultrasonic Sensor with 3.3V compatible controller boards like Nodemcu ESP8266, ESP32, Raspberry Pi Pico, STM32, and so on. In my upcoming video and Article, I will build an IoT-based Water Level monitoring system. So, consider subscribing, if you don’t want to miss any of my upcoming projects.
As this is a getting started tutorial, so, I will try my level best to explain all the basic things like for example;
The difference between the HC-SR04 Ultrasonic Sensor and the JSN SR04T Waterproof Ultrasonic Sensor, it’s Pinout, it’s interfacing with the Arduino, it’s programming, and practical testing. So, without any further delay, let’s get started!!!
You can also ready my article on IoT based Water level monitoring and automatic water pump control system using the ESP32 WiFi + Bluetooth Module, Waterproof Ultrasonic Sensor, and the New Blynk V2.0 more…
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
HC-SR04 vs. JSN SR04T Ultrasonic Sensor:
HC-SR04 and the waterproof JSN SR04T are both popular ultrasonic sensors that are commonly used in distance measurement applications. However, there are some differences between them that may make one a better choice depending on your specific needs.
The HC-SR04 is a low-cost ultrasonic sensor that can measure distances from 2cm to 4 meters with an accuracy of about 3mm. It has a working voltage range of 5V DC and consumes very low power. The HC-SR04 is easy to use, it can be easily interfaced with microcontrollers, and can be easily found in the market.
The JSN SR04T, on the other hand, is a more advanced ultrasonic sensor that has a longer sensing range of up to 6 meters and a higher accuracy of up to 1mm. It operates at a wider voltage range of 3-5V DC and has a built-in temperature compensation feature that helps to improve accuracy even in varying temperatures. However, the JSN SR04T is relatively more expensive and may be harder to find in the market.
In short, if you need a low-cost ultrasonic sensor with a shorter range and don’t require high accuracy, then the HC-SR04 may be a good choice. However, if you need a more advanced ultrasonic sensor with a longer range and higher accuracy, and you are willing to spend more, then the JSN SR04T could be a better option. For more technical specifications, features, and applications; read my article available on www.electroniclinic.com. Now, let’s go ahead and take a look at its Pinout.
Features:
- Small size and easy to use
- Wide power supply range and low power consumption
- High measurement accuracy and high resolution
- Small detection blind area, longer distance
- Diversified output modes, pulse width output, serial port output, switch output.
Applications:
Horizontal distance
Obstacle avoidance, automatic control
Traffic control
Security, industrial control
Artificial intelligence and research.
Monitoring of objects and their movement
JSN-SR04T Waterproof Ultrasonic Distance Sensor Module Specifications:
- Operating voltage : DC 3.3V to 5.5V (recommended 5V)
- Working current is less than 8mA
- Acoustic emission frequency : 40khz
- Farthest range : 6m
- Blind : 25cm
- Trig (control side) RX
- Echo (the receiver) TX
- GND(negative)
- Resolution : about 0.5cm
- Angle : less than 50 degrees
- Working temperature : -10 ~ 70 ℃
- Storage temperature : -20 ~ 80 ℃
JSN-SR04T Waterproof Ultrasonic Distance Sensor Module Pin Description:
S.No | Pin Name | Pin Description |
1 | 5V | Positive supply pin of the sensor |
2 | Trig | Input pin of sensor. This pin has to be kept high for 10us to initialize measurement by sending ultrasonic waves. |
3 | Echo | Output pin sensor. This pin goes high for a period that will be equal to the time taken for the ultrasonic wave to return to the sensor. |
4 | Gnd | This pin is connected to the Ground of the system. |
JSN SR04T Ultrasonic Sensor interfacing with Arduino:
Anyway, as usual, I am using my designed Arduino Nano development board, because this board already has an Oled display module, and a 5V regulated power supply. So this way, I don’t need to use extra jumper wires. And moreover, I can power up this project using a 12V adaptor, a 12V battery, or even a solar panel. The onboard 5V 7805 regulator will take care of the voltage.
Let me also tell you that it doesn’t matter If you don’t have a development board, or a 5V regulated power supply. You can still make this project. You can use your Laptop as the voltage source to power up the Arduino and all the other electronics. And once you learn how to use this Waterproof Ultrasonic sensor then you guys must build yourself a 5V regulated power supply.
I have a very detailed article on how to make a 5V and 3A power supply. You can also use the 7805 voltage regulator but keep in mind it has such a low output current from 500mA to maximum 1A. So, if you use 7805 voltage regulator, still you will need an external power supply in most of the projects if you are planning on using the GSM module, or some small dc motors etc. I have also designed a new Arduino Nano development board in which I am using a 5V and 3A power supply. So, if you are performing experiments using different microcontroller boards then I highly recommend you should make this 5V and 3A power supply. Because with 3A you can power up most of the sensors and breakout boards.
Connect the VCC and GND pins of the Waterproof Ultrasonic sensor JSN SR04T interface circuit to the Arduino 5V and GND pins. Connect the Trigger and Echo Pins to the Arduino Nano Pins 2 and 3 respectively.
The VCC and GND pins of the I2C supported SSD1306 Oled display module are connected to the Arduino 3.3V and GND pins. You can also connect the VCC pin to the Arduino 5V. The SCL and SDA pins of the Oled display module are connected to the Arduino Analog pins A5 and A4. A5 is the SCL and A4 is the SDA. Arduino Uno also has the same I2C pins.
Now, let’s go ahead and install the required libraries for the SSD1306 Oled display module.
Altium Designer + Altium 365 + Octopart:
Altium 365 lets you hold the fastest design reviews ever. Share your designs from anywhere and with anyone with a single click. it’s easy, leave a comment tagging your teammate and they’ll instantly receive an email with a link to the design. Anyone you invite can open the design using a web browser. Using the browser interface, you’re able to comment, markup, cross probe, inspect, and more. Comments are attached directly to the project, making them viewable within Altium designer as well as through the browser interface. Design, share, and manufacture, all in the same space with nothing extra to install or configure. Connect to the platform directly from Altium Designer without changing how you already design electronics. Altium 365 requires no additional licenses and comes included with your subscription plan.
Get real-time component insights as you design with Octopart built into Altium 365. Octopart is the fastest search engine for electronic parts and gives you the most up-to-date part data like specs, datasheets, cad models, and how much the part costs at different amounts etc. Right in the design environment so you can focus on your designs. Start with Altium Designer and Activate Altium 365. Search for electronic parts on Octopart.
Required Libraries for SSD1306 Oled Display:
Make sure you have the latest version of the Arduino IDE installed on your computer or laptop. Anyway, we are going to install two libraries for the SSD1306 Oled display module.
The Adafruit_GFX and the Adafruit_SSD1306. For this, go to the Sketch Menu, then to Include Library, and click on the Manage libraries.
Search for the Adafruit_GFX and install it.
As you can see I have already installed this library. Now search for the Adafruit_SSD1306 and install it.
As you can see I have also installed this library. Now, let’s go ahead and take a look at the programming.
JSN SR04T Ultrasonic Sensor Arduino Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// Define Trig and Echo pin: #define trigPin 2 #define echoPin 3 #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> // Define variables: long duration; int distance; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { // Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); delay(2000); display.clearDisplay(); display.setTextColor(WHITE); // Begin Serial communication at a baudrate of 9600: Serial.begin(9600); } void loop() { // Clear the trigPin by setting it LOW: digitalWrite(trigPin, LOW); delayMicroseconds(5); // Trigger the sensor by setting the trigPin high for 10 microseconds: digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds: duration = pulseIn(echoPin, HIGH); // Calculate the distance: // distance = duration*0.034/2; distance = (duration/2) / 29.1; // Print the distance on the Serial Monitor (Ctrl+Shift+M): Serial.print("Distance = "); Serial.print(distance); Serial.println(" cm"); display.clearDisplay(); // display R G B Values display.setTextSize(3); display.setCursor(10,20); display.print(distance); display.setTextSize(3); display.setCursor(70, 20); display.print("CM"); display.display(); } |
This is an Arduino sketch for measuring the distance using an ultrasonic sensor and displaying it on an OLED display module. The sensor is connected to the Arduino board using two pins: trigPin and echoPin. The sketch uses the pulseIn() function to measure the duration of the pulse sent by the ultrasonic sensor and calculates the distance based on the speed of sound. The distance is then displayed on the OLED screen using the Adafruit_SSD1306 library.
The setup function sets the mode of the trigPin and echoPin as output and input respectively. It also initializes the OLED screen and starts serial communication at a baud rate of 9600.
The loop function is the main function that is executed repeatedly. It first clears the trigPin and then sends a trigger signal to the ultrasonic sensor by setting the trigPin high for 10 microseconds. It then reads the duration of the pulse using the pulseIn() function and calculates the distance using the formula distance = duration/2 / 29.1, where 29.1 is the speed of sound in cm/us. The distance is then printed on the serial monitor and displayed on the OLED screen using the Adafruit_SSD1306 library. The OLED display shows the distance in centimeters.
Anyway, I have already uploaded this program and now let’s watch the JSN SR04T Waterproof Ultrasonic Sensor and Arduino in Action.
Waterproof Ultrasonic Sensor Practical Demonstration:
I successfully tested it by measuring the distance. For the practical demonstration watch the video tutorial given below.
Watch Video Tutorial:
ok, it is good article
it can help me
Can you give also an example for working with the JSN-SR04T v3.0 (manipulated to work under Mode1), and connected in 3-wire-mode (common use of echo and trigger pin)?
Hi there my name is Hoss I have a river boat with a 40hp motor on it I run it in shallow waters and frequently hit rocks or the river bottom wondering if you could help me with some advice and or design a device that would alert me and eventually I could tie it into my power trim to raise it before I hit my prop, I hope you can help out thank you
My code is now building correctly, however the output is not showing perfectly regardless of the distance at which my sensor output is displayed. This is the issue I’m facing.How can I figure it out?