Project tutorial

# Measure Distance Using JSN SR-04T + LCD © CC BY

Measure distance using JSN SR-04T waterproof ultrasound module and an LCD I2C (cm/inches).

• 2,242 views
• 1 comment
• 7 respects

## Components and supplies

 Arduino UNO & Genuino UNO
×1
 Adafruit Standard LCD - 16x2 White on Blue
×1
 JSN SR-04T
×1

### Intro:

Hi, in this tutorial I'm using JSN SR-04T waterproof ultrasound module to measure distance and display it on LCD I2C (in metric & imperial units), and I'm using the 2.0v which is as I read on some places "is unstable, and better to use v1.0"... Well if you want it to work correctly, don't use the "NewPing" library.

This module is waterproof, it's good for outdoor or for a parking/reverse sensor project. As I tested it doesn't measure distance underwater, if I did it wrong correct me.

### Tests:

This module doesn't measure under 20-22cm distance, the one I used measured up to 4.46m as maximum, the module worked after being put in water some time.

### Underwater Test:

I don't think this module was mean't to measure underwater but since the probe was waterproof I tought I should give it a try, and as you know the ultrasound modules sends soundwaves and they reflect back, in the codes that don't use the "NewPing" library you'll a line where you divide the duration by 58 to get the distance.

"58 represents an assumption that the speed of sound is

1000000 / 58 * 2 / 100 = 344.8 metres per second"

and since the speed of sound in water is around 1498 m/s, I did some quick math to get the 13 instead of 58, and uploaded the code but in water the distance value is stuck on 93-94cm.

## Code

##### Code_cm_Serial.inoArduino
Display distance on the Serial monitor in cm
/* This code works with JSN SR04 T ultrasound module
* It measures the distance and shows it on the Serial monitor
* Refer to www.SurtrTech. com or SurtrTech YT channel for more informations
*/

#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_In_Serial.inoArduino
Display distance in serial monitor in inches
/* This code works with JSN SR04 T ultrasound module
* It measures the distance and shows it on the Serial monitor
* Refer to www.SurtrTech. com or SurtrTech YT channel for more informations
*/

#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_LCD_cm.inoArduino
Display distance in LCD i²c in cm
/* This code works with JSN SR04 T ultrasound module and LCD ic screen
* It measures the distance and shows it on the LCD screen in cm
* Refer to www.SurtrTech. com or SurtrTech YT channel for more informations
*/

#include <LiquidCrystal_I2C.h> //Lcd library

#define I2C_ADDR 0x27 //or(0x3F) 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

void setup() {
pinMode(A0,OUTPUT);
digitalWrite(A0,HIGH);
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_LCD_In.inoArduino
Display distance in LCD i²c in inches
/* This code works with JSN SR04 T ultrasound module and LCD ic screen
* It measures the distance and shows it on the LCD screen in Inches
* Refer to 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

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);

}
##### Code_LCD_cm_Underwater.inoArduino
Code that was mean't to measure underwater if you want to give it a try or correct it
#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

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);

}

## Schematics

• 18 projects
• 74 followers

January 30, 2019

#### Members who respect this project

and 4 others

See similar projects
you might like

by remnis

• 25,307 views
• 16 respects

#### Simple Ultrasonic Distance Measurer With LCD Display

Project showcase by onatto22

• 6,123 views
• 22 respects

#### Measure Distance With Sonic Ranger And Record It On MicroSD

Project tutorial by Boian Mitov

• 2,531 views
• 20 respects

#### Making LCD Thermometer With Arduino And LM35/36

Project tutorial by Konstantin Dimitrov

• 69,653 views
• 65 respects

#### Soil Moisture Sensor With LCD Display

Project tutorial by Patel Darshil

• 23,794 views