MicroPython Projects
eBook - ePub

MicroPython Projects

A do-it-yourself guide for embedded developers to build a range of applications using Python

  1. 294 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

MicroPython Projects

A do-it-yourself guide for embedded developers to build a range of applications using Python

About this book

Explore MicroPython through a series of hands-on projects and learn to design and build your own embedded systems using the MicroPython Pyboard, ESP32, the STM32 IoT Discovery kit, and the OpenMV camera module.

Key Features

  • Delve into MicroPython Kernel and learn to make modifications that will enhance your embedded applications
  • Design and implement drivers to interact with a variety of sensors and devices
  • Build low-cost projects such as DIY automation and object detection with machine learning

Book Description

With the increasing complexity of embedded systems seen over the past few years, developers are looking for ways to manage them easily by solving problems without spending a lot of time on finding supported peripherals. MicroPython is an efficient and lean implementation of the Python 3 programming language, which is optimized to run on microcontrollers. MicroPython Projects will guide you in building and managing your embedded systems with ease.

This book is a comprehensive project-based guide that will help you build a wide range of projects and give you the confidence to design complex projects spanning new areas of technology such as electronic applications, automation devices, and IoT applications. While building seven engaging projects, you'll learn how to enable devices to communicate with each other, access and control devices over a TCP/IP socket, and store and retrieve data. The complexity will increase progressively as you work on different projects, covering areas such as driver design, sensor interfacing, and MicroPython kernel customization.

By the end of this MicroPython book, you'll be able to develop industry-standard embedded systems and keep up with the evolution of the Internet of Things.

What you will learn

  • Develop embedded systems using MicroPython
  • Build a custom debugging tool to visualize sensor data in real-time
  • Detect objects using machine learning and MicroPython
  • Discover how to minimize project costs and reduce development time
  • Get to grips with gesture operations and parsing gesture data
  • Learn how to customize and deploy the MicroPython kernel
  • Explore the techniques for scheduling application tasks and activities

Who this book is for

If you are an embedded developer or hobbyist looking to build interesting projects using MicroPython, this book is for you. A basic understanding of electronics and Python is required while some MicroPython experience will be helpful.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weโ€™ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere โ€” even offline. Perfect for commutes or when youโ€™re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access MicroPython Projects by Jacob Beningo in PDF and/or ePUB format, as well as other popular books in Informatica & Hardware. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
Print ISBN
9781789958034
eBook ISBN
9781789952537
Edition
1
Subtopic
Hardware

Customizing the MicroPython Kernel Start Up Code

Developing embedded software using MicroPython is relatively straightforward but there may come a time when there is a need to build a custom-printed circuit board, adjust the default pin settings in the kernel, handle failure modes, or simply build a software library in the MicroPython kernel. In order to do this, a developer will need to be familiar with the MicroPython kernel by examining it and the steps necessary to customize it, which we will do in this chapter.
The following topics will be covered in this chapter:
  • An overview of the MicroPython kernel
  • Navigating the startup code
  • Modifying the default GPIO initialization
  • Adding MicroPython modules to the kernel
  • Testing the results

Technical requirements

The example code used in this chapter can be found at https://github.com/PacktPublishing/MicroPython-Projects/tree/master/Chapter05.
In order to run the examples and build your own custom MicroPython kernel, you will need the following hardware and software:
  • A Linux machine or virtual machine
  • An STM32L4 IoT Discovery node
  • A RobotDyn I2C 8-bit PCA8574 I/O expander module, or equivalent
  • An Adafruit RGB pushbutton PN: 3423, or equivalent
  • A breadboard
  • 6" jumpers
  • A generic two-position switch
  • A 30-gauge wire-wrapping wire
  • A Terminal application (PuTTY, RealTerm, Terminal, or one of many others)
  • A text editor, such as Sublime Text

An overview of the MicroPython kernel

The MicroPython kernel is a collection of software libraries, code, and a Python interpreter that comes pre-built on a MicroPython board, such as the pyboard. Someone who is new to MicroPython may not even realize that their Python interpreter is made up of C modules. These modules are compiled and then programmed onto their board, which then reveals the filesystem and REPL that we have become so familiar with. In this chapter, we are going to look more closely at the kernel and explore how we can make our own modifications that will enhance our applications.

Downloading the MicroPython kernel

