Components and supplies
Arduino UNO
Standard LCD - 16x2 White on Blue
JSN SR-04T
Project description
Code
Code_In_Serial.ino
arduino
Display distance in serial monitor in inches
1/* This code works with JSN SR04 T ultrasound module 2 * It measures the distance and shows it on the Serial monitor 3 * Refer to www.SurtrTech. com or SurtrTech YT channel for more informations 4 */ 5 6#define TRIG 11 //Module pins 7#define ECHO 12 8 9void setup() { 10 11 Serial.begin(9600); // Serial monitoring 12 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 13 pinMode(ECHO, INPUT_PULLUP); 14 } 15 16 void loop() { 17 18 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 19 delayMicroseconds(2); 20 21 digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 22 delayMicroseconds(20); 23 24 digitalWrite(TRIG, LOW); // Send pin low again 25 float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 26 27 distance= distance/58; //Convert the pulse duration to distance 28 //You can add other math functions to calibrate it well 29 distance= distance*0.3937; //Convert from cm to inches 30 31 Serial.print("Distance "); 32 Serial.print(distance,2); 33 Serial.println(" Inches"); 34 35 delay(50); 36 37} 38 39
Code_cm_Serial.ino
arduino
Display distance on the Serial monitor in cm
1/* This code works with JSN SR04 T ultrasound module 2 * It measures the distance and shows it on the Serial monitor 3 * Refer to www.SurtrTech. com or SurtrTech YT channel for more informations 4 */ 5 6#define TRIG 11 //Module pins 7#define ECHO 12 8 9void setup() { 10 11 Serial.begin(9600); // Serial monitoring 12 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 13 pinMode(ECHO, INPUT_PULLUP); 14 } 15 16 void loop() { 17 18 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 19 delayMicroseconds(2); 20 21 digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 22 delayMicroseconds(20); 23 24 digitalWrite(TRIG, LOW); // Send pin low again 25 int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 26 27 distance= distance/58; //Convert the pulse duration to distance 28 //You can add other math functions to calibrate it well 29 30 Serial.print("Distance "); 31 Serial.print(distance); 32 Serial.println("cm"); 33 34 delay(50); 35 36} 37 38
Code_LCD_cm.ino
arduino
Display distance in LCD i²c in cm
1/* This code works with JSN SR04 T ultrasound module and LCD ic screen 2 * It measures the distance and shows it on the LCD screen in cm 3 * Refer to www.SurtrTech. com or SurtrTech YT channel for more informations 4 */ 5 6#include <LiquidCrystal_I2C.h> //Lcd library 7 8#define I2C_ADDR 0x27 //or(0x3F) I2C adress, you should use the code to scan the adress first (0x27) here 9#define BACKLIGHT_PIN 3 // Declaring LCD Pins 10#define En_pin 2 11#define Rw_pin 1 12#define Rs_pin 0 13#define D4_pin 4 14#define D5_pin 5 15#define D6_pin 6 16#define D7_pin 7 17 18#define TRIG 11 //Module pins 19#define ECHO 12 20 21LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 22 23void setup() { 24 pinMode(A0,OUTPUT); 25 digitalWrite(A0,HIGH); 26 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 27 pinMode(ECHO, INPUT_PULLUP); 28 lcd.begin (16,2); 29 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 30 lcd.setBacklight(HIGH); //Lighting backlight 31 lcd.home (); 32 } 33 34 void loop() { 35 36 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 37 delayMicroseconds(2); 38 39 digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 40 delayMicroseconds(20); 41 42 digitalWrite(TRIG, LOW); // Send pin low again 43 float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 44 45 distance = distance/58;//Convert the pulse duration to distance 46 //You can add other math functions to calibrate it well 47 48 lcd.clear(); 49 lcd.print("Distance:"); 50 lcd.setCursor(0,1); 51 lcd.print(distance,1); 52 lcd.setCursor(6,1); 53 lcd.print("cm"); 54 delay(500); 55 56} 57 58
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
1#include <LiquidCrystal_I2C.h> 2 3#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here 4#define BACKLIGHT_PIN 3 // Declaring LCD Pins 5#define En_pin 2 6#define Rw_pin 1 7#define Rs_pin 0 8#define D4_pin 4 9#define D5_pin 5 10#define D6_pin 6 11#define D7_pin 7 12 13#define TRIG 11 14#define ECHO 12 15 16LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 17 18void setup() { 19 20 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 21 pinMode(ECHO, INPUT_PULLUP); 22 pinMode(A0,OUTPUT); 23 digitalWrite(A0,HIGH); 24 lcd.begin (16,2); 25 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 26 lcd.setBacklight(HIGH); //Lighting backlight 27 lcd.home (); 28 } 29 30 void loop() { 31 32 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 33 delayMicroseconds(2); 34 35 digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 36 delayMicroseconds(20); 37 38 digitalWrite(TRIG, LOW); // Send pin low again 39 float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 40 41 distance = distance/13.3511; //13.3511 instead of 58 because the speed of a soundwave in water is far bigger than in air 42 43 lcd.clear(); 44 lcd.print("Distance:"); 45 lcd.setCursor(0,1); 46 lcd.print(distance,1); 47 lcd.setCursor(6,1); 48 lcd.print("cm"); 49 delay(500); 50 51} 52 53
Code_LCD_In.ino
arduino
Display distance in LCD i²c in inches
1/* This code works with JSN SR04 T ultrasound module and LCD ic screen 2 * It measures the distance and shows it on the LCD screen in Inches 3 * Refer to www.SurtrTech. com or SurtrTech YT channel for more informations 4 */ 5 6#include <LiquidCrystal_I2C.h> 7 8#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here 9#define BACKLIGHT_PIN 3 // Declaring LCD Pins 10#define En_pin 2 11#define Rw_pin 1 12#define Rs_pin 0 13#define D4_pin 4 14#define D5_pin 5 15#define D6_pin 6 16#define D7_pin 7 17 18#define TRIG 11 //Module pins 19#define ECHO 12 20 21LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 22 23void setup() { 24 25 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 26 pinMode(ECHO, INPUT_PULLUP); 27 lcd.begin (16,2); 28 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 29 lcd.setBacklight(HIGH); //Lighting backlight 30 lcd.home (); 31 } 32 33 void loop() { 34 35 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 36 delayMicroseconds(2); 37 38 digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 39 delayMicroseconds(20); 40 41 digitalWrite(TRIG, LOW); // Send pin low again 42 float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 43 44 distance = distance/58;//Convert the pulse duration to distance 45 //You can add other math functions to calibrate it well 46 distance= distance*0.3937; //Convert from cm to inches 47 48 lcd.clear(); 49 lcd.print("Distance:"); 50 lcd.setCursor(0,1); 51 lcd.print(distance,2); 52 lcd.setCursor(6,1); 53 lcd.print("In"); 54 delay(500); 55 56} 57 58
Code_LCD_In.ino
arduino
Display distance in LCD i²c in inches
1/* This code works with JSN SR04 T ultrasound module and LCD ic screen 2 * It measures the distance and shows it on the LCD screen in Inches 3 * Refer to www.SurtrTech. com or SurtrTech YT channel for more informations 4 */ 5 6#include <LiquidCrystal_I2C.h> 7 8#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here 9#define BACKLIGHT_PIN 3 // Declaring LCD Pins 10#define En_pin 2 11#define Rw_pin 1 12#define Rs_pin 0 13#define D4_pin 4 14#define D5_pin 5 15#define D6_pin 6 16#define D7_pin 7 17 18#define TRIG 11 //Module pins 19#define ECHO 12 20 21LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 22 23void setup() { 24 25 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 26 pinMode(ECHO, INPUT_PULLUP); 27 lcd.begin (16,2); 28 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 29 lcd.setBacklight(HIGH); //Lighting backlight 30 lcd.home (); 31 } 32 33 void loop() { 34 35 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 36 delayMicroseconds(2); 37 38 digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 39 delayMicroseconds(20); 40 41 digitalWrite(TRIG, LOW); // Send pin low again 42 float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 43 44 distance = distance/58;//Convert the pulse duration to distance 45 //You can add other math functions to calibrate it well 46 distance= distance*0.3937; //Convert from cm to inches 47 48 lcd.clear(); 49 lcd.print("Distance:"); 50 lcd.setCursor(0,1); 51 lcd.print(distance,2); 52 lcd.setCursor(6,1); 53 lcd.print("In"); 54 delay(500); 55 56} 57 58
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
1#include <LiquidCrystal_I2C.h> 2 3#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here 4#define BACKLIGHT_PIN 3 // Declaring LCD Pins 5#define En_pin 2 6#define Rw_pin 1 7#define Rs_pin 0 8#define D4_pin 4 9#define D5_pin 5 10#define D6_pin 6 11#define D7_pin 7 12 13#define TRIG 11 14#define ECHO 12 15 16LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 17 18void setup() { 19 20 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 21 pinMode(ECHO, INPUT_PULLUP); 22 pinMode(A0,OUTPUT); 23 digitalWrite(A0,HIGH); 24 lcd.begin (16,2); 25 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 26 lcd.setBacklight(HIGH); //Lighting backlight 27 lcd.home (); 28 } 29 30 void loop() { 31 32 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 33 delayMicroseconds(2); 34 35 digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 36 delayMicroseconds(20); 37 38 digitalWrite(TRIG, LOW); // Send pin low again 39 float distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 40 41 distance = distance/13.3511; //13.3511 instead of 58 because the speed of a soundwave in water is far bigger than in air 42 43 lcd.clear(); 44 lcd.print("Distance:"); 45 lcd.setCursor(0,1); 46 lcd.print(distance,1); 47 lcd.setCursor(6,1); 48 lcd.print("cm"); 49 delay(500); 50 51} 52 53
Code_LCD_cm.ino
arduino
Display distance in LCD i²c in cm
1/* This code works with JSN SR04 T ultrasound module and LCD ic screen 2 3 * It measures the distance and shows it on the LCD screen in cm 4 * Refer to www.SurtrTech. 5 com or SurtrTech YT channel for more informations 6 */ 7 8#include <LiquidCrystal_I2C.h> 9 //Lcd library 10 11#define I2C_ADDR 0x27 //or(0x3F) I2C adress, you should use 12 the code to scan the adress first (0x27) here 13#define BACKLIGHT_PIN 3 // Declaring 14 LCD Pins 15#define En_pin 2 16#define Rw_pin 1 17#define Rs_pin 0 18#define 19 D4_pin 4 20#define D5_pin 5 21#define D6_pin 6 22#define D7_pin 7 23 24#define 25 TRIG 11 //Module pins 26#define ECHO 12 27 28LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 29 30void 31 setup() { 32 pinMode(A0,OUTPUT); 33 digitalWrite(A0,HIGH); 34 pinMode(TRIG, 35 OUTPUT); // Initializing Trigger Output and Echo Input 36 pinMode(ECHO, INPUT_PULLUP); 37 38 lcd.begin (16,2); 39 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 40 41 lcd.setBacklight(HIGH); //Lighting backlight 42 lcd.home (); 43 } 44 45 46 void loop() { 47 48 digitalWrite(TRIG, LOW); // Set the trigger pin 49 to low for 2uS 50 delayMicroseconds(2); 51 52 digitalWrite(TRIG, 53 HIGH); // Send a 10uS high to trigger ranging 54 delayMicroseconds(20); 55 56 57 digitalWrite(TRIG, LOW); // Send pin low again 58 float distance 59 = pulseIn(ECHO, HIGH,26000); // Read in times pulse 60 61 distance = distance/58;//Convert 62 the pulse duration to distance 63 //You can add other 64 math functions to calibrate it well 65 66 lcd.clear(); 67 lcd.print("Distance:"); 68 69 lcd.setCursor(0,1); 70 lcd.print(distance,1); 71 lcd.setCursor(6,1); 72 73 lcd.print("cm"); 74 delay(500); 75 76} 77 78
Code_In_Serial.ino
arduino
Display distance in serial monitor in inches
1/* This code works with JSN SR04 T ultrasound module 2 * It measures 3 the distance and shows it on the Serial monitor 4 * Refer to www.SurtrTech. com 5 or SurtrTech YT channel for more informations 6 */ 7 8#define TRIG 11 //Module 9 pins 10#define ECHO 12 11 12void setup() { 13 14 Serial.begin(9600); // 15 Serial monitoring 16 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and 17 Echo Input 18 pinMode(ECHO, INPUT_PULLUP); 19 } 20 21 void loop() { 22 23 24 digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 25 26 delayMicroseconds(2); 27 28 digitalWrite(TRIG, HIGH); // Send a 10uS 29 high to trigger ranging 30 delayMicroseconds(20); 31 32 digitalWrite(TRIG, 33 LOW); // Send pin low again 34 float distance = pulseIn(ECHO, HIGH,26000); 35 // Read in times pulse 36 37 distance= distance/58; //Convert the pulse 38 duration to distance 39 //You can add other math functions 40 to calibrate it well 41 distance= distance*0.3937; //Convert from cm to inches 42 43 44 Serial.print("Distance "); 45 Serial.print(distance,2); 46 Serial.println(" 47 Inches"); 48 49 delay(50); 50 51} 52 53
Downloadable files
Wiring
Wiring
Wiring
Wiring
Comments
Only logged in users can leave comments