3

enter image description hereI am trying to connect a temperature sensor to my Raspberry Pi using this tutorial for Windows IoT Core.

I have connected all of the wires and MCP3002 correctly, but I cannot read in any values from my sensor when the app is deployed and run.

The sensor listed in the tutorial is the TMP36GZ and I have also tried a DS1822 for debugging purposes. Neither of them produce an error when connected the right way around, but the output is always 0.

I have swapped out the temp sensors for a potentiometer and that has been able to return values and print them to the screen.

All of the code is in C# and can be found on the linked tutorial.

Wiring Diagram

Update: Connected MCP3002 Pin 8 to 5V along with keeping the connection to the right leg of the TMP36GZ. Outputted values now hover between 158-164 even when held in hand etc(there is no reaction to heat). The temperature in the room is around 15-16C too. That would make sense to divide the output by 10, although I need to be able to measure an increase/decrease in heat when it is touched. I connected the potentiometer again and this produced accurate results from 0-1200 depending on the position of the knob.

My MainPage.xaml.cs:

`namespace TempSensor { /// /// An empty page that can be used on its own or navigated to within a Frame. /// public sealed partial class MainPage : Page { enum ADCChip { mcp3002, // 2 channel 10 bit mcp3008, // 8 channel 10 bit mcp3208 // 8 channel 12 bit } ADCChip whichADCChip = ADCChip.mcp3002;

    public MainPage()
    {
        this.InitializeComponent();
        this.InitializeComponent();
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(500);
        timer.Tick += Timer_Tick;
        timer.Start();

        whichADCChip = ADCChip.mcp3002;
        switch (whichADCChip)
        {
            case ADCChip.mcp3002:
                {
                    // To line everything up for ease of reading back (on byte boundary) we 
                    // will pad the command start bit with 1 leading "0" bit

                    // Write 0SGO MNxx xxxx xxxx
                    // Read  ???? ?N98 7654 3210
                    // S = start bit
                    // G = Single / Differential
                    // O = Chanel data 
                    // M = Most significant bit mode 
                    // ? = undefined, ignore
                    // N = 0 "Null bit"
                    // 9-0 = 10 data bits

                    // 0110 1000 = 1 0 pad bit, start bit, Single ended, odd (channel 0), MSFB only, 2 clocking bits
                    // 0000 0000 = 8 clocking bits
                    readBuffer = new byte[2] { 0x00, 0x00 };
                    writeBuffer = new byte[2] { 0x68, 0x00 };
                }
                break;

            case ADCChip.mcp3008:
                {
                    // To line everything up for ease of reading back (on byte boundary) we 
                    // will pad the command start bit with 7 leading "0" bits

                    // Write 0000 000S GDDD xxxx xxxx xxxx
                    // Read  ???? ???? ???? ?N98 7654 3210
                    // S = start bit
                    // G = Single / Differential
                    // D = Chanel data 
                    // ? = undefined, ignore
                    // N = 0 "Null bit"
                    // 9-0 = 10 data bits

                    // 0000 01 = 7 pad bits, start bit
                    // 1000 0000 = single ended, channel bit 2, channel bit 1, channel bit 0, 4 clocking bits
                    // 0000 0000 = 8 clocking bits
                    readBuffer = new byte[3] { 0x00, 0x00, 0x00 };
                    writeBuffer = new byte[3] { 0x01, 0x80, 0x00 };
                }
                break;

            case ADCChip.mcp3208:
                {
                    /* mcp3208 is 12 bits output */
                    // To line everything up for ease of reading back (on byte boundary) we 
                    // will pad the command start bit with 5 leading "0" bits

                    // Write 0000 0SGD DDxx xxxx xxxx xxxx
                    // Read  ???? ???? ???N BA98 7654 3210
                    // S = start bit
                    // G = Single / Differential
                    // D = Chanel data 
                    // ? = undefined, ignore
                    // N = 0 "Null bit"
                    // B-0 = 12 data bits


                    // 0000 0110 = 5 pad bits, start bit, single ended, channel bit 2
                    // 0000 0000 = channel bit 1, channel bit 0, 6 clocking bits
                    // 0000 0000 = 8 clocking bits
                    readBuffer = new byte[3] { 0x00, 0x00, 0x00 };
                    writeBuffer = new byte[3] { 0x00, 0x00, 0x00 };
                }
                break;
        }

        InitSPI();
    }

    private async void InitSPI()
    {
        try
        {
            var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
            settings.ClockFrequency = 500000;// 10000000;
            settings.Mode = SpiMode.Mode0; //Mode3;
            var controller = await SpiController.GetDefaultAsync();
            SpiDisplay = controller.GetDevice(settings);
            GpioStatus.Text = "Successfully Initialised GPIO";
        }

        /* If initialization fails, display the exception and stop running */
        catch (Exception ex)
        {
            throw new Exception("SPI Initialization Failed", ex);
        }
    }
    private void Timer_Tick(object sender, object e)
    {
        DisplayTextBoxContents();
    }
    public void DisplayTextBoxContents()
    {
        SpiDisplay.TransferFullDuplex(writeBuffer, readBuffer);
        res = convertToInt(readBuffer);
        textPlaceHolder.Text = res.ToString();

    }
    public int convertToInt(byte[] data)
    {
        int result = 0;
        switch (whichADCChip)
        {
            case ADCChip.mcp3002:
                {
                    /*mcp3002 10 bit output*/
                    result = data[0] & 0x03;
                    result <<= 8;
                    result += data[1];
                }
                break;
            case ADCChip.mcp3008:
                {
                    /*mcp3008 10 bit output*/
                    result = data[1] & 0x03;
                    result <<= 8;
                    result += data[2];
                }
                break;

            case ADCChip.mcp3208:
                {
                    /* mcp3208 is 12 bits output */
                     result = data[1] & 0x0F;
                     result <<= 8;
                     result += data[2];
                }
                break;
        }

        return result;
    }

    /*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        */


    byte[] readBuffer = null;                           /* this is defined to hold the output data*/
    byte[] writeBuffer = null;                          /* we will hold the command to send to the chipbuild this in the constructor for the chip we are using */


    private SpiDevice SpiDisplay;

    // create a timer
    private DispatcherTimer timer;
    int res;

}

} `

  • You reckon the connections are correct which rather implies the software is wrong (if it works for others that suggests you are using it wrongly). Anyhow there is insufficient information (photos, wiring diagram, code) to give a proper answer, – joan Dec 5 '17 at 11:53
  • Well it works for the potentiometer so the wiring must be ok? Could you think of any reasons why it would give values for that, but not a temperature sensor? Also, all of the code and diagrams are displayed on the tutorial page but I can copy them over if people would like. – Andre Dec 5 '17 at 11:57
  • I think people would need to know what you have actually done. – joan Dec 5 '17 at 12:09
  • @joan how's that? – Andre Dec 5 '17 at 12:22
  • It kind of hard to read your photo, but your MCP3002 pin 8 (VDD) doesn't seem to be connecting to any power source. – hcheung Dec 5 '17 at 13:41
2

The TMP36, in my experience, will not work with Raspberry Pi this way. I do understand that there are online sources where people say it works, but that's not been my experience.

I have made it work using an op-amp as a voltage follower. I got on the right track using the instructions on this thread: https://www.raspberrypi.org/forums/viewtopic.php?p=276458#p276458

The relevant part:

The ADC chip uses an internal capacitor to sample and hold the input voltage. As the ADC performs a conversion it connects an internal switch to allow the capacitor to charge up from the input source. The capacitor needs current to charge up and this is where the issue occurs. A check of the TMP036 data sheet shows it can only output a maximum of 50uA which is a pretty small current. Indeed it's so small that the TMP036 is overloaded by the current demand when the ADC connects the capacitor and this causes the TMP036 output voltage to drop giving the under-reading. It may also be that the sample period is too small to allow the capacitor to fully charge with the limited current.

If you configure an op-amp at unity gain, it will provide a high-impedance source when the measurement is taken.

schematic

simulate this circuit – Schematic created using CircuitLab

  • Low-impedance source of course, that's the point ;) – Ghanima May 22 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.