Temperature/Force Sensor Sample
This sample uses SPI communication. A temperature/force sensor is connected to a ADC, then ADC is connected to raspberry Pi2 through SPI Pins. Raspberry Pi2 reads the sensor analog input data and output it to the screen. This is basically a simplified version of Potentiometer sensor sample, which has a LED light as an extra output. You can also use a Force sensor in this sample. Try to press the Force sensor gentle or hard to see the data output difference. This sample only has C# version.
Read before start
This sample assumes that RaspBerry has been pre-setted up with below:
- Pi2 has been connected to HDMI monitor
- A SD image card has been plugged to Pi2
- An Ethernet cable has been plugged to Pi2
- Pi2 has been powered on
Parts needed
- 1 MCP3002 I/P ADC Converter
- Or 1 MCP3208 CI/P ADC Converter
- 1 Temperature sensor
- Or 1 Force sensor
- Raspberry Pi2 board
- 1 breadboard and a couple of wires
- HDMI Monitor
Parts Review
- MCP3002
The below lists the pin Layout of MCP3002
, and also it is the A/D converter that we are using in this sample
.
You can also use MCP3208 which are 16 pins. We will list its connections with Raspberry Pi2 as well in later section.
Parts Connection
- Connects the temperature Sensor to MCP3002;
Sensor output pin
(the mid pin) should be connected toCH0
on MCP3002;
If you are using a Force sensor which only has two legs, set the left leg to 5V, and connect the other Leg to CH0
on MCP3002
Detailed connection:
With each model of RaspBerry Pi2, the pin layout might be a little different. But the pin connection with MCP3002 should be as below:
- MCP3002: VDD/VREF - 5V on RaspBerry Pi2
- MCP3002: CLK - “SPI0 SCLK” on RaspBerry Pi2
- MCP3002: Dout - “SPI0 MISO” on RaspBerry Pi2
- MCP3002: Din - “SPI0 MOSI” on RaspBerry Pi2
- MCP3002: CS/SHDN - “SPIO CS0” on RaspBerry Pi2
- MCP3002: DGND - GND on RaspBerry Pi2
- MCP3002: CH0- Sensor Output Pin
2.Alternative: If you are using MCP3208
Connect the temperature Sensor to MCP3208; Sensor output pin
(the mid pin) should be connected toCH0
on MCP3208.
Detailed connection:
With each model of RaspBerry Pi2, the pin layout might be a little different. But the pin connection with MCP3208 should be as below:
- MCP3208: VDD - 5V on RaspBerry Pi2
- MCP3208: VREF - 5V on RaspBerry Pi2
- MCP3208: CLK - “SPI0 SCLK” on RaspBerry Pi2
- MCP3208: Dout - “SPI0 MISO” on RaspBerry Pi2
- MCP3208: Din - “SPI0 MOSI” on RaspBerry Pi2
- MCP3208: CS/SHDN - “SPIO CS0 on RaspBerry Pi2
- MCP3208: DGND - GND on RaspBerry Pi2
Look at the code
Let’s go through the code. We use a timer in the sample, and each time the ‘Tick’ event is called, we read the sensor data through ADC, and the value will be displayed on the screen.
- Timer Code Setup timer in C#:
public MainPage()
{
// ...
this.timer = new DispatcherTimer();
this.timer.Interval = TimeSpan.FromMilliseconds(500);
this.timer.Tick += Timer_Tick;
this.timer.Start();
// ...
}
private void Timer_Tick(object sender, object e)
{
DisplayTextBoxContents();
}
- Initialize SPI pin
private async void InitSPI()
{
try
{
var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
settings.ClockFrequency = 500000;// 10000000;
settings.Mode = SpiMode.Mode0; //Mode3;
string spiAqs = SpiDevice.GetDeviceSelector(SPI_CONTROLLER_NAME);
var deviceInfo = await DeviceInformation.FindAllAsync(spiAqs);
SpiDisplay = await SpiDevice.FromIdAsync(deviceInfo[0].Id, settings);
}
/* If initialization fails, display the exception and stop running */
catch (Exception ex)
{
throw new Exception("SPI Initialization Failed", ex);
}
}
- read the sensor data through SPI communication
/*RaspBerry Pi2 Parameters*/
private const string SPI_CONTROLLER_NAME = "SPI0"; /* For Raspberry Pi 2, use SPI0 */
private const Int32 SPI_CHIP_SELECT_LINE = 0; /* Line 0 maps to physical pin number 24 on the Rpi2 */
/*Channel configuration for MCP3208, Uncomment this if you are using MCP3208*/
// byte[] readBuffer = new byte[3]; /*this is defined to hold the output data*/
// byte[] writeBuffer = new byte[3] { 0x06, 0x00, 0x00 };//00000110 00; /* It is SPI port serial input pin, and is used to load channel configuration data into the device*/
/*Channel configuration for MCP3002, Uncomment this if you are using MCP3002*/
byte[] readBuffer = new byte[3]; /*this is defined to hold the output data*/
byte[] writeBuffer = new byte[3] { 0x68, 0x00, 0x00 };//00001101 00; /* It is SPI port serial input pin, and is used to load channel configuration data into the device*/
private SpiDevice SpiDisplay;
// create a timer
private DispatcherTimer timer;
int res;
public void DisplayTextBoxContents()
{
SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
res = convertToInt(readBuffer);
textPlaceHolder.Text = res.ToString();
}
- Convert sensor bit data to a number
/* This is the conversion for MCP3208 which is a 12 bits output; Uncomment this if you are using MCP3208 */
// public int convertToInt(byte[] data)
// {
// int result = data[1] & 0x0F;
// result
Deploy the sample
Choose Debug
and ARM
configuration, choose Remote Machine
, right click the project, under Property, click Debug tag, Put the Raspberry Pi2 IP in the Remote machine field, and unclick Use authentication
Press F5
If you are using Temp sensor, you can try to hold the sensor or apply some heat on it to see how the output change. If you are using Force sensor, you can hold it hard or gentle to see how the output change on the screen. You can also switch the sensor to a light sensor to play around with it.
Comments