Hello, I have been using a JSN SR04T model ultrasonic as a parking sensor until now. But this card is broken, the new card I ordered is AJ-SR04M model but no matter what I do, I cannot get any value. I thought it was broken and ordered a new card but that is the case. It constantly throws 0 value and occasionally throws a measurement value, although rare. I am using UNO and I have tested it with different softwares with correct cables but I have the same problem in all of them. I am using it as mode 0 and I have not made any changes, I am sharing my code, please help. I connect the RX pin to TRIG and the TX pin to ECHO.
By the way, I also tried the code with the NewPing library and no solution was found.
#define TRIG 9 //Module pins
#define ECHO 10
#define motor 11
void setup() {
Serial.begin(9600); // Serial monitoring
pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input
pinMode(ECHO, INPUT_PULLUP);
pinMode(motor, OUTPUT);
}
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");
if (distance > 1 && distance < 80)
{
digitalWrite(motor, HIGH);
delay(100);
}
else if (distance > 105)
{
digitalWrite(motor, LOW);
delay(100);
}
delay(3000);
}
I am using Uno, previously the jsn model card was working without any problems with this code. However, one day my card started to give 0 value, I was operating it in a well and it got corrosion and moisture, I cleaned it, it is receiving energy, the red light is solid, now it was sending 0, so I ordered a new card and the AJ model came, when I plug it in, it also gives 0, if you say the problem is with Arduino, I tried with 3 different Arduinos, the result is the same.
It shows the same 0, but I tried by attaching different sensors and my sensors are also intact. Finally, I tried a different code and solved it by connecting only the echo end and leaving the trigger idle.
It started to create problems again, my connection is like this, I actually switched to a new code and I only connect the echo pin, if I connect echo and trigger it throws 0, but if only echo is connected it throws value.But sometimes it starts the motor and never disables it, sometimes it sets a fixed value and after a while it sets ridiculous values like 880cm. Sometimes the motor goes in and out as these values are entered. But it was never like this before, it used to set a fixed value without any problems and the trigger and echo were connected, but now for some reason no matter what I do, the value is set to 0 in every card, every sensor, every connection and every software.
const int sensorPin = 2; // AJ-SR04M'nin RX/TRIG ve TX/ECHO pinlerini Arduino'nun 2 numaralı dijital pinine bağlayın
const int motorPin = 9; // Motor kontrol için Arduino'nun 9 numaralı dijital pinini kullanacağız (PWM pini olabilir)
long duration;
int distance;
void setup() {
pinMode(sensorPin, OUTPUT);
pinMode(motorPin, OUTPUT); // Motor pinini çıkış olarak ayarla
Serial.begin(9600);
}
void loop() {
// 1. Kısa bir LOW darbe gönder
digitalWrite(sensorPin, LOW);
delayMicroseconds(2);
// 2. 10 mikrosaniye sürecek bir HIGH tetikleme darbesi gönder
digitalWrite(sensorPin, HIGH);
delayMicroseconds(10);
digitalWrite(sensorPin, LOW);
// 3. Pini giriş olarak ayarla
pinMode(sensorPin, INPUT);
// 4. Yüksek pulsun süresini ölç
duration = pulseIn(sensorPin, HIGH);
// 5. Mesafeyi hesapla
distance = duration * 0.0343 / 2;
// 6. Seri monitöre mesafeyi yazdır
Serial.print("Mesafe: ");
Serial.print(distance);
Serial.println(" cm");
// 7. Mesafe kontrolüne göre motoru çalıştır veya durdur
if (distance > 1 && distance < 125) {
digitalWrite(motorPin, HIGH); // Mesafe 0 ile 120 cm arasındaysa motoru çalıştır
Serial.println("Motor ÇALIŞIYOR");
delay(100);
} else if (distance > 140) {
digitalWrite(motorPin, LOW);
Serial.println("Motor DURDU");
delay(100);
}
delay(3000); // Okumalar arasında kısa bir gecikme
}
The R19 resistor is empty, if I connect the sensor correctly, it always gives distance 0 in the form of vcc - gnd - trigger - echo. My card is new and I tried other cards and got the same result. However, if I disable the trigger pin, I can get a value.
Worth nothing if the value is not correct. Also, the code you linked above leaves the pin as input after first shot.
Are you sure you don't have tx / rx wrong? Try to swap them and use the "original" code.