Your First Program — GPIO & Built-in Sensors
Vanusele 9–14 | AlgajasõbralikThe 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.
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!
| Feature | Details |
|---|---|
| Processor | Dual-core, up to 240 MHz (super fast for its size!) |
| Wi-Fi | Built-in! Can connect to the internet |
| Bluetooth | Built-in! Can talk to your phone |
| GPIO Pins | Up to 34 pins you can program |
| Sisseehitatud andurid | Touch sensors, Hall effect sensor, Temperature sensor |
| Mälu | 520 KB RAM + 4 MB Flash |
| Toide | 3.3V (toidet saab anda USB kaudu!) |
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!
A context diagram shows how the ESP32 talks to the outside world. It's like a map of who's connected to whom!
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!
GPIO stands for General Purpose Input/Output. Need on ESP32 plaadi metallviigud, mis ühendavad seda välismaailmaga — nagu mikrokontrolleri käed!
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!
The ESP32 has sensors already built inside the chip — you don't need to connect any extra hardware!
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.
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.
Mõõdab temperatuuri chip itself (mitte ruumi). Kasulik jälgimaks, kas kiip läheb liiga kuumaks. Vahemik: ligikaudu -40°C kuni 125°C.
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!
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.
https://dl.espressif.com/dl/package_esp32_index.jsonIf 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!
Every maker's first program is Blink — making an LED turn on and off! The ESP32 has a built-in LED on GPIO 2.
| Joon | What It Does | In Simple Words |
|---|---|---|
#define LED_PIN 2 | Creates 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“ |
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.
A sequence diagram shows the order of events — who does what and when. Let's see what happens when the Blink program runs!
Kasutame ESP32 built-in capacitive touch sensor! Lisaosi pole vaja — puuduta lihtsalt puutetundliku viiguga ühendatud juhet.
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"!
The Hall sensor is built into the ESP32 chip. It detects magnetic fields — bring a magnet close and watch the values change!
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!
Nüüd ühendame external button (sisend) koos LED (väljund). Vajuta nuppu → LED süttib!
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!
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.
ESP32 GPIO works at 3.3V. Ära ühenda kunagi 5V otse. Kui vajad 5V andureid, kasuta voltage divider või level shifter.
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).
Salvesta kood alati selge nimega, näiteks blink_test_v1.ino. Loo uus versioon iga kord, kui teed suuri muudatusi, et saaksid tagasi minna!
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.
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!
Ready to test your skills? Try these challenges and level up!
Muuda Blink-programmi vilgutama 5 times fast (igaüks 100ms), seejärel peatu 2 sekundiks ja korda. Vihje: kasuta for loop!
Use the touch sensor to control LED brightness. Softer touch = dimmer, harder touch = brighter. Hint: use analogWrite() või ledcWrite() with PWM!
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?
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!
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!
| Function | What It Does | Example |
|---|---|---|
pinMode(pin, mode) | Set pin as INPUT or OUTPUT | pinMode(2, OUTPUT); |
digitalWrite(pin, val) | Set pin HIGH (on) or LOW (off) | digitalWrite(2, HIGH); |
digitalRead(pin) | Read HIGH or LOW from pin | int v = digitalRead(15); |
analogRead(pin) | Read analog value (0–4095) | int v = analogRead(34); |
touchRead(pin) | Read touch sensor value | int v = touchRead(4); |
hallRead() | Read Hall sensor value | int v = hallRead(); |
delay(ms) | Paus millisekundites | delay(1000); = 1 sec |
Serial.begin(baud) | Start serial communication | Serial.begin(115200); |
Serial.println(text) | Print text + new line | Serial.println("Hi!"); |