Robotika ● שיעור 3Lesson 3
→ חזרה← Back
קורס ארדואינו · מפגש 3Arduino course · Session 3

שלט רחוק
IR · RF · Bluetooth
Remote control
IR · RF · Bluetooth

קודם נסיים את המכונית ש"נוהגת לבד", ואז ניקח את ההגה: נלמד שלוש דרכים לשלוט במכונית מרחוק — באור, בגלי רדיו, ומהטלפון. First we finish the car that "drives itself", then we take the wheel: three ways to control the car from a distance — with light, radio waves, and your phone.

IR — שלט אינפרא-אדום (אור)Infrared remote (light)
RF — גלי רדיו 433MHz433MHz radio
BT — Bluetooth מהטלפוןBluetooth from your phone

ניווט: חצים ← → במקלדת, או הכפתורים למטה · לחצו EN לבדיקת מונחים באנגלית Navigate: ← → arrow keys, or the buttons below · press עב for Hebrew

2מה נעשה היוםWhat we'll do today

הרעיון הגדולThe big idea

  • אותן פונקציות תנועה (קדימה / אחורה / פנייה / עצירה) — רק הקלט משתנהSame motion functions (forward / back / turn / stop) — only the input changes
  • חיישן מחליט לבד ← אתם מחליטים עם שלטA sensor decides on its own → you decide with a remote
  • שלוש טכנולוגיות אלחוטיות וההבדלים ביניהןThree wireless technologies and how they differ

הבנייה — בשלביםThe build — in levels

  • שלב 0: לסיים את המכונית שנוהגת לבד (נסיעה + התחמקות)Level 0: finish the self-driving car (drive + avoid)
  • שלב 1: שליטה בשלט IR — בנייה מלאהLevel 1: control with an IR remote — full build
  • שלב 2: שליטה ברדיו RF433 — בנייה מלאהLevel 2: control over RF433 radio — full build
  • פינאלה: נהיגה מהטלפון ב-Bluetooth (הדגמה — נסיים בשיעור הבא)Finale: drive from your phone over Bluetooth (demo — we finish next lesson)
יכול להיות שלא נספיק הכול היום — וזה בסדר גמור. הכול כאן בשקופיות.We may not finish it all today — that's completely fine. Everything is here in the slides.

3תזכורת: המכונית משיעור 2Recap: the car from Lesson 2

בנינו מכונית עם 4 מנועים, מגשר L298N שנותן להם כוח, וחיישן HC-SR04 מלפנים. הקוד השתמש ב-if / else: אם קרוב — לעצור, אחרת — לנסוע. We built a car with 4 motors, an L298N H-bridge to power them, and an HC-SR04 sensor up front. The code used if / else: if close — stop, else — drive.

  • L298N — פין חלש של הארדואינו לא מפעיל מנוע; המגשר לוקח כוח מסוללה ומאפשר גם כיווןL298N — a weak Arduino pin can't run a motor; the H-bridge takes battery power and also sets direction
  • שתי פונקציות בלבד: forward() ו-stopCar()Only two functions: forward() and stopCar()
אבל — המכונית רק עצרה מול מכשול. זו עדיין לא "נהיגה עצמית".But — the car only stopped at an obstacle. That's not really "self-driving" yet.

חיווט המכונית (כמו בשיעור 2)The car's wiring (as in Lesson 2)

חיבורConnectionפיןPin
HC-SR04 Trig / EchoD9 / D10
ENA (מהירות שמאלleft speed)D5
IN1 / IN2 (שמאלleft)D2 / D3
IN3 / IN4 (ימיןright)D4 / D7
ENB (מהירות ימיןright speed)D6
4 מנועים, מגשר אחד: שני המנועים בכל צד מחוברים יחד לאותו ערוץ ב-L298N. לכן בקוד יש רק שני צדדים — שמאל וימין.4 motors, one H-bridge: the two motors on each side are wired together into the same L298N channel. So in code there are only two sides — left and right.

