Instantly share code, notes, and snippets.

Embed
What would you like to do?
NodeJS SPI Dump for MCP3008 (and Raspberry Pi)

NodeJS SPI Dump for MCP3008

This is a simple script that reads all eight analog channels of an MCP3008 each second and outputs the result to the console.

I created this script to ease debugging of the MCP3008 ADC connected to my Raspberry Pi.

If you need to troubleshoot the SPI connection in itself you can check out my guide how to test SPI through the loopback "debug mode" https://mikaelleven.wordpress.com/2015/12/10/troubleshooting-spi-on-raspberry-pi-nodejs/.

Installation

Download this gist and run the following command

npm install rpio

Usage

sudo node spi_dump.js
// NodeJS SPI Dump for MCP3008 - Created by Mikael Levén
var rpio = require('rpio');
rpio.spiBegin();
//rpio.spiChipSelect(0); /* Use CE0 (slave 0) */
//rpio.spiSetCSPolarity(0, rpio.LOW); /* Commonly chip enable (CE) pins are active low, and this is the default. */
//rpio.spiSetClockDivider(256); /* MCP3008 max is ~1MHz, 256 == 0.98MHz */
//rpio.spiSetDataMode(0);
process.stdout.write('\x1b[36m');
for (var channelHeader = 0; channelHeader <= 7; channelHeader++) {
process.stdout.write('ch' + channelHeader.toString() + (channelHeader == 7 ? '\x1b[0m\n' : '\t'));
}
setInterval(function() {
for (var channel = 0; channel <= 7; channel++) {
// Prepare TX buffer [trigger byte = 0x01] [channel 0 = 0x80 (128)] [placeholder = 0x01]
var sendBuffer = new Buffer([0x01, (8 + channel << 4), 0x01]);
var recieveBuffer = rpio.spiTransfer(sendBuffer, sendBuffer.length); // Send TX buffer and recieve RX buffer
// Extract value from output buffer. Ignore first byte.
var junk = recieveBuffer[0],
MSB = recieveBuffer[1],
LSB = recieveBuffer[2];
// Ignore first six bits of MSB, bit shift MSB 8 positions and
// finally combine LSB and MSB to get a full 10 bit value
var value = ((MSB & 3) << 8) + LSB;
process.stdout.write(value.toString() + (channel == 7 ? '\n' : '\t'));
};
}, 1000);
process.on('SIGTERM', function () {
process.exit(0);
});
process.on('SIGINT', function () {
process.exit(0);
});
process.on('exit', function () {
console.log('\nShutting down, performing GPIO cleanup');
rpio.spiEnd();
process.exit(0);
});
@nick-jonas

This comment has been minimized.

Show comment
Hide comment
@nick-jonas

nick-jonas Feb 3, 2016

I have the same connections as laid out in your article, but nothing gets printed out under the 7 channel numbers. I have two pots connected to 0 and 1 of the MCP3008 (and the chip's connected to a RPi B+). Any ideas on how to further debug this?

nick-jonas commented Feb 3, 2016

I have the same connections as laid out in your article, but nothing gets printed out under the 7 channel numbers. I have two pots connected to 0 and 1 of the MCP3008 (and the chip's connected to a RPi B+). Any ideas on how to further debug this?

@mikaelleven

This comment has been minimized.

Show comment
Hide comment
@mikaelleven

mikaelleven Feb 4, 2016

Try to explicitly set chip select, chip select polarity and clock divider (the commented lines below). You could also short circuit between ADC pin and either HIGH (+3,3V) or LOW (ground). You should get a solid reading of either 1023 or 0.

//rpio.spiChipSelect(0);                /* Chip select: use CE0 (default) */
//rpio.spiSetCSPolarity(0, rpio.LOW)    /* Commonly chip enable (CE) pins are active low, and this is the default. */
//rpio.spiSetClockDivider(256)          /* MCP3008 max is ~1MHz, 256 == 0.98MHz */
//rpio.spiSetDataMode(0);

If nothing of the above helps, could you please provide a screenshot or some more detailed information about what happens?

Owner

mikaelleven commented Feb 4, 2016

Try to explicitly set chip select, chip select polarity and clock divider (the commented lines below). You could also short circuit between ADC pin and either HIGH (+3,3V) or LOW (ground). You should get a solid reading of either 1023 or 0.

//rpio.spiChipSelect(0);                /* Chip select: use CE0 (default) */
//rpio.spiSetCSPolarity(0, rpio.LOW)    /* Commonly chip enable (CE) pins are active low, and this is the default. */
//rpio.spiSetClockDivider(256)          /* MCP3008 max is ~1MHz, 256 == 0.98MHz */
//rpio.spiSetDataMode(0);

If nothing of the above helps, could you please provide a screenshot or some more detailed information about what happens?

@nick-jonas

This comment has been minimized.

Show comment
Hide comment
@nick-jonas

nick-jonas Feb 4, 2016

I tried that, uncommenting so it was exactly the settings you have above (with the same wiring as in your article), and also using only one ADC wired to HIGH. It just outputs the following:

ch0 ch1 ch2 ch3 ch4 ch5 ch6 ch7

Shutting down, performing GPIO cleanup

Note: The 5v pin is also used to power a 2x16 character LCD screen, which is wired to the Pi via I2C. When testing the MCP30008, I've tried connected power to both 3v3 and the 5v of the Pi, both situations yielded the same result.

Here's a pic (took the ribbon out for the pic so you can see better):

img_20160204_105543

screen shot 2016-02-04 at 4 43 21 pm

nick-jonas commented Feb 4, 2016

I tried that, uncommenting so it was exactly the settings you have above (with the same wiring as in your article), and also using only one ADC wired to HIGH. It just outputs the following:

ch0 ch1 ch2 ch3 ch4 ch5 ch6 ch7

Shutting down, performing GPIO cleanup

Note: The 5v pin is also used to power a 2x16 character LCD screen, which is wired to the Pi via I2C. When testing the MCP30008, I've tried connected power to both 3v3 and the 5v of the Pi, both situations yielded the same result.

Here's a pic (took the ribbon out for the pic so you can see better):

img_20160204_105543

screen shot 2016-02-04 at 4 43 21 pm

@justin522

This comment has been minimized.

Show comment
Hide comment
@justin522

justin522 May 19, 2016

I'm having the same issue as nick-jonas. It looks like the issue is on line 20. spiTransfer now takes 3 arguments.

justin522 commented May 19, 2016

I'm having the same issue as nick-jonas. It looks like the issue is on line 20. spiTransfer now takes 3 arguments.

@stephanebachelier

This comment has been minimized.

Show comment
Hide comment
@stephanebachelier

stephanebachelier commented Sep 3, 2016

@mrfy

This comment has been minimized.

Show comment
Hide comment
@mrfy

mrfy Mar 15, 2017

Hello @nick-jonas @justin522, did you resolve this problem. I have the same.

Thanks for answer.
mrFy

mrfy commented Mar 15, 2017

Hello @nick-jonas @justin522, did you resolve this problem. I have the same.

Thanks for answer.
mrFy

@vanoorschot

This comment has been minimized.

Show comment
Hide comment
@vanoorschot

vanoorschot commented Jul 7, 2017

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment