Alin Li ESP32 avastaja

Your First Program — GPIO & Built-in Sensors

Vanusele 9–14 | Algajasõbralik

🔌 1. Mis on ESP32?

The ESP32 is a tiny but powerful microcontroller — think of it as a small brain that can control electronic things! It was made by a company called Espressif Systems.

Key Facts

Unlike your big computer, the ESP32 is made to do one job really well — read sensors, blink lights, control motors, and talk to the internet!

FeatureDetails
ProcessorDual-core, up to 240 MHz (super fast for its size!)
Wi-FiBuilt-in! Can connect to the internet
BluetoothBuilt-in! Can talk to your phone
GPIO PinsUp to 34 pins you can program
Sisseehitatud anduridTouch sensors, Hall effect sensor, Temperature sensor
Mälu520 KB RAM + 4 MB Flash
Toide3.3V (toidet saab anda USB kaudu!)
Fun Fact

The ESP32 costs about $3–$8 — cheaper than a sandwich! Yet it has Wi-Fi, Bluetooth, and dozens of sensors packed inside. Thousands of smart home devices use it!

🗺️ 2. Kontekstiskeem — üldpilt

A context diagram shows how the ESP32 talks to the outside world. It's like a map of who's connected to whom!

ESP32 System Context Diagram

ESP32 Mikrokontroller 🌡️ Sensors (Temperatuur, puude, Halli efekt) data in 💡 LEDs / Actuators (Valgustid, mootorid, sumistid) signals out 💻 Sinu arvuti (Arduino IDE — koodi üleslaadimine) USB 🌐 Wi-Fi (Internet / Pilv) wireless 📱 Bluetooth (Telefon / Seadmed) wireless 🔋 Toide (USB või aku) 🔘 Nupud (Kasutaja sisend)
Think About It

The ESP32 sits in the center of everything. It reads data from sensors and buttons (inputs), processes the information using your code, and sends commands to LEDs and motors (outputs). It can also talk wirelessly via Wi-Fi and Bluetooth!

📌 3. GPIO-viikude mõistmine

GPIO stands for General Purpose Input/Output. Need on ESP32 plaadi metallviigud, mis ühendavad seda välismaailmaga — nagu mikrokontrolleri käed!

What Can GPIO Do?

⬇️ Digital INPUT
Read ON (1) or OFF (0). Example: Is the button pressed?
⬆️ Digital OUTPUT
Send ON (1) or OFF (0). Example: Turn LED on!
📊 AnaloogSISEND (ADC)
Read values 0–4095. Example: How bright is the light?
📈 AnaloogVÄLJUND (DAC/PWM)
Send varying signals. Example: Dim an LED slowly.

GPIO Pin Map — ESP32 DevKit

Common ESP32 Pin Functions

ESP32 WROOM-32 USB 3V3 GND GPIO 15 GPIO 2 ★ GPIO 4 GPIO 5 GPIO 18 GPIO 19 GPIO 21 VIN (5V) GND GPIO 13 (T4) GPIO 12 (T5) GPIO 14 (T6) GPIO 27 (T7) GPIO 25 (DAC1) GPIO 26 (DAC2) GPIO 33 (ADC) Toide Digital I/O Touch Sensor DAC Output Built-in LED ★ ★ GPIO 2 is connected to the built-in LED on most ESP32 boards
Important

Be careful! The ESP32 works at 3.3V, mitte 5V. Ära ühenda 5V signaali otse GPIO-viiguga — see võib su plaati kahjustada. Mõnda viiku (GPIO 6–11) kasutatakse ka sisemiselt välkmälu jaoks — don't use those!

GPIO Visual Model — Inputs & Outputs

🌡️ Sensor
INPUT
🔘 Nupp
INPUT
GPIO 34 ←
GPIO 15 ←
ESP32
Processes Data
→ GPIO 2
→ GPIO 5
💡 LED
OUTPUT
🔊 Sumisti
OUTPUT

🔍 4. Sisseehitatud andurid

The ESP32 has sensors already built inside the chip — you don't need to connect any extra hardware!

🖐️ Puuteandurid (mahtuvuslikud)

