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

No signal from Pulse Sensor #13

Closed IvanSemin33 closed 3 years ago

IvanSemin33 commented 3 years ago

Hi

We’re trying to get an information stuff using pulse sensor. We use MCP3008, Raspberry Pi 4, Pulse Sensor and mcp-spi-adc.

Our code:

//pulse-sensor.js
const mcpadc = require('mcp-spi-adc');

const pulseSensor = mcpadc.openMcp3008(1, {speedHz: 1350000, busNumber: 0, deviceNumber: 0}, err => {
  console.log("Opened")
  if (err) throw err;

  setInterval(_ => {
    pulseSensor.read((err, reading) => {
      if (err) throw err;

      console.log(reading)
      console.log(reading.value);
    });
  }, 1000);
});

We always get 0

Result:

sudo node pulse-sensor.js
Opened
{ rawValue: 0, value: 0 }
0
{ rawValue: 0, value: 0 }
0

Please help🙏

fivdi commented 3 years ago

Please post the output of the following commands:

node -v
uname -a
groups
ls -l /dev/spi*

Please post the complete contents of the file /boot/config.txt.

Please post a photo of the hardware which clearly shows the wires on the Raspberry Pi GPIO header and how they are connected to the MCP3008.

EDIT: Also, please please post a link to a description of the pulse sensor connected to channel 1 on the MCP3008.

IvanSemin33 commented 3 years ago
ivan_semin@raspberrypi:~ $ node -v
v14.15.4
ivan_semin@raspberrypi:~ $ uname -a
Linux raspberrypi 5.4.83-v7l+ #1379 SMP Mon Dec 14 13:11:54 GMT 2020 armv7l GNU/Linux
ivan_semin@raspberrypi:~ $ groups
ivan_semin sudo video lpadmin gpio
ivan_semin@raspberrypi:/ $ ls -l /dev/spi*
crw-rw---- 1 root spi 153, 0 Jan 30 22:17 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 Jan 30 22:17 /dev/spidev0.1
ivan_semin@raspberrypi:/boot $ cat config.txt 
hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=82
start_x=1
gpu_mem=128
dtparam=spi=on
fivdi commented 3 years ago
ivan_semin@raspberrypi:~ $ groups
ivan_semin sudo video lpadmin gpio

The user, in this case ivan_semin, needs to be a member of group spi in order to use SPI. Add the user to group spi and try again.

fivdi commented 3 years ago

Does it work for user pi?

IvanSemin33 commented 3 years ago

image image image image image image

fivdi commented 3 years ago

Please add user ivan_semin to group spi, this is required.

Try the following: Disconnect the sensor from channel 1 on the MCP3008 and then connect channel 1 to the 3V3 power rail on the breadboard. Then run the program. What does it output? Is rawValue approximately 1023?

fivdi commented 3 years ago

I checked the photos. It looks like everything is connected up correctly to me.

IvanSemin33 commented 3 years ago

I added the user to the group

ivan_semin@raspberrypi:~ $ groups ivan_semin 
ivan_semin : ivan_semin sudo video spi gpio lpadmin

And connect channel 0 to 3v3 image

