# KY16 Flameセンサー

赤外線を検知して火を検知するセンサーです。

img

左から A0: アナログアウト GND: グラウンド +: 5V D0: デジタルアウト

通電時

img

火が出たら音楽がなるコード

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

FlamePin = 11
BuzzerPin = 10    # pin10

SPEED = 1 

# List of tone-names with frequency
TONES = {"c6":1047,
	"b5":988,
	"a5":880,
	"g5":784,
	"f5":698,
	"e5":659,
	"eb5":622,
	"d5":587,
	"c5":523,
	"b4":494,
	"a4":440,
	"ab4":415,
	"g4":392,
	"f4":349,
	"e4":330,
	"d4":294,
	"c4":262}

# Song is a list of tones with name and 1/duration. 16 means 1/16
SONG =	[
	["e5",16],["eb5",16],
	["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
	["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
	["b4",8],["p",16],["e4",16],["ab4",16],["b4",16],
	["c5",8],["p",16],["e4",16],["e5",16],["eb5",16],
	["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
	["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
	["b4",8],["p",16],["e4",16],["c5",16],["b4",16],["a4",4]
	]

def setup():
	GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
	GPIO.setup(BuzzerPin, GPIO.OUT)
	GPIO.setmode(GPIO.BOARD)	
	GPIO.setup(FlamePin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def playTone(p,tone):
        # calculate duration based on speed and tone-length
	duration = (1./(tone[1]*0.25*SPEED))

	if tone[0] == "p": # p => pause
		time.sleep(duration)
	else: # let's rock
		frequency = TONES[tone[0]]
		p.ChangeFrequency(frequency)
		p.start(0.5)
		time.sleep(duration)
		p.stop()

def run():
	p = GPIO.PWM(BuzzerPin, 440)
	p.start(0.5)
	for t in SONG:
		playTone(p,t)

def destroy():
	GPIO.output(BuzzerPin, GPIO.HIGH)
	GPIO.cleanup()                  

def myISR(ev=None):
	print("Flame is detected !")
	run()

def loop():
	GPIO.add_event_detect(FlamePin, GPIO.FALLING, callback=myISR)
	while True:
		pass

if __name__ == '__main__':
	setup()
	try:
		loop()
	except KeyboardInterrupt: 
		destroy()
		print ('end !')

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

動作