MicroPython Projects
eBook - ePub

MicroPython Projects

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

Jacob Beningo

  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

Jacob Beningo

Book details
Book preview
Table of contents
Citations

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

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is MicroPython Projects an online PDF/ePUB?
Yes, you can access MicroPython Projects by Jacob Beningo in PDF and/or ePUB format, as well as other popular books in Ciencia de la computación & Programación en Python. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781789952537

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 boardsand third-party boards that use an STM32 microcontrollerthat 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

Citation styles for MicroPython Projects

APA 6 Citation

Beningo, J. (2020). MicroPython Projects (1st ed.). Packt Publishing. Retrieved from https://www.perlego.com/book/1443351/micropython-projects-a-doityourself-guide-for-embedded-developers-to-build-a-range-of-applications-using-python-pdf (Original work published 2020)

Chicago Citation

Beningo, Jacob. (2020) 2020. MicroPython Projects. 1st ed. Packt Publishing. https://www.perlego.com/book/1443351/micropython-projects-a-doityourself-guide-for-embedded-developers-to-build-a-range-of-applications-using-python-pdf.

Harvard Citation

Beningo, J. (2020) MicroPython Projects. 1st edn. Packt Publishing. Available at: https://www.perlego.com/book/1443351/micropython-projects-a-doityourself-guide-for-embedded-developers-to-build-a-range-of-applications-using-python-pdf (Accessed: 14 October 2022).

MLA 7 Citation

Beningo, Jacob. MicroPython Projects. 1st ed. Packt Publishing, 2020. Web. 14 Oct. 2022.