GPIO et Python (2/9) – LED

GPIO et Python (2/9) - LED

Dans ce projet, vous apprendrez à allumer et éteindre 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 29 LED

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

cd gpio_python_code 

1593282605 616 GPIO et Python 29 LED

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

touch 2_on.py

1593282605 418 GPIO et Python 29 LED

3. Ensuite, créez un fichier pour notre script led off

touch 2_off.py

1593282605 434 GPIO et Python 29 LED

4. Modifiez le fichier 2_on.py à l’aide de nano 2_on.py et ajoutez le code suivant

#!/usr/bin/python



import RPi.GPIO as GPIO # import a library



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



GPIO.setup(17,GPIO.OUT) # set GPIO17 as an OUTPUT

GPIO.setup(27,GPIO.OUT) # set GPIO27 as am OUTPUT



print "Lights on" # print to the screen so we know what’s going on



GPIO.output(17,GPIO.HIGH) # set GPIO17 high, 3v3 will now be active on that pin

GPIO.output(27,GPIO.HIGH) # set GPIO27 high, 3v3 will now be active on that pin

1593282606 196 GPIO et Python 29 LED

5. Quittez et enregistrez le fichier, CTRL + x, Y, entrez

6. Modifiez le fichier 2_off.py à l’aide de nano 2_off.py et ajoutez le code suivant

#!/usr/bin/python



import RPi.GPIO as GPIO # import a library



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



GPIO.setup(17,GPIO.OUT) # set GPIO17 as an OUTPUT

GPIO.setup(27,GPIO.OUT) # set GPIO27 as am OUTPUT



print "Lights off" # print to the screen so we know what’s going on



GPIO.output(17,GPIO.LOW) # set GPIO17 low, 3v3 will now be de-active on that pin

GPIO.output(27,GPIO.LOW) # set GPIO27 low, 3v3 will now be de-active on that pin

2 off code

7. Quittez et enregistrez le fichier, CTRL + x, Y, entrez

8. Pour allumer vos LED, exécutez votre script 2_on.py:

sudo python 2_on.py

1593282607 58 GPIO et Python 29 LED

Vous pourriez voir quelques avertissements, pour l’amour de ce tutoriel, vous pouvez les ignorer.
Ils vous font simplement savoir que les broches GPIO utilisées dans le script peuvent avoir déjà
été configuré.

8. Pour éteindre vos LED, exécutez votre script 2_off.py:

sudo python 2_off.py

1593282607 549 GPIO et Python 29 LED

Vous verrez probablement des avertissements, pour l’amour de ce tutoriel, vous pouvez les ignorer.
Ils vous font simplement savoir que les broches GPIO utilisées dans le script peuvent avoir déjà
été configuré.