Comments: Qwiic Ultrasonic Distance Sensor (HC-SR04) Hookup Guide

Pages

Looking for answers to technical questions?

We welcome your comments and suggestions below. However, if you are looking for solutions to technical questions please see our Technical Assistance page.

  • xsk8rat / about 3 years ago / 1

    The sample code is nice, but something that didn't require the SSD1306 display would be more basic. It might help a beginner get started without having to strip out the code for the components which they did not purchase.

    #include <Wire.h>
    #define RESPONDER_BROADCAST_ADDR 0x00  //default address
    #define RESPONDER_ADDR 0x00       //RESPONDER_ADDR 0xA0-0xAF
    uint8_t distance_H = 0;
    uint8_t distance_L = 0;
    uint16_t distance = 0;
    
    void setup() {
      Wire.begin(); // join i2c bus (address optional for master)
      Serial.begin(9600);  // start serial for output
    }
    void loop() {
      Wire.beginTransmission(RESPONDER_ADDR); // transmit to device #8
      Wire.write(1);              // measure command: 0x01
      Wire.endTransmission();    // stop transmitting
    
      Wire.requestFrom(RESPONDER_ADDR, 2);    // request 6 bytes from RESPONDER device #8
      while (Wire.available()) { // RESPONDER may send less than requested
        distance_H = Wire.read(); // receive a byte as character
        distance_L = Wire.read();
        distance = (uint16_t)distance_H << 8;
        distance = distance | distance_L;
        Serial.print(distance);         // print the character
        Serial.println("mm");
        delay(1);
    
        delay(100);
      }
    }
    


If you've found an issue with this tutorial content, please send us your feedback!