• note for speed

    Will Whang03/23/2016 at 09:27 0 comments

    So for this project useful, I needs to have at least ~10 to 20 Khz of sampling speed.

    for the Arduino API, using analogRead() like this

    Serial.println(analogRead(A2)*2.048/51150.0/USB_resistor*1000.0;);
    gives me about ~600us per sample.

    to improve this, first of all is to speed up ADC clock, by changing the devider to 16, I have 1Mhz for ADC, thus 77khz of sample rate.(13 clock per sample)

    but now it gives about 500us per sample, so not only the ADC speed is important, there must be something slow down the code.

    and later, I found that the performance neck is converting from float to char.

    also note that in Arduino leonardo the USB endpoint of USB CDC is 64byte, so I send 12 samples in one buffer to maximize the usage.

    and I decide to leave the conversion to my computer, and modified my code to

    char data[60] = {0};
    for(int i=0;i<12;i++){
                int usb = analogRead(A2);
                char buf[4] = {0};
                itoa(usb,buf,10);
                memcpy(data+5*i,buf,4);
                data[4+5*i] = '\n';
    }
    Serial.write(( uint8_t*)data,60);
    which gives me about 45us per sample

    last but not least, because I know the ADC port I want to sample,

    I can bypass Arduino API and speed up a little bit, and I change my code to

    ADMUX =  (0 << 6) | (pin & 0x07);
    char data[60] = {0};
    for(int i=0;i<12;i++){
                ADCSRA = ADCSRA | (1<<ADSC);
                while (ADCSRA &  (1<<ADSC));
                uint8_t  low  = ADCL;
                uint8_t high = ADCH;
                uint16_t usb = ((high << 8) | low);
    
                char buf[4] = {0};
                itoa(usb,buf,10);
                memcpy(data+5*i,buf,4);
                data[4+5*i] = '\n';
    }
    Serial.write(( uint8_t*)data,60);

    finally, it gives me 35us per sample, which is about 28khz sampling rate.

  • Arduino IDE built in Serial plotter test

    Will Whang03/23/2016 at 04:49 0 comments

    After Arduino 1.6.7, Arduino LLC adds a new feature that can automatically plot the data from Serial using CSV format, so after finishing the hardware, the first test is using Serial plotter to check the data, and the first thing to test is my brand new Raspberry Pi 3, I'm quite curious about the wifi power peak looks like.

    and this is the result

    as you can see, the peak is about 400mA,but because of the limit of Serial plotter, I can't measure how long it takes to transmit wifi signal.

    I've also tested current under CPU stress test, using sysbench.

    not sure why there is a peak to 800mA.