Ẹ̀kọ́ ESP32 36/55 - Eré Ìdánwò Nọ́ńbà | Àkójọpọ̀ Ẹ̀kọ́ IoT ESP32 ti SunFounder
Nínú ẹ̀kọ́ yìí, a ó ṣẹ̀dá eré ìdánwò nọ́mbà tí ó dùn mọ́ ní lílo ESP32 microcontroller pẹ̀lú infrared remote àti LCD display. Ẹni tí ó ń ṣeré yóò gbìyànjú láti ṣe amojú tọ́ka nọ́mbà tí a ṣe àdánidá láàárín 0 sí 99, pẹ̀lú eré tí ń pèsè àmì ìtọ́ka nípa bóyá ìfojú tọ́ka náà ga jù tàbí kéré jù. Nípasẹ̀ iṣẹ́-ọnà yìí, ìwọ yóò kọ́ bí a ṣe ń lo infrared receiver, ṣe àfihàn àwọn iye lórí LCD, àti bí a ṣe ń ṣàkóso ìfẹ̀sì olùmúlò láti remote. O lè wo fídíò náà fún ìmọ̀ síwájú sí i (nínú fídíò ní 00:00).
Àlàyé Ohun-èlò
Àwọn èròjà pàtàkì tí a ń lò nínú iṣẹ́-ọnà yìí ní ESP32 microcontroller, infrared remote, infrared receiver, àti LCD display. ESP32 ṣiṣẹ́ gẹ́gẹ́ bí ẹ̀yà ìṣàkóso pàtàkì tí ó lè ṣàkóso ìbánisọ̀rọ̀ aláìlókùn, tí ó mú un di yíyan ààyò fún àwọn iṣẹ́ IoT. Infrared remote gba àwọn olùmúlò láyè láti ṣe àmújáde ìfojú tọ́ka láìní ìbáṣepọ̀ ara pẹ̀lú board, nígbà tí LCD ń ṣàfihàn ipò eré àti àwọn ìtọ́ka.
Infrared receiver ń ṣàwárí àwọn ifihan agbára láti remote àti ṣe ìtumọ̀ wọn fún lílo nínú eré. Kọ̀ọ̀kan bọ́tìì tí a tẹ̀ lórí remote bá iye pàtó kan tí ESP32 lè mọ̀. LCD display ń pèsè ojú ìsọ̀rọ̀ fún olùmúlò, tí ń fi ààyè ìfojú tọ́ka lọ́wọ́lọ́wọ́ àti bóyá ìfojú tọ́ka náà tọ̀nà.
Àwọn Àlàyé Datasheet
| Olùṣe | SunFounder |
|---|---|
| Nọ́mbà apá | ESP32 |
| Logic/IO folti | 3.3 V |
| Folti ìpèsè | 5 V (nípasẹ̀ USB) |
| Ìwọ̀nà jáde (fún ikanni) | 12 mA (pọ̀ jù) |
| Ìtọ́sọ́nà ìgbohùnsá PWM | 1 kHz |
| Àwọn ìpele ìṣàkóso ìfẹ̀sì | 0.3 * Vcc sí 0.7 * Vcc |
| Ìdìndì folti / RDS(on) / ìkún | 0.2 V |
| Àwọn ìwọ̀nà ooru | Ìwọ̀nà iṣẹ́: -40 sí 85 °C |
| Àpò | WROOM-32 module |
| Àwọn àkíyèsí / àwọn oríṣi | Ṣe àtìlẹ́yìn Wi-Fi àti Bluetooth |
- Rí i dájú pé gbogbo àwọn èròjà ní ìwọ̀nà 3.3 V àti 5 V níbi tí ó bá yẹ.
- Lo àwọn resistor ìfà sókè tí ó tọ̀nà fún IR receiver láti yẹra fún àwọn ìfẹ̀sì tí ń lọfo.
- Rò nípa lílo heat sink bí o bá ń ṣiṣẹ́ ní ìwọ̀nà jáde tí ó pọ̀ jù fún àkókò gígùn.
- Nígbà tí o bá ń lo PWM, pa ìwọ̀nà ìgbohùnsá mọ́ ní nǹkan bí 1 kHz fún iṣẹ́ tí ó dára jù.
- Ṣọ́ra pẹ̀lú ìsopọ̀; rí i dájú pé àwọn ìsopọ̀ ṣe dáradára láti yẹra fún àwọn ìkùnà tí ń wáyé lẹ́ẹ̀kọ̀ọ̀kan.
Àwọn Ìtọ́sọ́nà Ìsopọ̀

