Continuing the work with analog sensors on the Raspberry Pi, I decided to test the PulseSensor‘s behavior in building a pulse sensing application. The Pulse Sensor is a well-designed plug-and-play heart-rate sensor for Arduino but considering that it’s a simple analog sensor that sends values between 0 and 1023, it’s easy enough to use with the MCP3008 ADC on the RPi.
Here’s the python code that can be used to make a simple pulse sensing application:
#!/usr/bin/env python import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) def readadc(adcnum, clockpin, mosipin, misopin, cspin): if ( (adcnum > 7) or (adcnum < 0) ): return -1 GPIO.output(cspin, True) GPIO.output(clockpin, False) # start clock low GPIO.output(cspin, False) # bring CS low commandout = adcnum commandout |= 0x18 # start bit + single-ended bit commandout <<= 3 # we only need to send 5 bits here for i in range(5): if (commandout & 0x80): GPIO.output(mosipin, True) else: GPIO.output(mosipin, False) commandout <<= 1 GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout = 0 # read in one empty bit, one null bit and 10 ADC bits for i in range(12): GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout <<= 1 if (GPIO.input(misopin)): adcout |= 0x1 GPIO.output(cspin, True) adcout >>= 1 # first bit is 'null' so drop it return adcout # change these as desired - they're the pins connected from the # SPI port on the ADC to the Raspberry Pi</h1> SPICLK = 18 SPIMISO = 23 SPIMOSI = 24 SPICS = 25 GPIO.setwarnings(False) #set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) #pulse sensor connected to adc #0 pulse_adc = 0 #Threshold for pulse sensing (half of values between 0-1023) THRESH = 512 #pulse detection pulse = False while True: #read the analog pin analog_value = readadc(pulse_adc, SPICLK, SPIMOSI, SPIMISO, SPICS) #draw the equivalent number of points in an attempt to draw a vertical pulse sensing graph for i in range(analog_value / 100): print ".", #detect beats if (analog_value > THRESH): if (pulse == False): pulse = True print "Beat" else: print "" else: pulse = False print "" #hang out and do nothing for a tenth of a second time.sleep(0.1)
The application simply processes the values received in the ADC’s channel #0 and if the value is greater than a certain threshold (in this case 512, considering that is half of 1024 values) it senses a beat. It also ignores subsequent beats above that threshold and it only considers a new beat once the values go beneath the threshold again.
Running this code should produce a similar output to this (depending on your heart beat):
. . . . . . . . . . . . . . Beat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Beat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Beat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Beat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Beat . . . . . . . . . . . . . . . . . . . . . . . .
You can also create a more interesting application by having the RPi send the raw pulse sensor data through some sort of channel (web server, sockets, serial, etc…) and graphically present the results on another computer.
Comments
Hi Antonio!
I’m having dificulties with your code since there are no tabs or spaces in them. I can’t seem to run it as .py-file. Is there an easy fix? Because I can’t seem to figure out all the right tab positions (especially the ‘return adcout’ on line 39 is a sum’ bitch to get right). So if you have the code somewhere wíth tabs it would be amazing!
Hello there Robbert,
this is the script that I based mine on: https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/script
It should give you the necessary information.
Cheers
No indentation in code,so hard to understand
Hello there,
this is the script that I based mine on: https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/script
It should give you the necessary information.
Hi Sir,
The code doesn’t have any indentations can u tell me were I should give the indentations please
Hello there,
this is the script that I based mine on: https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/script
It should give you the necessary information.
[…] 《應用》 * Raspberry Pi IoT無線傳輸技術介紹 – ZigBee篇 * Pulse Sensing with the Raspberry Pi […]
How I Can display the output in figure or anything else after running the code as you did ?
Hi sir, will you explain the above code, I’m not aware of python code
Hello sir I can’t understand the code please give comments to every line. I need a phython code.I have only used raspberry pi and heart beat sensor to monitoring the heart rate and update to the webserver
Hello sir, i want ask to you about output this project. how to change output “.” to number?
You can print analog_value directly. That’s where the number value is stored.
Hello sir can i get a code for connecting digital pulse sensor to raspberry pi3
if ( (adcnum > 7) or (adcnum < 0) ): shows error in this line