All values from ADC

Hi all, I’m new in Arduino. I want get full signal from Arduino ADC. ADC is 10-bit so in one second I can get 1024 values, right ? Iam working with accelerometer.
I need next processing in Matlab especially set time vector (0:1/Fs:(length(signal)-1)/Fs) so I need know sampling frequency and all data from arduino. I tried it send to Serial monitor but its too slow. Iam trying write program with array but it wont show in serial monitor.

#define xPin 0    //define pin on arduino board
#define yPin 1
#define zPin 2

int x = 0;
int array[20];    //define array

void setup() 
{
  Serial.begin(9600);
  for(int i = 0; i < 20; i++);
  
}
int i;

void loop() 
{  
  x = analogRead(xPin);  //save data to x variable
  array[i]=x;                  //save x to array
  
 
}

void serial_print()
{
  for (i = 0; i < 20; i++)  
   {
    Serial.println(array[i]);
   }
}

My idea is save for example 20 values to array and then show in serial monitor where I can copy it.
If you have other idea or experience I will be glad :slight_smile: . Thanks

Profil

Hi!! i'm new in arduino too, i'm write some code to read data from an LDR in matlab maybe it can help.. the data are send by arduino one by one and read in matlab one by one, then it show the array of values..

p.d. sorry by my English is not my natal language (), also is my first post ::)

function x = LDR(n)
s = serial('COM2','BAUD',9600);
fopen(s);
x = zeros(1,n);
for i = 1:n 
y = str2num(fscanf(s));
x(i) = y;
i = i + 1;
end
fclose(s);
end

the "n" is the number of values you want to read... is not the great code ever but i think it can show you the way...

Hi all, I'm new in Arduino. I want get full signal from Arduino ADC. ADC is 10-bit so in one second I can get 1024 values, right ? Iam working with accelerometer.

That's not quite what a 10 bit ADC is all about. The frequency that you can get data is independent of the resolution. A 10 bit ADC has 10 bits of resolution. This means you can get a possible result of from 0 to 1023. If you are measuring a voltage that is about half way between AREF and GND, you will see a value of about 511. If you are measuring a voltage at GND the ADC read will be nearly zero, etc. The maximum number of different values you can read from the ADC depends on the amount of time it takes to sample the data. If you are looking to do a FFT or something like that depending on very accurate timing between samples, the ATMega328 might not be the best choice. I'll let someone who knows more comment on this. I've just read some posts that suggest the accuracy and/or timing using the onboard ADC isn't good enough for highly accurate measurements.

Getting a lot of data off chip is difficult also, as you are finding out.

Jim.

The frequency that you can get data is independent of the resolution.

That's not quite true - you can get more samples per second at lower resolution.

[quote That's not quite true - you can get more samples per second at lower resolution. ][/quote]

I wasn't aware that you could change the resolution of the ADCs on a ATmega328. Thanks,

Jim.

Characterization and Calibration of the ADC on an AVR http://www.atmel.com/dyn/resources/prod_documents/doc2559.pdf and Enhancing ADC resolution by oversampling http://www.atmel.com/dyn/resources/prod_documents/doc8003.pdf

I found both a very informative read about the adc in atmel controllers. It mentions higher sample rates with less accuracy, what kind of errors you encounter with adc's and so on.

regards,

Jeroen

Thanks guys for answers.

Hi!! i'm new in arduino too, i'm write some code to read data from an LDR in matlab maybe it can help.. the data are send by arduino one by one and read in matlab one by one, then it show the array of values

I tried it in Matlab too with COM port but I dont think that data are read one by one. If I want read e.g. 800 values, time have to be 3.125 seconds becouse COM port is 256Hz so (800/256) and I stopped the time and it was 7 second so it looks like sampling frequency is 114Hz(800/7). It doesnt matter how many samples I have I only need know exactly time of conversion and sampling frequency.

Serial.begin(9600);

This might be too slow for you? Have you tried to change the speed in matlab and arduino?

David

Have you tried to change the speed in matlab and arduino?

Thanks, changed it to 38400, its faster but still understanding. In Arduino program I set delay 1 ms so in one second I can get 1000 values. In Matlab in for loop a set 1000 values but it takes approximately 4 seconds.

Arduino

#define zPin 2
float z  = 0;
float accz  = 0;

void setup() 
  {
    Serial.begin(38400);
  }
int i = 1;
void loop() {
  
  z = analogRead(zPin)*0.004883;
  accz = (2.57-z)/0.085626;  //computing to real acc
  Serial.print(accz);
  Serial.println(", ");
  delay(1);
}

Matlab m.file

function [DATA]=arduino_02

s1 = serial('COM3');           %define serial port
s1.BaudRate = 38400;        %define baud rate
s1.DataBits = 8;

fopen(s1);                         %open serial port
settings = fgetl(s1);
clear data;

for i= 1:1000;
    data=fscanf(s1);             %read acc
    data1=str2double(data);  %change to double
    DATA(i,1)=data1;            %save to array
end

plot(DATA);

fclose(s1);                          % close serial port

Any ideas how change it to work correctly or other alternative ? :'(

If you want speed don't do any floating point math operation in the arduino, it takes hundreds of cycles to make that operations, just put the raw 10 bit data of the adc through the serial and repeat, also, no delay, in the delay the only thing that the arduino does is counting cycles until the sum into a 1mili second delay, the delay generated by the time that takes to send the data via serial is already enough.