- Embedded Programming with Microcontrollers and Python -
by Nicholas H. Tollervey
Book Details
Price
|
3.00 |
---|---|
Pages
| 210 p |
File Size
|
17,547 KB |
File Type
|
PDF format |
ISBN
| 978-1-491-97273-1 |
Copyright©
| 2018 Nicholas H. Tollervey |
In late 2012, I had an idea to build a tiny, low-powered computing machine that
could run scripts directly on the “bare metal” of the underlying hardware. The aim
was to make it extraordinarily easy for anyone—no matter his or her background nor
technical skill level—to be able to make a digital device that uses 1’s and 0’s to control
the real world. In my particular case, I wanted an easier way to program the little
robots that I was building at the time.
At first I didn’t have any particular scripting language in mind for this device, but
after investigating what would be a good choice for the language, and what people
would like writing in and would find easy to learn, I chose Python.
I was also interested in running a Kickstarter campaign, to see if people liked the idea
and to perhaps begin to build a community around the project. The first Kickstarter
campaign that ran at the end of 2013 really did kick start MicroPython: it raised
funds to build 3,000 pyboards upon which MicroPython would run; it brought nearly
2,000 people into the initial community; and it attracted some very talented programmers
who volunteered their time and expertise to contribute to and improve Micro‐Python.
My background is theoretical physics, so I approach the design and development of
MicroPython from a much more academic and research-oriented point of view, compared
to simply engineering a solution to a problem. I believe this has been part of
the reason for the success of MicroPython. From the outset it was not obvious that
MicroPython would even work, that Python could at all be shrunk down to such a
small size and run on tiny microcontrollers with very little memory. I treated the creation
of MicroPython like a research project and I used analysis combined with many
coding tricks, ignoring a lot of software development principles, in order to just get it working.
My general philosophy with MicroPython is to remain minimal yet usable. And the
name embodies this: “micro” makes reference to microcontrollers and embedded systems,
but also gives the feeling of minimalism. “Python” is the language and is a very
pragmatic and usable language, designed so humans can enjoy programming. On the
one extreme, making everything too minimal hurts (the ultimate minimalism is
nothing!), but on the other extreme, adding masses of usability features leads to over
burden and excessive resource usage. These ideas about balancing minimalism with
pragmatism apply not just to the design and development philosophy of Micro‐
Python but also to the wider computing industry and even beyond.
Since the first Kickstarter MicroPython has seen fantastic growth, and I, along with
many others in the community, have put an immense amount of work into Micro‐
Python, on both the hardware and software fronts. Today MicroPython stays true to
its original goal of making it easy for anyone to digitally control the real world, and is
widening its scope and seeing use by hobbyists and professionals, in education and
research, as well as commercial products. MicroPython has actually formed its own
little industry (soon to be much bigger!), and it’s very humbling to see one’s creation
grow and attract interest from so many diverse people around the world, and for so
many diverse applications.
MicroPython is constantly improving, the community of users is growing every day,
and the future looks very bright indeed. I hope that MicroPython continues to inspire
people, continues to help people learn, and continues to be used to build and control
systems, from coffee makers to space satellites. Digital computing devices need to be
programmed and MicroPython provides an efficient and enjoyable way to do just that.
Nicholas has been a great proponent of MicroPython since the early days and has
contributed immensely to its development, most notably from the community and
education side, and was key to the development and success of the micro:bit platform.
He knows MicroPython very well, particularly from the point of view of an end
user, and is well qualified to teach you about it.
So dive into it, read and enjoy this fantastic initiation into the world of MicroPython,
and go out and build some amazing digital devices!
— Damien P. George
Creator of MicroPython
July 2017
Table of Contents
Foreword. . . .. . . . . . vii
Preface. . . . . . . . . ix
1. What Is MicroPython?.. . . . . . . . 1
Why Micro? 2
Why Python? 4
MicroPython Genesis 9
Dive In! 12
2. PyBoard. . . . . . 15
The Hardware 17
Developer Setup 18
3. BBC micro:bit.. . . . . . 23
The Hardware 25
Developer Setup 27
4. Adafruit Circuit Playground Express. . . . 33
The Hardware 34
Developer Setup 36
5. ESP8266 / ESP32. . . . . . . . 39
The Hardware 41
Developer Setup 42
ESP8266 42
ESP32 45
6. Thinking Embedded. . . . . . . . . . 49
Human Drives 53
Abilities of Enchantment 56
Steps on the Ladder of Enchantment 60
7. Visual Feedback. . . . . . . . . . . 63
Blinkenlights 63
NeoPixels (Blinkenlights on Steroids) 67
Text, Images, and Animation 70
PyBoard Colour LCD Display 75
8. Input and Sensing. . . . . . . 81
Buttons and Capacitative Touch 81
Accelerometers, Gestures, and Compasses 89
Sound, Light, and Temperature 91
Sensing with Peripherals 95
9. GPIO. . . . . . . . . . . . . . . 99
Pins 99
UART 103
SPI 105
I2C 108
Miscellaneous GPIO Techniques and Protocols 110
10. Networking. . . . . . . . . 113
Circuit Playground Express Infrared 115
The micro:bit Radio 120
ESP8266/32 WiFi 128
MQTT 136
11. Sound and Music. . . . . . . . . . 141
Bleeps and Bloops 141
Music 151
Speech 154
12. Robots. . .. . . . . . . . . 163
Trundle Bot 163
Racer Bot 169
13. Idiomatic MicroPython. . . . . 175
The Zen of MicroPython 177
Memory 177
14. Next Steps. . . . . . . . . . 185
The Community 185
Going Deeper 186
Index. . . . . . . . 189
About the Author
Nicholas H. Tollervey is a classically trained musician, philosophy graduate, teacher,
writer, and software developer. He’s just like this biography: concise, honest, and full
of useful information.
He’s @ntoll on Twitter and blogs at http://ntoll.org/.