美文网首页
【树莓派+arduino实验记录15 】火焰传感器

【树莓派+arduino实验记录15 】火焰传感器

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

Arduino

/*******************************************
 * name:Flame Alarm
 * function: ignite a lighter near the flame sensor
 * Then the buzzer will beep.
 * and the LED on the flame sensor module and that attach to pin 13 of the arduino board will light up.
 * connection:
 * flame sensor module        Arduio Uno R3
 * D0                           8
 * VCC                          5V
 * GND                          GND 
 * 
 * Buzzer Module                Arduino Uno R3
 * SIG                           7
 * VCC                           5V
 * GND                           GND
 *********************************************/
 const int digitalInPin = 8;
 const int ledPin = 13;
 const int buzzerPin = 7;

 void setup()
 {
  pinMode(digitalInPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
 }

void loop()
{
  boolean stat = digitalRead(digitalInPin);
  Serial.print("D0: ");
  Serial.println(stat); 
  if (stat == HIGH)
  {
    digitalWrite(ledPin, LOW);
    noTone(7); //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 (stat == LOW)
  {
    digitalWrite(ledPin, HIGH);
    tone(7,320,200); //in pin7 generate a tone, it's frequency is 320Hz and the duration of the tone is 200 milliseconds
  }
  delay(500); //used to slow down readings while calibrating
}

树莓派

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(  "* Safe~ *\n");
                printf(  "*********\n\n");
                break;
        case 1:
                printf("\n*********\n");
                printf(  "* Fire! *\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;
    if (wiringPiSetup() == -1){
        printf("setup wiringPi failed !");
        return 1;
    }
    pcf8591Setup(PCF, 0x48);
    pinMode(D0pin, INPUT);
    status = 0;
    while(1)
    {
        analogVal = analogRead(PCF + 0);
        printf("%d\n", analogVal);
        tmp = digitalRead(D0pin);
        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 == 0:
        print('')
        print(' *********')
        print(' * Safe~ *')
        print(' *********')
        print('')

    if x == 1:
        print('')
        print(' *********')
        print(' * Fire! *')
        print(' *********')
        print('')

def loop():
    status = 1
    while True:
        print(ADC.read(0))
        tmp = GPIO.input(D0)
        if tmp != status:
            Print(tmp)
            status = tmp
        time.sleep(0.2)


if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        pass

网友评论

      本文标题:【树莓派+arduino实验记录15 】火焰传感器

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