Crackly audio when sending audio via bluetooth->arduino->DAC->3.5mm breakout board->headphones. Do not know the cause - bluetooth

I wrote a mix of python code and arduino code to make this work. I am sending an array of numbers (audio) to the HC-05 bluetooth module/arduino uno (these are both set to communicate via serial at 115200 baud, at least that is what I set for both (Serial.begin(x) for arduino and through AT commands for HC-05) ranging from 0-4095 as strings from python via bluetooth (bluetoothsocket(RFCOMM)).They are received character by character in the arduino and read into an array which will convert the char array into the single original unsigned int. Up to there I can confirm that chars were received and definitely constructed into integers. Those integer values are passed into a 12-bit DAC via I2C (SDA (A4)/SLC (A5) pins on the arduino. On the webpage (https://learn.adafruit.com/mcp4725-12-bit-dac-tutorial/using-with-arduino) it says that to increase the speed for the transmission you write this "TWBR = 12; // 400 khz" in the arduino script, I'm guessing. Otherwise the DAC will transmit at 100kHz; so I set the transmission speed to 400kHz for the DAC. When I connect the DAC output to the 3.5mm breakout/earbuds I only hear crackling, absolutely no "sound". The earbuds work just fine on my laptop, so the problem is something else. The DAC definitely outputs a voltage (triangle wave file from webpage works) and I tried two 3.5mm breakout boards (maybe shoddy soldering work?). Does anyone have an idea of what the issue could be or steps I could take to find what the error is? My guess is that somewhere the transmission rates/bit transfers do not line up, but that is what I'm trying to find out by asking.
On the python side, the code more or less looks like this:
*initializing socket, setting to non-blocking socket,etc..
for i in range((1000)): #just to test, the file Id like to send is maybe 300,000 strings
HC05_socket.send(soundchars[i])
And this is the arduino code:
#define ledPinr 4
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
int wait =10000; //
void setup() {
// put your setup code here, to run once:
pinMode(ledPinr, OUTPUT);
digitalWrite(ledPinr, LOW);
Serial.begin(115200);
dac.begin(0x62);
TWBR = 12; // 400 khz done in library
Serial.setTimeout(wait); // for now
}
void loop() {
// Read serial input:
char val[4]; // length 4 for 12-bit resolution
if (Serial.available()){
digitalWrite(ledPinr, LOW);
Serial.readBytesUntil(',', val, 4);
int num = atol(val);
dac.setVoltage(num, false);
Serial.print(num);
}
if (Serial.available()==0){
digitalWrite(ledPinr, HIGH);
}
}
Note: ignore the LED code lines, that is just to have an idea of the data flow as I run the program.

There are many reasons that can lead to crackling audio especially in these kind of setups (as I'm sure you're aware).
A couple of things :
Although in the example you link it says to write TWBR = 12, if you look at the source code of the library it checks for the #define TWBR macro. So I would change your code to have #define TWBR 12 before your setup() function.
How often are you receiving the bluetooth data? It doesn't look like you are handling what's happening when you receive no data the DAC will just freeze on whatever value you wrote last
Make sure you are calling the right address -> you didn't mention if A0 is connected to VCC in your setup.
First, be sure to call begin(addr) where addr is the i2c address
(default is 0x62, if A0 is connected to VCC its 0x63)
Side notes:
In my experience people try to avoid using atol(). If you look at adafruit's examples they use pgm_read_word() instead.
12 Bit isn't very high audio resolution for audio playback so most audio will be distorted (other than very basic digital sounds)
Make sure the audio you're sending from Python is playable (I don't know what your test case is)
In the end it could always be the soldering but I think it will be unlikely.

Related

How to Get Audio Out via the Wolfson WM8731 CODEC on the Altera DE2-115 educational FPGA board?

My group and I are trying to create a synthesizer out of a DE2-115 board for our undergraduate capstone project.
The only thing we can't figure out is how to get the frequencies mapped to the "keys" outputted properly through the audio port on board. We've scoured the web and all provided documentation included the datasheets for the codec but we can't figure out how to get it to work properly in VHDL.
Has anyone ever worked with outputting audio through the WM8731 and if so, would they be willing to help us out?
I did that some years ago, wasn't too hard, but I used a NIOS processor with SOPC builder.
I used the Altera University Program IP cores available here.
These cores provides different functionality related to the DE2 and possibly other altera sponsered-board.
According to my logs, I used 3 of these cores to make audio work.
The altera_up_avalon_audio_and_video_config, which is used to configure the audio CODEC chip at initialization.
The second IP provide data in/out interface with the audio chip: altera_up_avalon_audio.
The last one is altera_up_avalon_clocks. I can't remember exactly what it does, but as the name imply it's necessary for the clocking of the audio chip. I think it takes an input clock and generate a PLL to provide the right clock to the CODEC.
As I said, I used a NIOS processor, still according to my log, the C code I used is:
void audio_isr(void* context, alt_u32 id)
{
const int len = 2682358;
static signed char *ptr = test_snd;
unsigned int x[128];
alt_up_audio_dev *audio_dev = (alt_up_audio_dev *)context;
unsigned int n = alt_up_audio_write_fifo_space(audio_dev, ALT_UP_AUDIO_RIGHT);
for(unsigned int i = 0; i < n; i++) {
x[i] = 0x800000 + ((int)*ptr++) << 9;
if (ptr > test_snd+len) {
ptr = test_snd;
printf("Done\n");
}
}
alt_up_audio_write_fifo(audio_dev, x, n, ALT_UP_AUDIO_RIGHT);
alt_up_audio_write_fifo(audio_dev, x, n, ALT_UP_AUDIO_LEFT);
}
static void audio_init(void)
{
alt_up_audio_dev *audio_dev = alt_up_audio_open_dev (AUDIO_0_NAME);
if ( audio_dev == NULL)
printf ("Error: could not open audio device \n");
else
printf ("Opened audio device \n");
alt_up_audio_reset_audio_core(audio_dev);
alt_up_audio_disable_write_interrupt(audio_dev);
alt_up_audio_disable_read_interrupt(audio_dev);
alt_irq_register(AUDIO_0_IRQ, (void *)audio_dev, audio_isr);
alt_up_audio_enable_write_interrupt(audio_dev);
}
I don't remember how well that worked. Well enough to deserve a commit, but it was still a test, so don't give it too much importance. My final code was way too complicated to present here.
Hopefully, this is enough to get you started on the right track, which is to use Altera's IP. These IP are clear-source AFAIR, so if you don't want the NIOS, it should be simpler to start from their source than from scratch.
Probably you might require 3 modules, clock generator, audio configuration and audio serializer and deserializer. You no need to go for NIOS II based design. Plz check the altera lab experiment to understand how it works.
experiment link - https://www.altera.com/support/training/university/materials-lab-exercises.html#Digital-Logic-Exercises
pdf link - ftp://ftp.altera.com/up/pub/Altera_Material/Laboratory_Exercises/Digital_Logic/DE2-115/vhdl/lab12_VHDL.pdf.
also check for demo files

Characters not properly displayed in serial monitor in arduino

Can anyone tell me why the characters are not getting printed properly in the serial monitor of Arduino? I am pasting the arduino code.
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,5,4,3,2);
int bluetoothTx = 15;
int bluetoothRx = 14;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int incomingByte;
void setup() {
pinMode(53, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
delay(320); // IMPORTANT DELAY! (Minimum ~276ms)
bluetooth.print("$$$"); // Enter command mode
delay(15); // IMPORTANT DELAY! (Minimum ~10ms)
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
bluetooth.begin(9600); // Start bluetooth serial at 9600
lcd.print("done setup");
}
void loop()
{
lcd.clear();
Serial.print("in loop");
//Read from bluetooth and write to usb serial
if(bluetooth.available()) {
Serial.print("BT here");
char toSend = (char)bluetooth.read();
Serial.print(toSend);
lcd.print(toSend);
delay(3000);
}delay(3000);
}
Can anyone take a look into it. It does not print the character that I provide instead it prints something else like 'y' with 2 dots on top etc. Tried almost all the available solution.
Your issues could be one of a couple things. First and easiest to check is COMMON GROUND. Did you connect just the RX and TX pins or also the GND (ground) pin? Make sure that the ground from the BT mate is connected to the Arduino ground.
If you have done that, then your issue is with the baud rate. I'm pretty sure that SoftwareSerial can't read at baud rates beyond 57600. Arduino.cc docs say it can read at 115200, but other places say it will only write up to 115200.
To test this, you will either need to change the settings for this on the Bluetooth Mate or use a Mega or Leonardo which will have a hardware serial port (other than the one used for USB) which you should be able to configure for 115200.
If you try it with hardware serial either on a Mega or just using an FTDI or something and the messages still look garbled then perhaps the bluetooth mate is not actually configured to talk at 115200 as it claims. Trying reading the docs or testing with other baud rates.
Check whether error is present due to one of the following reasons:-
1) You haven't given any command to exit from the data mode. After setting the baudrate to 9600, you are directly switching to loop. You haven't given the command to exit the command mode.
2) I too had the same problem when I was using RN171 Wi-Fi module. The cause of the problem in my case was because I was sending data to Wi-Fi module in integer format instead of uint_8. While reading from the Wi-Fi module serially with arduino mega, I was reading it in the format of characters.
You have to remember that int is actually signed 16 bit integer. So while sending data to your Bluetooth module you have to send it as uint_8 or ASCII values of the characters that you want to send. You should also read it in the same format as you sent it.
3) If these are not the error then as calumb said, there can be error in setting the bluetooth module in command mode. You haven't checked for reply from bluetooth module whether it is really in command mode or not. You must read an CMD reply from bluetooth module and at the end of every command a reply of ack to conform that its really done what you want it to do.
This may be because of Bluetooth parsing data simultaneously. when sending two different data at the same time this may happens. try to control your data flow.

Can I use UART on MSP-EXP430F5529LP from Energia in order to communicate on pins p3.3 and p3.4 (rx and tx respectively)?

Can I use UART on MSP-EXP430F5529LP from Energia in order to communicate on pins p3.3 and p3.4 (rx and tx respectively)?
I already use UART in order to communicate with my PC via USB. To do so, I use Serial.println() and such. Now that one UART is taken, how do I configure and use second UART to go to these pins? Or would it be better to rewire my Bluetooth chip (BlueGiga wt32) to some other pins?
Configuration aside, Serial does not seem to allow for multiple UARTs. How does it know which UART to print to?
For some reason, I could not find any manual on interacting with wt32 from Energia, or on interacting with multiple UARTs on Energia.
Edit: found this link: http://forum.43oh.com/topic/3942-stellaris-lm4f120-multiple-uart-problem/
only, incidentally, they say it does not work. Still, a lead.. but it is my understanding that I still have to configure UART on those two pins, if at all possible.
Edit2 Found MultiSerial example in Energia:
/*
Multple serial test
Receives from the main serial port, sends to the others.
Receives from serial port 1, sends to the main serial (Serial 0).
The circuit:
* Any serial device attached to Serial port 1
* Serial monitor open on Serial port 0:
created 30 Dec. 2008
by Tom Igoe
This example code is in the public domain.
*/
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
}
Now, this may sound like a really noob question, but where is my Serial1?? Is it somewhere on my pinout? The corresponding page in Energia manual is just a stub.
Now, this link: http://energia.nu/Tutorial_SerialCallResponse.html has a pinout according to which P1.1 is TXD, P1.2 is RXSD, which is something that I have not seen documented elsewhere. I suspect that it is assigned in this particular example, only I don't see the assignment in the code; also, I suspect that this is the backchannel, unless the switch is turned. Confused!
Edit3: found SoftwareSerial example that turns pins of your choice into a RX and TX. So at least I probably have a software solution. Of course, I'd prefer hardware. The manual for the launchboard says the hardware supports up to 4 serial ports, but how? Where are the pins?
Sorry I keep adding to this. I'll tidy it out when there is a solution.
If you dont need those uart pins to talk to the host you can remove the jumpers on that board that connect rxd and txd and then connect those to your bluetooth module.
Serial.begin(); works for backchannel UART pins(Txd,Rxd between target ic & debugger ic marked in white squares in below image)
http://energia.nu/wordpress/wp-content/uploads/2015/03/2016-06-09-LaunchPads-MSP432-2.0-%E2%80%94-Pins-Maps.jpg
Serial1.begin(); works for P3_2 & P3_3

Arduino - Use serial print in the background

I would like to know if it would be somehow possible to handle a Serial.println() on an Arduino uno without holding up the main program.
Basically, I'm using the arduino to charge a 400V capacitor and the main program is opening and closing the gate on a MOSFET transistor for 15 and 20 microseconds respectively. I also have a voltage divider connected to the capacitor which I use to measure the voltage on the capacitor when its being charged with the Arduino. I use analogRead() to get the raw value on the pin, multiply the value by the required ratio and try to print that value to the serial console at the end of each cycle. The problem is, though, that in order for the capacitor to charge quickly, the delays need to be very small (in the range of microseconds) and the serial print command takes much longer than that to execute and therefore holds up the entire program.
My question therefore is wherther it would somehow be possible to get the command to execute on something like a different "thread" without holding up the main cycle. Is the 8bit AVR capable of doing something like this?
Thanks
Arduino does not support multithreading, but there are a few libraries that allow you to do the equivalent:
Arduino Multi-Threading Library
ArduinoThread
For more information, you can also visit this question: Does Arduino support threading?
The basic Arduino serial print functions are blocking, They watch the TX Ready flag then load the next byte to transmit. This means if you send "Hello World" the print function will block for as long as it take to load the UART to send 10 characters at your selected baud rate.
One way to deal with this is using high baud rate so you don't wait long.
The better way is to use interrupt driven transmit. This way your string of data to send is buffered with each character sent when the UART can accept the character. The call to send data just loads the TX buffer, loads the first character into the UART to start the process then returns.
This bufferd approach will still block if you fill the TX buffer. The send method would have to wait to load more into the buffer. A good serial lib might give you an easy way to check the buffer status so you could implement your own extra buffering.
Here is a library that claims to be interrupt driven. I've not used it myself. Let us know if it works for you.
https://code.google.com/p/arduino-buffered-serial/
I found this post: Serial output is now interrupt driven. Serial.print() just stuffs the data in a buffer. Interrupts have to happen for that data to actually be shifted out. posted: Nov 11, 2012. I have found that to be true BUT if you try to print more then the internal serial buffer will hold it will lock up until the buffer is at least partially emptied.

