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

ESP32 Programming Micro Controllers in Micro Python

Informasi Produk

Spesifikasi

  • 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)
  • Penerbit: Elektor International Media BV

Petunjuk Penggunaan Produk

Perkenalan

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

Memulai

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.

Sumber Daya Tambahan

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

Tanya Jawab Umum

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

Sang mantanamples 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.

Pemrograman

programemmbinegdsdeerdies

Microcontrollers in

MikroPython

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

Ini adalah Publikasi Elektor. Elektor adalah merek media
Elektor International Media BV PO Box 11, NL-6114-ZG Susteren, Belanda Telepon: +31 46 4389444
Seluruh hak cipta. Tidak ada bagian dari buku ini yang boleh direproduksi dalam bentuk materi apa pun, termasuk fotokopi, atau
menyimpan dalam media apa pun dengan cara elektronik dan baik sementara atau tidak disengaja untuk penggunaan lain dari publikasi ini, tanpa izin tertulis dari pemegang hak cipta kecuali sesuai dengan ketentuan Undang-Undang Desain dan Paten Hak Cipta 1988 atau berdasarkan ketentuan a lisensi yang dikeluarkan oleh Copyright Licensing Agency Ltd., 90 Tottenham Court Road, London, Inggris W1P 9HE. Permohonan izin pemegang hak cipta untuk mereproduksi bagian mana pun dari publikasi harus ditujukan kepada penerbit.
Pernyataan
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 Cetak ISBN 978-3-89576-723-4 eBuku
© Hak Cipta 2026 Elektor International Media
www.elektor.com Editor: Clemens Valens Prepress Production: D-Vision, Julian van den Berg Printers: IpskampEnschede, Belanda
Elektor adalah sumber informasi teknis penting dan produk elektronik terkemuka di dunia bagi para insinyur profesional, desainer elektronik, dan perusahaan yang ingin merekrut mereka. Setiap hari, tim internasional kami mengembangkan dan menyediakan konten berkualitas tinggi – melalui berbagai saluran media (termasuk majalah, video, media digital, dan media sosial) dalam berbagai bahasa – terkait desain elektronik dan elektronik DIY. www.elektormagazine.com
4

Isi
Isi
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

Isi
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

Dokumen / Sumber Daya

Elektor ESP32 Programming Micro Controllers in Micro Python [Bahasa Indonesia:] Panduan Pemilik
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

Referensi

Tinggalkan komentar

Alamat email Anda tidak akan dipublikasikan. Bidang yang wajib diisi ditandai *