10 GPIO-viiku tuvastavad, kui sa touch them! Pins: GPIO 0, 2, 4, 12, 13, 14, 15, 27, 32, 33. They work by sensing the tiny electrical charge in your finger.

🧲 Halli efekti andur

Detects magnetic fields near the chip! Hold a magnet close to the ESP32 and it can tell — useful for detecting if a door is open or a wheel is turning.

🌡️ Internal Temperature Sensor

Mõõdab temperatuuri chip itself (mitte ruumi). Kasulik jälgimaks, kas kiip läheb liiga kuumaks. Vahemik: ligikaudu -40°C kuni 125°C.

📊 ADC (analoog-digitaalmuundur)

Reads analog voltage on certain pins (0–3.3V) and converts it into a number (0–4095). This is how you read external sensors like light sensors, potentiometers, and more!

🛠️ 5. Arduino IDE seadistamine

Before we write code, we need to set up the Arduino IDE — the free software we use to write and upload programs to the ESP32.

1
Download Arduino IDE — Go to arduino.cc/en/software and download the free IDE for your computer (Windows, Mac, or Linux).
2
Lisa ESP32 plaadi tugi — Open Arduino IDE → File → Preferences. Kleebi väljale "Additional Board Manager URLs":
https://dl.espressif.com/dl/package_esp32_index.json
3
Install ESP32 Package — Go to Tools → Board → Boards Manager. Otsi "ESP32" ja klõpsa Install on "esp32 by Espressif Systems".
4
Select Your Board — Tools → Board → ESP32 Arduino → ESP32 Dev Module.
5
Select the Port — Connect ESP32 via USB. Go to Tools → Port and select the new COM port that appears.
Tip

If no port appears, you may need to install the CP2102 or CH340 USB driver — this depends on which USB chip your ESP32 board uses. Ask your instructor!

✨ 6. Your First Program — Blink!

Every maker's first program is Blink — making an LED turn on and off! The ESP32 has a built-in LED on GPIO 2.

The Program Flow

Start
→
setup() runs once
→
LED ON
→
Wait 1 sec
→
LED OFF
→
Wait 1 sec
→
🔁 Korda lõputult

The Code

C++ / Arduino // ============================================= // Alin Li ESP32 Explorer — vilgutamisprogramm // Sinu ESIMENE programm! Paneb LED-i vilkuma. // ============================================= // Samm 1: määra LED-i viik #define LED_PIN 2 // Sisseehitatud LED on GPIO 2 peal // Samm 2: setup() töötab plaadi käivitumisel ÜKS KORD void setup() { pinMode(LED_PIN, OUTPUT); // Ütle ESP32-le: „GPIO 2 on OUTPUT“ Serial.begin(115200); // Alusta arvutiga suhtlemist (sõnumite jaoks) Serial.println("Blink program started!"); } // Samm 3: loop() töötab IGAVESTI, ikka uuesti void loop() { digitalWrite(LED_PIN, HIGH); // Lülita LED SISSE (HIGH = 3.3V) Serial.println("LED is ON"); delay(1000); // Oota 1000ms (1 sekund) digitalWrite(LED_PIN, LOW); // Lülita LED VÄLJA (LOW = 0V) Serial.println("LED is OFF"); delay(1000); // Oota veel 1 sekund }

Code Breakdown

JoonWhat It DoesIn Simple Words
#define LED_PIN 2Creates a name for pin number„Nimetame GPIO 2 nimega 'LED_PIN'“
pinMode(LED_PIN, OUTPUT)Sets pin as output„Hei, ESP32, see viik hakkab signaale SAATMA“
Serial.begin(115200)Starts serial communication„Räägime arvutiga sellel kiirusel“
digitalWrite(LED_PIN, HIGH)Sends 3.3V to pin„Saada voolu! Lülita SISSE!“
digitalWrite(LED_PIN, LOW)Sends 0V to pin„Peata vool! Lülita VÄLJA!“
delay(1000)Waits 1000 milliseconds„Oota 1 sekund“
Upload Tip

