Cov txheej txheem:

Dimming thiab ci ntsa iab LED nrog Arduino: 7 Kauj Ruam (nrog Duab)
Dimming thiab ci ntsa iab LED nrog Arduino: 7 Kauj Ruam (nrog Duab)

Video: Dimming thiab ci ntsa iab LED nrog Arduino: 7 Kauj Ruam (nrog Duab)

Video: Dimming thiab ci ntsa iab LED nrog Arduino: 7 Kauj Ruam (nrog Duab)
Video: Tswv Yexus lub neej thaum pib txug thaum xaus 2024, Hlis ntuj nqeg
Anonim
Dimming thiab ci ntsa iab LED nrog Arduino
Dimming thiab ci ntsa iab LED nrog Arduino

Ua ntej koj pib tsim, koj yuav tsum tau txais cov ntaub ntawv raug:

  • 1 Arduino Board - Kuv tau siv qhov poob ntawm Arduino Uno, tab sis nws ua haujlwm ib yam nkaus.
  • 1 Potentiometer - kuv zoo li txawv dua li feem ntau, tab sis lawv kuj ua haujlwm tib yam.
  • 1 Cov qhob cij
  • Ob peb jumper cables
  • 1 LED thiab Resistor - Kuv xav kom tus tiv thaiv ntau dua 250 ohms rau kev nyab xeeb.
  • Lub khoos phis tawj nrog Arduino IDE teeb tsa

Thaum kawg, ceev faj! Koj tab tom ua haujlwm nrog cov khoom ntse thiab dej ntws ntawm no yog li ceev faj nrog txhua kauj ruam.

Kauj Ruam 1: Nqa Arduino Board rau Koj Lub Computer

Hook Up Arduino Board rau Koj Lub Computer
Hook Up Arduino Board rau Koj Lub Computer

Nqa koj tus Arduino mus rau koj lub khoos phis tawj siv USB cable uas tuaj nrog nws. Yog tias koj tsis tau teeb tsa koj li Arduino, txuas koj Arduino rau koj lub khoos phis tawj, thiab paub tseeb tias koj cov kev teeb tsa raug. Hauv qab "Cov Cuab Yeej," xaiv "Chaw Nkag" thiab xyuas kom tseeb tias koj nyem qhov uas koj txuas nrog koj Arduino ib yam. Tsis tas li, nco ntsoov hauv qab "Cov Cuab Yeej," koj muaj hom Arduino Board raug xaiv hauv "Pawg Thawj Coj."

Thaum koj ua tiav qhov ntawd, saib ntawm "Power" pins, "Analog in" pins, thiab "Digital" pins. Ua daim ntawv ceeb toom rau squigglies ("~") ib sab ntawm qee tus lej hauv ntu "Digital" pins. Cov squigglies txhais tau tias cov pins no siv Pulse Width Modulation (PWM), uas tsuas yog lub ntsiab lus zoo nkauj uas txhais tau tias nws tuaj yeem txhais cov cim sib piv rau digital. Qhov no yuav los yooj yim hauv cov kauj ruam tom ntej, yog li ua tib zoo ceeb toom.

Kauj Ruam 2: Muab Lub Hwj Huam Rau Koj Daim Ntawv Cuam Tshuam

Muab Lub Hwj Huam Rau Koj Daim Ntawv Cuam Tshuam
Muab Lub Hwj Huam Rau Koj Daim Ntawv Cuam Tshuam

Yog lawm, tam sim no koj tau teeb tsa txhua yam, nqa ob lub xov hluav taws xob txuas, thiab txuas ib lub xov tooj txuas los ntawm "5V" ntawm ntu "Fais fab" pins mus rau kab ntawm qhov hauv qab "+" kos npe. Txuas lwm lub jumper cable los ntawm "GND" ntawm ntu "Power" pins mus rau kem ntawm qhov hauv qab "-" kos npe. Qhov no yuav tsim lub zog thiab kab hauv av ntawm qhov ntawm koj lub khob cij.

Kauj Ruam 3: Siv Potentiometer

Siv Potentiometer
Siv Potentiometer

Yog tias koj twb paub tias lub hwj chim ntsuas hluav taws xob yog dab tsi thiab nws ua haujlwm li cas, koj tuaj yeem hla cov kauj ruam no. Yog tias koj tsis ua, kuv yuav piav qhia nws ntawm no.

Lub potentiometer muaj 3 tus pin. 2 tus pin ntawm sab laug thiab sab xis yog Lub Hwj Chim thiab Hauv av, thiab lawv rov qab tau, txhais tau tias koj tuaj yeem txuas 5V rau tus pin sab laug thiab GND rau tus pin sab xis thiab rov ua dua thiab nws tseem yuav ua haujlwm. Tus pin nruab nrab yog tus pin "data". Thaum koj tig lub hwj chim ntsuas, tus pin nruab nrab tsuas yog tso tawm qhov nyeem ntawv.

Kauj Ruam 4: Txuas Potentiometer

Txuas tus Potentiometer
Txuas tus Potentiometer

Tam sim no koj paub tias lub hwj chim ntsuas yog dab tsi, cia peb txuas nws lub khob cij. Koj yuav siv nws los hloov qhov ci ci ntawm LED. Khaws koj lub potentiometer ntawm koj lub khob cij. Kuv pom zoo kom koj ntxig nws mus rau nruab nrab ntawm kuv daim ntawv qhia zaub mov yog li kuv yuav muaj qhov chaw los txuas qee tus pin ntawm nws. Txuas tus laug (lossis sab xis) tus pin ntawm lub hwj chim rau kab ntawv fais fab ntawm koj lub khob cij thiab txuas sab xis (lossis sab laug) tus pin ntawm lub hwj chim rau hauv kab hauv av. Tam sim no siv lub jumper cable txuas rau "cov ntaub ntawv" tus pin ntawm koj lub zog ntsuas mus rau tus pin hauv ntu "Analog" pins. Kuv txuas kuv tus lej mus rau "A0."

