Hi all,
I am working on a vibration measurement project for my graduation project on the university, it will run connecting Arduino over USB to MATLAB/LABVIEW so I need higher baud rates over serial port which I can reach for now.
My idea is working with ADXL345 accelerometer at 3200Hz. So I begin testing with a sinewave example I found on internet and I modified it for my purposes (Later I will use ADXL345).
In the attached program it sends sinusoidal wave data points over Serial.println() command with upcoming format on each point: XxvalueYyvalueZzvalue. Example X25Y15Z2.
But the big issue is I can't reach more than 1300Hz with this method-tested on different serial monitors-. I tried to use Serial.begin() on different baudrates, but I can't reach more than 115200 bps (Tested on two Arduino Mega 2560, one Mega 1280, one Nano V3 and other UNO with same result). I made some test and calculations and using Serial.begin() at higher baudrates, my sample rate is the same. Decreasing Serial.begin() baudrate, decreases my sample rate-It's normal-.
For my purposes 2100 Hz would be perfect due I will use FFT, so my frequency spectrum would be 1050Hz. My purpose is obtaining 1000 or more Hz at the end of the process (as ISO 10816 demands). I estimated I need almost 160000bps for this, so I think 230400bps would be ok (still losing some data points from the accelerometer). Perfect would be 256000bps or more which is translated on 500000bps
How could I increase the baudrate communications from Arduino to the PC? Why is it limited? maybe it's needed to replace the bootloader? Any other suggestion?
Thank you in advance
//code modified for improvement from http://forum.arduino.cc/index.php?topic=8563.0
float wav1[3];//0 frequency, 1 unscaled amplitude, 2 is final amplitude
int average;
const int Pin = 9;
float time;
float percentage;
float templitude;
float offset = 2.5; // default value 2.5 volt as operating range voltage is 0~5V
float minOutputScale = 0.0;
float maxOutputScale = 5.0;
const int resolution = 5; //delay
const float pi = 3.14159;
void setup() {
wav1[0] = 230; //frequency of the sine wave
wav1[1] = 2.5; // 0 - 2.5 amplitude (Max amplitude + offset) value must not exceed the "maxOutputScale"
TCCR1B = TCCR1B & 0b11111000 | 1;//set timer 1B (pin 9) to 31250khz
pinMode(Pin, OUTPUT);
Serial.begin(230400);
}
void loop() {
time = micros()% 1000000;
percentage = time / 1000000;
templitude = sin(((percentage) * wav1[0]) * 2 * pi);
wav1[2] = (templitude * wav1[1]) + offset; //shift the origin of sinewave with offset.
average = mapf(wav1[2],minOutputScale,maxOutputScale,0,255);
String XS=String(average,DEC);
String YS=String(average,DEC);
String ZS=String(average,DEC);
//String pos_general=String(XS+"X"+YS+"Y"+ZS+"Z");
String pos_general=String("X"+XS+"Y"+YS+"Z"+ZS);
Serial.println(pos_general);
delayMicroseconds(resolution);
}
// function to map float number with integer scale - courtesy of other developers.
long mapf(float x, float in_min, float in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}