Nothing changed(

fivdi commented 3 years ago

In the last photo the wire appears to be connected to channel 1 but the text mentions channel 0. Was everything tested correctly here?

Have you tried with user pi to see If that works?

IvanSemin33 commented 3 years ago

In the last photo the wire appears to be connected to channel 1 but the text mentions channel 0. Was everything tested correctly here?

Sorry, now it in channel 0 image

Have you tried with user pi to see If that works?

Yes I've tried but it still doesn't work

fivdi commented 3 years ago

I'm not sure what the problem is. Here is what I see when I run the program from the first post with a temperature sensor connected to channel 5.

pi@raspberrypi:~/mcp-spi-adc $ node t.js 
Opened
{ rawValue: 217, value: 0.21212121212121213 }
0.21212121212121213
{ rawValue: 217, value: 0.21212121212121213 }
0.21212121212121213
{ rawValue: 218, value: 0.2130987292277615 }
0.2130987292277615
{ rawValue: 218, value: 0.2130987292277615 }
0.2130987292277615
{ rawValue: 229, value: 0.2238514173998045 }
0.2238514173998045
...
IvanSemin33 commented 3 years ago

I'm not sure what the problem is. Here is what I see when I run the program from the first post with a temperature sensor connected to channel 5.

Are u changed channel from 1 to 5? Like this: mcpadc.openMcp3008(5, {speedHz: 1350000, busNumber: 0, deviceNumber: 0},...

fivdi commented 3 years ago

Yes

IvanSemin33 commented 3 years ago

I've tried channel 5. Still nothing works

IvanSemin33 commented 3 years ago

EDIT: Also, please please post a link to a description of the pulse sensor connected to channel 1 on the MCP3008.

https://pulsesensor.com/pages/pulsesensor-raspberrypi

fivdi commented 3 years ago

I don't think this is a software problem with the mcp-spi-adc module. It's more likely to be a hardware problem. To find out, let us try reading from the MCP3008 with gpiozero which is a simple Python interface to GPIO devices for the Raspberry Pi.

If you haven't got gpiozero installed already, please install it as described here: https://gpiozero.readthedocs.io/en/stable/installing.html

After installing gpiozero, start the Python interpreter and run the following 3 line program from the REPL:

from gpiozero import MCP3008
tmp36 = MCP3008(channel=5)
tmp36.value

Here is what I see when I run the program:

pi@raspberrypi:~ $ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from gpiozero import MCP3008
>>> tmp36 = MCP3008(channel=5)
>>> tmp36.value
0.21446018563751834
>>> 

You can simply connect channel 5 on the MCP3008 directly to 3v3. The value printed should be approximately 1.0. What value do you see if you run this Python program?

IvanSemin33 commented 3 years ago

I've connected channel 5 on the MCP3008 to 3v3

test-pins.py

from gpiozero import MCP3008

channels = [0,1,2,3,4,5,6,7]

for ch in channels:
  tmp36 = MCP3008(channel=ch)
  print(ch,tmp36.value)

Result:

(0, 0.0004885197850512668)
(1, 0.0004885197850512668)
(2, 0.0004885197850512668)
(3, 0.0004885197850512668)
(4, 0.0004885197850512668)
(5, 0.0004885197850512668)
(6, 0.0004885197850512668)
(7, 0.0004885197850512668)

EDIT: I've diconnected MCP3008 from breadboard and got the same result

fivdi commented 3 years ago

Thank you for the feedback. This means that the problem is not in mcp-spi-adc and it's not in gpiozero. The problem is somewhere else. As mentioned above, I think it's a hardware problem, here are things that comes to mind:

Maybe the Linux SPI device driver is defect.

You'll need to perform further troubleshooting to figure out what the source of the problem is.

IvanSemin33 commented 3 years ago

thanks for ur help 🙏

IvanSemin33 commented 3 years ago

Why do I get these results while I have everything disconnected from my raspberry pi?

Code:

from gpiozero import MCP3008

channels = [0,1,2,3,4,5,6,7]

for ch in channels:
  tmp36 = MCP3008(channel=ch)
  print(ch,tmp36.value)

Result:

(0, 0.0004885197850512668)
(1, 0.0004885197850512668)
(2, 0.0004885197850512668)
(3, 0.0004885197850512668)
(4, 0.0004885197850512668)
(5, 0.0004885197850512668)
(6, 0.0004885197850512668)
(7, 0.0004885197850512668)
fivdi commented 3 years ago

This is how SPI works. The Linux SPI driver can't determine whether or not an SPI device is actually connected to the SPI bus. It simply shifts data out on the MOSI pin and shifts data in on the MISO pin. It also generates the appropriate clock signal.

This is unlike I2C where the driver can detect whether or not a device is actually connected to the I2C bus.

fivdi commented 3 years ago

You could perform an SPI loopback test to see if SPI on the Raspberry Pi is functioning correctly. A description of how to do this can be found at: https://importgeek.wordpress.com/2017/09/11/raspberry-pi-spi-loopback-testing/

This doesn't test the chip select or clock pins, but it should let you know if MOSI and MISO on the Pi are working.

You should disconnect the MCP3008 for the loopback test.