Hiya,

Context: Using Bela for a university project involving a ultrasound sensor controlled xylophone alt text

Help needed: The ultrasound sensors I'm using are the HC SR-04 ones, 2 which will be connected at either end of the xylophone. I'm having a hard time understanding where to connect the 4 outputs to on the Bela however.

Here is my current Bela set up. alt text

A0-7 are being used by the solenoids.

Am I right in thinking the HC SR-04 sensors wont need any analog out ports? I've found this thread:

https://forum.bela.io/d/283-how-do-i-connect-a-hc-sr04-ultrasonic-sensor

but it still doesn't make much sense to me. Is there any way I can get their input using a Pure Data sketch in the IDE? Will I have to do my whole project in C++ (I'm more comfortable with Pd as I've been using it for a couple years.)

Cheers for any help,
Sam

This seems like a fun project.

The source code I refer to from there is [this])https://github.com/BelaPlatform/Bela/blob/master/examples/06-Sensors/ultrasonic-distance/render.cpp). Wiring instructions are there and in the post you linked.

I understand you are familiar with Puredata and not so comfortable with C++, however I would recommend the following steps for you, which involve minimal tinkering with C++:

#1
remove at least one solenoid from your Bela, wire the sensor as described. Run the project and make sure everything works

#2

This implementation is using an analog out to the HC-SR04, because the latter requires a 5V input in order to work properly. To use the digitals, instead, you could replace the lines

analogWriteOnce(context, n / 2, gTriggerAnalogOutChannel, ...);

with

digitalWriteOnce(context, n, gTriggerDigitalOutChannel, ...);

after having declared and initialized gTriggerDigitalOutChannel and having called

pinMode(context, 0, gTriggerDigitalOutChannel, OUTPUT);

in setup() to initialize it as an output.

Once this is done, you could try to connect the sensor's TRIGGER input (which was previously connected to the analog pin ) to the digital pin defined in gTriggerDigitalOutChannel and check if it works.

#3
If the above does not work, it is probably because the digital out range is 0-3.3V and the TRIGGER input requires 5V. Therefore, you will need a simple amplifier to (e.g.: a transistor and a resistor) to amplify the signal from the digital output pin. Note: if your amplifier is inverting, you may have to swap HIGH with LOW in the digitalWrite() lines.

#4
Once you get it to work with the digital outputs at points #2 or #3, then you are ready to integrate this with PureData. Have a read here to get started, then you need to add the code from the project file you have been editing so far into a custom render.cpp file with puredata support:

  • create a new Pd projet in the IDE, called MYPROJECT

  • get the file in core/default_libpd_render.cpp and copy it into the project (e.g.: run the following in the terminal at the bottom of the IDE)

    cp core/default_libpd_render.cpp projects/MYPROJECT/render.cpp
  • edit the render.cpp file in MYPROJECTby:

    • copying the relevant global variables from the modified HC-SR04 example file to the top of render.cpp
    • copying the content of setup() from the modified HC-SR04 example file to the top of setup() in render.cpp
    • copying content of render() from the modified HC-SR04 example file to the top of render() in render.cpp
  • With the above changes, your render.cpp file will do the regular Puredata wrapping duties and also get new distance values from the sensors.
    • What is left to do is to send the distance read into Puredata. This can be done, for instance, adding a line like the following:
      	libpd_float("hcsr04_distance", distance);
      after the line where you compute distance. The line above will send the distance value to Pd, where it can be received with an object such as [receive hcsr04_distance] and used in your Pd patch.

Hopefully all the above should be doable for you: it's a matter of writing/editing 4/5 lines of C++ and then copy/paste them. Let us know how it goes.

    giuliomoro Hiya, thanks for the lengthy response, it's restored my hope in the project 🙂

    I've connected what I believe the instructions ask for in terms of the sensor, I think I've got the resistors wrong though because theres no output on the scope at all, I was getting a small blip every so often (not 60ms though), but now thats not even showing.

    Here's the hookup, I'm using a 2.2k resistor on the Echo and a 3.3k resistor on the GND
    https://i.imgur.com/vGj4RlH.jpg

      giuliomoro

      Hi, just attempting this step now

      edit the render.cpp file in MYPROJECT by:

      • copying the relevant global variables from the modified HC-SR04 example file to the top of render.cpp
      • copying the content of setup() from the modified HC-SR04 example file to the top of setup() in render.cpp
      • copying content of render() from the modified HC-SR04 example file to the top of render() in render.cpp

      however getting the following two errors when attempting to copy the contents of render():
      https://i.imgur.com/3TpT1Ho.png
      https://i.imgur.com/xOXAKqz.png

      edit: realised i was missing the } after
      " // Logging to the scope the pulse inputs and the distance
      scope.log(digitalRead(context, n, P8_08), distance/100);"

      It does not get rid of the first function definition error, it instead adds another one at the bottom
      https://i.imgur.com/IZ3nrqO.png

        sambilbow It does not get rid of the first function definition error, it instead adds another one at the bottom

        it probably means the number of { you copy/pasted still does not match the number of }. The fact that the error shows up at the bottom may be confusing, but it's because the parser thinks that you are still inside a function at that point, and so does not allow you to define a new one. This probably means that you are missing a }.

          giuliomoro Amazing, got it working. Here's where I put the distance object in the code: (bottom of the screenshot)

          However when I make this simple Pd patch to print the values, I'm not getting anything in the console

            That works for me. maybe add a rt_printf() statement next to libpd_float() to make sure the line is actually getting executed?

            Something like

            libpd_float("hcsr04_distance", distance);
            rt_printf("Sending distance to pd\n");

            sambilbow Amazing, got it working. Here's where I put the distance object in the code:

            Actually you may well have the problem that you are trying to send that message too often: you are sending it EVERY SAMPLE, whereas you probably only need to send it once the value is updated, so move your libpd_float() (and the rt_printf() debug line) immediately the line

            distance = duration / gRescale;

              Hmmm.
              Is the name of the [receive] object in the Pd patch exactly the same as the one you are sending to from C++?
              Maybe try to change both to hello so there is less chance of an error.

              Also, try to connect your [receive] object directly to an oscillator (maybe after multiplying [* 10]), such as:

              [receive hello]
              |
              [* 10]
              |
              [phasor~]
              |\
              |  \
              [dac~]

              just in case the message is being received but there is a problem with the printing.

              If this still does not work, can you try to run:

              examples/08-PureData/digital/_main.pd

              connecting at least one button to one of the inputs and see if you receive those messages?

                Triple checked the naming, theyre the same. Changed it to hello anyway, still no console printing. Tried the phasor and getting no audio output from the bela, I've run the puredata example and I'm getting 1 and 0 printed in the console when i connect and disconnect the jumper pins I have in Digital in 1 and 2.... 🙁

                giuliomoro I might be worth noting these things getting printed:

                Very occasionally:

                When I stop running the project: alt text

                Can you send me your render.cpp ? giulio @ bela.io

                Also, what version of Bela are you running?

                  you have a

                  return true;

                  at the eight line of setup(). This means that all the remainder of the function, where libpd is setup and initialized is not executed. Therefore you are basically running the project without libpd. I guess I should have specified that when describing the operations needed to achieve you result.
                  Remove that line and everything should work fine. Leave - however - the return true; line at the very end of setup().

                    giuliomoro Never mind, I see the one I need to get rid of, the one I keep is on line 350

                    giuliomoro I've removedf the return true; on the 13th line of setup and now when I click run project I get this error:

                    alt text