Book description
Learn how you can control LEDs, make music, and read sensor data using popular microcontrollers such as Adafruit Circuit Playground, ESP8266, and the BBC micro:bit
Key Features
- Load and execute your first program with MicroPython
- Program an IoT device to retrieve weather data using a RESTful API
- Get to grips with integrating hardware, programming, and networking concepts with MicroPython
Book Description
MicroPython is an open source implementation of Python 3 that runs in embedded environments. With MicroPython, you can write clean and simple Python code to control hardware instead of using complex low-level languages such as C and C++. This book guides you through all the major applications of the MicroPython platform to build and program projects that use microcontrollers.
This MicroPython book covers recipes that will help you experiment with the programming environment and hardware programmed in MicroPython. You'll find tips and techniques for building a variety of objects and prototypes that can sense and respond to touch, sound, position, heat, and light. This book will take you through the uses of MicroPython with a variety of popular input devices and sensors. You'll learn techniques to handle time delays and sensor readings, and apply advanced coding techniques to create complex projects. As you advance, you'll deal with Internet of Things (IoT) devices and integration with other online web services. In addition to this, you'll use MicroPython to make music with bananas and create portable multiplayer video games that incorporate sound and light animations into the gameplay.
By the end of this book, you'll have mastered the tips and tricks to troubleshoot your development problems and take your MicroPython project to the next level.
What you will learn
- Execute code without any need for compiling or uploading using REPL (read-evaluate-print-loop)
- Program and control LED matrix and NeoPixel drivers to display patterns and colors
- Build projects that make use of light, temperature, and touch sensors
- Configure devices to create Wi-Fi access points and use network modules to scan and connect to existing networks
- Use Pulse Width Modulation to control DC motors and servos
- Build an IoT device to display live weather data from the Internet at the touch of a button
Who this book is for
If you want to build and program projects that use microcontrollers, this book will offer you dozens of recipes to guide you through all the major applications of the MicroPython platform. Although no knowledge of MicroPython or microcontrollers is expected, a general understanding of Python is necessary to get started with this book.
Table of contents
- Title Page
- Copyright and Credits
- About Packt
- Contributors
- Preface
-
Getting started with MicroPython
- What is MicroPython?
- What is a microcontroller?
- What is CircuitPython?
- What is the Circuit Playground Express?
- Where to buy
- References
- Flashing the microcontroller firmware
- Executing your first program
- Using screen to access the REPL
- Using Mu to access the REPL
- Executing commands in the REPL
- Using the auto-reload feature
- Updating the CircuitPython Library
-
Controlling LEDs
- The Adafruit Circuit Playground Express layout
- Turning on the pin 13 LED
- Setting the brightness of the NeoPixel
- Controlling the color of a single NeoPixel
- Displaying an LED color using the RGB and hex code
- Setting an LED color using color names
- Setting all NeoPixels to the same color
- Setting a range of NeoPixels to one color
- Generating random NeoPixel LED colors
- Creating LED animations with random colors
- Creating LED animations with rainbow colors
- Creating Sound and Music
-
Interacting with Buttons
- The Adafruit Circuit Playground Express layout
- Detecting push button presses
- Controlling LEDs with push buttons
- Reading a slide switch
- Calling functions on the button state change
- Moving active LEDs with push buttons
- Playing a beep on the button press
- Detecting touch on a touchpad
- Monitoring the touchpad raw measurements
- Adjusting the touch threshold
- Reading Sensor Data
-
Button Bash Game
- Technical requirements
- Circuit Playground Express power
- Creating a class to detect a button state change
- Creating your own Python modules
- Adding button interactions to the event loop
- Creating a generator to get pixel colors
- Showing scores with the ScoreBoard class
- Detecting winners with the ScoreBoard class
- Adding the ScoreBoard class to the event loop
-
Fruity Tunes
- Technical requirements
- Circuit Playground Express touchpads
- Creating a class to react to touch events
- Creating a function to enable speaker output
- Creating a function to play audio files
- Using the NeoPixel object to control pixels
- Creating a touch handler to play sounds
- Creating a touch handler to light up pixels
- Creating an event loop to handle all touch events
-
Let's Move It, Move It
- Technical requirements
- DC motors
- Servos
- Adafruit CRICKIT
- Tuning a servo to the correct pulse width
- Setting the actuation range of a servo
- Setting the angle of a servo
- Sweeping a servo
- Controlling servos with buttons
- Controlling multiple servos
- Turning on a DC motor
- Setting the speed and direction of a DC motor
- Controlling a DC motor with buttons
- Coding on the micro:bit
-
Controlling the ESP8266
- Technical requirements
- The Adafruit Feather HUZZAH ESP8266
- Using the REPL over a serial connection
- Scanning for available Wi-Fi networks
- Configuring settings for AP mode
- Connecting to an existing Wi-Fi network
- Using the WebREPL over Wi-Fi
- Transferring files with the WebREPL CLI
- Controlling the blue and red LEDs
- Interacting with the Filesystem
-
Networking
- Technical requirements
- Performing a DNS lookup
- Creating a function to wait for internet connectivity
- Performing an HTTP request using raw sockets
- Performing an HTTP request using the urequests library
- Fetching JSON data from a RESTful web service
- Creating an HTTP server
- Creating a web handler module
- Controlling LEDs through the web server
- Developing a RESTful API to control the LEDs
- Interacting with the Adafruit FeatherWing OLED
-
Building an Internet of Things (IoT) Weather Machine
- Technical requirements
- Retrieving weather data from the internet
- Creating a function to get a city's weather
- Randomly selecting cities
- Creating a Screen object for text handling
- Creating a function to show a city's weather
- Providing visual feedback when fetching data
- Creating a function to display the weather for a random city
- Creating an IoT button to show the weather around the world
- Coding on the Adafruit HalloWing Microcontroller
- Other Books You May Enjoy
Product information
- Title: MicroPython Cookbook
- Author(s):
- Release date: May 2019
- Publisher(s): Packt Publishing
- ISBN: 9781838649951
You might also like
book
Python Cookbook, 3rd Edition
If you need help writing programs in Python 3, or want to update older Python 2 …
book
Grokking Algorithms
Grokking Algorithms is a fully illustrated, friendly guide that teaches you how to apply common algorithms …
book
Learning Go
Go is rapidly becoming the preferred language for building web services. While there are plenty of …
book
Designing Data-Intensive Applications, 2nd Edition
Data is at the center of many challenges in system design today. Difficult issues such as …