fivdi / mcp-spi-adc

MCP3002/4/8, MCP3202/4/8 and MCP3304 SPI analog to digital conversion with Node.js
MIT License
62 stars 10 forks source link

Max sampling rate? #9

Closed ZaneL closed 4 years ago

ZaneL commented 4 years ago

Just wondering if you've tested to see what the actual maximum achievable sampling rate is with this library?

With the overhead of nodejs I'm wondering if it's possible to get 5k samples/sec on a raspberry pi?

fivdi commented 4 years ago

The max. sampling rate will depend on many factors, for example:

The below program samples the voltage on channel 4 of an MCP3008 and prints the number of samples per second.

const mcpadc = require('mcp-spi-adc');
const assert = require('assert');

let voltageReading;
let samplesPerSec = 0;

const voltageSensor = mcpadc.open(4, err => {
  assert(!err, 'can\'t open voltage sensor');

  const next = _ => {
    voltageSensor.read((err, reading) => {
      assert(!err, 'can\'t read voltage sensor');
      voltageReading = reading;
      samplesPerSec += 1;
      next();
    });
  };

  next();
});

setInterval(_ => {
  console.log(
    'samples per sec: ' + samplesPerSec +
    ', v: ' + (voltageReading.value * 3.3)
  );

  samplesPerSec = 0;
}, 1000);

If this program is run using Node 14 on a Raspberry Pi 2 running Linux raspberrypi 4.19.57-v7+ with no additional system load it prints the following:

samples per sec: 3667, v: 1.6580645161290322
samples per sec: 3935, v: 1.6548387096774193
samples per sec: 4207, v: 1.6580645161290322
samples per sec: 4178, v: 1.661290322580645
samples per sec: 4168, v: 1.661290322580645
samples per sec: 4307, v: 1.661290322580645
samples per sec: 4481, v: 1.661290322580645
samples per sec: 4459, v: 1.661290322580645
samples per sec: 4390, v: 1.6580645161290322
samples per sec: 4319, v: 1.6580645161290322
samples per sec: 4021, v: 1.6580645161290322
samples per sec: 4226, v: 1.6580645161290322
samples per sec: 4198, v: 1.6580645161290322
samples per sec: 4255, v: 1.6580645161290322
samples per sec: 4404, v: 1.6580645161290322
samples per sec: 4429, v: 1.6580645161290322
samples per sec: 4443, v: 1.6580645161290322
samples per sec: 4402, v: 1.6580645161290322
samples per sec: 4478, v: 1.6580645161290322
samples per sec: 4480, v: 1.6580645161290322
samples per sec: 4481, v: 1.6580645161290322
samples per sec: 4432, v: 1.6548387096774193
samples per sec: 4472, v: 1.6580645161290322
samples per sec: 4473, v: 1.6580645161290322
samples per sec: 4424, v: 1.6580645161290322
samples per sec: 4470, v: 1.6548387096774193
samples per sec: 4477, v: 1.6548387096774193
samples per sec: 4399, v: 1.6548387096774193
samples per sec: 4465, v: 1.6548387096774193
samples per sec: 4467, v: 1.6548387096774193
samples per sec: 4471, v: 1.6580645161290322
samples per sec: 4427, v: 1.6548387096774193
samples per sec: 4473, v: 1.6548387096774193
samples per sec: 4463, v: 1.6580645161290322
samples per sec: 4430, v: 1.6580645161290322
samples per sec: 4486, v: 1.6548387096774193
samples per sec: 4549, v: 1.6580645161290322
samples per sec: 4495, v: 1.6580645161290322
samples per sec: 4548, v: 1.6580645161290322
samples per sec: 4542, v: 1.6548387096774193
samples per sec: 4514, v: 1.6580645161290322
samples per sec: 4545, v: 1.6580645161290322
samples per sec: 4545, v: 1.6580645161290322
samples per sec: 4432, v: 1.6580645161290322
samples per sec: 4468, v: 1.6580645161290322
samples per sec: 4474, v: 1.6580645161290322
samples per sec: 4475, v: 1.6580645161290322
...

Give the program a try on your system to see what's possible.

Note that Linux is not a real time operating system so there are no guarantees about timing.

ZaneL commented 4 years ago

Thank you so much!!

meta-meta commented 2 years ago

Any ideas how to get a faster sampling rate? I'm on a Pi Zero W, reading 6 pins and getting maybe 600 samples per second. I don't necessarily need all 10 bits if that makes a difference. I'm trying to get a digital reading from IR sensors which I can do with GPIO but being able to decide 0|1 with a threshold in software is valuable.