i have been trying to work with Waterproof ultrasonic sensor AJ-SR04m but i am not getting any genuine outputs from it .I have tried its mode 1 which requires open ckt. i.e no short ckt. at R19 .
I am currently using Arduino UNO as the microcontroller .
IF anyone has previously worked with it successfully .Kindly help.
The code I am using :-
#define TRIG_PIN 9 // Pin connected to Trig
#define ECHO_PIN 8 // Pin connected to Echo
void setup() {
Serial.begin(9600); // Initialize Serial Monitor
pinMode(TRIG_PIN, OUTPUT); // Set Trig as output
pinMode(ECHO_PIN, INPUT); // Set Echo as input
Serial.println("AJ-SR04M in HR-04 Trigger Mode Initialized");
}
void loop() {
// Trigger the ultrasonic pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the duration of the pulse
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance in cm
float distance = (duration * 0.034) / 2;
// Display the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Pulse duration: ");
Serial.println(duration);
delay(1000); // Wait for half a second before the next measurement
}
what host microcontroller are you using?
how have you connected the AJ-SR04m to the host
post your code using code tags e.g. click < CODE > and paste you code where it says 'type or paste code here
here is code I used with the UNO trigger connected to pin 12 and echo to pin 11
// Arduino - simple sonar distance demonstrator using ultrasocic transducer
// trigger connected to pin 12 and echo to pin 11
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(12, OUTPUT); // trigger pulse
pinMode(11, INPUT); // echo pulse
Serial.begin(115200);
}
// the loop function runs over and over again forever
void loop() {
// trigger pulse high approx 10 microseconds
digitalWrite(12, HIGH); // HIGH is the voltage level
digitalWrite(12, HIGH);
digitalWrite(12, HIGH);
// now trigger low
digitalWrite(12, LOW); // voltage LOW
// wait for echo pulse to go high
while(!digitalRead(11)) ;
// start timer
unsigned long StartTime = micros();
// wait for end of echo pulse (goes low)
while(digitalRead(11)) ;
unsigned long CurrentTime = micros();
// calculate length of echo pulse in microseconds
unsigned long HighLevelTime = CurrentTime - StartTime;
// calculate distance High level time * vecocity (340M/sec)/2
float distance=HighLevelTime*340.0/20000; // in cm
Serial.print("ADRM1 Time ");
Serial.print(HighLevelTime, DEC);
Serial.print(" microseconds, distance ");
Serial.print(distance, 2);
Serial.println(" cm");
delay(1000); // short delay before next pulse
}
running the code of post 5 on a UNO as I vary the distance of sensor from a flat surface gives
ADRM1 Time 1412 microseconds, distance 24.00 cm
ADRM1 Time 1428 microseconds, distance 24.28 cm
ADRM1 Time 1464 microseconds, distance 24.89 cm
ADRM1 Time 1888 microseconds, distance 32.10 cm
ADRM1 Time 2356 microseconds, distance 40.05 cm
ADRM1 Time 2952 microseconds, distance 50.18 cm
ADRM1 Time 3516 microseconds, distance 59.77 cm
ADRM1 Time 4012 microseconds, distance 68.20 cm
ADRM1 Time 4528 microseconds, distance 76.98 cm
ADRM1 Time 5344 microseconds, distance 90.85 cm
ADRM1 Time 5864 microseconds, distance 99.69 cm
ADRM1 Time 4812 microseconds, distance 81.80 cm
ADRM1 Time 5940 microseconds, distance 100.98 cm
ADRM1 Time 4132 microseconds, distance 70.24 cm
ADRM1 Time 5640 microseconds, distance 95.88 cm
ADRM1 Time 8164 microseconds, distance 138.79 cm
ADRM1 Time 8084 microseconds, distance 137.43 cm
haha ....thanks Tom for the suggestion.I really liked your tom....thanks...tom.
i came across the newping library but i did not want to use it cause firstly it is so so much easier to use this ultrasonic and i wanted to do it raw and not using library .cause the working and communication is so easy for this module .
and secondly i just wanted to check thesensor if it was working or not .Originally i want to integrate it with the rpi4b but there it was not working so tried it on arduino just to check its functionality .
thanks for the pic "horace".I will surely now try it after 3-4 days and will tell you but as much as i can see in this image i was doing the same and used the same code given by code but my readings were not changing idk why ...what you think might be possible reason .
i was giving 5v and trigger on 12 and echo on pin 11 of arduino UNO but still no result .