GPIO et Python (3/9) – LED clignotante

GPIO et Python (3/9) - LED clignotante

Dans ce projet, vous apprendrez à faire clignoter une LED!

Choses dont vous aurez besoin:

Carte SD Raspberry Pi +
Clavier + souris
Moniteur + câble HDMI
Source de courant
Planche à pain
1x LED rouge
1x LED bleue
2x 330? Résistance
3x fils de pontage M / F

Conditions préalables:

Dernière version de Rasbian installée sur votre carte SD
Configuration du Raspberry Pi avec un clavier, une souris et un moniteur

GPIO et Python 39 LED clignotante

1. Changez le répertoire courant en notre répertoire gpio_python_code:

cd gpio_python_code 

1593275404 331 GPIO et Python 39 LED clignotante

2. Commencez par créer un fichier pour notre script led clignotant

touch 3_blink.py

1593275404 919 GPIO et Python 39 LED clignotante

3. Créez un fichier pour notre script clignotant pour toujours

touch 3_blink_forever.py

1593275404 655 GPIO et Python 39 LED clignotante

4. Modifiez le script 3_blink.py à l’aide de nano 3_blink.py, ajoutez le code suivant:

#!/usr/bin/python



from time import sleep # import the time function from the sleep library

import RPi.GPIO as GPIO # import our GPIO library



GPIO.setmode(GPIO.BCM) # set the board numbering system to BCM



# setup our output pins

GPIO.setup(17,GPIO.OUT)

GPIO.setup(27,GPIO.OUT)



# Turn LEDs on

print “lights on”

GPIO.output(17,GPIO.HIGH)

GPIO.output(27,GPIO.HIGH)

sleep(1) # sleep for 1 second



# Turn LEDs off

print “lights off” 

GPIO.output(17,GPIO.LOW)

GPIO.output(27,GPIO.LOW)

sleep(1)



# Turn LEDs on

print “lights on”

GPIO.output(17,GPIO.HIGH)

GPIO.output(27,GPIO.HIGH) 

sleep(1)



# Turn LEDs off

print “lights off”

GPIO.output(17,GPIO.LOW)

GPIO.output(27,GPIO.LOW)

GPIO.cleanup() # the clean-up function will reset all the configurations made in this script. This will stop the warnings we got from the tutorial 2.

1593275404 176 GPIO et Python 39 LED clignotante

5. Modifiez le script 3_blink_forever.py à l’aide de nano 3_blink_forever.py, ajoutez le code suivant:

#!/usr/bin/python



# import libraries

from time import sleep

import RPi.GPIO as GPIO



GPIO.setmode(GPIO.BCM) # set pin numbering system to bcm



# setup our output pins

GPIO.setup(17,GPIO.OUT)

GPIO.setup(27,GPIO.OUT)



# create an infinite loop

while True:

    # turn leds on

    print “lights on”

    GPIO.output(17,GPIO.HIGH)

    GPIO.output(27,GPIO.HIGH)



    sleep(1) # sleep 1 second



    # turn leds off

    print “lights off”

    GPIO.output(17,GPIO.LOW)

    GPIO.output(27,GPIO.LOW)

    sleep(1) # sleep 1 second

1593275404 113 GPIO et Python 39 LED clignotante

6. Exécutez votre script 3_blink.py

sudo python 3_blink.py

1593275404 218 GPIO et Python 39 LED clignotante

7. Exécutez votre script 3_blink_forever.py

sudo python 3_blink_forever.py

1593275404 655 GPIO et Python 39 LED clignotante

8. Pour arrêter votre script 3_blink_forever.py, appuyez simplement sur ctrl + c