Skip to content

Instantly share code, notes, and snippets.

@ypelletier
Last active December 21, 2020 00:50
Un détecteur de mouvement infrarouge passif est branché au Raspberry Pi. Lorsqu'un mouvement est détecté, on joue un fichier audio wav.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Un détecteur de mouvement infrarouge passif est branché
au Raspberry Pi.
Lorsqu'un mouvement est détecté, on joue un fichier audio wav
situé dans le même répertoire que le présent fichier.
Pour plus de détails:
http://electroniqueamateur.blogspot.com/2017/09/detecteur-de-mouvement-infrarouge.html
'''
# importation des bibliothèques
import RPi.GPIO as GPIO # nécessaire pour recevoir le signal du détecteur
import pygame.mixer # nous utilisons pygame pour la gestion du fichier wav
from pygame.mixer import Sound
# initialisations
pygame.mixer.init()
GPIO.setmode(GPIO.BCM)
GPIO.setup(23,GPIO.IN) # pin du PIR configurée en entrée
etatPrecedent = 0 # la dernière fois qu'on a vérifié...y avait-il du mouvement?
while True:
mouvement = GPIO.input(23)
if mouvement: # mouvement détecté
if etatPrecedent == 0:
print ("Detection d'un mouvement!")
pygame.mixer.music.load("mon_son.wav")
pygame.mixer.music.play();
while pygame.mixer.music.get_busy() == True:
continue
etatPrecedent = mouvement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment