Full Chapter Programming Microcontrollers With Python Experience The Power of Embedded Python 1St Edition Armstrong Subero 2 PDF
Full Chapter Programming Microcontrollers With Python Experience The Power of Embedded Python 1St Edition Armstrong Subero 2 PDF
https://textbookfull.com/product/programming-microcontrollers-
with-python-experience-the-power-of-embedded-python-1st-edition-
armstrong-subero-2/
https://textbookfull.com/product/programming-pic-
microcontrollers-with-xc8-1st-edition-armstrong-subero/
https://textbookfull.com/product/programming-with-micropython-
embedded-programming-with-microcontrollers-and-python-1st-
edition-nicholas-h-tollervey/
https://textbookfull.com/product/programming-with-micropython-
embedded-programming-with-microcontrollers-and-python-first-
edition-nicholas-h-tollervey/
MicroPython for the Internet of Things: A Beginner’s
Guide to Programming with Python on Microcontrollers
2nd Edition Bell
https://textbookfull.com/product/micropython-for-the-internet-of-
things-a-beginners-guide-to-programming-with-python-on-
microcontrollers-2nd-edition-bell/
https://textbookfull.com/product/learning-scientific-programming-
with-python-1st-edition-christian-hill/
https://textbookfull.com/product/introduction-to-scientific-
programming-with-python-joakim-sundnes/
https://textbookfull.com/product/programming-with-python-for-
social-scientists-1st-edition-phillip-brooker/
https://textbookfull.com/product/learning-scientific-programming-
with-python-2nd-edition-christian-hill/
Programming
Microcontrollers
with Python
Experience the Power of
Embedded Python
—
Armstrong Subero
Programming
Microcontrollers with
Python
Experience the Power
of Embedded Python
Armstrong Subero
Programming Microcontrollers with Python: Experience the Power of
Embedded Python
Armstrong Subero
Basse Terre, Moruga, Trinidad and Tobago
v
Table of Contents
vi
Table of Contents
Flip-Flop��������������������������������������������������������������������������������������������������������������72
Registers and the Shift Register�������������������������������������������������������������������������73
Multiplexers and Demultiplexers������������������������������������������������������������������������75
Conclusion����������������������������������������������������������������������������������������������������������76
vii
Table of Contents
Medium-Scale Systems��������������������������������������������������������������������������������������97
High-Performance Systems��������������������������������������������������������������������������������98
Distributed Embedded Systems��������������������������������������������������������������������������99
Seven Steps to Developing an Embedded Product�������������������������������������������100
Step 1: Idea Creation�����������������������������������������������������������������������������������100
Step 2: Requirements Specifications�����������������������������������������������������������100
Step 3: Functional Design����������������������������������������������������������������������������101
Step 4: Rapid Prototyping����������������������������������������������������������������������������101
Step 5: Testing���������������������������������������������������������������������������������������������102
Step 6: Securing Your System���������������������������������������������������������������������103
Step 7: Bringing to Market���������������������������������������������������������������������������104
Conclusion��������������������������������������������������������������������������������������������������������105
viii
Table of Contents
Functions����������������������������������������������������������������������������������������������������������120
Lambda Functions���������������������������������������������������������������������������������������������120
Exception Handling�������������������������������������������������������������������������������������������121
Object-Oriented Programming��������������������������������������������������������������������������121
Random and Time���������������������������������������������������������������������������������������������123
Python vs. CircuitPython�����������������������������������������������������������������������������������124
How Does My Python Program Run?����������������������������������������������������������������124
Conclusion��������������������������������������������������������������������������������������������������������125
ix
Table of Contents
x
Table of Contents
SPI Communications�����������������������������������������������������������������������������������������194
Deeper into SPI�������������������������������������������������������������������������������������������������196
SPI Circuit Connection���������������������������������������������������������������������������������������197
SPI with CircuitPython Program������������������������������������������������������������������������197
I2C Communications�����������������������������������������������������������������������������������������199
Deeper into I2C�������������������������������������������������������������������������������������������������200
I2C Support in CircuitPython�����������������������������������������������������������������������������201
The MPU6050����������������������������������������������������������������������������������������������������201
I2C with MCU Schematic�����������������������������������������������������������������������������������202
I2C Circuit Connection Tips�������������������������������������������������������������������������������203
I2C with CircuitPython Program������������������������������������������������������������������������204
Adding Libraries������������������������������������������������������������������������������������������������206
MPU6050 with CircuitPython Program��������������������������������������������������������������206
Conclusion��������������������������������������������������������������������������������������������������������208
xi
Table of Contents
xii
Table of Contents
Index�������������������������������������������������������������������������������������������������285
xiii
About the Author
Armstrong Subero started learning electronics at the age of 8. One of
the happiest memories in his childhood was when he finally figured out
how to make a light bulb. It took off from there as he taught himself more
advanced topics in electronics, before delving into computer architecture,
and eventually discovering the joys of microcontrollers and FPGAs.
He currently works for the Ministry of National Security in his country;
writes software; designs circuits, courses, and robots; writes books; and
blogs about technology on www.trinirobotics.com in his free time.
He is also a contributing author to Free Code Camp and has degrees in
Computer Science and Liberal Arts and Sciences from Thomas Edison
State University. He is the author of Programming PIC Microcontrollers
with XC8 and Codeless Data Structures and Algorithms both published by
Apress Media LLC.
xv
About the Technical Reviewer
Sai Yamanoor is an embedded systems engineer working for an industrial
gases company in Buffalo, NY. His interests, deeply rooted in DIY and
open source hardware, include developing gadgets that aid behavior
modification. He has published two books with his brother, and in his
spare time, he likes to contribute to build things that improve quality of
life. You can find his project portfolio at http://saiyamanoor.com.
xvii
Acknowledgments
I want to thank my family.
I want to thank everyone who ever said anything positive to me or
taught me something. I heard it all, and it meant something.
I want to thank God most of all, because without God I wouldn’t be
able to do any of this.
xix
CHAPTER 1
Getting Ready
You have decided to learn about microcontrollers using Python. While it
would be nice to jump straight into developing cool stuff, there are a few
steps you must take to get ready first. If you have experience with software
development, you may be accustomed to just opening an editor and typing
code right away. However, microcontroller development is a little more
involved than that. After all, you will be writing programs for a separate
computer, so there is a lot more to set up. A microcontroller development
environment consists of not only software but an entire hardware
ecosystem that must be understood to unlock all the magic these devices
have to offer.
In this chapter, we will look at setting up an environment for
development. By the end of the chapter, you will be ready to select a
microcontroller board, and you’ll have the software tools and hardware
devices needed to begin your development with CircuitPython. Let’s get
started!
I ntroduction to Microcontrollers
In our modern world, computers are ubiquitous. They have become
smaller, easier to use, and more integrated into our daily lives. A few years
ago, to connect to the Internet and check your email, you would have
had to sit down at a large desktop device. Today, you can do the same
thing with a computer that fits in the palm of your hand and places all the
information in the world at your fingertips.
© Armstrong Subero 2021 1
A. Subero, Programming Microcontrollers with Python,
https://doi.org/10.1007/978-1-4842-7058-5_1
Chapter 1 Getting Ready
2
Chapter 1 Getting Ready
3
Chapter 1 Getting Ready
4
Chapter 1 Getting Ready
Assembly Language
At one point in time, microcontrollers were programmed exclusively in
assembly language. Today, assembly language is reserved for devices
with limited memory and for situations where the programmer needs to
squeeze every drop of performance out of the microcontroller. Assembly
language is useful in these situations because a lot of assembly instructions
5
Chapter 1 Getting Ready
C
C has long been the language of choice in the embedded world. It was
made to run on memory-constrained devices such as microcontrollers. C
gives us the ability to control the underlying hardware efficiently – one C
instruction translates into several assembly language instructions – and
it can match the speed of assembly language for most applications. Since
C has been used for so long, a lot of the code that is written has been
battle tested and proven to work for the applications they are intended. C
users have access to a large code base with helpful information and code
snippets. However, the language requires a good understanding of the
hardware and is difficult for beginners to break into.
C++
As time progressed and embedded devices became more powerful, some
microcontroller manufacturers and software vendors began adding C++
support to their devices. C++ is slowly gaining traction in the professional
embedded space. In the hobbyist realm, however, C++ finds widespread
use on the Arduino platform. C++ is a large and difficult language to learn,
however. Many of the features that make C++ more effective to use than
C in general-purpose computing applications, sometimes cannot be
6
Chapter 1 Getting Ready
BASIC
In the early 2000s, if a beginner was getting started with microcontrollers
and did not fancy assembly, BASIC was the programming language to use.
BASIC stands for Beginners’ All-Purpose Symbolic Instruction Code and
is an easy programming language to use. There would usually be a BASIC
interpreter on the microcontroller chip to run the instructions.
BASIC eventually fell in popularity because the boards that ran it
cost a lot of money relative to what they were capable of. Additionally,
running the BASIC interpreter slowed the chips down and took up too
many resources on the already constrained 8-bit devices. Also, the tools
and software for the most popular BASIC devices were all closed source,
so people could not make their own BASIC devices. When open source
alternatives like the Arduino came around, devices like the BASIC Stamp
thus fell out of favor.
Rust
The Rust programming language is new compared to C (which is almost
half a century old) and is designed to upset the C and C++ hold on systems
programming, including embedded programming. As microcontrollers
become more powerful and factors like concurrency (the ability to carry
out multiple processes at once) start to matter, Rust’s advantages over C
begin to show. Rust is better suited to concurrency because it can handle
a data race, which is when two devices try to access the same location in
memory at the same time.
7
Chapter 1 Getting Ready
Python
Python is a relative newcomer to the embedded space, and it could
become a major player in the field. Python is a lot simpler than C to learn
and is one of the most popular programming languages today. While
BASIC was also easier than C for beginners, Python has several advantages
over BASIC that make it better suited for use as an embedded language.
Notably, while the popular BASIC microcontrollers were closed source,
Python is open source, allowing you to run Python on your custom device
if you desire. Python files can also be compiled to make them smaller,
allowing you to create tight, memory-efficient programs.
Many people say that interpreted languages like Python are not
suited to the limitations of microcontrollers. This may have once been
true, but with today’s more powerful devices, it is entirely possible for
microcontrollers to run interpreted languages without hiccups like the
speed limitations experienced by older BASIC devices. For extremely
time-efficient computing, also called real-time computing, interpreted
languages are still not suitable. However, Python should have no problem
meeting the speed requirements of the majority of microcontroller
projects.
8
Chapter 1 Getting Ready
9
Chapter 1 Getting Ready
the board and get it up and running. A development board allows you to
use, program, and prototype with the microcontroller without worrying
about the hardware setup.
At the time of writing, CircuitPython supports over 140 boards, and the
list keeps growing. You can view the list here on the CircuitPython website:
https://circuitpython.org/downloads. Any one of these compatible
boards will work with this book. You can also choose to create your
own custom device that supports CircuitPython, a process I will discuss
at the end of this chapter. For beginners, however, using a premade,
CircuitPython-compatible board will always be a better choice. It will
ensure that the hardware is working and allow you to get started more
quickly with writing software for your device.
In this section, we will look at some preconfigured devices that can run
CircuitPython. Though many companies supply microcontroller boards
that are capable of running CircuitPython, Adafruit devices have the best
support since they originated the language and have an entire ecosystem
built around CircuitPython with their development environment.
We’ll look at some of the boards that they have available, along with
some popular boards from other manufacturers that can be used with
CircuitPython. This list is not exhaustive, but the boards presented here
will be compatible with the examples discussed in this book.
10
Chapter 1 Getting Ready
11
Chapter 1 Getting Ready
12
Chapter 1 Getting Ready
13
Another random document with
no related content on Scribd:
officers assembled to await our return, to whom we had to narrate
the whole of the incidents of the night’s adventure. Our surgeon
announced that Mr. Osborne was in his cot, and was suffering much
from his sprained ankle; so Mr. Thompson mercifully left him alone,
vowing he would have his revenge at a more suitable opportunity.
The next morning the American captain, his wife, and the rest of
the party came off and breakfasted with Captain Graves by the
latter’s express invitation; and from the peals of laughter that issued
from within, it was evident that host and guests were in the highest
good-humour. Afterwards the Americans briefly inspected the ship,
and then rowed back to the Snapping Turtle; while we made every
preparation for departing in search of the Flying-fish and her piratical
crew.
After the ship’s company had had their dinner, the boatswain piped
“Up anchor!” and half an hour later we were standing out to sea
under a full head of steam.
CHAPTER II.
WE STORM THE FORT.