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

ESP32 Programming Micro Controllers in Micro Python

Product Information

Specifications

  • 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)
  • Publisher: Elektor International Media B.V.

Product Usage Instructions

Introduction

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

Getting Started

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.

Additional Resources

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

FAQs

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

The 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.

“`

Programming

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

This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V. PO Box 11, NL-6114-ZG Susteren, The Netherlands Phone: +31 46 4389444
All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder’s permission to reproduce any part of the publication should be addressed to the publishers.
Declaration
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 Print ISBN 978-3-89576-723-4 eBook
© Copyright 2026 Elektor International Media
www.elektor.com Editor: Clemens Valens Prepress Production: D-Vision, Julian van den Berg Printers: Ipskamp, Enschede, The Netherlands
Elektor is the world’s leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content – via a variety of media channels (including magazines, video, digital media, and social media) in several languages – relating to electronics design and DIY electronics. www.elektormagazine.com
4

Contents
Contents
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6

Contents
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
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

Documents / Resources

Elektor ESP32 Programming Micro Controllers in Micro Python [pdf] Owner's Manual
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

References

Leave a comment

Your email address will not be published. Required fields are marked *