|
| 1 | +--- |
| 2 | + |
| 3 | +featured: micropython-101 |
| 4 | +title: '1. Micropython Enviroment - REPL' |
| 5 | +description: 'Learn the basics for loops on MicroPython.' |
| 6 | +author: 'Pedro Lima' |
| 7 | +hero_image: "./hero-banner.png" |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +REPL, which stands for Read-Eval-Print Loop, is an interactive environment that makes programming in MicroPython fast and flexible. REPL allows you to enter code line-by-line and see the results immediately. Although sometimes overlooked it is a great way for testing and debugging on the fly. |
| 12 | + |
| 13 | +We’ll walk through what REPL does, why it’s useful, and how you can use it to enhance your MicroPython experience. |
| 14 | + |
| 15 | +## What is REPL? |
| 16 | + |
| 17 | +The REPL process involves four basic steps: |
| 18 | + |
| 19 | +- **R - ead**: Reads and accepts your code input line-by-line. |
| 20 | +- **E - val**: Evaluates the entered code. |
| 21 | +- **P - rint**: Displays the result of the code execution. |
| 22 | +- **L - oop**: Repeats the process, allowing you to enter new code immediately. |
| 23 | + |
| 24 | +Think of REPL as a conversation between you and your MicroPython environment, where each line you type gets an instant response, making it ideal for exploring ideas and troubleshooting. |
| 25 | + |
| 26 | +## How REPL Works in MicroPython |
| 27 | + |
| 28 | +In MicroPython, REPL can be accessed via Arduino Labs for MicroPython terminal, enabling you to run code in real-time directly on your device. Here’s a step-by-step guide on using it: |
| 29 | + |
| 30 | +1. **Connect to REPL**: Open Arduino Labs for MicroPython and establish a connection to your microcontroller. Once connected, you’ll see a prompt (`>>>`), indicating the REPL is ready. |
| 31 | +2. **Enter Code**: Type any valid MicroPython code directly after the prompt. When you press Enter, REPL will execute your code. |
| 32 | +3. **See Results Instantly**: Any output, return values, or errors are displayed right away. If your code contains expressions, REPL will evaluate and print them for you. |
| 33 | +4. **Iterate**: The loop resets, and you can immediately enter more code, building on previous commands or testing new lines independently. |
| 34 | + |
| 35 | +## Example: Using REPL in MicroPython |
| 36 | + |
| 37 | +Let's see a simple REPL session to add two numbers interactively: |
| 38 | + |
| 39 | +```python |
| 40 | +>>> a = 5 |
| 41 | +>>> b = 3 |
| 42 | +>>> a + b |
| 43 | +8 |
| 44 | +``` |
| 45 | +TODO: GIF showing step by step so people understand to run each individually !()[] |
| 46 | + |
| 47 | +In this REPL session: |
| 48 | + |
| 49 | +- **Define variables**: We set `a` to 5 and `b` to 3. |
| 50 | +- **Perform calculation**: Entering `a + b` immediately returns `8`, showing the result without having to write a full script. |
| 51 | + |
| 52 | +## Common Uses of REPL |
| 53 | + |
| 54 | +REPL is particularly useful for: |
| 55 | + |
| 56 | +- **Testing code snippets**: Quickly check how individual pieces of code behave. |
| 57 | +- **Debugging**: Test small parts of a larger project interactively. |
| 58 | +- **Learning**: Practice MicroPython commands and get instant feedback, ideal for beginners. |
| 59 | + |
| 60 | +## Going Further With REPL |
| 61 | + |
| 62 | +REPL in MicroPython is an usefull tool that enables instant testing and feedback, making coding more interactive and efficient. Whether you're learning, debugging, or exploring ideas, REPL provides a flexible and powerful coding experience. |
| 63 | + |
| 64 | +**Experiment with REPL** |
| 65 | + |
| 66 | +- **Explore Built-in Functions**: Use functions like `len()`, `sum()`, or `type()` and see the results immediately, allowing you to get a quick feel for MicroPython’s standard library. |
| 67 | +- **Test Sensor and Pin Outputs**: Quickly check real-time data from sensors or pin states by writing simple lines to read values directly, helping you troubleshoot hardware setups. |
| 68 | +- **Debug Variable Values**: Enter and modify variables on the spot to test changes in data or simulate inputs, allowing you to observe how your program behaves step-by-step. |
| 69 | +- **Practice Logical Expressions**: Experiment with logical statements and conditionals to understand how `if` statements, comparisons, and loops work in MicroPython. |
| 70 | + |
| 71 | +By mastering REPL, you’ll be well-equipped to make the most of MicroPython’s interactive capabilities and streamline your development process. |
0 commit comments