Láti ṣètò ìsopọ̀ fún eré ìfojú tọ́ka nọ́mbà, bẹ̀rẹ̀ nípa ìsopọ̀ infrared receiver. So okun pupa láti ọ̀tún pin ti receiver sí 3.3 V ìpèsè agbára lórí ESP32. Okun dúdú yóò lọ sí ilẹ̀, nígbà tí òsì pin ti infrared receiver so sí pin 14 lórí ESP32.
Nígbà tí o bá ṣe èyí, so LCD display. So VCC pin ti LCD sí 5 V ìpèsè lórí ESP32 àti pin ilẹ̀ sí ilẹ̀. SDA àti SCL pins ti LCD yẹ kí a so sí pins 21 àti 22 lẹ́sẹ̀sẹ̀. Rí i dájú pé àwọn ààyè méjì òfo wà láàárín àwọn ìsopọ̀ SDA àti SCL láti yẹra fún àwọn ìforígbárí pin. Níkẹyìn, rí i dájú láti yọ àwọn ìbòrà ṣíṣù láti bátì kí o tó fi agbára sí board.
Àwọn Àpẹẹrẹ Kóòdù & Ìtọ́sọ́nà
Àwọn àpẹẹrẹ kóòdù tí ó tẹ̀le yìí ṣàlàyé àwọn apá pàtàkì ti ètò tí a ń lò fún eré ìfojú tọ́ka nọ́mbà. A bẹ̀rẹ̀ nípa fífi àwọn ilé-ìkàwé pàtàkì sí inú àti ṣàlàyé àwọn ìdámọ̀ pàtàkì.
#include
#include
#include
#include
const uint16_t IR_RECEIVE_PIN = 14;
IRrecv irrecv(IR_RECEIVE_PIN);
decode_results results;
Nínú àyọkà yìí, a fi àwọn ilé-ìkàwé sí inú fún ṣíṣe LCD àti iṣẹ́ IR receiver. Pin fún infrared receiver jẹ́ ìtumọ̀ sí 14, àti a ṣẹ̀dá àwọn àpẹẹrẹ ti àwọn ohun pàtàkì láti ṣàkóso ìfẹ̀sì àti ìjàde.
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
irrecv.enableIRIn();
initNewValue();
}
Àyọkà yìí ṣàfihàn iṣẹ́ setup níbi tí a ti ń bẹ̀rẹ̀ LCD, bẹ̀rẹ̀ ìbánisọ̀rọ̀ serial, àti ṣíṣe IR receiver. Iṣẹ́ initNewValue() ni a ń pè láti ṣẹ̀dá nọ́mbà àdánidá tuntun fún ẹni tí ń ṣeré láti ṣe amojú tọ́ka.
bool detectPoint() {
if (count > pointValue) {
if (count < upper) upper = count;
} else if (count < pointValue) {
if (count > lower) lower = count;
} else if (count == pointValue) {
count = 0;
return 1;
}
count = 0;
return 0;
}
Iṣẹ́ yìí ń ṣàyẹ̀wò ìfojú tọ́ka ẹni tí ń ṣeré lòdì sí nọ́mbà àdánidá, tí ń ṣàtúnṣe àwọn ìwọ̀nà òkè àti ìsàlẹ̀ ní ìbámu. Bí ìfojú tọ́ka náà bá tọ̀nà, ó tún count ṣètò sí 0, àti ó padà pẹ̀lú òtítọ́.
Kóòdù kíkún ni a gbé sí ìsàlẹ̀ àpilẹ̀kọ yìí fún ìtọ́kasí rẹ.
Ìfihàn / Ohun tí o yẹ kí o rí
Ni gbogbo ohun ti a ti so pọ ati pe koodu ti gbe soke, ere naa yoo beere lọwọ rẹ lati tẹ nọmba eyikeyi lori ẹrọ isakoso latọna jijin. Ere naa yoo fun ọ ni esi lori awọn akiyesi rẹ, ti o n ṣe imudojuiwọn iwọn awọn nọmba ti o ṣeeṣe titi ti o fi ṣe akiyesi nọmba ibi-afẹde naa ni deede. Ti o ba tẹ bọtini POWER, ere naa yoo tun bẹrẹ ati ki o bẹrẹ lẹẹkansi (ninu fidio ni 02:30).
Awọn idiwọ ti o wọpọ pẹlu rii daju pe olugba infurarẹẹdi ti wa ni iṣalaye daradara ati pe gbogbo awọn asopọ ti wa ni aabo. Ti ere naa ko ba dahun, ṣayẹwo ipese agbara ati rii daju pe ọkọ ati ibudo ti o tọ ni a yan ninu Arduino IDE.
Awọn Akoko Fidio
- 00:00 Ibẹrẹ
- 2:17 Ifihan si iṣẹ akanṣe ere naa
- 4:37 Sisopọ Awọn Okun
- 6:15 Alaye Koodu Arduino
- 12:32 Yiyan Bọọdi ESP32 ati Ibudo COM ninu Arduino IDE
- 16:16 Ṣiṣere ere nọmba akiyesi
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
// Define the IR receiver pin
const uint16_t IR_RECEIVE_PIN = 14;
// Create an IRrecv object
IRrecv irrecv(IR_RECEIVE_PIN);
// Create a decode_results object
decode_results results;
const long interval = 1000;
unsigned long previousMillis = 0;
// Initialize the LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the input number value
int count = 0;
// Initialize the random lucky point
int pointValue = 0;
// Initialize the upper and lower limit tips
int upper = 99;
int lower = 0;
void setup() {
// Initialize the LCD screen
lcd.init();
lcd.backlight();
// Start the serial communication
Serial.begin(9600);
// Enable the IR receiver
irrecv.enableIRIn();
// Initialize a new lucky point value
initNewValue();
}
void loop() {
// If a signal is received from the IR receiver
if (irrecv.decode(&results)) {
bool result = 0;
String num = decodeKeyValue(results.value);
// If the POWER button is pressed
if (num == "POWER") {
initNewValue(); // Initialize a new lucky point value
}
// If the CYCLE button is pressed
else if (num == "CYCLE") {
result = detectPoint(); // Detect the input number
lcdShowInput(result); // Show the result on the LCD screen
}
// If a number button (0-9) is pressed,
//add the digit to the input number
//and detect the number if it is greater than or equal to 10
else if (num >= "0" && num <= "9") {
count = count * 10;
count += num.toInt();
if (count >= 10) {
result = detectPoint();
}
lcdShowInput(result);
}
irrecv.resume();
}
}
// Function to initialize a new lucky point value
void initNewValue() {
// Set the random seed based on the analog value from pin A0
randomSeed(analogRead(A0));
// Generate a new random lucky point value
pointValue = random(99);
// Reset the upper and lower limit tips
upper = 99;
lower = 0;
// Show the welcome message on the LCD screen
lcd.clear();
lcd.print(" Welcome!");
lcd.setCursor(0, 1);
lcd.print("Press Any Number");
// Reset the input number value
count = 0;
// Print the lucky point value to the serial monitor
Serial.print("point is ");
Serial.println(pointValue);
}
// Detect the input number
//and update the upper/lower limit tips accordingly
bool detectPoint() {
if (count > pointValue) {
if (count < upper)upper = count;
}
else if (count < pointValue) {
if (count > lower)lower = count;
}
// If the input number is equal to the lucky point value,
else if (count == pointValue) {
// Reset the input number and return true
count = 0;
return 1;
}
// Reset the input number and return false
count = 0;
return 0;
}
// Show the input number and the upper/lower limit tips
void lcdShowInput(bool result) {
lcd.clear();
// If the input number is equal to the lucky point value
if (result == 1)
{
// Show the success message and initialize a new lucky point value
lcd.setCursor(0, 1);
lcd.print(" You've got it! ");
delay(5000);
initNewValue();
return;
}
lcd.print("Enter number:");
lcd.print(count);
lcd.setCursor(0, 1);
lcd.print(lower);
lcd.print(" < Point < ");
lcd.print(upper);
}
// Function to decode the key value from the IR receiver signal
String decodeKeyValue(long result)
{
switch(result){
case 0xFF6897:
return "0";
case 0xFF30CF:
return "1";
case 0xFF18E7:
return "2";
case 0xFF7A85:
return "3";
case 0xFF10EF:
return "4";
case 0xFF38C7:
return "5";
case 0xFF5AA5:
return "6";
case 0xFF42BD:
return "7";
case 0xFF4AB5:
return "8";
case 0xFF52AD:
return "9";
case 0xFF906F:
return "+";
case 0xFFA857:
return "-";
case 0xFFE01F:
return "EQ";
case 0xFFB04F:
return "U/SD";
case 0xFF9867:
return "CYCLE";
case 0xFF22DD:
return "PLAY/PAUSE";
case 0xFF02FD:
return "BACKWARD";
case 0xFFC23D:
return "FORWARD";
case 0xFFA25D:
return "POWER";
case 0xFFE21D:
return "MUTE";
case 0xFF629D:
return "MODE";
case 0xFFFFFFFF:
return "ERROR";
default :
return "ERROR";
}
}
Common Course Links
Common Course Files
Orisun & itọkasi
-
Iwe-ẹriESP32 Tutorial 36/55 - SunFounder doc page for guessing numberdocs.sunfounder.com
Fáìlìs📁
Ko si awọn faili ti o wa.