Connect with us

How to control sampling rate of MCP3008 that interface with Arduino Mega 2560 Board

Discussion in 'Microcontrollers, Programming and IoT' started by jayfoonglw, May 22, 2014.

Scroll to continue with content
  1. jayfoonglw

    jayfoonglw

    7
    0
    May 22, 2014
    Hello, my problem is how to control the sampling rate of MCP3008 such as 100 samples per seconds, 1000 samples per seconds in the Matlab Coding since the maximum sampling rate of MCP3008 is 200ksps (based on datasheet of MCP3008)?This is because I noticed that this coding only produce 1 sample per second (for 8 channels) when write the data (ADC value) into a excel file.

    Thanks you very much and very appreciate for helping. Hereby, I attached the previous Matlab codes used for performs ADC operation by using MCP3008 in Arduino Mega 2560 Board as shown in below.

    Code:
    %a=arduino('COM27')  %create the object tha represents our arduino & we establish the serial port when is connected in my case is com 27
    clear ('tt','count','time','r','t','voltage','adcvalue','adcvalue1','voltage','channel1','channel2','channel3','channel4','channel5','channel6','channel7', 'channel8','v','tic','toc');
    
    i = 1;
    j = 1;
    n = 0;
    r = 1;
    y = 0;
    counter = 1;
    
    in=0;%in pin
    tic;%start a time count
    time(150)=0;%pre allocate memory to speed up execution of code
    v(150)=0;
    ib=0;
    
    fid = fopen('C:\Users\Foong Lik Wei\Desktop\try2.csv', 'w+') ;
    fclose(fid);
    
    fid = fopen('C:\Users\Foong Lik Wei\Desktop\try2.csv', 'a') ;
      
       fprintf(fid, '\n') ;
       fprintf(fid, '\n') ;
       fprintf(fid, ',Timestamp') ; % Print the time string
       fprintf(fid, ',Elapsed Time (s)') ; % Print the time string
       fprintf(fid, ',Channel 1') ; % Print the time string
       fprintf(fid, ',Channel 2') ; % Print the time string
       fprintf(fid, ',Channel 3') ; % Print the time string
       fprintf(fid, ',Channel 4') ; % Print the time stringfprintf(fid, ',Channel 1') ; % Print the time string
       fprintf(fid, ',Channel 5') ; % Print the time string
       fprintf(fid, ',Channel 6') ; % Print the time string
       fprintf(fid, ',Channel 7') ; % Print the time string
       fprintf(fid, ',Channel 8') ; % Print the time string
    fclose(fid) ;
    
    
    SELPIN= 53;
    DATAOUT = 51;
    DATAIN = 50;
    SPICLOCK = 52;
    
    a.pinMode(SELPIN, 'OUTPUT');
    a.pinMode(DATAOUT, 'OUTPUT');
    a.pinMode(DATAIN, 'INPUT');
    a.pinMode(SPICLOCK, 'OUTPUT');
    
    a.digitalWrite(SELPIN,1);   % '1' means HIGH
    a.digitalWrite(DATAOUT,0);  % '0' means LOW 
    a.digitalWrite(SPICLOCK,0);
    
    
    while toc<20   
    for op = 1: 1 : 8
    adcvalue = 0;
    
    a.digitalWrite(SELPIN,0);
    
    
      if  op == 1
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
     
      
         elseif op == 2
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
      
      
         elseif op == 3
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
      
          
         elseif op == 4
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
          
      
         elseif op == 5
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
          
          
         elseif op == 6
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
     
         elseif op == 7
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,0);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
       
          
         elseif op == 8
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
        
         a.digitalWrite(DATAOUT,1);
         a.digitalWrite(SPICLOCK,1);
         a.digitalWrite(SPICLOCK,0);
          
      end
    
    
    a.digitalWrite(SPICLOCK, 1);
    a.digitalWrite(SPICLOCK, 0);
    a.digitalWrite(SPICLOCK, 1);
    a.digitalWrite(SPICLOCK, 0);
    for kk = 9: -1: 0
      dd = a.digitalRead(DATAIN);
      uu = bitshift (dd, kk);
      adcvalue = adcvalue + uu;
     
      a.digitalWrite(SPICLOCK, 1);
      a.digitalWrite(SPICLOCK, 0);
    end   
       
    a.digitalWrite(SELPIN, 1);
    
    adcvalue1(op) = adcvalue;
    
    end   
    
    adcvalue1;
    channel1(counter) = (adcvalue1(1) /1023)*5;
    channel2(counter) = (adcvalue1(2) /1023)*5;
    channel3(counter) = (adcvalue1(3) /1023)*5;
    channel4(counter) = (adcvalue1(4) /1023)*5;
    channel5(counter) = (adcvalue1(5) /1023)*5;
    channel6(counter) = (adcvalue1(6) /1023)*5;
    channel7(counter) = (adcvalue1(7) /1023)*5;
    channel8(counter) = (adcvalue1(8) /1023)*5;
      hello1 = transpose(channel1);
     
      hello2 = transpose(channel2);
      hello3 = transpose(channel3);
      hello4 = transpose(channel4);
      hello5 = transpose(channel5);
      hello6 = transpose(channel6);
      hello7 = transpose(channel7);
      hello8 = transpose(channel8);
       
      counter = counter + 1; 
       
      n = n +1; 
              
       
        ib=ib+1;
        time(ib)=toc;%creates a vector containing the diferents moments given by "toc"
        v1(ib)=hello1(r);%vector that stores the sampled values ??of voltage
        v2(ib)=hello2(r);
        v3(ib)=hello3(r);
        v4(ib)=hello4(r);
        v5(ib)=hello5(r);
        v6(ib)=hello6(r);
        v7(ib)=hello7(r);
        v8(ib)=hello8(r);
          
      timestamp = datenum(now);
      c = datevec ( timestamp );
      s{r} = datestr ( c, 'yyyy-mm-dd-HH:MM:SS' );
      s2 = transpose(s);
      s2 ;
    
    time(j) = toc;
    j = j + 1;
    realtime = transpose(time);
    realtime;
    
    fid = fopen('C:\Users\Foong Lik Wei\Desktop\try2.csv', 'a') ;
    
    fprintf(fid, '\n');  % Print the data values
       fprintf(fid, ',');  % Print the data values
       fprintf(fid, '%s', s2{r}) ; % Print the time string
       fprintf(fid, ',%12.3f', realtime(r));
       fprintf(fid, ',%12.3f', v1(r));  % Print the data values
       fprintf(fid, ',%12.3f', v2(r));
       fprintf(fid, ',%12.3f', v3(r));
       fprintf(fid, ',%12.3f', v4(r));
       fprintf(fid, ',%12.3f', v5(r));
       fprintf(fid, ',%12.3f', v6(r));
       fprintf(fid, ',%12.3f', v7(r));
       fprintf(fid, ',%12.3f', v8(r));
      
    fclose(fid) ;
    
    r = r + 1;  
      
    end
    
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-