Click the → Upload button in Arduino IDE. Some ESP32 boards need you to hold the BOOT button while uploading. Release it when you see "Connecting..." in the console.

📋 7. Sequence Diagram — What Happens Step by Step

A sequence diagram shows the order of events — who does what and when. Let's see what happens when the Blink program runs!

Blink Program — Sequence Diagram

Your Computer ESP32 CPU GPIO 2 (LED) Serial SETUP Upload code via USB pinMode(2, OUTPUT) Serial.begin(115200) LOOP 🔁 korda digitalWrite(2, HIGH) print "LED is ON" 1 sekund digitalWrite(2, LOW) print "LED is OFF" 1 sekund ↩ Go back to start of loop()

🖐️ 8. Project: Touch Sensor

Kasutame ESP32 built-in capacitive touch sensor! Lisaosi pole vaja — puuduta lihtsalt puutetundliku viiguga ühendatud juhet.

Kuidas see töötab

Finger touches wire
→
Touch value drops
→
ESP32 reads value
→
If < threshold → LED ON!
Touch Sensor // ============================================= // Alin Li ESP32 Explorer — puuteandur // LED-i süütamiseks puuduta GPIO 4! // ============================================= #define LED_PIN 2 // Sisseehitatud LED #define TOUCH_PIN 4 // Puutetundlik viik (T0) #define THRESHOLD 40 // Puutetundlikkus (väiksem = tundlikum) void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(115200); Serial.println("Touch sensor ready! Touch GPIO 4..."); } void loop() { int touchValue = touchRead(TOUCH_PIN); // Loe puuteväärtus Serial.print("Touch value: "); Serial.println(touchValue); if (touchValue < THRESHOLD) { digitalWrite(LED_PIN, HIGH); // Puudutatud! LED SEES Serial.println("*** TOUCHED! ***"); } else { digitalWrite(LED_PIN, LOW); // Pole puudutatud. LED VÄLJAS } delay(100); // Väike viivitus jadaväljundi ülekoormamise vältimiseks }
How Touch Sensing Works

Your body has a tiny electrical charge. When you touch the wire, the capacitance changes. The ESP32 measures this change — when untouched, values are high (~50–80). When touched, they drop (~10–20). The THRESHOLD of 40 is the "decision line"!

🧲 9. Project: Hall Effect Sensor

The Hall sensor is built into the ESP32 chip. It detects magnetic fields — bring a magnet close and watch the values change!

Hall Sensor // ============================================= // Alin Li ESP32 Explorer — Halli andur // Tuvasta magnetid ESP32 kiibi lähedal! // ============================================= #define LED_PIN 2 #define MAG_THRESHOLD 50 // Kohanda oma magneti järgi void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(115200); Serial.println("Hall sensor ready! Bring a magnet close..."); } void loop() { int hallValue = hallRead(); // Loe magnetvälja Serial.print("Hall value: "); Serial.println(hallValue); // Magnet tuvastatakse, kui väärtus on väga suur või väike if (abs(hallValue) > MAG_THRESHOLD) { digitalWrite(LED_PIN, HIGH); Serial.println("🧲 MAGNET DETECTED!"); } else { digitalWrite(LED_PIN, LOW); } delay(200); }
Try This!

Hold a refrigerator magnet near the metal can on the ESP32 (seal asub kiip). Proovi magneti eri külgi — põhjapoolus muudab väärtuse positiivseks ja lõunapoolus negatiivseks!

🔘 10. Projekt: nupu sisend + LED-i väljund

Nüüd ühendame external button (sisend) koos LED (väljund). Vajuta nuppu → LED süttib!

Wiring Diagram

Button → ESP32 → LED Connection

