|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "f3918b49-9476-4415-a7e9-68539c202181", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Project 1 - Circuit 1A: Blinking an LED\n", |
| 9 | + "\n", |
| 10 | + "You can find LEDs in just about any source of light, from the bulbs lighting your home\n", |
| 11 | + "to the tiny status lights flashing on your home electronics. Blinking an LED is the\n", |
| 12 | + "classic starting point for learning how to program embedded electronics. It’s the\n", |
| 13 | + "“Hello, World!” of microcontrollers. In this circuit, you’ll write code that makes an LED blink on and off.\n", |
| 14 | + "\n", |
| 15 | + "\n", |
| 16 | + "\n", |
| 17 | + "## New Components\n", |
| 18 | + "\n", |
| 19 | + "### Light-Emitting Diodes (LEDs)\n", |
| 20 | + "\n", |
| 21 | + "LEDs are small lights made from a silicon diode. They come in different colors, brightnesses and sizes. LEDs (pronounced el-ee-dees)\n", |
| 22 | + "have a positive (+) leg and a negative (-) leg, and they will only let electricity flow through them in one direction. LEDs can also burn out if too much electricity flows through them, so you should always use a resistor to limit the current when you wire an LED into a circuit.\n", |
| 23 | + "\n", |
| 24 | + "\n", |
| 25 | + "\n", |
| 26 | + "### Resistors\n", |
| 27 | + "\n", |
| 28 | + "Resistors resist the flow of electricity. You can use them to protect sensitive\n", |
| 29 | + "components like LEDs. The strength of a resistor (measured in ohms) is marked on\n", |
| 30 | + "the body of the resistor using small colored bands. Each color stands for a number,\n", |
| 31 | + "which you can look up using a resistor chart. One can be found at the back of this book\n", |
| 32 | + "\n", |
| 33 | + "## New Concepts\n", |
| 34 | + "\n", |
| 35 | + "### Polarity\n", |
| 36 | + "\n", |
| 37 | + "Many electronics components have polarity, meaning electricity can (and\n", |
| 38 | + "should) flow through them in only one direction. Polarized components, like an LED, have a positive and a negative leg and only work when electricity flows\n", |
| 39 | + "through them in one direction. Some components, like resistors,\n", |
| 40 | + "do not have polarity; electricity can flow through them in either direction.\n", |
| 41 | + "\n", |
| 42 | + "\n", |
| 43 | + "\n", |
| 44 | + "### OHM's Law\n", |
| 45 | + "\n", |
| 46 | + "**OHM’S LAW** describes the relationship between the three fundamental elements of electricity: **voltage**, **resistance** and **current**. This relationship can be represented by this equation:\n", |
| 47 | + "\n", |
| 48 | + "***V = I • R***\n", |
| 49 | + "\n", |
| 50 | + "**V** = Voltage in volts\n", |
| 51 | + "\n", |
| 52 | + "**I** = Current in amps\n", |
| 53 | + "\n", |
| 54 | + "**R** = Resistance in ohms (Ω)\n", |
| 55 | + "\n", |
| 56 | + "This equation is used to calculate what resistor values are suitable to sufficiently limit the current flowing to the LED so that it does not get too hot and burn out.\n", |
| 57 | + "\n", |
| 58 | + "### Digital Output \n", |
| 59 | + "\n", |
| 60 | + "\n", |
| 61 | + "\n", |
| 62 | + "\n", |
| 63 | + "When working with microcontrollers such as the RedBoard, there are a variety of pins to which you can connect electronic components. Knowing which pins perform which functions is important when building your circuit. In this circuit, we will be using what is known as a digital output. There are 14 of these pins found on the RedBoard. A digital output only has **two states: ON or OFF**. These two states can also be thought of as **HIGH or LOW, TRUE or FALSE**. When an LED is connected to one of these pins, the pin can only perform two jobs: turning on the LED and turning off the LED. We’ll explore the other pins and their functions in later circuits.\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + "## New Ideas\n", |
| 67 | + "\n", |
| 68 | + "**ELECTRICAL SAFETY:** Never work on your circuits while the board is connected to\n", |
| 69 | + "a power source. The SparkFun RedBoard operates at 5 volts, which, while not enough to\n", |
| 70 | + "injure you, is enough to damage the components in your circuit. \n", |
| 71 | + "\n", |
| 72 | + "**COMPONENT ORIENTATION & POLARITY:** Instructions on how to orient each of\n", |
| 73 | + "the new components will be given before each circuit diagram. Many components have\n", |
| 74 | + "polarity and have only one correct orientation, while others are nonpolarized.\n", |
| 75 | + "\n", |
| 76 | + "\n", |
| 77 | + "\n", |
| 78 | + "## Hookup Guide\n", |
| 79 | + "\n", |
| 80 | + "**READY TO START HOOKING EVERYTHING UP?** Check out the circuit diagram and\n", |
| 81 | + "hookup table below to see how everything is connected.\n", |
| 82 | + "\n", |
| 83 | + "\n", |
| 84 | + "\n", |
| 85 | + "### Hookup Tables\n", |
| 86 | + "\n", |
| 87 | + "Many electronics beginners find it helpful to have a coordinate system when building their circuits. For each circuit, you’ll find a hookup table that lists the coordinates of each component or wire and where it connects to the RedBoard, the breadboard, or both. The breadboard has a letter/number coordinate system, just like the game Battleship.\n", |
| 88 | + "\n", |
| 89 | + "\n", |
| 90 | + "\n", |
| 91 | + "…means one end of a component connects to digital pin 34 on your RedBoard and\n", |
| 92 | + "the other connects to J2 on the breadboard\n", |
| 93 | + "\n", |
| 94 | + "\n", |
| 95 | + "\n", |
| 96 | + "\n" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "id": "1bd52930-d783-460a-923b-41a3c37d2f2b", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "## Blinking the LED\n", |
| 105 | + "\n", |
| 106 | + "Now that your circuit is build, it's time to blink the LED. This is done using MicroPython, which is running on the RedBoard.\n", |
| 107 | + "\n", |
| 108 | + "The first step is to connect your RedBoard to a USB port on this computer.\n", |
| 109 | + "\n", |
| 110 | + "Select the \"Connect\" button at the bottom right of this screen and a panel is displayed\n", |
| 111 | + "\n", |
| 112 | + "Select the \"Connect Device\" Button, and when the selection dialog appears, select the port with that displays ***Board in FS mode (...)*** or *TBD*\n", |
| 113 | + "\n", |
| 114 | + "\n", |
| 115 | + "\n", |
| 116 | + "With the RedBoard connected, use the following MicroPython commands to blink the LED. \n", |
| 117 | + "\n", |
| 118 | + "### Using MicroPython\n", |
| 119 | + "\n", |
| 120 | + "The following MicroPython commands are entered to blink the LED on your board. \n", |
| 121 | + "\n", |
| 122 | + "**REMEMBER** To enter a MicroPython command, hold down either the Control (on Windows) or Command (on Mac) key when pressing *Enter*\n", |
| 123 | + "\n", |
| 124 | + "#### Step 1 - Setup\n", |
| 125 | + "\n", |
| 126 | + "To blink the LED, we need to enable the board pin **34** (the pin that the LED is connected to in the circuit). \n", |
| 127 | + "\n", |
| 128 | + "To do this we **load the Pin definition for the board**\n", |
| 129 | + "\n" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": null, |
| 135 | + "id": "4494b462-505b-4a23-8ebc-28b54152bd88", |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "from machine import Pin" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "id": "2d94be96-a8c9-4932-8111-58d10a8622a9", |
| 145 | + "metadata": {}, |
| 146 | + "source": [ |
| 147 | + "Now **create a Pin variable for the LED pin, number 34**. Also define it as an **output pin**, so we can turn it on and off" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "id": "432bb00a-9160-4e56-a08e-716ab922f60c", |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [], |
| 156 | + "source": [ |
| 157 | + "led_pin = Pin(34, Pin.OUT)" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "markdown", |
| 162 | + "id": "27ad33ac-2e51-409c-8a12-753da4384740", |
| 163 | + "metadata": {}, |
| 164 | + "source": [ |
| 165 | + "We turn the LED on by setting the pin value to **high** or on" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": null, |
| 171 | + "id": "cd2ae013-674c-4f33-8827-8c3d672d07a5", |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [], |
| 174 | + "source": [ |
| 175 | + "led_pin.high()" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "markdown", |
| 180 | + "id": "f8c6243c-33b8-4eb1-8065-e22e5e6d3814", |
| 181 | + "metadata": {}, |
| 182 | + "source": [ |
| 183 | + "And to turn the LED off, we set the pin to **low**" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": null, |
| 189 | + "id": "52a875da-ccd3-437d-b310-e2ce58de53bd", |
| 190 | + "metadata": {}, |
| 191 | + "outputs": [], |
| 192 | + "source": [ |
| 193 | + "led_pin.low()" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "markdown", |
| 198 | + "id": "3cb0f029-afa4-4a0b-9846-cc874d67879a", |
| 199 | + "metadata": {}, |
| 200 | + "source": [ |
| 201 | + "To *blink* the LED, we can turn it on, wait a period of time and then turn it off. This is done by sleeping between the **on** and **off** commands. \n", |
| 202 | + "\n", |
| 203 | + "To do this in MicroPython, we need a sleep function. Let's load the **sleep** function, which will sleep for a number of seconds.." |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": null, |
| 209 | + "id": "73457bed-1444-4300-ba58-4809bf9068f8", |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [], |
| 212 | + "source": [ |
| 213 | + "from time import sleep" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "markdown", |
| 218 | + "id": "1ea7c7a6-7dfb-4bbd-bab5-da4b52af14bf", |
| 219 | + "metadata": {}, |
| 220 | + "source": [ |
| 221 | + "Now lets blink the LED - sleeping for 1 second between turning the LED on and off" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "id": "95e90c57-57d0-4e26-b8f8-0d4f0720b46a", |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [], |
| 230 | + "source": [ |
| 231 | + "led_pin.high()\n", |
| 232 | + "sleep(1)\n", |
| 233 | + "led_pin.low()\n", |
| 234 | + "sleep(1)\n", |
| 235 | + "led_pin.high()\n", |
| 236 | + "sleep(1)\n", |
| 237 | + "led_pin.low()" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "markdown", |
| 242 | + "id": "54d14da9-3d4f-4493-98a5-3dd0c025733f", |
| 243 | + "metadata": {}, |
| 244 | + "source": [ |
| 245 | + "Now we have blinked the LED! \n", |
| 246 | + "\n", |
| 247 | + "Now we'll present the idea of a **for loop**. A loop repeats a statement for a number of specified times. \n", |
| 248 | + "\n", |
| 249 | + "To blink our LED 10 times, use the following command:" |
| 250 | + ] |
| 251 | + }, |
| 252 | + { |
| 253 | + "cell_type": "code", |
| 254 | + "execution_count": null, |
| 255 | + "id": "73237269-9f49-4145-b03d-7ba0f6bc8664", |
| 256 | + "metadata": {}, |
| 257 | + "outputs": [], |
| 258 | + "source": [ |
| 259 | + "for i in range(10):\n", |
| 260 | + " led_pin.high()\n", |
| 261 | + " sleep(1)\n", |
| 262 | + " led_pin.low()\n", |
| 263 | + " sleep(1)" |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "markdown", |
| 268 | + "id": "d91669fb-aea7-411c-927b-99724540a33c", |
| 269 | + "metadata": {}, |
| 270 | + "source": [ |
| 271 | + "When this command runs, it blinks the LED 10 times by setting the LED high, then low and sleeping inbetween each step. " |
| 272 | + ] |
| 273 | + }, |
| 274 | + { |
| 275 | + "cell_type": "markdown", |
| 276 | + "id": "10d5c3fe-852f-42f7-8378-85dc8c8c7c3e", |
| 277 | + "metadata": {}, |
| 278 | + "source": [ |
| 279 | + "## You've Completed Circuit 1A!\n", |
| 280 | + "\n", |
| 281 | + "Continue to circuit 1B to learn about analog signals and potentiometers\n", |
| 282 | + "\n", |
| 283 | + "" |
| 284 | + ] |
| 285 | + } |
| 286 | + ], |
| 287 | + "metadata": { |
| 288 | + "kernelspec": { |
| 289 | + "display_name": "MicroPython upydevice kernel", |
| 290 | + "language": "python", |
| 291 | + "name": "micropython-upydevice" |
| 292 | + }, |
| 293 | + "language_info": { |
| 294 | + "codemirror_mode": "python", |
| 295 | + "file_extension": ".py", |
| 296 | + "mimetype": "text/x-python", |
| 297 | + "name": "python", |
| 298 | + "pygments_lexer": "python" |
| 299 | + } |
| 300 | + }, |
| 301 | + "nbformat": 4, |
| 302 | + "nbformat_minor": 5 |
| 303 | +} |
0 commit comments