Hi all,
i hope someone can help me with a problem i'm facing with the jsn sr04t v2.0.
While building a distance meter, with two sensors (oposite 180 degree, so each facing to one side), i find the reading too unprecise.
I wrote the following code, but even with an average for trying to eliminate floating, it's still very unprecise.
#include <Wire.h>
#include <LCD.h> // For LCD
#include <LiquidCrystal_I2C.h>
// SDA - A4
// SCL - A5
// LCD for debug and setup - after everything is fine, no LCD
// Set the LCD I2C address, if it's not working try 0x27
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// Define TRIG_ and ECHO_ pin:
#define TRIG_Pin_1 1
#define ECHO_Pin_1 2
#define TRIG_Pin_2 3
#define ECHO_Pin_2 4
#define AVG 20
int counter1;
int counter2;
int count = 0;
void setup() {
pinMode(TRIG_Pin_1, OUTPUT);
pinMode(TRIG_Pin_2, OUTPUT);
pinMode(ECHO_Pin_1, INPUT_PULLUP);
pinMode(ECHO_Pin_1, INPUT_PULLUP);
// Open the Serial Monitor at 9600 baudrate to see ping results:
lcd.begin(16, 2); // iInit the LCD for 16 chars 2 lines
lcd.backlight(); // Turn on the backligt (try lcd.noBaklight() to turn it off)
lcd.clear();
// Serial.begin(9600); // Serial monitoring
}
void loop() {
digitalWrite(TRIG_Pin_1, LOW); // Set the trigger pin to low for 2uS
digitalWrite(TRIG_Pin_2, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIG_Pin_1, HIGH); // Send a 10uS high to trigger ranging
digitalWrite(TRIG_Pin_2, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(20);
digitalWrite(TRIG_Pin_1, LOW); // Send pin low again
int distance1 = pulseIn(ECHO_Pin_1, HIGH,26000); // Read in times pulse
distance1= distance1/58; //Convert the pulse duration to distance
counter1 = counter1 + distance1;
digitalWrite(TRIG_Pin_2, LOW); // Send pin low again
int distance2 = pulseIn(ECHO_Pin_2, HIGH,26000); // Read in times pulse
distance2= distance2/58; //Convert the pulse duration to distance
//You can add other math functions to calibrate it wel
counter2 = counter2 + distance2;
count++;
if (count == AVG)
{
count = 0;
counter1 = counter1/AVG;
lcd.setCursor(0, 0);
lcd.print("Dis 1 = ");
// Send ping, get distance in cm and print result (0 = outside set distance range):
lcd.print(counter1);
lcd.print(" cm");
counter2 = counter2/AVG;
lcd.setCursor(0, 1);
lcd.print("Dis 2 = ");
// Send ping, get distance in cm and print result (0 = outside set distance range):
lcd.print(counter2);
lcd.print(" cm");
}
// delay(50);
}
Anyone with experience with the jsn sr04t v2.0 able to help me out to tune this, so reading becomes more precise? It must acquire distance fast and at least 90% precise.
THank you in advance.