HC-SR04 Qwiic - I2C Address

The [software section of setup guide](https://learn.sparkfun.com/tutorials/qw … rogramming) is lacking.

The only information you share about the I2C addressing is in the comments of the code snippet you provide. If you were to add a couple sections, the majority of posts regarding HC-SR04 would have never been posted.

Here are the missing setup instructions:


NOTE: The HC-SR04 Qwiic comes from the factory with an I2C address of 0x00. This address is reserved by the I2C standard, and should be changed immediately.

To change the address, simply write the new, valid (0x02-0x7F) address to the device over I2C.


wire->begin();

wire->beginTransmission(0x00);

wire->write(<valid_address>);

wire->endTransmission();

wire->end();

NOTE: Once the address has been changed from 0x00, it cannot be changed back. However, the address can be changed to a valid address (0x02-0x7F) as frequently as you like.


It would be nice, if you provided similar detailed instructions regarding the read transaction, but that can easily be inferred from the sample code you have provided.

Thanks for the feedback - we’ll get this updated!

If, like documented here, each transmitted byte is interpreted as a command to modify the I2C address, how can I send the address of the register that should be read next?

Or are there no addressable registers? In this case, how will I know after several readings that the reader is still in synchronization to the sensor.

Like in your code example I transmit a 0x02 to the sensor at the beginning. According to the comments of your code, this is necessary to tell the sensor that the sequence of one reading cycle includes two bytes. As described here this will change the address of the sensor to 2 after the next power up, which happens indeed, like an I2C- address finder is indicating. That’s not what I would expect, but is OK for me now. The problem is, that without correct setting of the sensors internal address pointer, each reading of the sensors value is 0x00 0x00. That makes this device useless to me.

Any update on this issue? I had success with rewriting the address to one of my US sensors to 0x07 and getting readings, but when trying 0x06 I get “IIC testing…” in the serial monitor. Any thoughts? We are a first-time team participating in the robotics challenge at a community college with very little help. So any feedback ASAP would be greatly appreciated! We are overall trying to use 4 HC-SR04s over Qwiic and can’t find a way around this complication. We have had great success with 4 VL53L1Xs and with 4 Sharp GYA01 IRs.

The suggested method worked fine for me.

It seems the gist is, send 0x01 to start a measurement. Send anything else (0x02 … 0x7f) to set the address.

Hello,

I have purchased, HC_SRO4 ultrasonic sensor. I am trying to implement with ESP32 dev Module.

I have taken reference, code from google. But the sensor which I have purchased is giving 0cm as output if I change the sensor position/distance. Will you please confirm whether this is hardware issue or board issue.

Thank you.

// Include required libraries

#include <Arduino.h>

// Define pin connections

const int triggerPin = 2; // Connect the trigger pin of the HR-SC04 sensor to GPIO 2

const int echoPin = 4; // Connect the echo pin of the HR-SC04 sensor to GPIO 4

void setup() {

// Start Serial communication

Serial.begin(115200);

// Set trigger pin as OUTPUT

pinMode(triggerPin, OUTPUT);

// Set echo pin as INPUT

pinMode(echoPin, INPUT);

}

void loop() {

// Trigger the HR-SC04 to send ultrasonic pulses

digitalWrite(triggerPin, LOW);

delayMicroseconds(2);

digitalWrite(triggerPin, HIGH);

delayMicroseconds(10);

digitalWrite(triggerPin, LOW);

// Measure the time it takes for the ultrasonic waves to return

long duration = pulseIn(echoPin, HIGH);

// Calculate the distance in centimeters

float distance_cm = duration * 0.034 / 2;

// Print the distance to the Serial Monitor

Serial.print("Distance: ");

Serial.print(distance_cm);

Serial.println(" cm");

// Wait for a moment before taking the next measurement

delay(1000);

}