⚠️ הסוללה של המנועים והארדואינו חייבים גראונד משותף.⚠️ The motor battery and the Arduino must share a common ground.

4רגע — למה שני פיני IN לכל צד?Wait — why two IN pins per side?

פין דיגיטלי יודע רק שני מצבים: דלוק / כבוי. הוא יכול להדליק מנוע — אבל לא להפוך אותו. כדי לנסוע אחורה צריך להחליף את הקוטביות (+/–) של המנוע. A digital pin has only two states: on / off. It can turn a motor on — but it can't reverse it. To go backward you must swap the motor's polarity (+/–).

בדיוק לזה נועד המגשר H (H-bridge): לכל ערוץ יש זוג פינים (IN1+IN2) שקובעים לאן זורם הזרם — כלומר את הכיוון. פין EN נפרד שולט במהירות (PWM). That's exactly what an H-bridge is for: each channel has a pair of pins (IN1+IN2) that set which way the current flows — i.e. the direction. A separate EN pin sets the speed (PWM).

IN1IN2תוצאהResult
10קדימהForward
01אחורהReverse
00עצירה (חופשי)Stop (coast)
11בלימהBrake
לכן: 4 מנועים, 2 צדדים, 2 ערוצים — אבל 4 פיני IN (זוג לכל צד לכיוון) + 2 פיני EN (למהירות).So: 4 motors, 2 sides, 2 channels — but 4 IN pins (a pair per side for direction) + 2 EN pins (for speed).

שנו את IN1 / IN2 ואת המהירות — וראו את המנוע:Toggle IN1 / IN2 and the speed — and watch the motor:

5שלב 0: מכונית שמתחמקת (לא רק עוצרת)Level 0: a car that avoids (not just stops)

מכונית שנוהגת לבד צריכה לצאת מהצרה: אם חסום — לסגת קצת, להסתובב, ולהמשיך. בשביל זה צריך שני כיוונים חדשים. A self-driving car must get out of trouble: if blocked — back up a bit, turn, and carry on. For that we need new directions.

ארבע תנועות מה-L298NFour moves from the L298N

  • קדימה: שני הצדדים קדימהForward: both sides forward
  • אחורה: שני הצדדים אחורה (הופכים IN1/IN2)Backward: both sides reverse (flip IN1/IN2)
  • פנייה ימינה: שמאל קדימה, ימין אחורה ← מסתובב במקוםTurn right: left forward, right back → spins in place
  • פנייה שמאלה: הפוךTurn left: the opposite
הטריק: כיוון מנוע נקבע ע"י מי מה-IN גבוה. הופכים אותם — והגלגל מסתובב הפוך.The trick: motor direction is set by which IN pin is HIGH. Flip them — the wheel spins the other way.

אחורה = להפוך את IN1/IN2Backward = flip IN1/IN2

void forward() {
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
}
void backward() {
  digitalWrite(IN1, LOW);  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);  digitalWrite(IN4, HIGH);
}
void turnRight() {      // left fwd, right back
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);  digitalWrite(IN4, HIGH);
}

אותה מהירות ל-ENA/ENB בכל תנועה.Same speed on ENA/ENB for every move.

6שלב 0: ההחלטה של המכוניתLevel 0: the car's decision

אותו if / else משיעור 2 — אבל בענף ה"חסום" יש עכשיו רצף קטן של פעולות במקום סתם עצירה. The same if / else from Lesson 2 — but the "blocked" branch now runs a small sequence instead of just stopping.

if (distanceCm <= 15) {   // blocked
  stopCar();   delay(200);
  backward();  delay(400);
  turnRight(); delay(400);
} else {
  forward();               // clear -> drive
}

גררו את המרחק וראו מה המכונית מחליטה:Drag the distance and watch the car decide:

מרחקDistance: 45 cm

7לוקחים את ההגה 🕹️Take the wheel 🕹️

