Internal MCU temperature?

On the photon is there any higher level support for retrieving values from the internal temperature sensor or is it currently only possible through utilization of the low level I/O structures?

@Melx, there are no high level API calls for the internal sensor. However, you can look here for code that works on the older Core based on the STM32F103:

1 Like

Keep in mind that the sensor has pretty terrible consistency between chips so it’s not really useful unless used for relative measurements (or if calibrated first)

From the 203 reference manual:

The temperature sensor output voltage changes linearly with temperature. 
The offset of this linear function depends on each chip due to process variation 
(up to 45 °C from one chip to another).
1 Like
// CODE FOR PHOTON
// STM32F2xx INTERNAL TEMP --> XIVELY STREAM

// #include "stm32f2xx.h"

__IO uint16_t ADCConvertedValue = 0;
__IO uint16_t TempSTM32 = 0;

ADC_InitTypeDef       ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef      GPIO_InitStructure;

void CpuTempInit()
{
  /* Enable peripheral clocks ------------------------------------------------*/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  /* ADC1 configuration ------------------------------------------------------*/
  /* Configure ADC Channel12 pin as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  ADC_DeInit();
  /* ADC Common Init */
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 Init */
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel16 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_480Cycles);
  ADC_TempSensorVrefintCmd(ENABLE);
  ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
}

void setup()
{
  CpuTempInit();
}

void loop()
{
  float adcVolt = 0;
  float diffVolt = 0;
  float temp1 = 0;
  float temp2 = 0;
  ADC_SoftwareStartConv(ADC1);
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  ADCConvertedValue = ADC_GetConversionValue(ADC1);
  adcVolt = ADCConvertedValue * 3.3 / 4096;
  diffVolt = adcVolt - 0.76;
  temp1 = diffVolt / (2.5 / 1000);
  temp2 = temp1 + 25;
  xivelyTemp(temp2);
  delay(30000);
}

#define FEED_ID "your Feed ID"
#define XIVELY_API_KEY "your API Key"
// use STM32temp as Channel

TCPClient client;

void xivelyTemp(float temperature)
{
  if (client.connect("api.xively.com",8081))
  {
    client.print("{");
    client.print("  \"method\" : \"put\",");
    client.print("  \"resource\" : \"/feeds/");
    client.print(FEED_ID);
    client.print("\",");
    client.print("  \"params\" : {},");
    client.print("  \"headers\" : {\"X-ApiKey\":\"");
    client.print(XIVELY_API_KEY);
    client.print("\"},");
    client.print("  \"body\" :");
    client.print("    {");
    client.print("      \"version\" : \"1.0.0\",");
    client.print("      \"datastreams\" : [");
    client.print("        {");
    client.print("          \"id\" : \"STM32temp\",");
    client.print("          \"current_value\" : \"");
    client.print(temperature);
    client.print("\"");
    client.print("        }");
    client.print("      ]");
    client.print("    },");
    client.print("  \"token\" : \"0x123abc\"");
    client.print("}");
    client.println();
  } 
  client.flush();
  client.stop();
}
1 Like