Before you can get a feel for what it contains, you need to download the kernel so that you can navigate through its directory structure. The kernel is easy to download. I recommend downloading it on a Linux machine or within a Linux virtual machine. The build process for MicroPython is easier in this environment, which we will be going through later on in this chapter.
Make sure that you have Git installed on your machine by typing the following into your Terminal:
sudo apt-get install git
If Git is already installed, you should see a screen similar to this:
If Git is not installed, it will go through the installation process to install it on your development machine. Once Git is installed, you can use the following command in the terminal to clone MicroPython:
sudo clone https://github.com/micropython/micropython.git
Once the command is issued, MicroPython will be cloned into a directory called micropython, as follows:
Congratulations! You now have the MicroPython kernel on your development machine and it's time to jump in and start getting familiar with it.

MicroPython kernel organization

If you navigate to the micropython directory where you cloned the MicroPython kernel, you'll find that the kernel is organized as in the following screenshot:
Upon examination, you'll notice that there are several top-level directories. The information contained in each of these directories can be found in the following table:
Folder
Description
docs
Contains documentation for different major ports.
drivers
Contains external device drivers for items, such as displays, memory devices, radios, and SD cards.
examples
Contains example Python scripts.
extmod
Contains additional (non-core) modules that are implemented in C, such as crypto and filesystems.
mpy-cross
The MicroPython cross-compiler, which generates bytecode from scripts.
ports
Contains all the different architecture ports supported by MicroPython.
py
The Python implementation, which includes the Python core, compiler, libraries, and runtime.
tests
Contains the test framework for MicroPython.
tools
Contains scripts that can be useful for developing the MicroPython kernel.
I recommend that you briefly take some time to explore these directories and get a better feel for what is in these folders. We will spend most of our time in the ports/STM32 directory but there could be tools or modules elsewhere that it would be useful to know about, depending on what you are looking to modify in the kernel.

Becoming familiar with the STM32L475SE_IOT01A port

If you navigate deeper into the ports folder, you'll find that there are now more than a dozen different ports for MicroPython. In my opinion, the stm32 port is the best supported port, but some of the wireless chips are also good contenders, such as the esp8266. You can see that all the architectures that are currently supported in MicroPython are contained within the port's directory, as in the following screenshot:
If you open up the stm32 folder, you'll find a few different items of interest. First, it contains all the drivers and C code modules that are required to drive an STM32 microcontroller. These files are actually generated from the STM32 HAL, which is provided by STMicroelectronics, and are periodically updated as STMicroelectronics makes updates and changes to their HAL. For the most part, you won't need to modify any of these driver files; they just play a supporting role in the microcontroller's peripherals and capabilities.
Next, you'll notice that there are several folders in the stm32 directory. Most of these again contain the supporting code modules for features such as booting the microcontroller and other advanced features, such as USBs. The folder that is really the most interesting to us is the boards folder.
The boards folder contains several different types of files and folders, including the following:
  • Supported board folders, which are all the different boards supported by MicroPython
  • STM32 derivative linker files, which define the memory maps for the different processors
  • STM32 derivative pin maps, which describe what each pin does on the processor
You can see all the different STM32 boardsโ€”and third-party boards that use an STM32 microcontrollerโ€”that have currently been ported to MicroPython in the following screenshot:
As seen in the preceding screenshot, the STM32/boards folder contains all the STM32 MicroPython ports that currently exist.
As you can see, there are more than two dozen different boards supported, including the pyboard, which can be seen in the PYBV3, PYBV4, PYBV10, and PYBV11 folders. Since we have already looked at it, we are going to investigate the STM32L475E_IOT01A development board in more detail in this chapter.
The STM32L475E_IOT01A board is particularly interesting b...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. About Packt
  4. Contributors
  5. Preface
  6. Down the Rabbit Hole with MicroPython
  7. Managing Real-Time Tasks
  8. Writing a MicroPython Driver for an I/O Expander
  9. Developing an Application Test Harness
  10. Customizing the MicroPython Kernel Start Up Code
  11. A Custom Debugging Tool to Visualize Sensor Data
  12. Device Control Using Gestures
  13. Automation and Control Using Android
  14. Building an Object Detection Application Using Machine Learning
  15. The Future of MicroPython
  16. Downloading and Running MicroPython Code
  17. Assessments
  18. Other Books You May Enjoy