Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mcp-spi-adc

fivdi136MIT3.2.0

MCP3002/4/8, MCP3202/4/8 and MCP3304 SPI analog to digital conversion

mcp3008, mcp3004, mcp3002, mcp3208, mcp3204, mcp3202, mcp3304, spi, adc, iot, raspberry, raspi, rpi, pi, beaglebone, linux

readme

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.

changelog

3.2.0 / Oct 17 2021

  • add mcp3201 support

3.1.1 / Apr 30 2021

  • drop support for node.js 8 and 13, add support for node.js 15 and 16
  • update dependencies

3.1.0 / Apr 25 2020

  • update dependencies (spi-device v3.1.0, jshint v2.11.0)
  • drop support for node.js 6, add support for node.js 14

3.0.0 / Sep 22 2019

  • update dependencies (spi-device v3.0.0)
  • drop support for node.js v4

2.0.7 / Sep 07 2019

  • update dependencies (spi-device v2.0.9)

2.0.6 / Jun 16 2019

  • remove node 11 from build
  • update dependencies
  • update npm keywords

2.0.5 / Mar 15 2019

  • update dependencies (spi-device v2.0.7)
  • document node 12 support
  • lint with jshint
  • add travis build
  • add badges

2.0.4 / Oct 02 2018

  • update dependencies (spi-device v2.0.4)

2.0.3 / Jul 28 2018

  • update dependencies (spi-device v2.0.3)
  • code style

2.0.2 / Apr 15 2018

  • update dependencies (spi-device v2.0.2, lodash.clone v4.5.0)
  • code style
  • add test to call open, read and close for all supported chips

2.0.1 / Apr 07 2018

  • update dependencies (spi-device v2.0.1)

2.0.0 / Feb 25 2018

  • document node 9 support
  • update dependencies (spi-device v2.0.0)
  • drop support for node.js v0.10, v0.12, v5 and v7

1.0.0 / Oct 14 2017

  • update dependencies (spi-device v1.0.0)
  • document supported node versions

0.4.0 / Jan 05 2017

  • MCP3304 support

0.3.1 / Oct 03 2016

  • bugfix: the mcp3002 has 2 channels not 8

0.3.0 / Oct 02 2016

  • MCP3002 support
  • MCP3202 support

0.2.1 / Oct 02 2016

  • documentation

0.2.0 / Oct 02 2016

  • MCP3004 support
  • MCP3204 support

0.1.0 / Oct 02 2016

  • MCP3208 support

0.0.1 / May 31 2016

  • documentation

0.0.0 / May 28 2016

  • initial release