1
SOUND_PIN = 4
FLAME = 2
LED = 26
LED2 = 18
PIR = 6

import RPi.GPIO as GPIO
import time
import datetime
import os
import picamera


GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR,GPIO.IN)
GPIO.setup(SOUND_PIN,GPIO.IN)
GPIO.setup(FLAME,GPIO.IN)
GPIO.setup(LED,GPIO.OUT)
GPIO.setup(LED2,GPIO.OUT)

count = 0
state = 0

def SSENSOR(PIR):
    time.sleep(1)
    if GPIO.input(PIR):
        GPIO.output(LED,True)
        print "pir"
        time.sleep(1)
        GPIO.output(LED,False)

        nowtime = datetime.datetime.now()
        print nowtime

        camera.capture(str(nowtime) + '.jpg')

    else:
        GPIO.output(LED,False)

def DETECTED(SOUND_PIN):
    time.sleep(1)
    if GPIO.input(SOUND_PIN):

        print "sound detected"

    else:
        GPIO.output(LED,False)



def FIRE(FLAME):
    time.sleep(1)
    if GPIO.input(FLAME):
        print "fire"
        GPIO.output(LED2,True)
        time.sleep(1)
        GPIO.output(LED2,False)
        time.sleep(1)
        GPIO.output(LED2,True)
        time.sleep(1)
while True:
    DETECTED(SOUND_PIN)
    FIRE(FLAME)
    SSENSOR(PIR)

This is part of my code. It works. But I don't do anything, it print "sound detected", "fire" repeatedly. Is it code problem or sensor problem? (I want to detect flame and sound)

re))))) It's my all code. SSENSOR(Motion Sensor) works very well, so I remove it on photo.

enter image description here

enter image description here

  • 1
    There is nothing obviously wrong with the code. However we can't say if the code is wrong as the snippet is incomplete. We also need to see your wiring. – joan Dec 9 '17 at 15:52
  • Thanks for your reply. It's all codes about sound and frame sensor. I connect sound sensor(do = GPIO4, + = 3v, gnd = GND), frame sensor(do = GPIO25, + =3v, gnd = GND). When I disconnect sound sensor, "sound detected" do not appear. Is it sensor problem? – jackson Dec 9 '17 at 16:07
  • We need to see the full code and a photo of the wiring. – joan Dec 9 '17 at 16:37
  • It's my full code and photo. – jackson Dec 9 '17 at 17:09
  • In your particular case a good approach is to decouple the debugging of software and hardware. I.e. write a very, very simple script (it's probably a three-liner) to repeatedly poll the input pin and print its state. Now play with the respective sensor and watch what happens. If you're sure the sensor behaves the way intended proceed with your original script. – Ghanima Dec 9 '17 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.