Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


materiel:rpi:gpio:accueil

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
materiel:rpi:gpio:accueil [2016/12/05 18:58]
resonance [GPIO]
materiel:rpi:gpio:accueil [2018/05/15 16:16] (Version actuelle)
resonance
Ligne 1: Ligne 1:
 ====== RPI - GPIO ====== ====== RPI - GPIO ======
  
 +Utilisations : entrées, sorties, ...
  
 +
 +===== Ligne de commande =====
 + Elevate your permissions to root user to allow IO access
 +
 +
 +sudo -i
 +
 +Setup to control pin as output
 +
 +Substitute 23 in the following commands for your GPIO number (GPIO, not pin number)
 +
 +
 +echo "23" > /sys/class/gpio/export
 +echo "out" > /sys/class/gpio/gpio23/direction
 +
 +Control Output State
 +
 +
 +echo "1" > /sys/class/gpio/gpio23/value
 +echo "0" > /sys/class/gpio/gpio23/value
 +
 +Setup to read pin as an input
 +
 +Substitute 23 in the following commands for your GPIO number (GPIO, not pin number)
 +
 +
 +echo "23" > /sys/class/gpio/export
 +echo "in" > /sys/class/gpio/gpio23/direction
 +
 +Read its state
 +
 +
 +cat /sys/class/gpio/gpio23/value
 +
 +To Release A Pin Afterwards
 +
 +
 +echo "23" > /sys/class/gpio/unexport
 +
 +===== Avec Python =====
 +Avec la  biblio RPI.gpio et la biblio gpiozero : 
 +Here’s a list of devices which currently supported:
 +  * LED (also PWM LED allowing change of brightness)
 +  * RGB LED
 +  * Buzzer
 +  * Motor
 +  * Button
 +  * Motion Sensor
 +  * Light Sensor
 +  * Analogue-to-Digital converters MCP3004 and MCP3008
 +  * Robot
  
  
 [[https://www.youtube.com/watch?v=q_NvDTZIaS4|Vidéo d'introduction]] [[https://www.youtube.com/watch?v=q_NvDTZIaS4|Vidéo d'introduction]]
  
 +**Ressources**
 +  * La documentation de [[https://gpiozero.readthedocs.io/en/v1.3.1/recipes.html#button|gpiozero]]
 +  * [[https://www.raspberrypi.org/blog/gpio-zero-a-friendly-python-api-for-physical-computing/|gpiozero - tutorial]]
 +  * Article [[materiel:rpi:pwm:accueil|RPI - PWM]]
 +
 +
 +
 +=====Pins=====
 +<WRAP group>
 +<WRAP half column>{{:materiel:rpi:gpio:pi3_gpio.png?500|}} **RPI 3**</WRAP>
 +<WRAP half column>{{:materiel:rpi:raspberry_pins.png|}} **RPI B+**</WRAP>
 +</WRAP>
 +
 +===== Sorties =====
 +<WRAP group>
 +<WRAP half column>
 +{{:materiel:rpi:gpio:led-gpio17-500x297.png|}}
 +</WRAP>
 +
 +<WRAP half column>
 +<code python>
 +from gpiozero import LED
 +from time import sleep
 +
 +led = LED(17)
 +
 +while True:
 +    led.on()
 +    sleep(1)
 +    led.off()
 +    sleep(1)
 +</code>
 +</WRAP>
 +</WRAP>
 +
 +
 +===== Entreés =====
 +
 +<WRAP group>
 +<WRAP half column>
 +{{:materiel:rpi:gpio:rpi-button.png|}}
 +</WRAP>
 +
 +<WRAP half column>
 +<code python>
 +from gpiozero import Button
 +mybutton = Button(2)
 +mybutton.wait_for_press()
 +print('You pushed me')
 +</code>
 +</WRAP>
 +</WRAP>
 +
 +
 +**Lancer une fonction**
 +<code python>
 +from gpiozero import Button
 +from signal import pause
 +   
 +def say_hello():
 + print("Hello!")
 +
 +button = Button(2)
 +button.when_pressed = say_hello
 +    
 +pause()
 +</code>
 +
 +**Lancer un programme**
 +<code python>
 +from gpiozero import Button
 +from signal import pause
 +import subprocess
 +
 +button = Button(2)
 +button.wait_for_press()
 +subprocess.call(['omxplayer test.mkv'], shell=True)
 +</code>
 +
 +**Déclencher deux sons avec deux boutons**
 +Avec Pygame : http://nerdparadise.com/tech/python/pygame/basics/part3/
 +
 +<WRAP center round info 60%>
 +  * FORMATS MP3 POSSIBLES AVEC pygame.mixer.music.load("Got-inspiration.mp3")
 +  * FORMATS WAVE AVEC sound =  pygame.mixer.Sound("/usr/share/scratch/Media/Sounds/Electronic/Zoop.wav"). The Sound can be loaded from an OGG audio file or from an uncompressed WAV.
 +</WRAP>
 +
 +<code python>
 +from gpiozero import Button
 +from signal import pause
 +import pygame
 +
 +b1 = Button(2)
 +b2 = Button(3)
 +
 +pygame.mixer.init()
 +son1 =pygame.mixer.Sound('/usr/share/scratch/Media/Sounds/Electronic/Zoop.wav')
 +son2 =pygame.mixer.Sound('/usr/share/scratch/Media/Sounds/Electronic/Laser1.wav')
 +
 +def launch_sound1():
 +    son1.play()
 +
 +def launch_sound2():
 +    son2.play()
 +
 +b1.when_pressed = launch_sound1
 +b2.when_pressed = launch_sound2
 +
 +pause()
 +</code>
 +
 +
 +
 +**Même chose en plus souple**
 +<code java>
 +from gpiozero import LED, Button
 +from signal import pause
 +import pygame
 +
 +
 +button1 = Button(2)
 +button2 = Button(3)
 +led = LED(17)
 +pygame.mixer.init()
 +monson1 =pygame.mixer.Sound('/usr/share/scratch/Media/Sounds/Electronic/Zoop.wav')
 +monson2 =pygame.mixer.Sound('/usr/share/scratch/Media/Sounds/Electronic/Laser1.wav')
 +
 +# dictionnaire clé/valeur pour lister les sons
 +sounds = {button1: monson1,
 +          button2: monson2}
 +
 +def stateON(button):
 +    sounds[button].play()
 +    led.on()
 +
 +def stateOFF(button):
 +    sounds[button].stop()
 +    led.off()  
 +
 +def btntest():
 +    # parcours l'ensemble des clés du dictionnaire sounds
 +    for button in sounds.keys():
 +        # when_pressed envoie apr defaut le bouton en tant qu'argument de la fonction stateON / stateOFF
 +        button.when_pressed = stateON
 +        button.when_released = stateOFF
 +
 +btntest()
 +pause()
 +</code>
 +
 +
 +
 +
 +===== Entrée / Sortie =====
 +
 +<WRAP group>
 +<WRAP half column>
 +{{:materiel:rpi:gpio:rpi-gpio-button-led.png|}}
 +</WRAP>
 +
 +<WRAP half column>
 +<code python>
 +from gpiozero import LED, Button
 +from signal import pause
 +led = LED(17)
 +button = Button(2)
 +button.when_pressed = led.on
 +button.when_released = led.off
 +pause()
 +<code>
 +</WRAP>
 +</WRAP>
 +
 +
 +
 +-----
 +
 +
 +FIXME
  
  
  
-RPI 3 - GPIO 
-{{:materiel:rpi:gpio:pi3_gpio.png?600|}} 
  
-==== Pins Raspberry ==== 
-  * http://raspi.tv/download/rpigpio.txt 
-{{:materiel:rpi:gpio-control-example-large.png?500|}} 
  
  
-==== Pins Raspberry B+ ==== 
  
-{{:materiel:rpi:raspberry_pins.png|}} 
  
 ==== Installation ==== ==== Installation ====
/home/resonancg/www/wiki/data/attic/materiel/rpi/gpio/accueil.1480960728.txt.gz · Dernière modification: 2016/12/05 18:58 de resonance