class: center, middle, inverse, title-slide .title[ # Same Blink, No Resistors — Meet the Arduino ] .subtitle[ ## Programmable Electronics — Unit 3 ] .date[ ### Class 16 ] --- # Learning Targets .lt-box[ - Set up VS Code with PlatformIO, and upload a program to an Arduino. - Make an LED blink from code, and change how fast it blinks by editing a number. - Use a pushbutton to change what the program does while it runs. - Compare a 555 blinker with a coded blinker: what each one costs, and what each one is better at. ] .eu-label[Essential Understanding:] For four classes, three parts decided how fast the 555 blinked. Today a line of text decides it. A microcontroller is a chip whose behaviour you *write* instead of *wire*. You give up some simplicity: it needs a computer, a cable, and a program. In return, changing your mind costs about five seconds. --- # Agenda .agenda-box[ 1. Today's build, start to finish 2. Set up VS Code and PlatformIO 3. **Build 1:** make an LED blink 4. **Build 2:** a button that makes it blink faster 5. **Build 3:** a reaction-timer game 6. What just happened? Now we talk 7. Exit ticket ] .center[ **Today you build first and ask questions after.** Follow the steps, get it working, and write down anything that surprised you. We'll explain the details once everyone has a blinking LED. ] --- class: small-font # Today's build, start to finish .pull-left.w48[ **Set up** (once per laptop) <ol start="1"><li>Open <strong>VS Code</strong> and install the <strong>PlatformIO</strong> extension.</li><li>Create a new project: board <strong>Arduino Uno</strong>, framework <strong>Arduino</strong>.</li></ol> **Build 1: blink** <ol start="3"><li>Wire an <strong>LED + 470 Ω</strong> from <strong>pin 13</strong> to <strong>GND</strong>.</li><li>Paste the Blink code into <code>src/main.cpp</code>.</li><li>Plug in the board, click <strong>Upload</strong>, and watch it blink.</li><li>Change the numbers. Upload again.</li></ol> ] .pull-right.w48[ **Build 2: the button** <ol start="7"><li>Add a <strong>pushbutton</strong> from <strong>pin 2</strong> to <strong>GND</strong>.</li><li>Replace the code with the button version and upload.</li><li>Hold the button down. The blink speeds up.</li></ol> **Build 3: the game** (same wiring) <ol start="10"><li>Upload the reaction timer and open the <strong>Serial Monitor</strong>. Beat your partner.</li></ol> **Then** <ol start="11"><li>We stop and talk about what you just did.</li></ol> .font-small[ Partner up: one laptop, one Arduino, one USB cable, one breadboard. You need an LED, a 470 Ω resistor, a pushbutton, and four jumper wires. ] ] --- class: small-font # Set up: VS Code + PlatformIO We're using **VS Code** (a code editor) with **PlatformIO** (an add-on that knows how to talk to an Arduino). .pull-left.w48[ **Install PlatformIO** *(skip if it's already done on your laptop)* 1. Open **VS Code**. 2. Click the **Extensions** icon in the left bar (four squares), or press **Ctrl+Shift+X** (Mac: **⌘⇧X**). 3. Search **PlatformIO IDE** and click **Install**. 4. **Wait.** The bottom-right corner says it is installing PlatformIO Core. This can take a few minutes. Click **Reload** if it asks. 5. An **ant-head icon** appears in the left bar. That's PlatformIO. ] .pull-right.w48[ **Make a project** 1. Click the **ant head** → **PIO Home** → **+ New Project**. 2. **Name:** `blink` · **Board:** type `Uno` and pick **Arduino Uno** *(Atmel AVR)*. **Not** "UNO Q", "R4" or "WiFi". · **Framework:** Arduino → **Finish**. 3. In the file list on the left, open **`src` → `main.cpp`**. Your code goes here. **The buttons you'll use** are in the blue bar at the bottom of the window: - **✓ Build** checks your code. - **→ Upload** sends it to the board. - **🔌 Serial Monitor** comes later in the unit. ] --- class: small-font # Build 1: make an LED blink .pull-left.w44[ <img src="assets/schematics/arduino-blink-button.svg" alt="Arduino Uno: pin 13 through a 470 ohm resistor and an LED to ground; pin 2 through a pushbutton to ground" style="width:100%;max-height:260px;"/> 1. **Unplug the board.** Wire only the **top row** for now: **pin 13 → 470 Ω → LED → GND**. LED **long leg toward the resistor**. 2. Replace everything in `main.cpp` with this code. 3. Plug in the USB cable. Click **→ Upload**. Wait for **SUCCESS**. ] .pull-right.w52[ ```cpp #include <Arduino.h> void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500); } ``` **It works?** Your LED *and* the little **L** light on the board blink together. Now change both `500`s to `100` and upload again. Then try `1000` and `100`. ] --- class: small-font # Build 2: a button that makes it blink faster .pull-left.w44[ <img src="assets/schematics/arduino-blink-button.svg" alt="Arduino Uno: pin 13 through a 470 ohm resistor and an LED to ground; pin 2 through a pushbutton to ground" style="width:100%;max-height:220px;"/> 1. **Unplug the board.** Add the **middle row**: put the button **across the centre trench**. Wire one leg to **pin 2** and the **diagonally opposite** leg to **GND**. 2. Replace the code with this version and upload. 3. **Hold the button down.** It blinks fast. Let go, and it slows down again. ] .pull-right.w52[ ```cpp #include <Arduino.h> void setup() { pinMode(13, OUTPUT); pinMode(2, INPUT_PULLUP); } void loop() { int wait = 500; if (digitalRead(2) == LOW) { // pressed wait = 100; } digitalWrite(13, HIGH); delay(wait); digitalWrite(13, LOW); delay(wait); } ``` No resistor on the button? Correct. The board has one built in, and `INPUT_PULLUP` turns it on. We'll come back to why. ] --- class: small-font # Build 3: reaction-timer game <style>.code-tight .remark-code { font-size: 14px; line-height: 1.2; }</style> .pull-left.w56.code-tight[ ```cpp #include <Arduino.h> void setup() { pinMode(13, OUTPUT); pinMode(2, INPUT_PULLUP); Serial.begin(9600); randomSeed(analogRead(A0)); Serial.println("Wait for the LED, then press!"); } void loop() { digitalWrite(13, LOW); delay(random(2000, 5000)); // wait 2-5 s if (digitalRead(2) == LOW) { // already pressed? Serial.println("Too soon! Let go."); while (digitalRead(2) == LOW) { } return; } digitalWrite(13, HIGH); // GO! unsigned long start = millis(); while (digitalRead(2) == HIGH) { } // wait for press unsigned long t = millis() - start; digitalWrite(13, LOW); Serial.print("Your time: "); Serial.print(t); Serial.println(" ms"); while (digitalRead(2) == LOW) { } // wait for release delay(1000); } ``` ] .pull-right.w40[ **Same wiring as Build 2.** Nothing to change on the breadboard. 1. Replace the code and **upload**. 2. Click **🔌 Serial Monitor** in the bottom bar. Messages from the board appear there. 3. Wait for the LED. Press as fast as you can. Your time prints in milliseconds. 4. **Take turns.** Best of five, against your partner. **Then make it yours:** - Print **"New record!"** when someone beats the best time so far. - Hold the button while it's waiting. What happens? Find the lines that catch it. - Make the LED blink three times as a "ready" signal before each round. ] --- class: small-font # Stuck? .pull-left.w48[ **Upload fails** - **No port found / upload error:** try another USB cable. Some cables only carry power and can't carry data. - **Red squiggles or errors when you build:** look for a missing `;`, a missing `}`, or a capital letter where there shouldn't be one. `Delay` is not `delay`. - **"Arduino.h not found":** the project wasn't created with framework **Arduino**. Make a new project. - **First upload is very slow:** PlatformIO is downloading tools. Let it finish. - **No ✓ → 🔌 buttons at the bottom:** VS Code has the wrong folder open. **File → Open Folder** → the folder with `platformio.ini`. ] .pull-right.w48[ **Uploads fine, but...** - **Only the L light blinks:** your LED is backwards, or not in the same rows as its resistor. - **Nothing changes when you press:** a jumper isn't really in **pin 2** or **GND**, or the button isn't pushed all the way into the board. - **It blinks fast all the time:** you used two button legs that are always connected inside. Use **diagonally opposite** legs. **Serial Monitor** - **Where's the Serial Monitor?** The **🔌 plug icon** in the blue bar at the bottom. It opens in the terminal panel. Also: ant head → **Project Tasks → uno → Monitor**, or **Upload and Monitor** to do both. - **Serial Monitor shows nothing:** press the board's reset button. **"Port busy" on upload:** close the monitor first (click the terminal, **Ctrl+C**). ] --- class: center, middle # What just happened? Everyone has a blinking LED. Now we talk about it. --- class: small-font # What did "faster" cost? .pull-left.w48[ **The 555** 1. Power off. 2. Redo `\(f = \dfrac{1.44}{(R_A + 2R_B)\,C}\)` for the new rate. 3. Find that resistor in the kit, or build it from two. 4. Swap it in, power up, and count blinks. To get a speed you could change *while it ran*, you needed a potentiometer. ] .pull-right.w48[ **The Arduino (today)** 1. Change `500` to `100`. 2. Upload. To get a speed you could change while it ran, you added **a button and four lines of code**. The button didn't set the speed. It asked the program a yes/no question, and the program decided. ] <div style="clear:both;height:10px"></div> .emphasis-center[ Same LED, same blink. The thing doing the deciding moved from **the parts** into **the code**. ] --- class: small-font # Reading the code you just ran .pull-left.w48[ - **`setup()`** runs **once**, when the board powers up. It tells each pin its job: 13 is an output, 2 is an input. - **`loop()`** runs **forever**, over and over. It's an **astable**: it never settles. - **`delay(wait)`** pauses for `wait` **milliseconds**. It does the job the resistors and capacitor did on the 555. ] .pull-right.w48[ - **`digitalRead(2)`** asks: is pin 2 HIGH or LOW right now? - **`if (...) { ... }`** only runs the inside when the answer is yes. - **Pressed reads LOW.** That's backwards from what you'd expect, and it's because of `INPUT_PULLUP`. It gets a whole class soon. .font-small[ **Try this and notice:** tap the button quickly while it's blinking slowly. Sometimes it ignores you. Why? *(Hint: what is the program doing during a `delay`?)* ] ] --- # Exit ticket Two sentences on a slip, one each: .q-box[ 1. **One thing that was easier with code** than with the 555. Be specific: name a moment from today. 2. **One thing the 555 has going for it** that the Arduino doesn't. ] .font-small[ Our question all year has been *what is doing the deciding, and what did we give up to make the deciding easier?* Today, the deciding moved into a program, and you can't wire your way back out of that. ] --- class: center, middle # Wrap-up — in your notebook .hw-box[ - **Plan a traffic light.** On paper: how many seconds should **green**, **yellow**, and **red** each stay on? Write down why you chose each number. It's one of the projects you can build next. - **Finish the exit-ticket thought.** In one sentence: if changing the program is this easy, why would anyone still use a 555? ]