Dear Bela Forum,

We're looking for a distance sensor to work with the Bela board, detecting distances of 50-300 cm or larger. On the forum, I have found a lot of information on the HC-SR04 sensor, which seems to work well.

Does anyone have experience with Seeedstudio sensors, particularly these ones?:
- https://www.seeedstudio.com/Seeedstudio-Grove-TF-Mini-LiDAR.html?utm_source=blog&utm_medium=blog
- https://www.seeedstudio.com/Grove-Ultrasonic-Distance-Sensor.html?utm_source=blog&utm_medium=blog
Do they work out-of-the-box with Bela?

What about these from Adafruit?:
- Time of Flight https://www.adafruit.com/product/3317
- IR distance sensor https://www.adafruit.com/product/164

Are there other distance sensors that have proven to work well with Bela?
We are interested in ultrasonic but also IR or laser sensors.

    eve of 50-300 cm or larger

    that's a longer range than what most IR sensors can do. For instance, the Sharp GP2Y0A21YK0F only does 80cm.

    I have done some work to support the TeraRanger Evo 15m and this comes also in 3m version that could work for your application. See https://forum.bela.io/d/1629-longer-range-tof-distance-sensors-with-bela

    It looks like this just requires connecting it via UART and read an 8-byte message, so straightforward to use with Bela.

    This looks like a HC-SR04 but using a single pin. Can be used from Bela, but needs some adaptations of the existing code. Namely, it needs to call pinMode() before and after each write to the trigger pin.

    https://forum.bela.io/d/2897-bela-pure-data-and-gy-530-vl53l0x/10

    this is great as it has an analog voltage output, which means it can be easily read from the Bela's analog inputs. However, the usable range is smaller than what you require.

      a year later

      Hello,

      I am trying to make the grove ultrasonic distance sensor work, but still get no data from the sensor. Since I am very new to bela, I stayed close to Sensors/ultrasonic-distance-sensor/render.cpp.

      giuliomoro This looks like a HC-SR04 but using a single pin. Can be used from Bela, but needs some adaptations of the existing code. Namely, it needs to call pinMode() before and after each write to the trigger pin.

      I tried to call pinMode() before and after writing on the gTrigDigitalOutPin, which is set to the same pin as gEchoDigitalInPin.

      bool setup(BelaContext *context, void *userData)
      {
      	// Set the mode of digital pins
      	pinMode(context, 0, gTrigDigitalOutPin, OUTPUT); // writing to TRIGGER pin
      	pinMode(context, 0, gEchoDigitalInPin, INPUT); // reading from ECHO pin
      	pulseIn.setup(context, gEchoDigitalInPin, HIGH); //detect HIGH pulses on the ECHO pin
      	scope.setup(2, context->digitalSampleRate);
      	return true;
      }
      
      void render(BelaContext *context, void *userData)
      {
      	for(unsigned int n = 0; n < context->digitalFrames; ++n){
      		gTriggerCount++;
      		bool state;
      		if(gTriggerCount == gTriggerInterval){
      			gTriggerCount = 0;
      			state = HIGH;
      		} else {
      			state = LOW;
      		}
      		pinMode(context, 0, gTrigDigitalOutPin, OUTPUT);
      		digitalWrite(context, n, gTrigDigitalOutPin, state); //write the state to the trig pin
      		pinMode(context, 0, gEchoDigitalInPin, INPUT);
      		int pulseLength = pulseIn.hasPulsed(context, n); // will return the pulse duration(in samples) if a pulse just ended
      		float duration = 1e6 * pulseLength / context->digitalSampleRate; // pulse duration in microseconds
      		static float distance = 0;
      		if(pulseLength >= gMinPulseLength){
      			// rescaling according to the datasheet
      			distance = duration / gRescale;
      		}
      		static int count = 0;
      		if(count > 5000){ // we do not want to print the value every time we read it
      			rt_printf("pulseLength: %d, distance: %fcm\n", pulseLength, distance);
      			count = 0;
      		}
      		++count;
      		// Logging to the scope the pulse inputs (gEchoDigitalInPin) and the distance
      		scope.log(digitalRead(context, n, gEchoDigitalInPin) * 0.5, distance/100);
      	}
      }

      Is there something else I could try?

        the example as it was given should work with an HC-SR04. Do you know what chip is on your sensor? Can you describe your wiring?

        6 days later

        unfortunately it just says MCU on the chip and in the schematics. My wiring goes GND to GND in the top left of the board, VCC to 5V, SIG to D7.

          alexv VCC to 5V,

          that you shouldn't do if SIG is going directly to D7 as you risk damaging the Bela board.

          alexv it just says MCU on the chip

          Is that really so? The chip itself has written "MCU" on it, or is it the PCB that does? Can you post a picture of the chip?

            7 days later

            giuliomoro Is that really so? The chip itself has written "MCU" on it, or is it the PCB that does? Can you post a picture of the chip?

            sorry, the chip itself has nothing written on it. The schematics say MCU and it seems seeed studio doesn't have more infos on it either.

            https://wiki.seeedstudio.com/Grove-Ultrasonic_Ranger/

            I tried connecting with 3.3 V and it doesn't work either. Is it possible that I already damaged to the Bela board?

            From the library's source code it doesn't look like a HC-SR04 because in duration() it's using a single pin for input and output. As the page shows it works with a Raspberry Pi it should work with Bela's 3.3V without issues. You should set the pint to an output, set it low for one frame, high for one frame, low for one frame. Then set it to an input and read the duration of the incoming high pulse, i.e.: once you start reading a high value, count the frames until the value goes low. The length of the pulse will be proportional to the distance.