General

AndroidThings: Reading Analog Pulse Sensor Data in Raspberry Pi via ADC

Praveena Kumara D

17 Feb 2017

AndroidThings

In our continued fascination with AndroidThings, we decided to try out integrating Raspberry Pi with the Pulse Sensor. Pulse Sensor is a well-known heart rate sensor which provides analog output. Pulse Sensor is a simple sensor with 3 wires positive, negative and analog output. However, Raspberry Pi doesn’t have any analog input pins. Hence we decided to use analog to digital converter (ADC) MCP3008. MCP3008 is a 10 bit 8 channel analog to digital converter which supports only SPI (Serial Peripheral Interface).Using the available 8 channels, we can connect up to 8 Analog sensors and convert the data to digital transformation values.

One of the important promises of AndroidThings is that it gets pure Android developers one layer closer to the world of IoT ( Internet of Things). Like us, most of the rookies who are excited to try out AndroidThings on Raspberry Pi may not really be aware of all these ADCs, the way to communicate and to read data. This article tries to explain the connections in as much detail as possible.

Once we have the MCP3008 plugged into the breadboard, we need to connect this to Raspberry Pi. Below diagram explains what pins are available in MCP3008.

Figure 3. Pinout of the MCP3008 IC

Channels from CH0 to CH7 are the pins where we can connect analog sensors. In our case, we have only one analog sensor ie Pulse Sensor and we connected it to CH0.

Rest of the pins in the MCP3008 can be connected to Pi as per the table below.

VDD

P1 17

VRef

P1-17

AGND

Ground

CLK

GPIO11 ( P1-23)

DOUT

GPIO9 ( P1-21)

DIN

GPIO10 ( P1 19)

CS

GPIO8 (P1 24)

DGND

Ground

You can refer the detailed Raspberry Pi pinout diagram here – https://pinout.xyz/pinout/pin17_3v3_power

CS pin stands for Chip Select pin and it should be connected to one of the SPI bus supported in Pi. Pi has two SPI interfaces, SPI0 and SPI1 meaning we can communicate with two SPI devices at the max. In this experiment, we connected the CS pin from MCP3008 with SPI0 bus (BCM8, Pin 24) of Raspberry Pi.

After wiring these connections carefully we plugged in pulse sensor at the CH0 While it’s+’ and ‘-’ pins were connected to a source (pin17 of Pi) and the ground respectively. You can use a small resistor to not connect pulse sensor directly with the source (even though 3.3V should be fine). Once you connect everything, pulse sensor’s tiny green led will light up.

Reading Data from MCP3008

Though we were able to wire everything properly, it took a while for us to understand how to read data from MCP3008. As per SPI communication document, first, we will need to configure the SPI device with the mode, bits per word and frequency.

device.setMode(SpiDevice.MODE0);
 
 device.setBitsPerWord(8); // 8 BPW
 
 device.setFrequency(1000000); // 1MHz
 
 device.setBitJustification(false)

LksjHHBkl3Tg2km 9dlv WDU3kjXbse3T4Fr95YZMK2

Before reading data, we have to write to the MCP3008 about the channel number to which we have plugged in the sensor ( CH0 in our case).

Format:
 
 FIrst Byte : 00000001
 
 Second Byte :( 8+channel)<<4
 
 Third Byte: 00000000 ( just a plain 0)

Where channel in the second byte indicates the channel number where we have connected our Analog sensor and in our case, we have the channel number 0 (CH0), hence our Second-byte data will be the Second Byte: 1000<<4 (10000000)

byte[] data=new byte[3];
 
 byte[] response=new byte[3];
 
 data[0]=1;
 
 int a2dChannel=0;
 
 data[1]= (byte) (8 << 4); //1000000
 
 data[2] = 0;
 
 //full duplex mode
 
 spiDevice.transfer(data,response,3);

lm1QSFS1Dgwrx5QVQpjru3U5Wj1cQlrZ6MKWY9uvc84doERBHxQkvMhYvYlP6Teje6z3Zc82cgXgDMvHoKBQ tjwghcfkzvwJEScWZcm5kZn Dz4HMXodd sH7m9ozWY1br4jPIH .     vAZWl TyoBHYHiTL NZRfFtIGkZ713ZYvGJkE lEgCXTxF alPNJdD j3vewnmDRK2XmWkUGzld9BQ5TlQ okI687zTDEr1M6Oi1Yx00qUnI tg

Check out the code from our repo for more details – https://bitbucket.org/pkumarad/androidthings-pulsesensor-adc