Koodu Arduino ati Fidio fun Modulu Ijinna IR Sharp
Nínú ẹ̀kọ́ yìí, a óò ṣàwárí bí a ṣe lè lo ẹ̀yà Sharp IR distance module láti díwọ̀n àwọn ọ̀nà jíjìn àti láti mú àwọn ìgbésẹ̀ ṣẹlẹ̀ nígbà tí a bá rí ọ̀nà jíjìn tí a díwọ̀n. Iṣẹ́-ọwọ́ yìí yóò ṣàfihàn bí a ṣe lè ṣètò àwọn ohun èlò àti kọ kóòdù Arduino láti ka àwọn ìwọ̀n ọ̀nà jíjìn àti láti mú buzzer ṣiṣẹ́ nígbà tí a bá rí ohun kan nínú ìwọ̀n kan pàtó. Ìwọ yóò kọ́ bí a ṣe lè so àwọn ẹ̀yà pọ̀ àti láti lóye àwọn ìdámọ̀ pàtàkì nínú kóòdù náà.

Fún iṣẹ́-ọwọ́ yìí, a óò lo ẹ̀yà Sharp IR sensor tí ó ń tàn ìmọ́lẹ̀ infrared àti tí ó ń díwọ̀n ọ̀nà jíjìn sí ohun kan nígbà tí ìmọ́lẹ̀ náà bá ṣe àfihàn. Nígbà tí a bá rí ohun kan nínú ìwọ̀n náà, buzzer yóò ṣiṣẹ́, tí yóò sì fún wa ní ọ̀nà tí ó rọrùn ṣùgbọ́n tí ó gbéṣẹ́ láti fi agbára ẹ̀yà Sharp IR module hàn. Rí i dájú pé o wo fídíò náà fún ìtọ́sọ́nà ojú tí ó wúlò lórí ìṣètò àti kóòdù (nínú fídíò ní 00:00).
Àlàyé Ohun Èlò
Àwọn ẹ̀yà pàtàkì ti iṣẹ́-ọwọ́ yìí ní Sharp IR distance sensor, Arduino board, àti buzzer. Ẹ̀yà Sharp IR sensor ní olùtàn àti olùgbà tí wọ́n ń ṣiṣẹ́ papọ̀ láti díwọ̀n àwọn ọ̀nà jíjìn. Nígbà tí ìmọ́lẹ̀ infrared láti ọ̀dọ̀ olùtàn bá kan ohun kan, ó ń padà sí ọ̀dọ̀ olùgbà, tí ó sì ń jẹ́ kí ó ṣe àkàwé ọ̀nà jíjìn dá lórí àkókò tí ìmọ́lẹ̀ náà gbà láti padà.
Arduino ń ṣiṣẹ́ gẹ́gẹ́ bí olùdarí, tí ó ń ka àwọn ìwọ̀n ọ̀nà jíjìn láti ọ̀dọ̀ IR sensor àti tí ó ń darí buzzer dá lórí àwọn ìwọ̀n náà. Buzzer yóò dun nígbà tí a bá rí ohun kan nínú ìwọ̀n kan pàtó, tí yóò sì mú ìṣètò yìí wúlò fún àwọn ohun èlò bíi ìwárí ìdíwọ́ ní robotics.
Àwọn Àlàyé Datasheet
| Olùṣelọ́pọ̀ | Sharp |
|---|---|
| Nọ́mbà ẹ̀yà | GP2Y0A21YK0F |
| Fóltíìjì Logic/IO | 5 V |
| Fóltíìjì ìpèsè | 4.5 – 5.5 V |
| Fóltíìjì ìjàde | 0.4 – 2.5 V |
| Ìwọ̀n díwọ̀n | 10 – 80 cm |
| Àkókò ìdáhùn | 50 ms |
| Ìwọ̀n ìjàde | 20 mA |
| Ìpó | Module |
| Àwọn àkíyèsí / àwọn ìyàtọ̀ | Wà ní ọ̀pọ̀lọpọ̀ ìwọ̀n ọ̀nà jíjìn |
- Rí i dájú pé ìpèsè agbára tó tọ́ (4.5 – 5.5 V) wà láti yẹra fún ìbàjẹ́.
- Lo resistor láti díwọ̀n ìwọ̀n ìṣàn bí ó bá pọn dandan.
- Mú sensor náà mọ́ fún àwọn ìwọ̀n tí ó pé.
- Ṣàkíyèsí ìdílọ́wọ́ ìmọ́lẹ̀ àyíká.
- Wiring gbọ́dọ̀ múlẹ̀ dáradára láti yẹra fún àwọn ìsopọ̀ tí kò dúró ṣinṣin.
Àwọn Ìtọ́sọ́nà Wiring

Láti so ẹ̀yà Sharp IR distance module pọ̀, so pin ilẹ̀ (ground) ti module náà pọ̀ sí pin GND lórí Arduino. Lẹ́yìn náà, so pin VCC ti module náà pọ̀ sí pin 5V lórí Arduino. Pin ìjàde ifihan (signal output) láti ọ̀dọ̀ Sharp IR module yìí gbọ́dọ̀ so pọ̀ sí pin 2 lórí Arduino, èyí tí yóò ka àwọn data ọ̀nà jíjìn.
Fún buzzer, so pin tí ó gùn (positive) pọ̀ sí pin 8 lórí Arduino àti pin tí ó kúrú (negative) pọ̀ sí GND. Rí i dájú pé gbogbo àwọn ìsopọ̀ múlẹ̀ dáradára àti pé àwọn ẹ̀yà náà ní agbára tí ó tọ̀nà láti rí i dájú pé wọ́n ṣiṣẹ́ dáradára.
Àwọn Àpẹẹrẹ Kóòdù & Ìtọ́sọ́nà
Ní ìsàlẹ̀ yìí ni àyọkà kúkúrú láti inú kóòdù Arduino níbi tí a ti ń ṣàlàyé àwọn pin àti tí a ń ṣètò sensor náà. Ìdámọ̀ IR ni a ṣàlàyé gẹ́gẹ́ bí pin analog tí a ń lò fún ifihan sensor.
#define IR A0 // ṣàlàyé pin ifihan
SharpIR SharpIR(IR, model); // Bẹ̀rẹ̀ SharpIR pẹ̀lú pin tí a ṣàlàyé
Kóòdù yìí ń bẹ̀rẹ̀ ẹ̀yà Sharp IR sensor lórí pin analog tí a pàtó. Ìdámọ̀ model ń ṣàlàyé irú sensor tí a ń lò, èyí tí a ṣètò sí 1080 fún GP2Y0A21YK0F model nínú ọ̀ràn yìí.
Nínú ìgbésẹ̀ ìṣètò (setup), a bẹ̀rẹ̀ ìbánisọ̀rọ̀ serial láti ṣàkíyèsí àwọn ìwọ̀n ọ̀nà jíjìn:
void setup() {
Serial.begin(9600); // Bẹ̀rẹ̀ ìbánisọ̀rọ̀ serial
Serial.println("Robojax Sharp IR "); // Tẹ̀ ìwé sí ibi ìṣàkíyèsí serial
}
Ìgbésẹ̀ ìṣètò yìí ń bẹ̀rẹ̀ ìbánisọ̀rọ̀ serial ní 9600 baud, tí ó ń jẹ́ kí Arduino rán àwọn data ọ̀nà jíjìn sí Serial Monitor fún àkíyèsí.
Nínú ìgbésẹ̀ loop, a ka ọ̀nà jíjìn láti ọ̀dọ̀ sensor àti tí a ń tẹ̀ é jáde:
void loop() {
int dis = SharpIR.distance(); // Ka ọ̀nà jíjìn
Serial.print("Mean distance: "); // Tẹ̀ ọ̀nà jíjìn sí ibi ìṣàkíyèsí serial
Serial.println(dis); // Ṣàfihàn ìwọ̀n ọ̀nà jíjìn
}
Àyọkà yìí ń fi hàn bí a ṣe lè ka ọ̀nà jíjìn nígbà gbogbo àti tí a ṣe lè tẹ̀ é jáde sí Serial Monitor. Àṣàyàn dis ń di ìwọ̀n ọ̀nà jíjìn tí Sharp IR sensor díwọ̀n mú.
Ìṣàfihàn / Ohun Tí O Lè Retí
Nígbà tí ìṣètò bá parí, o lè retí pé buzzer yóò dun nígbàkígbà tí a bá rí ohun kan nínú ìwọ̀n tí a pàtó ti Sharp IR sensor. Serial Monitor yóò ṣàfihàn "Mean distance" pẹ̀lú ìwọ̀n ọ̀nà jíjìn ní centimeters. Bí kò bá sí ohun kan nínú ìwọ̀n náà, ibi ìṣàkíyèsí yóò fi ìyẹn hàn pẹ̀lú (nínú fídíò ní 02:30).
Àwọn ìṣòro tí ó wọ́pọ̀ ní láti rí i dájú pé sensor náà tọ́ka sí ọ̀nà tí ó tọ̀nà àti pé kò sí àwọn ohun tí ń dí ìmọ́lẹ̀ infrared lọ́wọ́. Tún ṣàyẹ̀wò pé ìpèsè agbára náà dúró ṣinṣin láti yẹra fún àwọn ìwọ̀n tí kò dúró.
Àwọn Àkókò Fídíò
- 00:00 - Ìfihàn iṣẹ́-ọwọ́ náà
- 02:30 - Ìṣàfihàn iṣẹ́ sensor náà
- 03:45 - Àlàyé wiring
- 05:00 - Ìtọ́sọ́nà kóòdù
/*
* Sharp IR (infrared) distance measurement module for Arduino
* Measures the distance in cm.
* Original library: https://github.com/guillaume-rico/SharpIR
* Watch video instruction for this code: https://youtu.be/GL8dkw1NbMc
*
* Full explanation of this code and wiring diagram is available at
* my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62
* Written by Ahmad Shamshiri on Feb 03, 2018 at 07:34
* in Ajax, Ontario, Canada. www.robojax.com
*
* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place.
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
*/
// Sharp IR code for Robojax.com
#include <SharpIR.h>
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// 430 for GP2Y0A41SK
/*
2 to 15 cm GP2Y0A51SK0F use 1080
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series use 430
10 to 80 cm GP2Y0A21YK0F use 1080
10 to 150 cm GP2Y0A60SZLF use 10150
20 to 150 cm GP2Y0A02YK0F use 20150
100 to 550 cm GP2Y0A710K0F use 100550
*/
SharpIR SharpIR(IR, model);
void setup() {
// Sharp IR code for Robojax.com
Serial.begin(9600);
Serial.println("Robojax Sharp IR ");
}
void loop() {
// Sharp IR code for Robojax.com
delay(500);
unsigned long startTime=millis(); // takes the time before the loop on the library begins
int dis=SharpIR.distance(); // this returns the distance to the object you're measuring
// Sharp IR code for Robojax.com
Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);
//Serial.println(analogRead(A0));
unsigned long endTime=millis()-startTime; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(endTime);
// Sharp IR code for Robojax.com
}
Ohun ti o le nilo
-
Amazon
Orisun & itọkasi
-
Ita gbangbaSharp IR GP2Y0A51SK0F datasheet (PDF)global.sharp
-
Ita gbangbaSharp Library from RoboJob.comrobojax.com
Fáìlìs📁
Awọn ile-ikawe Arduino (zip)
-
Arduino library for sharp IR distance sensor
robojax-sharp_IR-master.zip
Fọọmu data (pdf)
-
Sharp IR GP2Y0A21YK Infrared distance sensor datasheet
robojax_sharp_IR_GP2Y0A21YK_datasheet.pdf0.36 MB
Fayile Fritzing
-
sharp ir sensor GP2Y0A02YK0F
sharp ir sensor GP2Y0A02YK0F .fzz
Awọn faili miiran
-
Sharp GP2Y0A41SK0F Infrared distance sensor datasheet
robojax_sharp_IR_GP2Y0A41SK0F_datasheet.pdf0.84 MB -
Sharp GP2Y0A710K0F Infrared distance sensor datashee
robojax_sharp_IR_GP2Y0A710K0F_datasheet.pdf0.33 MB