Giter Club home page Giter Club logo

mcp-spi-adc's Introduction

Build Status npm Version Downloads Per Month

mcp-spi-adc

MCP3002/4/8, MCP3201/2/4/8 and MCP3304 SPI analog to digital conversion with Node.js on Linux boards like the Raspberry Pi or BeagleBone.

mcp-spi-adc supports Node.js versions 10, 12, 14, 15 and 16.

Contents

Installation

npm install mcp-spi-adc

In order to use mcp-spi-adc SPI must be enabled. How SPI is enabled varies from board to board. For example, on the Raspberry Pi the raspi-config tool can be used enable SPI. On the BeagleBone Black the config-pin utility can be used.

Usage

Determine the temperature using a TMP36 analog temperature sensor wired to channel 5 on an MCP3008 SPI A/D converter.

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

const tempSensor = mcpadc.open(5, {speedHz: 20000}, err => {
  if (err) throw err;

  setInterval(_ => {
    tempSensor.read((err, reading) => {
      if (err) throw err;

      console.log((reading.value * 3.3 - 0.5) * 100);
    });
  }, 1000);
});

Note how the optional configuration option speedHz is used to configure the SPI clock frequency in Hertz for reading the value from the TMP36 temperature sensor. The default SPI clock frequency for the MCP3008 is 1350000Hz but lowering it to 20000Hz gives a more acurate temperature reading. In general, it's not necessary to lower the clock speed to read a value.

The default clock speed of 1350000Hz for the MCP3008 is derived from the MCP3008 datasheet. The maximum sampling rate at VDD = 2.7V is 75 ksps and each sample requires an 18-bit transfer. 75000 x 18 = 1350000. 1350000Hz is a conservative frequency in the above circuit as VDD is 3.3V.

Supported Devices

Device Channels Channel Numbers Default Clock Frequency Resolution Raw Value Range
MCP3002 2 0-1 1200000Hz 10-bit 0-1023
MCP3004 4 0-3 1350000Hz 10-bit 0-1023
MCP3008 8 0-7 1350000Hz 10-bit 0-1023
MCP3201 1 0 800000Hz 12-bit 0-4095
MCP3202 2 0-1 900000Hz 12-bit 0-4095
MCP3204 4 0-3 1000000Hz 12-bit 0-4095
MCP3208 8 0-7 1000000Hz 12-bit 0-4095
MCP3304 8 0-7 1050000Hz 13-bit 0-4095

API Documentation

All methods are asynchronous and take a completion callback as their last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined.

Functions

Class AdcChannel

openMcp3002(channel[, options], cb)

openMcp3004(channel[, options], cb)

openMcp3008(channel[, options], cb)

openMcp3201(channel[, options], cb)

openMcp3202(channel[, options], cb)

openMcp3204(channel[, options], cb)

openMcp3208(channel[, options], cb)

openMcp3304(channel[, options], cb)

open(channel[, options], cb) - alias for openMcp3008(channel[, options], cb)

  • channel - the number of the channel to open, see channel numbers in supported devices
  • options - an optional object specifying channel configuration options
  • cb - completion callback

Asynchronous open. Returns a new AdcChannel object. The completion callback gets one argument (err). The AdcChannel object returned should not be used before the completion callback is called.

The following channel configuration options are supported:

  • busNumber - the SPI bus number, 0 for /dev/spidev0.n, 1 for /dev/spidev1.n, ..., default 0
  • deviceNumber - the SPI device number, 0 for /dev/spidevn.0, 1 for /dev/spidevn.1, ..., default 0
  • speedHz - a number representing the SPI clock frequency for reading from the channel in Hertz, see default clock frequency in supported devices

adcChannel.read(cb)

  • cb - completion callback

Asynchronous read. The completion callback gets two arguments (err, reading). The reading argument is an object with the following properties:

  • value - the value read from the channel scaled to a value between 0 and 1
  • rawValue - the value read from the channel, see raw value range in supported devices

Returns this.

adcChannel.close(cb)

  • cb - completion callback

Asynchronous close. Frees system resources used by this instance. The completion callback gets one argument (err). Returns null.

mcp-spi-adc's People

Contributors

fivdi avatar rxseger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mcp-spi-adc's Issues

Error: ENOENT, No such file or directory

I'm trying to get the MCP3008 to work on my Raspberry Pi 4. I'm getting a fatal error as below. Any ideas?

/home/pi/webserver.js:152
if (err) throw err;
^
Error: ENOENT, No such file or directory

Line 152 is in this bit of code:

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

const tempSensor = mcpadc.open(5, {speedHz: 20000}, err => {
    if (err) throw err;

The installation of the package went like this:

npm install mcp-spi-adc
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No repository field.

+ [email protected]
updated 1 package and audited 140 packages in 2.536s

Support for TI ADS1256

Hi,
Would you consider a pull request adding support for the ADS1256 from TI?
It's a 24-bit SPI ADC available on a commodity shield called Waveshare High Precision AD/DA.
Proper handling would require multiple SPI transactions and individual pin management (reset, data ready etc).
Additional features are PGA, differential readings etc.
However, basic handling seems to work with minimal changes on top of your current implementation.
Would you take such changes into consideration?

Max sampling rate?

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?

Typescript interface?

Is it possible to have the type script interface for this library?
This would be great.
Thanks

MCP3208 differential mode

Is it possible to specify differential mode for any of the channels? This feature is available in the chip. If not, how about adding it?

Thanks,
Tom

Error Message

Hello i get the Error Message

`

javascript.0 2022-08-26 20:22:10.636 info found 0 vulnerabilities
javascript.0 2022-08-26 20:22:10.629 info 7 packages are looking for funding run npm fund for details
javascript.0 2022-08-26 20:22:10.624 info added 99 packages, changed 1 package, and audited 101 packages in 38s
javascript.0 2022-08-26 20:21:40.823 error WARN deprecated [email protected]: request has been deprecated, see request/request#3142
javascript.0 2022-08-26 20:21:40.820 error npm
javascript.0 2022-08-26 20:21:40.711 error WARN deprecated [email protected]: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
javascript.0 2022-08-26 20:21:40.710 error npm
javascript.0 2022-08-26 20:21:40.609 error WARN deprecated [email protected]: This module is part of Node.js core and does not need to be installed separately. It is now unmaintained.
javascript.0 2022-08-26 20:21:40.607 error npm
javascript.0 2022-08-26 20:21:40.451 error WARN deprecated [email protected]: this library is no longer supported
javascript.0 2022-08-26 20:21:40.449 error npm
javascript.0 2022-08-26 20:21:31.295 info npm install mcp-spi-adc --omit=dev (System call)
`

And when i try this Script the javascript Instanz crash in IoBroker

`createState('Analogmodul.Channel0',0,read1);
createState('Analogmodul.Channel1',0,read2);
createState('Analogmodul.Channel2',0,read3);
createState('Analogmodul.Channel3',0,read4);

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

function read1(){
const input1 = mcpadc.openMcp3204(0, {busNumber: busNumber, deviceNumber:deviceNumber, speedHz: 20000}, (err) => {
if (err) throw err;

setInterval(() => {
input1.read((err, reading) => {
if (err) throw err;

  setState("Analogmodul.Channel0",(reading.rawValue)),true;
});

}, Interval);
});
}

function read2(){
const input2 = mcpadc.openMcp3204(1, {busNumber: busNumber, deviceNumber:deviceNumber, speedHz: 20000}, (err) => {
if (err) throw err;

setInterval(() => {
input2.read((err, reading) => {
if (err) throw err;

  setState("Analogmodul.Channel1",(reading.rawValue)),true;
});

}, Interval);
});
}

function read3(){
const input3 = mcpadc.openMcp3204(2, {busNumber: busNumber, deviceNumber:deviceNumber, speedHz: 20000}, (err) => {
if (err) throw err;

setInterval(() => {
input3.read((err, reading) => {
if (err) throw err;

  setState("Analogmodul.Channel2",(reading.rawValue)),true;
});

}, Interval);
});
}

function read4(){
const input4 = mcpadc.openMcp3204(3, {busNumber: busNumber, deviceNumber:deviceNumber, speedHz: 20000}, (err) => {
if (err) throw err;`

Has someone an Idea?

Thanks in advance

No signal from Pulse Sensor

Hi

We’re trying to get an information stuff using pulse sensor.
We use MCP3008, Raspberry Pi 4, Pulse Sensor and mcp-spi-adc.

Our code:

//pulse-sensor.js
const mcpadc = require('mcp-spi-adc');

const pulseSensor = mcpadc.openMcp3008(1, {speedHz: 1350000, busNumber: 0, deviceNumber: 0}, err => {
  console.log("Opened")
  if (err) throw err;

  setInterval(_ => {
    pulseSensor.read((err, reading) => {
      if (err) throw err;

      console.log(reading)
      console.log(reading.value);
    });
  }, 1000);
});

We always get 0

Result:

sudo node pulse-sensor.js
Opened
{ rawValue: 0, value: 0 }
0
{ rawValue: 0, value: 0 }
0

Please help🙏

Troubleshooting basic setup

I'm trying to read analog signals with my Raspberry Pi 4 and Node js, and I'm following the instructions to get your module working

It seems like I'm almost there, but I wonder if you could help me with some final troubleshooting.

I have everything connected the same way as in your diagram, and I've enabled SPI on my Raspberry Pi and restarted it. I have an Express server running on the Pi, and running your sample code in there. It seems to be running fine, and it console.logs out the value every second. In my case, because I'm not using a temperature sensor I took out the math. I just log the raw value and I'll do the math later.

I expect that when I connect the 3.3V rail to pin 5 on the MCP3008, the value that the Pi logs should be 1023, right? But no matter what I try, it always outputs 0. I've tried connecting it to each of the input pins on the MCP one at a time in case I was mistaken about which pin I was using, but it always outputs 0. I also checked that I do have 3.3V on that rail, and it's being supplied to pins 15 and 16. I've double-checked all of the connections too.

One thing that I don't understand is that the Pi doesn't throw an error when I disconnect the MCP from it. I'd have thought that if it couldn't communicate with the MCP it would throw an error?

Do you have any suggestions about what steps I could take to troubleshoot it? I'm very stumped and I can't find any helpful resources about it on the internet. Thanks in advance for any help.

Here is the code I'm running, which is pretty much identical to yours except the math that operates on the value is removed:

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

const sensor = mcpadc.open(0, {speedHz: 20000}, err => {
  if (err) throw err;

  setInterval(_ => {
    sensor.read((err, reading) => {
      if (err) throw err;

      console.log(reading.value);
    });
  }, 1000);
});

Error: EPERM, device closed, operation not permitted

Hi. I try to read one photocell / sensor with MCP3008. Got this error code:

{ Error: ENOENT, No such file or directory errno: 2, code: 'ENOENT', syscall: 'open' }
/home/pi/tests/1/node_modules/mcp-spi-adc/mcp-spi-adc.js:109
  this._device.transfer(message, function (err, message) {
               ^

Error: EPERM, device closed, operation not permitted
    at AdcChannel.read (/home/pi/tests/1/node_modules/mcp-spi-adc/mcp-spi-adc.js:109:16)
    at Timeout._onTimeout (/home/pi/tests/1/index.js:32:15)
    at ontimeout (timers.js:458:11)
    at tryOnTimeout (timers.js:296:5)
    at Timer.listOnTimeout (timers.js:259:5)

My code:

var PhotoCell = mcpadc.open(5, {speedHz: 20000}, function (err) {
  if (err) console.log(err);

  setInterval(function () {
    PhotoCell.read(function (err, reading) {
      if (err) console.log(err);

      console.log((reading.value * 3.3 - 0.5) * 100);
    });
  }, 1000);
});

Wiring is ok - just like in readme. Node v9.5.0. Need some help please.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.