Cov txheej txheem:

Tswj Ntau LEDs Nrog Python thiab Koj Raspberry Pi's GPIO Pins: 4 Cov Kauj Ruam (nrog Duab)
Tswj Ntau LEDs Nrog Python thiab Koj Raspberry Pi's GPIO Pins: 4 Cov Kauj Ruam (nrog Duab)

Video: Tswj Ntau LEDs Nrog Python thiab Koj Raspberry Pi's GPIO Pins: 4 Cov Kauj Ruam (nrog Duab)

Video: Tswj Ntau LEDs Nrog Python thiab Koj Raspberry Pi's GPIO Pins: 4 Cov Kauj Ruam (nrog Duab)
Video: Mathematics with Python! Sequences 2024, Hlis ntuj nqeg
Anonim
Tswj Ntau LEDs Nrog Python thiab Koj Raspberry Pi's GPIO Pins
Tswj Ntau LEDs Nrog Python thiab Koj Raspberry Pi's GPIO Pins

Cov Lus Qhia no qhia txog yuav ua li cas tswj ntau tus GPIO pins ntawm koj li RaspberryPi kom muaj zog 4 LEDs. Nws tseem yuav qhia koj txog cov lus tsis raug thiab cov nqe lus hais hauv Python.

Peb Cov Lus Qhia yav dhau los Siv Koj Lub Raspberry Pi's GPIO Pins kom Tswj tau LED qhia pom yuav ua li cas hloov ib qho LED rau thiab tawm los ntawm kev siv GPIO.output hais kom ua. Cov Lus Qhia no tsim los ntawm qhov kev paub ntawd los qhia koj yuav ua li cas kom tau txais kev tswj hwm ntau dua ntawm koj lub Circuit Court.

Kauj ruam 1: Yam Koj Yuav Tsum Tau

Koj Yuav Tsum Tau Dab Tsi
Koj Yuav Tsum Tau Dab Tsi
Koj Yuav Tsum Tau Dab Tsi
Koj Yuav Tsum Tau Dab Tsi

- RaspberryPi nrog Raspbian twb tau teeb tsa. Koj tseem yuav tsum muaj peev xwm nkag tau Pi siv Monitor, Mouse thiab Keyboard lossis los ntawm Remote Desktop. Koj tuaj yeem siv txhua tus qauv ntawm Raspberry Pi. Yog tias koj muaj ib qho ntawm Pi Zero cov qauv, koj yuav xav muab qee lub taub hau txuas rau GPIO chaw nres nkoj.

- LEDs Liab, Xiav, Daj thiab Ntsuab

- Ib Lub Cuab Yeej Tsis Txaus Siab Prototyping Breadboard

4 x 330 ohm Resistors

- Qee tus txiv neej rau poj niam jumper xov hlau

Kauj Ruam 2: Tsim Koj Li Circuit Court

Tsim Koj Li Circuit Court
Tsim Koj Li Circuit Court
Tsim Koj Li Circuit Court
Tsim Koj Li Circuit Court

Ua kom muaj cov kab lus saum toj no ntawm koj daim ntawv qhia zaub mov kom ntseeg tau tias tsis muaj ib qho ntawm cov khoom siv coj mus kov thiab tias LEDs tau txuas nrog txoj hauv kev.

Koj txheeb xyuas qhov ua tau zoo thiab coj tsis zoo (qhov sib txawv) ntawm koj li LEDs li cas? Yog tias koj saib ntawm LED ze, koj yuav pom tias nws muaj ob daim me me ntawm cov hlau sab hauv cov xim xim. Cov no hu ua Anode thiab Cathode. Cathode yog qhov loj tshaj ntawm ob thiab tseem txuas nrog cov LEDs tsis zoo.

Thaum koj tau txheeb xyuas koj lub Circuit Court, txuas lub jumper cables koj Raspberry Pi's GPIO pins los ntawm kev ua raws daim duab saum toj no.

Kauj Ruam 3: Tsim Tsab Ntawv los Tswj thiab Xeem cov LEDs

Tsim Tsab Ntawv los Tswj thiab Xeem cov LEDs
Tsim Tsab Ntawv los Tswj thiab Xeem cov LEDs

Ntawm koj tus Raspberry Pi, qhib IDLE (Ntawv Qhia> Programming> Python 2 (IDLE)).

Qhib ib qhov haujlwm tshiab mus rau File> New File. Tom qab ntawv ntaus (lossis theej thiab muab tshuaj txhuam) cov cai hauv qab no:

ntshuam RPi. GPIO ua GPIO

ntshuam sijhawm GPIO.setmode (GPIO. BCM) GPIO.setup (17, GPIO. OUT) GPIO.setup (18, GPIO. OUT) GPIO.setup (22, GPIO. OUT) GPIO.setup (23, GPIO. OUT) GPIO.output (17, True) time.sleep (3) GPIO.output (17, False) time.sleep (1) GPIO.output (18, True) time.sleep (3) GPIO.output (18, False) time.sleep (1) GPIO.output (22, True) time.sleep (3) GPIO.output (22, False) time.sleep (1) GPIO.output (23, True) time.sleep (3) GPIO. cov zis (23, Tsis tseeb)

Txuag koj txoj haujlwm li multilights.py (File> Save As) hauv koj li Raspberry Pis Documents folder.

Ntawm koj tus Raspberry Pi qhib Terminal (Ntawv Qhia> Accessories> Terminal) thiab mus rau koj cov ntawv tais ceev tseg los ntawm kev ntaus cov hauv qab no:

cd/home/pi/Cov ntaub ntawv

Tam sim no koj tuaj yeem khiav koj tsab ntawv tshiab los ntawm kev ntaus cov hauv qab no:

nab hab sej multilights.py

Lub teeb yuav coj nws tig mus qhib thiab tua. Cov ntawv sau saum toj no siv sijhawm.sleep hais kom tsim ib ncua ntawm txhua kauj ruam, ua rau txhua lub teeb nyob rau 3 vib nas this thiab tos rau 1 thib ob ua ntej tig lub teeb tom ntej.

Kauj Ruam 4: Ntxiv Kev Nyuaj Siab los ntawm Kev Siv Cov Ntsiab Lus thiab Cov Nqe Lus Raws Cai

Los ntawm kev siv Parameters thiab Cov Nqe Lus Raws Cai peb tuaj yeem ua cov ntawv sau saum toj no hloov pauv tau ntau dua.

Parameter tso cai rau koj khaws tus nqi uas koj tuaj yeem siv tom qab hauv tsab ntawv. Qhov feem ntau ntawm cov txiaj ntsig yog cov hlua (cov ntawv), cov lej (tus lej tag nrho) lossis ntab (cov lej zauv).

Nqe Lus Pom Zoo yuav txiav txim siab seb puas yuav tsum tau ua ntu ntu ntawm cov cai yuav tsum tau ua los ntawm kev tshuaj xyuas seb puas muaj qee qhov xwm txheej tau ntsib. Tus mob kuj tuaj yeem koom nrog qhov ntsuas.

Qhib IDLE ntawm koj li Raspberry Pi thiab qhib qhov haujlwm tshiab (Cov Ntaub Ntawv> Cov Ntaub Ntawv Tshiab). Tom qab ntawv ntaus cov hauv qab no. Ceev faj kom ntseeg tau tias txhua qhov kev nkag (tabs) suav nrog los ntawm kev siv lub tab tab:

ntshuam RPi. GPIO ua GPIO

ntshuam sijhawm los ntawm sys ntshuam argv uas yog = argv [1] ledaction = argv [2] LEDa = 17 LEDb = 18 LEDc = 22 LEDd = 23 GPIO.setmode (GPIO. BCM) GPIO.setup (LEDa, GPIO. OUT) GPIO. setmode (GPIO. BCM) GPIO.setup (LEDb, GPIO. OUT) GPIO.setmode (GPIO. BCM) GPIO.setup (LEDc, GPIO. OUT) GPIO.setmode (GPIO. BCM) GPIO.setup (LEDd, GPIO. OUT) yog ledaction == "tawm": yog qhov twg == "a": GPIO.output (LEDa, Cuav) yog qhov twg == "b": GPIO.output (LEDb, Cuav) yog tias qhov twg == "c": GPIO.output (LEDc, Cuav) yog qhov twg == "d": GPIO.output (LEDd, Cuav) yog qhov twg == "tag nrho": GPIO.output (LEDa, Cuav) GPIO.output (LEDb, Cuav) GPIO. output (LEDc, False) GPIO.output (LEDd, False) yog ledaction == "on": yog qhov twg == "a": GPIO.output (LEDa, True) yog qhov twg == "b": GPIO.output (LEDb, Tseeb) yog qhov twg == "c": GPIO.output (LEDc, Tseeb) yog qhov twg == "d": GPIO.output (LEDd, Tseeb) yog qhov twg == "tag nrho": GPIO.output (LEDa, Tseeb) GPIO.output (LEDb, Tseeb) GPIO.output (LEDc, Tseeb) GPIO.output (LEDd, Tseeb)

Txuag koj txoj haujlwm raws li kev tswj hwm.py (Cov Ntaub Ntawv> Txuag Raws Li) hauv koj cov ntawv tais ceev tseg.

nab hab sej controllight.py b rau

Qhov thib ob LED yuav tsum qhib. Tam sim no ntaus cov hauv qab no:

nab hab sej controllight.py b tawm

Qhov thib ob LED yuav tsum tua tawm.

Hauv kab 5, 6, 7 & 8, peb tsim qhov ntsuas LEDa, LEDb, LEDc thiab LEDd los khaws qhov twg GPIO tus pin peb tau txuas nrog rau LED. Qhov no ua rau peb siv lwm tus GPIO pins yam tsis tas yuav hloov pauv ntau rau tsab ntawv.

Piv txwv li, yog tias peb yuav txuas thawj LEDs coj mus rau Pin 3 (GPIO 2) hloov, peb tsuas yog yuav tsum hloov kab 5 rau cov hauv qab no:

Luas = 2

Kab 4 khaws cov txiaj ntsig koj tau ntaus tom qab tswj xyuas.py rau hauv qhov ntsuas uas yog (c) thiab kev coj ua (ntawm). Tsab ntawv tom qab ntawd siv cov kev ntsuas no, suav nrog Cov Nqe Lus Hais Txog Tus Kheej los txiav txim siab qhov LED los tswj thiab seb yuav hloov nws li lossis tawm.

Kab 16 (yog ledaction == "on":) yog nqe lus hais raws cai. Cov kab kev nkag mus uas ua raws kab lus no tsuas yog ua haujlwm yog tias cov lus pom zoo tau ua tiav. Hauv qhov xwm txheej no, qhov xwm txheej yog qhov kev coj ua muaj cov ntawv sau.

Los ntawm kev nyeem ntawv los ntawm tsab ntawv lwm Cov Lus Hais Raws Cai, koj puas tuaj yeem kwv yees tias yuav muaj dab tsi tshwm sim thaum koj ntaus cov lus txib hauv qab no hauv Terminal?

nab hab sej controllight.py tag nrho

Vim li cas ho tsis muab nws mus thiab tshaj tawm koj cov lus teb hauv cov lus hauv qab no.

Pom zoo: