I'm not sure how/where to change the code of the ultrasonic distance example to work for digital output 8 and digital in 14. I need to use a digital out because it's a bela mini. I think I connected everything right. but not sure how to code and be able to read it in pure data. Any pointers? thanks
Conecting hc sr 04 sensors to bela mini
- Edited
I just updated the example to work with Bela Mini, but I don't have a sensor to test it with. Can you try it out?
https://github.com/BelaPlatform/Bela/blob/dev/examples/Sensors/ultrasonic-distance/render.cpp
Note that you need a resistor divider to go into Bela's digital inputs, as explained in the description.
As for using it with Pd, have a look here. But first please test the C++ example I made above for you!
- Edited
giuliomoro Ok will do. Since I want to use d8 as output and d15 as input I would just have to put those numbers instead of 0 and 1? Can I just copy and paste the code in the ide? or should I drag the edited file?
yes.
Well i don't see anything in the scope but don't really know what I should be looking for.
The scope should show the response from the ECHO pin (a brief pulse) and a scaled version of the distance. Did you connect the ECHO pin of the module via a resistor divider? What resistor values did you use for that? This wiring diagram was made for a Bela (not BelaMini) but it should be clear how the resistor divider should be wired.
@Tractor could you please try the example linked above (which uses a digital out for the TRIG out instead of analog as you may've been using so far(?)) and see if it works for you?
Yes! I see something in the scope red vertical line that moves from left to right. Although doesn't seem to be affeted by my proximity to the sensor
tkay I see something in the scope red vertical line that moves from left to right.
The red line is the signal from the echo pin. Seeing it is a good thing: it means it's being received. It's position in the screen is pretty much random and shouldn't matter much. There should also be a blue line there (although you may not see it if it's at exactly 0): does it move with distance?
Also, the console in the main IDE tab should print several times per second. What does it print?
It doesn't print anything...I have been trying with a 4.7k resistor b/c I don't have 2.2k would that make it not work?
I actually stumbled upon an HC-SR04 yesterday so now I tested the example. It was working, though the printing was a bit weird (extremely slow), so I fixed that:
https://github.com/BelaPlatform/Bela/blob/dev/examples/Sensors/ultrasonic-distance/render.cpp
where did you put the 8 and 15? You should do it at lines 58 and 59 where gTrigDigitalOutPin
and gEchoDigitalInPin
are defined.
- Edited
Once I changed resistor values it started printing something other than zeroes. I used 47 for resistor 1 and 100 for resistor 2 is this not enough division to protect the bela? Would 1k and 2.2 be the ideal set? or 100k and 220 be the same effect? It printed the values shown in the image. The scope shows red lines but no blue ones. is there something missing? could I do a custom render to use this input in Pure Data from a receive object?
tkay g. I used 47 for resistor 1 and 100 for resistor 2 is this not enough division to protect the bela? Would 1k and 2.2 be the ideal set? o
the division is enough but you are drawing a lot of current for no good reason. See here for recommendations on resistos: https://forum.bela.io/d/1598-fritzing-diagram-for-hc-sr04-to-bela/4 . The ratio is important, but also the minimum value.
tkay The scope shows red lines but no blue ones. i
The blue line will display a distance of up to 100cm by default. To see more than that you need to change the scale of the corresponding signal in the scope options or change the /100
in:
scope.log(digitalRead(context, n, gEchoDigitalInPin) * 0.5, distance/100);
Awesome, the sensor is working great now. With the last edit you did. Thanks for that. Now, is there a way to get these readings with this code in a PD via a custom render ? I imagine somebody has done it before?
sure there's this old one I made a while back, which is being discussed right now.
I get these errors when I use > giuliomoro this old one. This one(https://thetractorshed.neocities.org/render-c_plus_plus_and_pd_for_HCSR04.txt) works but when I try to change the analog out to digital it gives me erros too
loads up with no errors and the pd patch prints a distance of cero. I am using d0 d1 as my output and input respectively like you did in the original ultrasonic sensor file instead of the analog that this file expects. But I don't know how to change that on the tractorshed file, is there a simple way?
the file that you referred to as
brings up with a bunch of errors despite that I believe it's configured the way I need( d0 d1 output input although it also uses d2 and d3 )I would need to deactivate those so that I can use for other purposes. any reason this one you believe why this one is not working here is the full error messages:
In file ./include/legacy/UdpServer.h: [warning] You included file UdpServer.h. This is now deprecated. You should instead use #include <libraries/UdpServer/UdpServer.h> [-W#warnings] column: 2, line: 2
In file ./include/legacy/Midi.h: [warning] You included file Midi.h. This is now deprecated. You should instead use #include <libraries/Midi/Midi.h> [-W#warnings] column: 2, line: 2
In file ./include/legacy/Scope.h: [warning] You included file Scope.h. This is now deprecated. You should instead use #include <libraries/Scope/Scope.h> [-W#warnings] column: 2, line: 2
In file ./include/legacy/PulseIn.h: [warning] You included file PulseIn.h. This is now deprecated. You should instead use #include <libraries/PulseIn/PulseIn.h> [-W#warnings] column: 2, line: 2
no member named 'init' in 'PulseIn' column: 13, line: 317
no member named 'init' in 'PulseIn' column: 14, line: 319
In file included from /root/Bela/projects/pd3/render.cpp:15:
./include/legacy/UdpServer.h:2:2: warning: You included file UdpServer.h. This is now deprecated. You should instead use #include <libraries/UdpServer/UdpServer.h> [-W#warnings]
#warning You included file UdpServer.h. This is now deprecated. You should instead use #include <libraries/UdpServer/UdpServer.h>
^
In file included from /root/Bela/projects/pd3/render.cpp:16:
./include/legacy/Midi.h:2:2: warning: You included file Midi.h. This is now deprecated. You should instead use #include <libraries/Midi/Midi.h> [-W#warnings]
#warning You included file Midi.h. This is now deprecated. You should instead use #include <libraries/Midi/Midi.h>
^
In file included from /root/Bela/projects/pd3/render.cpp:17:
./include/legacy/Scope.h:2:2: warning: You included file Scope.h. This is now deprecated. You should instead use #include <libraries/Scope/Scope.h> [-W#warnings]
#warning You included file Scope.h. This is now deprecated. You should instead use #include <libraries/Scope/Scope.h>
^
In file included from /root/Bela/projects/pd3/render.cpp:22:
./include/legacy/PulseIn.h:2:2: warning: You included file PulseIn.h. This is now deprecated. You should instead use #include <libraries/PulseIn/PulseIn.h> [-W#warnings]
#warning You included file PulseIn.h. This is now deprecated. You should instead use #include <libraries/PulseIn/PulseIn.h>
^
4 warnings generated.
/root/Bela/projects/pd3/render.cpp:317:13: error: no member named 'init' in 'PulseIn'
pulseIn.init(context, gEchoDigitalInPin, HIGH);
/root/Bela/projects/pd3/render.cpp:319:14: error: no member named 'init' in 'PulseIn'
pulseIn2.init(context, gEchoDigitalInPin2, HIGH);
~~~~~~~~ ^
2 errors generated.
make: *** [/root/Bela/projects/pd3/build/render.o] Error 1
Use this one instead.
Follow instructions to enable analog out instead of digital out if you so wish
(also note the receiver name is now distance
(not distance1
))
Awesome, thanks so much. Works beautifuly much better than the pd only methods.
Yes, I did try that one. And got useful readings but not as precise
hmm that's sad. It would be interesting to compare them side-by-side
- Edited
giuliomoro
I found the same as @tkay: as commented elsewhere, regular pulsing jumps in the Pd patch - introducing a kind of yodelling effect - which I tried to filter out. I experimented with the [delay] object for the output and that improved things a bit, but the combined C++/Pd approach still gives much better results.
OK thanks I will have another look
- Edited
tkay
Hello!
I'm back trying to use the HC-SR-04 sensor, this time with a regular full size bela. However I'm getting some errors. I'm not sure if this:
giuliomoro this one instead.
was meant to be used with the mini only. I am now using the analog out instead and as instructed i commented out the #define ULTRASONIC_DISTANCE_DIGITAL_OUT I connected the echo in (with resistor divider of course) in digital in 8 (pd 19)
What am I missing?
Here's the full error message:
In file render.cpp: no matching function for call to 'sys_doio' column: 3, line: 656
In file render.cpp: call to function 'strcmp' that is neither visible in the template definition nor found by argument-dependent lookup column: 11, line: 59
/root/Bela/projects/FX/render.cpp:656:3: error: no matching function for call to 'sys_doio'
sys_doio();
~~~~
/usr/local/include/libpd/s_stuff.h:243:12: note: candidate function not viable: requires single argument 'pd_that', but no arguments were provided
extern int sys_doio(struct _pdinstance* pd_that);
^
/root/Bela/projects/FX/render.cpp:59:11: error: call to function 'strcmp' that is neither visible in the template definition nor found by argument-dependent lookup
if(0 == strcmp(id, db[n].first.c_str()))
^
/root/Bela/projects/FX/render.cpp:544:13: note: in instantiation of function template specialization 'getIdxFromId<Trill *>' requested here
int idx = getIdxFromId(sensorId, gTouchSensors);
^
/usr/include/string.h:140:12: note: 'strcmp' should be declared prior to the call site
extern int strcmp (const char *s1, const char *s2)
^
2 errors generated.
make: *** [/root/Bela/projects/FX/build/render.o] Error 1
- Edited
This has been "canonized" and the 2024 advice is: use the example PureData/custom-render-ultrasonic-distance-sensor that comes with the latest Bela core code. If it's not present on your board, it means you have to update your core code following this procedure.
Awesome! thanks so much again
Is it possible to recieve the values in a faster interval? it seems to be coming in pulses of 1 second
The code seems to indicate it is being sent out every 60 milliseconds, you can check that by adding in render.cpp
the following lines:
static unsigned int last;
unsigned int current = context->audioFramesElapsed + n;
rt_printf("%u (%u) HIGH\n", current, current - last);
last = current;
at line 1659 (before the state = HIGH;
line, within those curly braces).
Then re run the program and see what gets printed there and how often.