Elektor ESP32 Programming Micro Controllers in Micro Python Owner’s Manual

ESP32 Programming Micro Controllers in Micro Python

Informações do produto

Especificações

  • Product Name: Programming Microcontrollers in MicroPython
  • Author: Clemens Valens
  • Examples for: Raspberry Pi Pico and ESP32
  • ISBN: 978-3-89576-722-7 (Print), 978-3-89576-723-4 (eBook)
  • Editora: Elektor International Media BV

Instruções de uso do produto

Introdução

Welcome to the guide on programming microcontrollers in
MicroPython. This guide provides examples for using MicroPython
with Raspberry Pi Pico and ESP32.

Começando

Before you begin, ensure you have the necessary hardware, such
as Raspberry Pi Pico or ESP32 board, and the book “Programming
Microcontrollers in MicroPython.”

Programming Microcontrollers

Follow the step-by-step instructions provided in the book to
program microcontrollers using MicroPython. The book includes
examples and explanations to help you understand the concepts.

Recursos adicionais

For additional resources and support, visit the Elektor website
at www.elektor.com.

Perguntas frequentes

1. Can I use this book for programming other
microcontroladores?

O examples and instructions in this book are specifically
tailored for Raspberry Pi Pico and ESP32. While some concepts may
apply to other microcontrollers, it is recommended to refer to
specific documentation for different models.

2. Is prior programming experience required?

Basic programming knowledge is beneficial but not mandatory. The
book includes beginner-friendly examples to help you get started
with MicroPython programming.

“`

Programação

programemmbinegdsdeerdies

Microcontrollers in

MicroPython

With Examples for Raspberry Pi Pico and ESP32

Clemens Valens
1

Programming Microcontrollers in MicroPython
With Examples for Raspberry Pi Pico and ESP32

By Clemens Valens

Esta é uma publicação da Elektor. Elektor é a marca de mídia da
Elektor International Media BV PO Box 11, NL-6114-ZG Susteren, Holanda Telefone: +31 46 4389444
Todos os direitos reservados. Nenhuma parte deste livro pode ser reproduzida em qualquer forma material, incluindo fotocópia ou
armazenar em qualquer meio por meio eletrônico e seja ou não transitória ou incidentalmente para algum outro uso desta publicação, sem a permissão por escrito do detentor dos direitos autorais, exceto de acordo com as disposições da Lei de Designs e Patentes de Direitos Autorais de 1988 ou sob os termos de um licença emitida pela Copyright Licensing Agency Ltd., 90 Tottenham Court Road, Londres, Inglaterra W1P 9HE. Os pedidos de permissão do detentor dos direitos autorais para reproduzir qualquer parte da publicação devem ser endereçados aos editores.
Declaração
The author and publisher have made every effort to ensure the accuracy of the information contained in this book. They do not assume, or hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident, or any other cause.
ISBN 978-3-89576-722-7 Imprimir ISBN 978-3-89576-723-4 e-book
© Copyright 2026 Elektor International Media
www.elektor.com Editor: Clemens Valens Produção de pré-impressão: D-Vision, Julian van den Berg Impressoras: Ipskamp, Enschede, Holanda
A Elektor é a principal fonte mundial de informações técnicas essenciais e produtos eletrônicos para engenheiros profissionais, designers eletrônicos e empresas que buscam engajá-los. Diariamente, nossa equipe internacional desenvolve e fornece conteúdo de alta qualidade – por meio de diversos canais de mídia (incluindo revistas, vídeos, mídia digital e redes sociais) em diversos idiomas – relacionado a design eletrônico e eletrônica "faça você mesmo". www.elektormagazine.com
4

Conteúdo
Conteúdo
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Chapter 1 · What Is MicroPython?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Program Structure and Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Accessing Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Performance and Memory Considerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Libraries and Ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Error Handling and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 When to Use MicroPython. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Variables and Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Chapter 2 · MicroPython with Thonny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Overview of Thonny and MicroPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Installing Thonny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Installing MicroPython on the Raspberry Pi Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Installing MicroPython on the ESP32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Installing MicroPython on Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Writing and Running Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Using the REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Saving Files to the Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Using Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Hello-World Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Best Practices for Using Thonny and MicroPython . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter 3 · Working with Digital Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 What Are Digital Signals? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Configuring Digital Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Controlling an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5

Programming Microcontrollers in MicroPython
Reading a Pushbutton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Pushbutton-Controlled LED Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Chapter 4 · Working with Analog Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 What Are Analog Signals? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Measuring Analog Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Generating Analog(-like) Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Controlling LED Brightness with PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Controlling LED Brightness with a Potentiometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Using True Analog Output (DAC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Chapter 5 · Working with the Serial Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 What Is Serial Communication? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Baud Rate and Data Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Using the USB Serial Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Sending Numbers and Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Receiving Data (UART). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Controlling an LED (UART) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Chapter 6 · Working with I²C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 What Is I²C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Master and Slave Devices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Pull-Up Resistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 I²C Pins on Common Boards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Using I²C in MicroPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Scanning the I²C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Device Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Writing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6

Conteúdo
Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Logic Levels and Level Shifting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Repeated Start Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Hello Display! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Chapter 7 · Working with SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 What Is SPI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 SPI vs I²C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 SPI Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Using SPI in MicroPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 SPI Modes and Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Selecting a Slave Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Transferring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Reading Multiple Bytes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Managing Multiple Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Comparing UART, I²C and SPI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Logic Levels and Electrical Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Chapter 8 · Working with Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Why Time Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Blocking Delays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Measuring Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Rollover and Safe Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Measuring Short Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 One-Shot Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Debouncing Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Choosing the Right Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7

Programming Microcontrollers in MicroPython
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Chapter 9 · Working with Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
What Is an Interrupt? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Polling vs Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Interrupts in MicroPython. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Rules for Writing ISRs (Interrupt Callbacks) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 External Interrupts (GPIO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Button Debouncing with Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Sharing Data Between ISR and Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Safer ISR Work: Using micropython.schedule(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Platform Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Chapter 10 · Interacting with the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Transducers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Key Sensor Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Interfacing Sensors with Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Examples of Common Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Best Practices for Sensor Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Safety and Power Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Chapter 11 · Example Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Basic Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Machine Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Alarms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Robotics and Servo Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Automotive (model car controller concept) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Home Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
8

Documentos / Recursos

Elektor ESP32 Programming Micro Controllers in Micro Python [pdf] Manual do Proprietário
ESP32 Programming Micro Controllers in Micro Python, ESP32, Programming Micro Controllers in Micro Python, Micro Controllers in Micro Python, Controllers in Micro Python, in Micro Python, Python

Referências

Deixe um comentário

Seu endereço de e-mail não será publicado. Os campos obrigatórios estão marcados *