美文网首页
【树莓派+arduino实验记录16 】可燃性气体传感器

【树莓派+arduino实验记录16 】可燃性气体传感器

作者: Geekero | 来源:发表于2020-09-07 18:58 被阅读0次

    Arduino

    /**************************************
     * name: Flammable Gas Detection
     * function:ingite a ligther. Then the sensor detects the gas emitted.
     * Thus, the led on the gas sensor and that attach to pin 13 on the arduino Uno
     * board will light up.Also you can see the value at A0 and D0 printed on Serial Monitor.
     * connection:
     * MQ-2 gas sensor   Uno R3
     * D0                   7
     * A0                   A0
     * GND                  GND
     * VCC                  VCC
     **************************************/
     const int ledPin = 13; 
     const int analogPin = A0;
     const int digitalPin = 7;
     const int buzzerPin = 8;
     int Astate=0;
     boolean Dstate=0;
    
     void setup()
     {
      //set the pins state
      pinMode(digitalPin, INPUT);
      pinMode(ledPin, OUTPUT);
      pinMode(buzzerPin, OUTPUT);
      Serial.begin(9600);
     }
    
    void loop()
    {
      Astate = analogRead(analogPin);
      Serial.println(Astate);
      Dstate = digitalRead(digitalPin);
      Serial.println(Dstate);
    
      if (Dstate == HIGH)
      {
        digitalWrite(ledPin,LOW); //turn off
         noTone(buzzerPin); //if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.
      }
      if (Dstate == LOW)
      {
        digitalWrite(ledPin, HIGH); //turn on
        tone(buzzerPin,320,200); //in pin7 generate a tone, it's frequency is 320Hz and the duration of the tone is 200 milliseconds
      }
      delay(200);
    }
    

    树莓派

    C

    #include <stdio.h>
    #include <wiringPi.h>
    #include <pcf8591.h>
    #define  PCF  120
    #define  D0pin 0
    #define  Buzz  1
    
    void Print(int x)
    {
        switch(x)
        {
            case 1:
                    printf("\n*******\n");
                    printf("* Safe~ *\n");
                    printf("*********\n\n");
                    break;
            case 0:
                    printf("\n*************\n");
                    printf("* Danger Gas! *\n");
                    printf("***************\n\n");
                    break;
            default:
                    printf("\n********************\n");
                    printf("* Print value error. *\n");
                    printf("**********************\n\n");
                    break;
        }
    }
    
    int main()
    {
        int analogVal;
        int tmp, status, count;
    
        if (wiringPiSetup() == -1){
            printf("set up wiringPi failed !\n");
        }
        pcf8591Setup(PCF, 0x48);
        pinMode(D0pin, INPUT);
        pinMode(Buzz, OUTPUT);
        digitalWrite(Buzz, HIGH);
        status = 0;
        count = 0;
        
        while(1)
        {
            analogVal = analogRead(PCF + 0);
            printf("%d\n", analogVal);
            tmp = digitalRead(D0pin);
            if (tmp != status)
            {
                Print(tmp);
                status = tmp;
            }
            if (status == 0)
            {
                count ++;
                if (count % 2 == 0)
                        {digitalWrite(Buzz, HIGH);}
                else
                        {digitalWrite(Buzz, LOW);}
            }
            else
            {
                count = 0;
                digitalWrite(Buzz, HIGH);
            }
            delay(200);
        }
        return 0;
    

    Python

    #!/usr/bin/env python
    import PCF8591 as ADC
    import RPi.GPIO as GPIO
    import time
    import math
    
    D0 = 17
    Buzz = 18
    GPIO.setmode(GPIO.BCM)
    
    def setup():
        ADC.setup(0x48)
        GPIO.setup(D0, GPIO.IN)
        GPIO.setup(Buzz, GPIO.OUT)
        GPIO.output(Buzz, 1)
    
    def Print(x):
        if x == 1:
            print('')
            print('*********')
            print('* Safe~ *')
            print('*********')
            print('')
        if x == 0:
            print('')
            print('***************')
            print('* Danger Gas! *')
            print('***************')
            print('')
    
    def loop():
        status = 1
        count = 0
        while True:
            print(ADC.read(0))
    
            tmp = GPIO.input(D0)
            if tmp != status:
                Print(tmp)
                status = tmp
            if status == 0:
                count += 1
                if count % 2 == 0:
                    GPIO.output(Buzz, 1)
                else:
                    GPIO.output(Buzz, 0)
            else:
                GPIO.output(Buzz, 1)
                count = 0
    
            time.sleep(0.2)
    
    def destroy():
        GPIO.output(Buzz, 1)
        GPIO.cleanup()
    
    
    if __name__ == '__main__':
        try:
            setup()
            loop()
        except KeyboardInterrupt:
            destroy()
    

    相关文章

      网友评论

          本文标题:【树莓派+arduino实验记录16 】可燃性气体传感器

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