🔘 Nupp Üks jalg → GPIO 15 Teine jalg → GND GPIO 15 GND ESP32 Your code here! GPIO 2 💡 LED (Sisseehitatud, GPIO 2)
Button + LED // ============================================= // Alin Li ESP32 Explorer — nupp + LED // Vajuta nuppu GPIO 15 peal → GPIO 2 LED süttib // ============================================= #define LED_PIN 2 #define BUTTON_PIN 15 void setup() { pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT_PULLUP); // Kasuta sisemist tõmbetakistit Serial.begin(115200); Serial.println("Button + LED ready!"); } void loop() { int buttonState = digitalRead(BUTTON_PIN); // INPUT_PULLUP tähendab: vajutamisel LOW, vabastamisel HIGH if (buttonState == LOW) { digitalWrite(LED_PIN, HIGH); Serial.println("Button PRESSED → LED ON"); } else { digitalWrite(LED_PIN, LOW); } delay(50); // Väike viivitus kontaktivärina vältimiseks }
What is INPUT_PULLUP?

The ESP32 has a built-in pull-up resistor inside. When you use INPUT_PULLUP, on viigu näit tavaliselt HIGH (1) ning langeb väärtusele LOW (0), kui vajutad nuppu, mis ühendab selle GND-ga. Nii pole lisatakistit vaja — ESP32-l on see sees olemas!

💡 11. Nõuanded ja head tavad

Tip #1 — Use Serial Monitor

Lisa alati Serial.println() messages to your code! Open the Serial Monitor (Tööriistad → Jadapordi monitor, määra boodikiiruseks 115200), et näha, mida sinu ESP32 teeb. Justkui annaksid oma arendusplaadile hääle.

Tip #2 — Voltage Matters

ESP32 GPIO works at 3.3V. Ära ühenda kunagi 5V otse. Kui vajad 5V andureid, kasuta voltage divider või level shifter.

Tip #3 — Safe GPIO Pins

Best pins to use: GPIO 2, 4, 5, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33. Avoid: GPIO 6–11 (flash memory), GPIO 34–39 (input-only, no pull-up).

Tip #4 — Save Your Work

Salvesta kood alati selge nimega, näiteks blink_test_v1.ino. Loo uus versioon iga kord, kui teed suuri muudatusi, et saaksid tagasi minna!

Tip #5 — Read the Errors

If uploading fails, read the orange/red text in the Arduino console! Common fixes: check USB cable, select correct board, hold BOOT button, or try a different USB port.

Tip #6 — The Power of delay()

delay(ms) pauses everything. For simple projects it's fine, but for advanced projects, learn about millis() which lets you time things without freezing your code!

🏆 12. Väljakutsed sulle!

Ready to test your skills? Try these challenges and level up!

⭐ Challenge 1 — Speed Blink

Muuda Blink-programmi vilgutama 5 times fast (igaüks 100ms), seejärel peatu 2 sekundiks ja korda. Vihje: kasuta for loop!

⭐⭐ Challenge 2 — Touch Brightness

Use the touch sensor to control LED brightness. Softer touch = dimmer, harder touch = brighter. Hint: use analogWrite() või ledcWrite() with PWM!

⭐⭐ Challenge 3 — Magnet Counter

Count how many times a magnet passes by the Hall sensor and display the count on Serial Monitor. Can you make it count only once per pass?

⭐⭐⭐ Challenge 4 — Secret Knock

Use the touch sensor to create a "secret knock" pattern. The LED only turns on if you tap the wire in the right pattern (e.g., tap-tap-pause-tap). Think about timing!

⭐⭐⭐ Challenge 5 — Multi-Sensor Dashboard

Read ALL sensors (touch, hall, and a button) at once, and print a formatted dashboard to the Serial Monitor that updates every second. Make it look nice with labels and separators!

📝 Quick Reference Card

FunctionWhat It DoesExample
pinMode(pin, mode)Set pin as INPUT or OUTPUTpinMode(2, OUTPUT);
digitalWrite(pin, val)Set pin HIGH (on) or LOW (off)digitalWrite(2, HIGH);
digitalRead(pin)Read HIGH or LOW from pinint v = digitalRead(15);
analogRead(pin)Read analog value (0–4095)int v = analogRead(34);
touchRead(pin)Read touch sensor valueint v = touchRead(4);
hallRead()Read Hall sensor valueint v = hallRead();
delay(ms)Paus millisekunditesdelay(1000); = 1 sec
Serial.begin(baud)Start serial communicationSerial.begin(115200);
Serial.println(text)Print text + new lineSerial.println("Hi!");