fivdi
Posts: 192
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

MCP3008 SPI A/D Converter with Node.js

Sat May 28, 2016 7:38 pm

There's a new Node.js package called mcp-spi-adc for analog-to-digital conversion using the MCP3008 SPI A/D converter. It's quite good (even if I might say so myself :)) and can also handle tricky devices like the TMP36 analog temperature sensor. It's based on the equally new low-level SPI package called spi-device which handles all the low-level SPI details.

mcp-spi-adc can be found at https://github.com/fivdi/mcp-spi-adc
spi-device can be found at https://github.com/fivdi/spi-device

Both packages support all versions of Node.js from v0.10.29 upto and including the latest version which is currently v6.2.0.

Other Node.js packages that you may find interesting are:

i2c-bus for low-level I2C at https://github.com/fivdi/i2c-bus
pigpio for fairly high-level GPIO, PWM, servo control, interrupt handling, alerts and more at https://github.com/fivdi/pigpio (this is a Node.js wrapper for Joan's pigpio C library)

Heater
Posts: 8131
Joined: Tue Jul 17, 2012 3:02 pm

Re: MCP3008 SPI A/D Converter with Node.js

Sun May 29, 2016 1:50 am

fivdi,

That is all good stuff. Thanks for the heads up.

fivdi
Posts: 192
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: MCP3008 SPI A/D Converter with Node.js

Sun May 29, 2016 10:08 am

Thanks Heater.

There's probably at least a dozen similar SPI A/D converters like the MCP3004, MCP3204, and MCP3208. mcp-spi-adc could easily be extended to support these converts.

Feel free to create an issue at the mcp-spi-adc github repository if you would like support added for additional MCPnnn A/D converters.

thadg
Posts: 5
Joined: Fri Aug 11, 2017 7:10 pm

Re: MCP41xxx SPI digital potentiometer with Node.js

Sun Oct 22, 2017 9:32 pm

Hi fivdi,

Thanks for offering your module.

I am trying to blink an LED off for 5 seconds using a MCP41xxx with a Pi Zero.
I do not know if this is the proper place to request help. If you think this request should be posted elsewhere,
please let me know.

Anyway:
The circuit is below the code.

The "spi.open" works as if i change:
sendBuffer: new Buffer([0x11, 0xFF]), // Initially turn on LED
to:
sendBuffer: new Buffer([0x11, 0x00]), // Initially turn off LED
the LED turns off.

However, the "mcp41050.transfer" block may be set up incorrectly.
Not sure if : message[0].sendBuffer[1] = 0x00; is the right way to set the register.

The result when the code runs is:
"LED is initially on with control value of 17 and a value of 255
LED is now shut off for 5 seconds with a control value of 17 and a value of 0
turn LED back on with control value of 17 and a value of 255"
BUT the LED does not shut off at all.

I am baffled. Any suggestions would be helpful.
thanks, Thad

Code: Select all

'use strict';

var spi = require('spi-device'),
  mcp41050;

// Drive the digital potentiometer with values
// initially turn on (FF), turn off (00) LED, wait for 5 seconds and turn on (FF) the LED

// The MCP41050 is coupled to the SPI interface of the Pi Zero
mcp41050 = spi.open(0, 0, function (err) {
  // An SPI message is an array of one or more read+write transfers
  var message = [{
    sendBuffer: new Buffer([0x11, 0xFF]), // Initially turn on LED
    byteLength: 2,
    speedHz: 61000 // Use a low bus speed to set values
  }];
  
  if (err) throw err;
  
  console.log('LED is initially on with control value of ' + message[0].sendBuffer[0] + ' and a value of ' + message[0].sendBuffer[1]);

  mcp41050.transfer(message, function (err, message) {

	
function shutoff() {
    // shut off LED
	message[0].sendBuffer[0] = 0x11;
    	message[0].sendBuffer[1] = 0x00;
	console.log('LED is now shut off for 5 seconds with a control value of ' + message[0].sendBuffer[0] + ' and a value of ' + message[0].sendBuffer[1]);
}

function turnon() {
    	// turn on LED
	message[0].sendBuffer[0] = 0x11;
    	message[0].sendBuffer[1] = 0xFF;
	console.log('turn LED back on with control value of ' + message[0].sendBuffer[0] + ' and a value of ' + message[0].sendBuffer[1]);
}

// call the first chunk of code right away
shutoff();

// call the rest of the code and have it execute after 5 seconds
setTimeout(turnon, 5000);
	
	    if (err) throw err;
		
  });
Image
Attachments
mcp41050_schem.png
mcp41050_schem.png (87.81 KiB) Viewed 791 times

fivdi
Posts: 192
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: MCP3008 SPI A/D Converter with Node.js

Sat Oct 28, 2017 5:34 am

If I understand correctly, the goal is to blink the LED forever but the program only turns the LED on, then off, then on and then stops. Is this correct? If so, please try the following to see if it works:

Code: Select all

'use strict';

var spi = require('spi-device'),
  mcp41050;

// The MCP41050 is coupled to the SPI interface of the Pi Zero
mcp41050 = spi.open(0, 0, function (err) {
  // An SPI message is an array of one or more read+write transfers
  var message = [{
    sendBuffer: new Buffer([0x11, 0xFF]),
    byteLength: 2,
    speedHz: 61000 // Use a low bus speed to set values (is this really needed?)
  }];

  if (err) throw err;

  function blinkLed() {
    // Turn LED on.
    message[0].sendBuffer[1] = 0xff;
    mcp41050.transfer(message, function (err, message) {
      if (err) throw err;

      setTimeout(function() {
        // Turn LED off.
        message[0].sendBuffer[1] = 0x00;
        mcp41050.transfer(message, function (err, message) {
          if (err) throw err;

          setTimeout(blinkLed, 5000);
        });
      }, 5000);
    });
  };

  blinkLed();
});
Note that I didn't test this code.

thadg
Posts: 5
Joined: Fri Aug 11, 2017 7:10 pm

Re: MCP3008 SPI A/D Converter with Node.js

Mon Oct 30, 2017 11:17 am

Hi fivdi,

Thanks for providing the working code. This is what I was looking for.

thadg

fivdi
Posts: 192
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: MCP3008 SPI A/D Converter with Node.js

Mon Oct 30, 2017 8:09 pm

thadg wrote:
Mon Oct 30, 2017 11:17 am
Thanks for providing the working code. This is what I was looking for.
You're welcome ;)

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 4 guests