fivdi / mcp-spi-adc

MCP3002/4/8, MCP3202/4/8 and MCP3304 SPI analog to digital conversion with Node.js
MIT License
62 stars 10 forks source link

Error: EPERM, device closed, operation not permitted #2

Closed thrazu closed 6 years ago

thrazu commented 6 years ago

Hi. I try to read one photocell / sensor with MCP3008. Got this error code:

{ Error: ENOENT, No such file or directory errno: 2, code: 'ENOENT', syscall: 'open' }
/home/pi/tests/1/node_modules/mcp-spi-adc/mcp-spi-adc.js:109
  this._device.transfer(message, function (err, message) {
               ^

Error: EPERM, device closed, operation not permitted
    at AdcChannel.read (/home/pi/tests/1/node_modules/mcp-spi-adc/mcp-spi-adc.js:109:16)
    at Timeout._onTimeout (/home/pi/tests/1/index.js:32:15)
    at ontimeout (timers.js:458:11)
    at tryOnTimeout (timers.js:296:5)
    at Timer.listOnTimeout (timers.js:259:5)

My code:

var PhotoCell = mcpadc.open(5, {speedHz: 20000}, function (err) {
  if (err) console.log(err);

  setInterval(function () {
    PhotoCell.read(function (err, reading) {
      if (err) console.log(err);

      console.log((reading.value * 3.3 - 0.5) * 100);
    });
  }, 1000);
});

Wiring is ok - just like in readme. Node v9.5.0. Need some help please.

fivdi commented 6 years ago

It looks like SPI hasn't been enabled. If a Raspberry Pi is being used, which appears to be the case, then SPI can be enabled with the raspi-config tool which can be called from the command line with the following command:

sudo raspi-config

Navigate to "5 Interfacing Options >> P4 SPI" and answer the question "Would you like the SPI interface to be enabled?" with "Yes", then answer "Ok", then answer "Finish".

If a fairly recent version Raspbian is being used a reboot should not be necessary. On an older version of Raspbian a reboot will be necessary.

After SPI has been enabled it should work.

This should be mentioned in the README.

thrazu commented 6 years ago

Thanks for your straight answer! I have enabled SPI through

sudo raspi-config

command and everything works great now.

fivdi commented 6 years ago

That's good news 😄

I'll leave this issue open as a reminder that a note should be added to the README saying that SPI needs to be enabled before it can work.

One additional point, your program contains the following line of code:

console.log((reading.value * 3.3 - 0.5) * 100);

The formula (reading.value * 3.3 - 0.5) * 100 is for a TMP36 temperature sensor so it will need to be changed for a photocell.

thrazu commented 6 years ago

Thanks for your advice. I will adapt the formula to work with the photocell.

fivdi commented 6 years ago

The README was updated mentioning that SPI must be enabled to use mcp-spi-adc. See 33ed477e613987bf3f31df5b0aadcac3834a8692.

amcelroy commented 4 years ago

Sorry to re-open this, but I am using an MCP3004 and am getting this when trying to read channels 0, 1, and 2. When reading channel 0 and 1, there are no issues, but reading all 3 produces this error.

fivdi commented 4 years ago

@amcelroy I can't reproduce the error. Please open a new issue containing the following information: