Is possible to use ultrasonic like HC-SR04 with Bela mini that do not have a Analog out?
Thanks

Yes. It uses the digital outputs, not the analog outputs. You may need an external transistor to boost the output level from 3.3V to 5V, but in my trials it worked fine with the raw 3.3V output from the board .

a month later

can you tell me the wiring you use to work with the raw 3.3v output?
Thanks

UPDATE
I tried to use these simple wiring

SENSOR-BELA

VCC-5V
TRIGGER-D0
ECHO-D1
GND-GND

and it works with ultrasonic-distance example modification (I turned all Analog mentions to Digital).

My next step is to include this script to a custom render.cpp to use the sensor in Pure Data patch.
I tried to run the customRender example but it doesn't work. I get this error:

This project requires the number of inputs and the number of outputs to be the same
Couldn't initialise audio rendering
Error: unable to initialise audio
Makefile:524: recipe for target 'runide' failed
make: *** [runide] Error 255
Bela stopped

Ideas?
Thanks

I tried with a new SD card flashed with new version and it works.

Now I'm trying to send sensor value from render.cpp to PD...

I found this:
int libpd_float(const char *destination, float value)

I tried
int libpd_float(const char ultrasonicvalue, float distance)

where distance is the value of the sensor and ultrasonicvalue is the PD variable...

in PD i tried
[r ultrasonicvalue]
but it not works...

thanks for helping

Can you post a bit more context around your code and report the error you get? The line you wrote is not valid C++ syntax. If possible, paste just the difference between the default file and the one you have modified. If this is not possible, paste the whole file.

Ultrasonic sensor is getting right the value (Tried without PD).
The problem is the passing of the variable from render.cpp to PD. I can't find the right syntax.

the point of the code where I calcolate the distance is in VOID RENDER part of render.cpp

it's like:

 for(unsigned int n = 0; n<context->digitalFrames; n++){
        gTriggerCount++;
        if(gTriggerCount == gTriggerInterval){
            gTriggerCount = 0;
            digitalWriteOnce(context, n / 2, gTriggerDigitalOutPin, HIGH); //write the status to the trig pin
        } else {
            digitalWriteOnce(context, n / 2, gTriggerDigitalOutPin, LOW); //write the status to the trig pin
        }
        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){
            static int count = 0;
            // rescaling according to the datasheet
            distance = duration / gRescale;
            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 -= 5000;
            }
            ++count;
        }
        
        //
        //HERE I WANT TO SEND distance value to “ultra1” named variable to PD 
        //

       }

right you should be able to use

    libpd_float("ultra1", distance);

The best place the line is immediately after

distance = duration / gRescale;

so you only send the value when you have a new measuerement.

Then, if your Pd patch has the following:

[loadbang]
|
[metro 100]
|
|   [receive ultra1]
|   |
[f   ]
|
[print distance: ]

this should print it on the console (note that I am not using simply

[receive ultra1]
|
[print distance: ]

as that would print too many messages, flooding your console, so I am printing every so often with [metro]. But if you are using the message directly, you don't need the [metro] part above.)

a year later

Hi,

I'm also interested in this.

My wiring:

Bela -> sensor
5V -> Vcc
Digital 0 -> Trig
Digital 1 -> Echo
GND -> GND

sensor -> voltage divider
echo-/\/\/\--|--/\/\/--gnd (on Bela)
      2.22k  |   4.22k
             |
       Bela digital in

(this ^ is supposed to look like the schematics you posted)

I tried the example code and changed every mention of 'analog' to digital. Looking at the scope at http://192.168.7.2/scope/ nothing is happening.

Do you have an idea what could be wrong?

Thank you!

    pritzcobinger (this ^ is supposed to look like the schematics you posted)

    you should then enclose it in tripe backticks (i.e.: ```), e.g.:
    ```
    /\/\/--+--/\/\/
    |
    |
    P1--+-- P2
    ```
    will look like

    /\/\/\--+--/\/\/
            |
            |
        P1--+-- P2

    pritzcobinger I tried the example code and changed every mention of 'analog' to digital.

    which example code? this one Sensors/ultrasonic-distance?

    pritzcobinger changed every mention of 'analog' to digital.

    you also need to initialise the digital pin you are using for output as an OUTPUT in setup(), e.g.:

        pinMode(context, 0, YOUR_OUTPUT_PIN, OUTPUT); // trigger output