שימו לב לדבר הכי חשוב היום: פונקציות התנועה נשארות בדיוק אותו דבר. מה שמשתנה זה רק מי מחליט איזו פונקציה להפעיל. Notice the most important thing today: the motion functions stay exactly the same. All that changes is who decides which function to call.

// self-driving: the SENSOR decides
if (distance <= 15) turnRight();
else                forward();

// remote: YOU decide (a button)
if (button == UP)    forward();
else if (button == RIGHT) turnRight();

אותו "גוף", מוח אחר. זה מה שהופך רובוט לגמיש.Same "body", different brain. That's what makes a robot flexible.

שלוש דרכים לשלוח פקודה אלחוטיתThree ways to send a command wirelessly

IRRF 433Bluetooth
מדיוםMediumאורLightרדיוRadioרדיוRadio
קו ראייה?Line of sight?חובהRequiredלאNoלאNo
טווחRange~5 מ'~5 mעשרות-מאות מ'10s–100s m~10 מ'~10 m
השלטThe remoteמוכןReady-madeבונים בעצמנוWe build itהטלפוןYour phone
כיווןDirectionחד-כיווניOne-wayחד-כיווניOne-wayדו-כיווניTwo-way

8שלב 1: שלט IR — איך זה עובדLevel 1: IR remote — how it works

בדיוק כמו שלט הטלוויזיה: השלט מהבהב אור אינפרא-אדום שאנחנו לא רואים. כל כפתור שולח קוד מספרי אחר. המקלט על המכונית מפענח את הקוד. Just like your TV remote: it flashes infrared light we can't see. Each button sends a different numeric code. The receiver on the car decodes it.

  • 🔦 קו ראייה חובה — האור לא עובר דרך קיר או יד🔦 Line of sight required — the light won't pass a wall or hand
  • ⬅️ חד-כיווני — השלט מדבר, המכונית רק מקשיבה➡️ One-way — the remote talks, the car only listens
  • ✅ פשוט וזול — השלט כבר מוכן✅ Simple and cheap — the remote is ready-made

נסו את השלט הווירטואלי — לחצו על החצים והמכונית תזוז:Try the virtual remote — press the arrows and the car moves:

9שלב 1: קודם — ללמוד את קודי השלטLevel 1: first — learn the remote's codes

כל שלט שולח מספרים אחרים, אז קודם קוראים אותם פעם אחת ורושמים. מעלים את הסקצ' הקצר, פותחים Serial Monitor, ולוחצים על כל כפתור. Every remote sends different numbers, so first we read them once and write them down. Upload the short sketch, open the Serial Monitor, and press each button.

הרכיבThe part

  • מודול מקלט IR (VS1838 / HX-M121) + השלט הקטןIR receiver module (VS1838 / HX-M121) + the small remote
  • ספרייה: IRremote (בתפריט Manage Libraries, לחפש)Library: IRremote (Manage Libraries → search)
חיווט המקלט: OUT→D8, VCC→5V, GND→GND. שלושה חוטים בלבד.Receiver wiring: OUT→D8, VCC→5V, GND→GND. Just three wires.

קוראים את הקודיםRead the codes

#include <IRremote.h>
const int IR_RECEIVE_PIN = 8;

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN);
}
void loop() {
  if (IrReceiver.decode()) {
    Serial.println(IrReceiver.decodedIRData.command, HEX);
    IrReceiver.resume();
  }
}

📄 code/level1_ir_read_codes.ino · רושמים את הקוד של ▲ ▼ ◀ ▶ ו-OK.📄 code/level1_ir_read_codes.ino · write down the code for ▲ ▼ ◀ ▶ and OK.

10שלב 1: המכונית נוהגת בשלט IRLevel 1: the car drives by IR remote

ממלאים את חמשת הקודים שרשמנו, וכל כפתור מפעיל אחת מפונקציות התנועה שכבר יש לנו. Fill in the five codes we wrote down, and each button calls one of the motion functions we already have.

if (IrReceiver.decode()) {
  uint8_t cmd = IrReceiver.decodedIRData.command;
  if      (cmd == BTN_UP)    forward();
  else if (cmd == BTN_DOWN)  backward();
  else if (cmd == BTN_LEFT)  turnLeft();
  else if (cmd == BTN_RIGHT) turnRight();
  else if (cmd == BTN_OK)    stopCar();
  lastCmd = millis();
  IrReceiver.resume();
}
// no button for a moment -> stop, so it won't run away
if (millis() - lastCmd > 200) stopCar();

חיווט השלבThis level's wiring

חיבורConnectionפיןPin
מקלט IRIR receiver OUTD8
ENA / ENBD5 / D6
IN1 / IN2D2 / D3
IN3 / IN4D4 / D7
חיווט המנועים לא השתנה משלב 0 — רק הוספנו את מקלט ה-IR ל-D8. הורדנו את חיישן המרחק.The motor wiring is unchanged from level 0 — we just added the IR receiver on D8. The distance sensor is removed.

📄 code/level1_ir_car.ino📄 code/level1_ir_car.ino

11שלב 2: RF433 — רדיו במקום אורLevel 2: RF433 — radio instead of light

RF משתמש בגלי רדיו בתדר 433MHz. בניגוד ל-IR, רדיו לא צריך קו ראייה — הוא עובר דרך קירות ומגיע רחוק הרבה יותר. RF uses radio waves at 433MHz. Unlike IR, radio needs no line of sight — it passes through walls and reaches much farther.

לחצו "שלח" עם ובלי קיר — ותראו את ההבדל:Press "send" with and without a wall — and see the difference:

חיסרון: אין קו ראייה = אין "לכוון" — הרדיו מגיע לכל הכיוונים. וגם: שני רדיואים באותו תדר יכולים להפריע זה לזה.Downside: no line of sight = no "aiming" — radio goes everywhere. Also: two radios on the same frequency can interfere.

12שלב 2: הפעם בונים גם את השלטLevel 2: this time we build the remote too

ב-IR השלט היה מוכן. ב-RF יש לנו זוג מודולים — משדר (TX) ומקלט (RX) — אז המשדר הוא ארדואינו שני עם כפתורים, והמקלט על המכונית. With IR the remote came ready. With RF we get a pair of modules — a transmitter (TX) and a receiver (RX) — so the remote is a second Arduino with buttons, and the receiver is on the car.

  • ספרייה: rc-switch — שולחים ומקבלים מספרים פשוטיםLibrary: rc-switch — send and receive simple numbers
  • מסכימים על קוד לכל כיוון (11=קדימה, 12=אחורה...)Agree on a code per direction (11=fwd, 12=back...)
  • אנטנה: חוט ~17 ס"מ משפר מאוד את הטווחAntenna: a ~17 cm wire greatly improves range
⚠️ שינוי חיווט אחד: ה-RX חייב פין פסיקה (D2 או D3). זה בדיוק ה-IN1 שלנו! אז מזיזים את IN1 מ-D2 ל-D8, וה-RX יושב על D2. ⚠️ One wiring change: the RX needs an interrupt pin (D2 or D3). That's exactly our IN1! So we move IN1 from D2 to D8, and the RX sits on D2.

המשדר (השלט)The transmitter (remote)

long code = CODE_STOP;
if (digitalRead(BTN_UP)==LOW)   code=CODE_UP;
else if(digitalRead(BTN_LEFT)==LOW) code=CODE_LEFT;
// ...
mySwitch.send(code, 24);   // over the air

המקלט (המכונית)The receiver (car)

if (mySwitch.available()) {
  long code = mySwitch.getReceivedValue();
  if (code==CODE_UP) forward();
  // ... map codes to moves
  mySwitch.resetAvailable();
}

📄 level2_rf_transmitter.ino · level2_rf_car.ino📄 level2_rf_transmitter.ino · level2_rf_car.ino

13פינאלה: נהיגה מהטלפון 📱Finale: drive from your phone 📱

הפעם אין שלט נפרד בכלל — הטלפון הוא השלט. מודול HC-05/HC-06 על המכונית מדבר עם אפליקציית Dabble החינמית, שיש בה ג'ויסטיק על המסך. This time there's no separate remote at all — your phone is the remote. An HC-05/HC-06 module on the car talks to the free Dabble app, which has an on-screen gamepad.

  • Bluetooth הוא דו-כיווני — אפשר גם לשלוח נתונים חזרה לטלפון (למשל מרחק)Bluetooth is two-way — you can also send data back to the phone (e.g. distance)
  • בלי לכתוב קוד בצד הטלפון — Dabble עושה את זהNo phone-side coding — Dabble handles it
⚠️ ב-Uno ספריית Dabble מדברת עם המודול דרך SoftwareSerial, ולא דרך D0/D1. ברירת המחדל שלה היא D2/D3 — פיני המנוע שלנו — לכן המודול על D11/D12: TXD → D11, RXD ← D12 (דרך מפל מתח 1k/2k), ובקוד Dabble.begin(9600, 11, 12). ⚠️ On the Uno, Dabble talks to the module through SoftwareSerial, not D0/D1. Its default is D2/D3 — our motor pins — so the module goes on D11/D12: TXD → D11, RXD ← D12 (through a 1k/2k divider), with Dabble.begin(9600, 11, 12) in the code.

הקוד — שוב אותן פונקציותThe code — the same functions again

#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#include <Dabble.h>

void loop() {
  Dabble.processInput();       // read the phone
  if      (GamePad.isUpPressed())    forward();
  else if (GamePad.isDownPressed())  backward();
  else if (GamePad.isLeftPressed())  turnLeft();
  else if (GamePad.isRightPressed()) turnRight();
  else                             stopCar();
}

📄 code/bonus_bluetooth_car.ino · נחבר ונסיע מהטלפון בשיעור הבא.📄 code/bonus_bluetooth_car.ino · we'll wire it and drive from phones next lesson.

14סיכום ואתגריםSummary & challenges

הרעיון האחד להיוםThe one idea for today

מכונית אחת, כמה "מוחות": חיישן, IR, RF, טלפון. פונקציות התנועה תמיד אותן — מפרידים בין "מה לעשות" ל"איך הפקודה מגיעה". One car, several "brains": sensor, IR, RF, phone. The motion functions are always the same — separate "what to do" from "how the command arrives".

איזו טכנולוגיה מתי?Which technology when?

  • IR — קרוב, בקו ישר, זול (צעצועים, טלוויזיה)IR — close, straight line, cheap (toys, TV)
  • RF — רחוק, דרך קירות, בלי לכוון (שער חשמלי, אזעקה)RF — far, through walls, no aiming (gates, alarms)
  • Bluetooth — מהטלפון, דו-כיווני (מכונית חכמה)Bluetooth — from a phone, two-way (smart car)

אתגרים לבית / לשיעור הבאChallenges for home / next lesson

  • לסיים את חיבור ה-Bluetooth ולהסיע מהטלפוןFinish the Bluetooth wiring and drive from a phone
  • להוסיף מהירות: כפתור שמגדיל/מקטין את SPEEDAdd speed: a button that raises/lowers SPEED
  • שלט + חיישן יחד: המכונית מקבלת פקודות, אבל מסרבת להתנגש בקיר גם אם תכוונו אליוRemote + sensor together: the car takes commands, but refuses to crash into a wall even if you steer at it
רמז לאתגר האחרון: לפני forward() בדקו את חיישן המרחק — בדיוק ה-if/else משיעור 2, אבל כשכבה עליון על השלט.Hint for the last one: before forward(), check the distance sensor — exactly Lesson 2's if/else, but layered on top of the remote.

💻 כל קבצי הקוד של השיעור — הורדה והסברAll of this lesson's code — download & walkthrough

תודה — נתראה בשיעור הבא! 👋Thanks — see you next lesson! 👋