SurtrTech
Published © CC BY

Measure Distance Using JSN SR-04T + LCD

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

BeginnerFull instructions provided4,487
Measure Distance Using JSN SR-04T + LCD

Things used in this project

Story

Read more

Schematics

Wiring

Code

Code_cm_Serial.ino

Arduino
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.ino

Arduino
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.ino

Arduino
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 

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

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.ino

Arduino
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 

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

Code_LCD_cm_Underwater.ino

Arduino
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 

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

Credits

SurtrTech

SurtrTech

9 projects • 123 followers
YT Channel bit.ly/35Ai76l, run by Automation and Electrical Engineer, Electronics amateur, no IT background so you may see wreckage in codes

Comments