Kauj ruam 5: LED

Cov LED
Cov LED

Tam sim no hais tias lub hwj chim nyob hauv, cov kauj ruam tom ntej yog txuas LED. Ntxig lub LED mus rau koj lub khob cij thiab siv lub jumper cable los txuas tus "Digital" tus pin nrog "~" ib sab ntawm nws nrog cov ceg ntev ntawm LED (tsis txhob sib xyaw nws nrog ceg luv, txwv tsis pub nws yuav tsis ua haujlwm). Tam sim no koj yuav tsum tso tus tiv thaiv kom tiv thaiv koj cov LED los ntawm kev hlawv. Muab ib qho kawg ntawm tus tiv thaiv rau tib kab ua luv luv ntawm koj li LED, thiab lwm qhov kawg hauv kab hauv av ntawm koj lub rooj noj mov.

Kauj ruam 6: Sijhawm rau Code

Sijhawm rau Code!
Sijhawm rau Code!

Zoo kawg! Txhua yam nyob hauv qhov chaw. Sijhawm rau Code!

Hauv daim duab, Kuv muaj tus piv txwv ntawm qhov kuv tau ua. Thaum pib, yuav muaj ob txoj haujlwm: "tsis muaj teeb meem teeb tsa ()" thiab "tsis muaj dab tsi voj ()." Yog tias koj tshiab rau Arduino, teeb tsa () muaj nuj nqi yog siv rau "teeb tsa" yam uas koj txuas nrog rau tus pin ntawm Arduino Board. Lub voj () muaj nuj nqi yog qhov twg khawv koob tiag tshwm sim: nws yooj yim loops los ntawm cov cai koj sau hauv txoj haujlwm.

Thawj ob kab, Kuv siv cov lej sib txawv "LED" thiab teeb nws rau 6 (6 yog tus pin kuv txuas tus LED ntawm kuv daim ntawv lo rau, yog li yog koj siv tus lej sib txawv, teeb nws rau tus lej tus lej ntawd). Kuv kuj teeb tsa tus lej sib txawv "potentiometer" rau "A0" vim tias yog tus pin kuv txuas kuv tus potentiometer rau (dua, yog tias koj siv tus pin sib txawv, teeb tsa koj qhov sib txawv rau tus pin ntawd).

Hauv kev teeb tsa () muaj nuj nqi, Kuv pib Serial Monitor (Kuv yuav tham tom qab ntawd) thiab ntaus "pinMode (LED, OUTPUT)." Cov lus no cia Arduino paub tias tus pin 6 (uas sib npaug sib txawv "LED") yog qhov tso tawm, lub ntsiab lus nws yuav raug tso tawm qhov hluav taws xob. Kuv tsis ntaus "pinMode (potentiometer, INPUT)" vim tias, los ntawm lub neej ntawd, nws twb yog cov tswv yim.

Hauv lub voj () ua haujlwm, tsim thiab teeb tsa qhov sib txawv (Kuv siv "pob qhov rooj") sib npaug rau yam twg los xij ntawm lub hwj chim ntsuas tau nyeem siv "analogRead (/*koj lub npe rau tus lej potentiometer pin*/)" (rau kuv nws yog analogRead (potentiometer)). Tom qab ntawd "daim ntawv qhia" qhov sib txawv. Qhov ntawd txhais li cas? Potentiometer siv tus nqi nruab nrab ntawm 1 txog 1024, thiab nws yuav tsum nyob nruab nrab ntawm 1 txog 255 rau koj li LED kom ci thiab ci kom raug. Qhov "daim ntawv qhia" ua haujlwm faib lub hwj chim sib npaug li ntawm 1/255, uas yuav muaj txiaj ntsig zoo thaum ua haujlwm ntawm LED.

Qhov txuas ntxiv no yog nyob ntawm yeem, tab sis siv Serial Monitor, koj tuaj yeem pom tus nqi ntawm cov khoom siv hluav taws xob. Yog tias koj pib Serial monitor hauv qab teeb tsa () muaj nuj nqi thiab nug nws kom luam tawm qhov sib txawv hauv lub voj () muaj nuj nqi (Kuv tau ua "Serial.println (pob qhov rooj)" uas qhia rau kuv saib xyuas tus nqi ntawm pob qhov rooj), thaum koj pib program thiab nyem rau ntawm lub iav tsom iav icon nyob rau sab xis saum toj, yuav muaj cov npe loj ntawm cov lej uas yuav raug hloov kho tas li. Cov lej ntawd yuav yog tus nqi tam sim no ntawm koj lub hwj chim thaum koj tig nws.

Thaum kawg, sau tus nqi ntawm lub hwj chim (uas kuv khaws cia hauv qhov sib txawv "pob qhov rooj") rau LED los ntawm kev ntaus "analogWrite (/*xijpeem koj hu koj lub npe LED sib txawv*/,/*xijpeem koj lub npe koj lub zog potentiometer sib txawv*/)" (hauv kuv qhov xwm txheej, kuv ntaus "analogWrite (LED, potentiometer)").

Sau thiab xa cov cai thiab ua si nrog koj cov dimmable LED!

Kauj ruam 7: Txaus siab rau

Nrog koj zoo siab! Koj ua nws!

Pom zoo: