Home

Awesome

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.

<img src="https://raw.githubusercontent.com/fivdi/mcp-spi-adc/master/example/pi-mcp3008-tmp36.png">
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

DeviceChannelsChannel NumbersDefault Clock FrequencyResolutionRaw Value Range
MCP300220-11200000Hz10-bit0-1023
MCP300440-31350000Hz10-bit0-1023
MCP300880-71350000Hz10-bit0-1023
MCP320110800000Hz12-bit0-4095
MCP320220-1900000Hz12-bit0-4095
MCP320440-31000000Hz12-bit0-4095
MCP320880-71000000Hz12-bit0-4095
MCP330480-71050000Hz13-bit0-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)

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:

adcChannel.read(cb)

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

Returns this.

adcChannel.close(cb)

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