Arduino UNOと温度センサーMCP9701で温度をシリアル出力(テーブル方式)

x MCP9701-E/TO 販売コード 103199

目的
安価に温度を測る。

秋月で売っている安いやつ

1℃　→　19.5mV　ステップ

0℃　→　400mV　センター

0V → -20.512820512820512820512820512821℃
0.4V → 0℃
3.3V →　148.71794871794871794871794871794‬℃
5V　→　235.89743589743589743589743589744℃

![o_con567.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/97e03d64-8542-c543-ba4e-247a9b52972a.jpeg)



```c++



//SER_MCP9701_table_UNO_1

static const char data[][5]={
"00.0","00.5","01.0","01.5","02.0","02.5","03.0","03.5","04.0","04.5","05.0","05.5","06.0","06.5","07.0","07.5","08.0","08.5","09.0","09.5",
"10.0","10.5","11.0","11.5","12.0","12.5","13.0","13.5","14.0","14.5","15.0","15.5","16.0","16.5","17.0","17.5","18.0","18.5","19.0","19.5",
"20.0","20.5","21.0","21.5","22.0","22.5","23.0","23.5","24.0","24.5","25.0","25.5","26.0","26.5","27.0","27.5","28.0","28.5","29.0","29.5",
"30.0","30.5","31.0","31.5","32.0","32.5","33.0","33.5","34.0","34.5","35.0","35.5","36.0","66.5","37.0","37.5","38.0","38.5","39.0","39.5"
};

#include <Arduino.h>

//初期化処理
void setup()
{

  //シリアルポートの初期化
  Serial.begin(9600);

} //setup

//メインループ
void loop()
{

  //センサーの値を読み込む
  int s = analogRead(A0); // UNO

  //温度の表示
  s = s >> 1; //0-512
  if (s >= (40 + 80 + 1)) {
    Serial.print("40.0");
  } else if (s <= 40) {
    Serial.print("-0.0");
  } else {
    Serial.print(data[(s - 41)]); 
  } //endif
  Serial.println();

  //1秒の待ち
  delay(1000);

} //loop




```

https://paiza.io/ja/projects/new?language=c


テストプログラム




```c++


static const char data[][5]={
"00.0","00.5","01.0","01.5","02.0","02.5","03.0","03.5","04.0","04.5","05.0","05.5","06.0","06.5","07.0","07.5","08.0","08.5","09.0","09.5",
"10.0","10.5","11.0","11.5","12.0","12.5","13.0","13.5","14.0","14.5","15.0","15.5","16.0","16.5","17.0","17.5","18.0","18.5","19.0","19.5",
"20.0","20.5","21.0","21.5","22.0","22.5","23.0","23.5","24.0","24.5","25.0","25.5","26.0","26.5","27.0","27.5","28.0","28.5","29.0","29.5",
"30.0","30.5","31.0","31.5","32.0","32.5","33.0","33.5","34.0","34.5","35.0","35.5","36.0","66.5","37.0","37.5","38.0","38.5","39.0","39.5"
};

#include <stdio.h>

int main(void){
  
    int       s;        //ADCの値
    float     v;        //電圧
    float     t;        //温度
    int ss;
    
    for(int s=0;s<512;s++){
        printf("[%d]  ",s);
    
        //センサーのADC値を電圧に変換
        v = s * ( 5.0 / 512.0 );
        
        //センサーの電圧を温度に変換
        t = (v - 0.4) * (1.0 / 0.0195);


        printf("%f  ",t);
        
        
        
        printf("%d*2+",(int)t);
        
        printf("%d",  ( ((int)(t*10.0)  - (int)t*10) >= 5 )  ?  1 : 0   );
        
        printf("   ");
        
        ss=s;
        //printf("  %d ", ((s-41)>>1) );
        
      if(s >= (40+80+1)) {
           printf("40.0");
      } else if (s <= 40) {
           printf("-0.0");
      } else {
           printf("%d ", ((s-41)>>1) );
           printf("%d ",  (s-41) & 0x01 );
           printf(data[(s-41)]);
      }
        
        
        
        printf(",\r\n");


    }//for

    
    
}


```


```

...
[80]  19.551283  19*2+1   19 1 19.5,
[81]  20.052084  20*2+0   20 0 20.0,
[82]  20.552885  20*2+1   20 1 20.5,
[83]  21.053686  21*2+0   21 0 21.0,
[84]  21.554487  21*2+1   21 1 21.5,
[85]  22.055288  22*2+0   22 0 22.0,
...



```