Waterproof ultrasonic module JSN SR-04T to measure distance with Arduino
Hello, this tutorial is about measuring distance in “cm” and “inches”, using an ultrasonic module, it’s called JSN SR-04 T, similar to the famous HC-SR04, but this one has a waterproof probe, and we will wire it with Arduino Uno board and LCD i2c screen.

This module is handy if you want to use it to measure a distance outdoor or in a not “electronic friendly” environment as the probe and its wire make it possible to let the Arduino in a safe place. Also if you notice the probe is pretty much the same used in car parking sensors.
Here are few information about the module from its datasheet:

For this project we gonna need: Arduino UNO board, LCD i²c screen and the JSN SR-04T, it is possible to use any display you want, just get the right library and code.
Wiring:

That’s pretty much the wiring both modules are wired with GND and 5V, Trigger and Echo pins are with D11 and D12, then the LCD SDA and SCL are with A4 and A5, in the video you’ll see that the Vcc of the LCD is wired with A0, it’s because I didn’t want to use a breadboard so I put A0 as a HIGH OUTPUT which provides 5V.
Libraries:
For most ultrasonic module we usually use the NewPing library but as you can see it doesn’t work well with the module but you can try it: Download here or Download here.
LCD i²c NewLiquidCrystal library: Download here.
Codes:
N.B: In the video tutorial I wired the A0 with LCD Vcc but in the codes below I removed those two lines, you can add them back if you want.
here are the 4 codes I’ve worked with, you’ll find measuring in both Metric and Imperial with both Serial monitor and LCD: Download here, otherwise check below
Code 1: Library example – NewPingExample
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// --------------------------------------------------------------------------- // Example NewPing library sketch that does a ping about 20 times per second. // --------------------------------------------------------------------------- #include <NewPing.h> #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. } void loop() { delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. Serial.print("Ping: "); Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range) Serial.println("cm"); } |
Code 2: Displays in “cm” on the Serial monitor
|
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 |
/* This code works with JSN SR04 T ultrasound module * It measures the distance and shows it on the Serial monitor * Refer to http://www.SurtrTech. com or SurtrTech YT channel for more information */ #define TRIG 11 //Module pins #define ECHO 12 void setup() { Serial.begin(9600); // Serial monitoring pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input pinMode(ECHO, INPUT_PULLUP); } void loop() { digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS delayMicroseconds(2); digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging delayMicroseconds(20); digitalWrite(TRIG, LOW); // Send pin low again int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse distance= distance/58; //Convert the pulse duration to distance //You can add other math functions to calibrate it well Serial.print("Distance "); Serial.print(distance); Serial.println("cm"); delay(50); } |
Code 3: Displays in “inches” on the Serial monitor
|
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 |
/* This code works with JSN SR04 T ultrasound module * It measures the distance and shows it on the Serial monitor * Refer to http://www.SurtrTech. com or SurtrTech YT channel for more information */ #define TRIG 11 //Module pins #define ECHO 12 void setup() { Serial.begin(9600); // Serial monitoring pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input pinMode(ECHO, INPUT_PULLUP); } void loop() { digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS delayMicroseconds(2); digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging delayMicroseconds(20); digitalWrite(TRIG, LOW); // Send pin low again float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse distance= distance/58; //Convert the pulse duration to distance //You can add other math functions to calibrate it well distance= distance*0.3937; //Convert from cm to inches Serial.print("Distance "); Serial.print(distance,2); Serial.println(" Inches"); delay(50); } |
Code 4: Displays in “cm” on the LCD i2c
|
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 |
/* This code works with JSN SR04 T ultrasound module and LCD i²c screen * It measures the distance and shows it on the LCD screen in cm * Refer to http://www.SurtrTech. com or SurtrTech YT channel for more information */ #include <LiquidCrystal_I2C.h> //Lcd library #define I2C_ADDR 0x27 //or(0x3F) I2C address, you should use the code to scan the address first (0x27) here #define BACKLIGHT_PIN 3 // Declaring LCD Pins #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 #define TRIG 11 //Module pins #define ECHO 12 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); void setup() { pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input pinMode(ECHO, INPUT_PULLUP); lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //Lighting backlight lcd.home (); } void loop() { digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS delayMicroseconds(2); digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging delayMicroseconds(20); digitalWrite(TRIG, LOW); // Send pin low again float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse distance = distance/58;//Convert the pulse duration to distance //You can add other math functions to calibrate it well lcd.clear(); lcd.print("Distance:"); lcd.setCursor(0,1); lcd.print(distance,1); lcd.setCursor(6,1); lcd.print("cm"); delay(500); } |
Code 5: Displays in “inches” on the LCD i2c
|
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 |
/* This code works with JSN SR04 T ultrasound module and LCD i²c screen * It measures the distance and shows it on the LCD screen in Inches * Refer to http://www.SurtrTech. com or SurtrTech YT channel for more informations */ #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here #define BACKLIGHT_PIN 3 // Declaring LCD Pins #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 #define TRIG 11 //Module pins #define ECHO 12 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); void setup() { pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input pinMode(ECHO, INPUT_PULLUP); lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //Lighting backlight lcd.home (); } void loop() { digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS delayMicroseconds(2); digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging delayMicroseconds(20); digitalWrite(TRIG, LOW); // Send pin low again float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse distance = distance/58;//Convert the pulse duration to distance //You can add other math functions to calibrate it well distance= distance*0.3937; //Convert from cm to inches lcd.clear(); lcd.print("Distance:"); lcd.setCursor(0,1); lcd.print(distance,2); lcd.setCursor(6,1); lcd.print("In"); delay(500); } |
Push further with code 6:
If you want to try to measure underwater here’s a code with a few modification, check to video for more information: Download the code here.
|
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 |
#include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here #define BACKLIGHT_PIN 3 // Declaring LCD Pins #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 #define TRIG 11 #define ECHO 12 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); void setup() { pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input pinMode(ECHO, INPUT_PULLUP); pinMode(A0,OUTPUT); digitalWrite(A0,HIGH); lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //Lighting backlight lcd.home (); } void loop() { digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS delayMicroseconds(2); digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging delayMicroseconds(20); digitalWrite(TRIG, LOW); // Send pin low again float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse distance = distance/13.3511; //13.3511 instead of 58 because the speed of a soundwave in water is far bigger than in air lcd.clear(); lcd.print("Distance:"); lcd.setCursor(0,1); lcd.print(distance,1); lcd.setCursor(6,1); lcd.print("cm"); delay(500); } |
Tests:
The module works pretty fine, but for close up measures it won’t be good and it’s better to use the HC SR04T, but this one has a waterproof, so it depends on your project.



Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.

3 thoughts on “Waterproof ultrasonic module JSN SR-04T to measure distance with Arduino” Leave a comment ›