How can I digitalRead a pin and convert data to String on Arduino?

I'm doing a communication system with 2 Arduinos Leonardo. To transmit I had to convert a String in bits and send them through a PWM pin. The question is, how can I do the receiver?
My idea is to receive data in a digital pin, so I can convert LOW/HIGH to '0' and '1', put 8 bits in a string, convert to char and then I have my final String.
Some info:
- I'm using PWM because I want to implement FSK modulation, sending bit by bit;-
- I have some hardware to get communication system working;
- My physical channel for communication is Serial (via Arduino pins);
- In the receiver hardware will provide me TTL signals, so I will have 0V/5V. That's why I think digitalRead() should work;
- Data is sent via PWM and received in two level signals;
- I'll have to do some protocol to send/receive data, but still don't know how...
This is the code I wrote:
void setup() {
Serial.begin(9600);
while (!Serial) {;}
}
void loop() {
char bitPosition = 0;
String inMessage = 0;
while (Serial.available() > 0) {
char outChar;
boolean digValue = digitalRead(inPin);
for(bitPosition = 7; bitPosition >= 0; ){
if(digValue == LOW){
bitWrite(outChar, bitPosition, 0);
}
if(digValue == HIGH){
bitWrite(outChar, bitPosition, 1);
}
bitPosition--;
}
inMessage.concat(outChar);
if(inMessage != 0){
Serial.println("Received: " + inMessage);
}
}
}
I want this code to transform HIGH/LOW states from digitalRead in a string of characters. 0/1 -> char (outChar) -> String (inMessage). I don't know if i'm not thinking right. Suggestions?
Should I have to do something about baudrate to receive/transmit data? How?
Any help/suggestion would be appreciated.
Look at how SoftwareSerial is implemented in Arduino for a clue as to how to get this done.
http://arduino.cc/en/Reference/SoftwareSerial
"The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name "SoftwareSerial")."
Well, your problem if far more complicated that you believe. I have read your similar (identical?) question posted on EE.SE.
It seams that you haven't catch a fundamental thing about your issue.
It is not a software problem. If I summarize your issue, globally.
You want to send data from arduino A to arduino B.
You plan to generate a FSK signal using a PWM pin of arduino A.
first comment: How ? Look-up table of a sine wave where the pointer moves according to the phase modulation? This seems easy. So let's believe this it the way you choose.
(here you say it is a logic signal, but on EE.SE you say it's a wireless signal)
Now you have a signal at XX kHz (we don't know the XX value. But accordint to the Xtal speed of your Arduino, I can't be much greater than ~500Khz)
On arduino B, you have to solve the problem of clock recovery because on arduino B you don't have access to the clock of arduino A. This means that you can't compare the signal you are getting with a reference clock. When you analyse the received signal you get a certain phase relative to your local clock. But because you cannot know the reference phase of ardunio A, you cannot say if the receive bit is -90° or +90°. Thus you can't assign a 0 or 1 to the received signal.
This is not a trivial problem. According to your answer to the question and comments here or on EE.SE, you are trying to solve implementation problem of an algorithm that is probably not working. Your should first try to find a suited algorithm that you may simulate using MATLAB (or the free OCTAVE), make sure it is working, and next try to implement it on embedded hardware.
You receive a lot of answer on EE.SE about what do you want to do. It's because it makes no sens to solve an issue about implementing something that looks false to an expert.
Thus, if you want meaningful answers. Please spend more time explaining us what and how, conceptually, you plan to solve your problem.
Add information about how your think your FSK works. How do you plan to use PWM to do FSK, this in not trivial. And how you plan, conceptually to detect that a received symbol is a 1 or a 0.
As it is, my answer is : Event if we can solve you copy bit issue, it won't solve your problem.

Resources