i bought SU04 ultrasonic sensor which is compatible with pixhawk… but i wanted to use it with arduino, I’m not getting the accurate results as per the specifications giving in datasheet… can you tell me what’s wrong??
Hello @Hazel_grace,
Could you post a picture of your wiring? Maybe there is a problem there…
Also, if you can, post a source code.
I’m sorry if the wiring is a bit messy… still working on it… I’ll explain you… the SDA and SCL pins of the sensor are connected via resistors to the arduino … also i connected a buzzer on pin 13
this is a better wiring diagram
#include <Wire.h>
int const buzzPin = 13;
int const motorPin = 9;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
pinMode(buzzPin,OUTPUT);
pinMode (motorPin,OUTPUT);
}
int readingInCms = 0;
int address = 112;
void loop()
{
// step 1: instruct sensor to read echoes
Wire.beginTransmission(address); // transmit to device #112 (0x70)
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x51)); // command sensor to measure in “centimeters” (0x51)
// use 0x51 for centimeters
Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen
delay(1000);
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(address); // transmit to device #112
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor
if (2 <= Wire.available()) // if two bytes were received
{
readingInCms = Wire.read(); // receive high byte (overwrites previous reading)
readingInCms = readingInCms << 8; // shift high byte to be high 8 bits
readingInCms |= Wire.read(); // receive low byte as lower 8 bits
}
if (readingInCms <=400 && readingInCms >= 50) {
Serial.print ("Object detected at: ");
Serial.println (readingInCms);
digitalWrite(buzzPin,HIGH);
digitalWrite(motorPin,HIGH);}
else {
digitalWrite(buzzPin,LOW);
digitalWrite(motorPin,LOW);
}
}
this is the coding…in here after i want when the object is detected within 400cm the buzzer should buzz… now it works somewhat fine for near obstacles but doesn’t work at all for far objects… i wanna know if the SU04 ultrasonic sensor is totally compatible with arduino
with a 9V battery
Hey @Hazel_grace,
Could it be that it was the high current draw which caused the Arduino to reset?
Also, try to put delay about 50ms after each measurement to reduce echo.