PiicoDev p30 Ultrasonic Arduino code (within)

I love the PiicoDev hardware. I love to program in Arduino’s C language.

I have quickly created test code to work with the PiicoDev Ultrasonic Distance measurement. It is not efficient and I will clean up later and put on github but for anyone wanting to get the Ultrasonic Distance Measurement working on a Pico in Arduino this will hopefully help get you going:

//PiicoDev p30 Ultrasonic test code
//Authored by Nigel Hungerford-Symes (based on python code by Michael Ruppe and Adafruit SR04 examples)
#include <Wire.h>
#define SLAVE_ADDR 0x35       
uint8_t distance_H = 0;
uint8_t distance_L = 0;
uint16_t distance = 0;
float mm_per_microsecond = 0.343;
float reported_distance = 0.0;



void setup() {
  Wire.setSDA(8);
  Wire.setSCL(9);
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(115200);
  while(!Serial)delay(10);
  delay(3000);

  Serial.println("Pico W up");  //tested on a Pico W board, but Pico is also fine.
    

  Serial.println("IIC testing......");
}



void loop() {
  Wire.beginTransmission(SLAVE_ADDR); // transmit to device
  Wire.write(5);              // measure command: 0x05 (RAW)
  Wire.endTransmission();    // stop transmitting

  Wire.requestFrom(SLAVE_ADDR, 2);    // request 6 bytes from slave device #8
  while (Wire.available()) { // slave 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;
    reported_distance = distance * mm_per_microsecond;
    reported_distance = reported_distance / 2;  //raw is round trip, need half
    Serial.print(reported_distance);         // print the character
    Serial.println(" mm");

    delay(1000);
  }
}
2 Likes

Hi Nigel,

Amazing work - thanks for sharing! PS: If you havent already the code on the sensors microcontroller can also be found in the Repo.

Liam