MCP cover image
See in Github
2021-10-17

MCP3002/4/8,MCP3202/4/8和MCP3304 SPI类似于Node.js的数字转换

5

Github Watches

10

Github Forks

62

Github Stars

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" alt="MCP-SPI-ADC image">

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.

相关推荐

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • Contraband Interactive
  • Emulating Dr. Jordan B. Peterson's style in providing life advice and insights.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • rustassistant.com
  • Your go-to expert in the Rust ecosystem, specializing in precise code interpretation, up-to-date crate version checking, and in-depth source code analysis. I offer accurate, context-aware insights for all your Rust programming questions.

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • lumpenspace
  • Take an adjectivised noun, and create images making it progressively more adjective!

  • apappascs
  • 发现市场上最全面,最新的MCP服务器集合。该存储库充当集中式枢纽,提供了广泛的开源和专有MCP服务器目录,并提供功能,文档链接和贡献者。

  • ShrimpingIt
  • MCP系列GPIO Expander的基于Micropython I2C的操作,源自ADAFRUIT_MCP230XX

  • Mintplex-Labs
  • 带有内置抹布,AI代理,无代理构建器,MCP兼容性等的多合一桌面和Docker AI应用程序。

  • ravitemer
  • 一个功能强大的Neovim插件,用于管理MCP(模型上下文协议)服务器

  • jae-jae
  • MCP服务器使用剧作《无头浏览器》获取网页内容。

  • patruff
  • Ollama和MCP服务器之间的桥梁,使本地LLMS可以使用模型上下文协议工具

  • n8n-io
  • 具有本机AI功能的公平代码工作流程自动化平台。将视觉构建与自定义代码,自宿主或云相结合,400+集成。

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • pontusab
  • 光标与风浪冲浪社区,查找规则和MCP

    Reviews

    4 (1)
    Avatar
    user_ZNcEeOOa
    2025-04-17

    I've been using the mcp-spi-adc from fivdi and it has been a game changer for my projects! The integration was seamless and the performance exceeded my expectations. It's evident that a lot of thought and expertise went into this package. Highly recommend it for anyone needing ADC capabilities in their SPI applications! Check it out here: https://github.com/fivdi/mcp-spi-adc