Hello guys, my first post here. I'm making a project where I need some range of values from some sensor to control a Pure data values, I tried using the Serial.Write function from Arduino and sending it to an Analog Input, but the PWM from Arduino is not really precise and reliable. I bought an "Arduino Due" which has 3 RX/TX ports, I was hoping serial communication would make it, but I can't find the way. I would really appreciate some help. Thanks in advance.
How do I make Bela receive serial communication from arduino within pure data?
- Edited
memorg1120 Serial.Write function from Arduino and sending it to an Analog Input
hmmm how is that supposed to work?
memorg1120 , but the PWM from Arduino is not really precise and reliable.
You'd need at least a lowpass filter for an Arduino's PWM output to be readable from an analog input.
I spent an afternoon getting some serial in into Pd to work. You'll need to:
- update your board to the latest
dev
branch (see here) - also have a Pd file similar to this in your project (source here)
Feedback welcome .... I guess it's hard to provide a universal way of parsing the serial input, so it would be nice for either allowing format specifiers (e.g.: ffssff
to parse a space-separated input into a list of float-float-symbol-symbol-float-float). Another useful feature could be to pass the input to Pd as "binary", i.e.: as a list of floats between 0 and 255, each representing one byte. This is equivalent to netsend/netreceive -b
feature, but it's mostly unclear to me what that is supposed to be used for unless you have a dedicated external that can parse that back into useful stuff (e.g.: oscparse/oscformat).
- Edited
Hey man, thanks for the great answer, I can see all the hard work you did.
giuliomoro hmmm how is that supposed to work?
Sorry, I meant analogWrite.
giuliomoro I followed your steps, but i don't think that i succeeded. Arduino is running the example 'AnalogInOutSerial' at 115200 rate I connected it to the USB in of the BeagleBone and build your pd example with the render.cpp, but it doesn't print anything. I'm getting a successful build with this error : Error from tcgetattr: Input/output error. Any pieces of advice?
Did you change /dev/ttyS4 in the patch for the path of the usb serial device you are using?
giuliomoro how can i find it? im a bit confused of how can i access bela's terminal without ssh.
Just type ls /dev/tty{USB,ACM}*
in the console at the bottom of the IDE. That should display most USB connected UART devices
PS: if you want to connect to the board via SSH instead, use ssh root@bela.local
Hello! I stumbled upon this thread because I try to do something similar. I got it to run the way you described with the above pd sketch and render.cpp. I am sending a series of floats as text, separated by space and ending with a newline character. It works somehow but also it seems there are numbers getting lost on the way. I am sending ten numbers (changed the unpack and pack accordingly) but I get sometimes 5, sometimes 7, sometimes ten and also they seem to get mixed up. I tried to reduce the frequency in which the Arduino sends but even at once per second it happens. Any ideas why? Thanks in advance!
I also tried to send the numbers as bytes (I mapped it to 0-255 so one byte per number because it doesn't really matter whether I get the actual values for me) and changed "newline" to "none" but then I got only zeroes ...
I have made some improvements to the SERIAL features in the past months and I just pushed a simpler but better documented example (PureData/serial). So make sure you get the latest dev
branch of Bela on your board and that you remove any .cpp file from your Pd project, then try again and come back if you still have trouble.
Among the new features, you can now set bytes
as the type and receive individual bytes as a value between 0 and 255.
Thank you very much! I tried and it seems to work now.
- Edited
giuliomoro Hi! I'm trying to make sense of Serial reading into PureData, and I can find " a simpler but better documented example (PureData/serial)". I've got the lastest dev branch of Bela on my board. Any advise?
I'm trying to send data from arduino to PureData running on Bela, i'm running the PD patch that is shared upper, and printing the values. Even though they look good on Arduino Serial Monitor, In Bela its strange, I'm not receiving every values, and it's changing variables order. I was trying to look for the example that you wrote about.
Sorry if this is a simple question that i'm not getting, and thank you!
This is the example: https://github.com/BelaPlatform/Bela/tree/dev/examples/PureData/serial
If it's not on your board, then it's likely that you are not actually running the latest dev
branch. From the IDE go to settings -> other system functions -> version info and report what you see there.
- Edited
giuliomoro , hmmm i'm still trying to make sense of "running the latest dev branch". I know i've updated Bela recently.
Ok, just got to the instructions on hwo to update to experimental release, and i'm updating to the dev branch right now!
This is was what i got on my version info:
This is after updating to dev branch:
And there is it! Thank you!
Unfortunately i'm getting inconsistent readings more than 5 variables.. Until 5 variables, i get the values print on Bela console right. With 6 variables values start jumping in a strange way.
On the Arduino:
Serial.print(gY, 0);
Serial.print(" ");
Serial.print(gZ, 0);
Serial.print(" ");
Serial.print(val_bme_temp, 0);
Serial.print(" ");
Serial.print(val_bme_alti, 0);
Serial.print(" ");
Serial.print(val_bme_humi, 0);
Serial.print(" ");
Serial.println(val_peso, 0);
Thank you again ;_;
Hey, I cant send more that 4 variable values, any suggestion? Thank you!
sergio_coutinho With 6 variables values start jumping in a strange way.
Can you say more about this? I am wondering if what's happening is that the data is received and passed to Pd just fine and the issue is only with the printing
giuliomoro hey,
Arduino:
Serial.print(aX, 0);
Serial.print(" ");
Serial.print(aY, 2);
Serial.print(" ");
Serial.print(aZ, 2);
Serial.print(" ");
Serial.print(aSqrt, 2);
Serial.print(" ");
Serial.print(gX, 0);
Serial.print(" ");
Serial.print(gY, 0);
Serial.print(" ");
Serial.print(gZ, 0);
Serial.print(" ");
Serial.print(val_bme_temp, 0);
Serial.print(" ");
Serial.print(val_bme_press, 0);
Serial.print(" ");
Serial.print(val_bme_alti, 0);
Serial.print(" ");
Serial.print(val_bme_humi, 0);
Serial.print(" ");
Serial.print(val_peso, 0);
Serial.print("\n");
delay(200);
and in Bela PureData, i've tried multiple prints, but the ones that should work:
Inconsistent readings, at least printing:
nothing shows on the last unpacked float, and the first ones are inconsistent (like the console printed values)
Thank you for your help again!
ok there was a bug in the code handling that. Try creating a new render.cpp file in your Pd project and paste the content of this one into it, then rebuild the project and try again: https://raw.githubusercontent.com/BelaPlatform/Bela/dev/core/default_libpd_render.cpp
giuliomoro It works! Thank you!
With this render file i have no problem in unpack all variables coming from serial!
- Edited
ok thanks that's available for everyone on the dev branch now. If you update your board to that, you will no longer need the cpp file in the project and the update will apply across all projects.
giuliomoro thanks
What if I don't want to connect via the USB cable, but instead via the Arduino Tx and Rx pins? Could I connect those pins (with a logic level shifter?) to the Bela somehow and have at least one-way serial communication from the Arduino to PD on the Bela? Which pins would I use in the Bela? And how would I define the serial port in PD?
- Edited
Yes that's doable. On the software side, the latest dev
branch of the Bela core code contains example PureData/uart-communication
. Make sure you update your board to the latest dev
branch following the instructions here.
Hardware-wise, if your Arduino has 3.3V signal levels, then you can connect the two directly. If your Arduino has 5V signal levels, on the other hand, you need some way of adapting that voltage to Bela's 3.3V. A dedicate logic level shifter IC would be the ideal solution, but you could toy around with a passive one, too. Remember that Bela cannot take unbound current from 5V sources into its pins, so protection is needed.
For 5V Arduino -> 3.3V Bela you could try something like this:
Arduino
Tx---+
|
R1 4k7
| Bela
+-----------Rx
|
R2 8k2
|
GND
R1 and R2 is the resistor divider with gain R2/(R1+R2) and R1's high value doubles up as a current limiter. Values are not critical, but keep R1 > 1k and pick R2 so that R2/(R1+R2) * 5V is between 2.7V and 3.3V.
Passive level shifting to go from Bela's Tx at 3.3V to Arduino's Rx at 5V is not possible, but it may be that there is no need for level shifting: the ATMEGA328p's datasheet states in table 28.2 "DC CHARACTERISTICS" that an input can be registered as HIGH if it's as low as 0.6Vcc, i.e.: 0.65 = 3.0V. So it may just work with a direct connection. However, I'd add some protection to avoid burning Bela in case the Arduino pin sends out 5V at some point, either during boot or because of a but in the fw. Put 10k in series and that should be more than enough.
giuliomoro
Hi! Just to know: if we update Bela Board from IDE (or other ways) - at present 8h - do we get this update of render.cpp for serial communication or we need to getting it by hand?
If you update to the dev
branch, then it's already there.
giuliomoro I have a 5v arduino mega, because my sensors are 5v and also need the interrupt ports because of the speed. Would either of these level shifters work?
https://www.sparkfun.com/products/12009
https://www.adafruit.com/product/395
In Bela, can I use any of the digital pins for Rx, or do I need to use one specifically?
And in PD, which port do I use instead of /dev/ttyUSB0 in your example above? [new myserial /dev/ttyUSB0 9600 newline floats(
or how do I find a list of available ports in PD?
giuliomoro If your Arduino has 5V signal levels, on the other hand, you need some way of adapting that voltage to Bela's 3.3V.
Also, here you're talking about the digital pins, right? Because I have the following working using the analog input pin with 5v. If you press the button, both LEDs light up at the same time. Is my setup here going to be problematic in the long run?
On the Arduino:
`#include <EasyButton.h>
#define BUTTON_PIN 6
#define LED_PIN 8
#define BELA_PIN 10
#define BAUDRATE 9600
EasyButton button(BUTTON_PIN);
// Callback function to be called when the button is pressed.
void onPressed()
{
Serial.println("Button pressed");
}
void setup()
{
Serial.begin(BAUDRATE);
pinMode(LED_PIN, OUTPUT);
pinMode(BELA_PIN, OUTPUT);
button.begin();
button.onPressed(onPressed);
}
void loop()
{
button.read(); // Continuously read the status of the button.
// light the LED if pressed
if(button.isPressed()){
digitalWrite(LED_PIN, LOW);
digitalWrite(BELA_PIN, HIGH);
} else {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BELA_PIN, LOW);
}
}
`
On the Bela Mini with Pd:
drumminhands Would either of these level shifters work?
I think so. Or you can try the passive solution proposed above first and see if it works well enouhg.
drumminhands In Bela, can I use any of the digital pins for Rx, or do I need to use one specifically?
You can only use dedicated UART pins. There are three sets on BelaMini , but only UART 4 does not have any conflicts:
That's on P2.05 (Rx) and P2.07 (Tx). To enable these, run:
config-pin P2.05 uart;
config-pin P2.07 uart;
in the Bela IDE's console.
drumminhands And in PD, which port do I use instead of /dev/ttyUSB0 in your example above? [new myserial /dev/ttyUSB0 9600 newline floats( or how do I find a list of available ports in PD?
If using UART4, use /dev/ttyS4
.
drumminhands Also, here you're talking about the digital pins, right?
Yes, the 3.3V limit applies to all pins on the P1 and P2 headers (actually, some have even lower 1.8V limits).
@giuliomoro Hi! I am doing something similar and came across this thread. I am sending values for two variables using Arduino uno.
I am using this PD. However, I am unable to see any values in output console. I get following:
Running project ...
Running Pd 0.51-4
Audio channels in use: 2
Analog channels in use: 8
Digital channels in use: 16
No MIDI device enabled
Error from tcgetattr: Input/output error
Error tcgetattr: Input/output error
pd 0.51.4
bonk version 1.5
fiddle version 1.1 TEST4
pique 0.1 for PD version 23
sigmund~ version 0.07
I am not sure what exactly needs in my current patch...what could possibly cause this issue?
did you enable the UART via the device tree overlay? See here https://learn.bela.io/using-bela/technical-explainers/device-tree-overlays/#image-v034-or-above
giuliomoro Yes.. Just enabled it..added the below in uEnv
uboot_overlay_addr2=/lib/firmware/BB-BELA-00A1.dtbo
uboot_overlay_addr3=/lib/firmware/BB-BELA-CTAG-SPI-00A0.dtbo
uboot_overlay_addr4=/lib/firmware/BB-UART4-00A0.dtbo
but no output prints in console
Running project ...
Running Pd 0.51-4
Audio channels in use: 2
Analog channels in use: 8
Digital channels in use: 16
No MIDI device enabled
pd 0.51.4
bonk version 1.5
fiddle version 1.1 TEST4
pique 0.1 for PD version 23
sigmund~ version 0.07
root@bela ~/Bela#
at least the error went away. What are you sending to it and how are you wiring your device? I recommend you use 'none' instead of 'newline' until you figure out that the basics are working.