美文网首页
【树莓派+arduino实验记录13 】模拟温度传感器

【树莓派+arduino实验记录13 】模拟温度传感器

作者: Geekero | 来源:发表于2020-09-01 11:24 被阅读0次

    Arduino

    //Analog Temperature Sensor
    const int digitalPin = 7; //Analog Temperature Sensor pin D0 to pin7
    int analogPin = A0; //Analog Temperature Sensor pin A0 to pin A0
    const int ledPin = 13; //bulit-in LED light
    
    //variables will change:
    boolean Dstate = 0;  //variable for readind status of D0
    int Astate = 0;  //variable for reading status of A0
    
    void setup()
    {
      pinMode(ledPin, OUTPUT);
      pinMode(digitalPin, INPUT);
      Serial.begin(9600);
    }
    
    void loop()
    {
      Astate = analogRead(analogPin);
      Dstate = digitalRead(digitalPin);
      Serial.print("D0:");
      Serial.println(Dstate);
      Serial.print("A0:");
      Serial.println(Astate);
      // check if the pushbutton is pressed.
      //if it is, the buttonaState is HIGH:
      if (Dstate == HIGH) 
      {
        digitalWrite(ledPin, HIGH); //turn on
      }
      else{
        digitalWrite(ledPin, LOW); //turn off
      }
      delay(1000);
    }
    

    树莓派

    C

    #include <stdio.h>
    #include <wiringPi.h>
    #include <pcf8591.h>
    #include <math.h>
    #define  PCF  120
    #define  D0pin  0
    
    void Print(int x)
    {
        switch(x)
        {
            case 0:
                    printf("\n************\n");
                    printf(  "* Too Hot! *\n");
                    printf(  "************\n\n");
                    break;
            case 1:
                    printf("\n***********\n");
                    printf(  "* Better~ *\n");
                    printf(  "***********\n\n");
                    break;
            default:
                    printf("\n**********************\n");
                    printf(  "* Print value error. *\n");
                    printf(  "**********************\n\n");
                    break;
        }
    }
    
    int main()
    {
        unsigned char analogVal;
        double Vr, Rt, temp;
        int tmp, status;
        
        if (wiringPiSetup() == -1){
            printf("setup wiringPi failed !");
            return 1;
        }
        // Setup pcf8591 on base 120, and address 0x48
        pcf8591Setup(PCF, 0x48);
        pinMode(D0pin, INPUT);
        status = 0;
        while(1) //loop forever
        {
            printf("loop");
            analogVal = analogRead(PCF + 0);
            Vr = 5 * (double)(analogVal) / 255;
            Rt = 10000 * (double)(Vr) / (5 - (double)(Vr));
            temp = 1 / (((log(Rt/10000)) / 3950) + (1 / (273.15 + 25)));
            temp = temp - 273.15;
            printf("Current temperature: %1f\n", temp);
    
            //For a threshold, uncomment one of the code for
            //which module you use.DONOT UNCOMMENT BOTH!
            //--------------------------------------------
            //1. For Analog Temperature module(with D0)
            tmp = digitalRead(D0pin);
    
            //2. For Thermister module(with sig pin)
            if (temp > 33) tmp = 0;
            else if (temp < 31) tmp = 1;
            //----------------------------------------------
            if (tmp != status)
            {
                    Print(tmp);
                    status = tmp;
            }
            delay(200);
        }
        return 0;
    }
    

    Python

    #!/usr/bin/env python
    import PCF8591 as ADC
    import RPi.GPIO as GPIO
    import time
    import math
    D0 = 17
    GPIO.setmode(GPIO.BCM)
    
    def setup():
        ADC.setup(0x48)
        GPIO.setup(D0, GPIO.IN)
    
    def Print(x):
        if x == 1:
            print('')
            print('***********')
            print('* Better~ *')
            print('***********')
            print('')
        if x == 0:
            print('')
            print('************')
            print('* Too Hot! *')
            print('************')
            print('')
    
    def loop():
        status = 1
        tmp = 1
        while True:
            analogVal = ADC.read(0)
            Vr = 5 * float(analogVal) / 255
            Rt = 10000 * Vr / (5 - Vr)
            temp = 1/((math.log(Rt / 10000) / 3950) + (1 / (273.15 + 25)))
            temp = temp - 273.15
            print('Temperature = {}C'.format(temp))
            # For a thredshold, uncomment one of code for 
            # which module you use, DONOT UNCOMMENT BOTH!
            ####################################################
            # 1. For Analog Temperature module(with D0)
            # tmp = GPIO.input(D0)
            #
            # 2. For Thermister module(with sig pin)
            if temp > 33:
                tmp = 0
            elif temp < 31:
                tmp = 1
            ####################################################
    
            if tmp != status:
                Print(tmp)
                status = tmp
    
            time.sleep(0.2)
    
    
    if __name__ == '__main__':
        try:
            setup()
            loop()
        except KeyboardInterrupt:
            pass
    

    相关文章

      网友评论

          本文标题:【树莓派+arduino实验记录13 】模拟温度传感器

          本文链接:https://www.haomeiwen.com/subject/zwzmsktx.html