MicroPython Cookbook

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

  1. Title Page
  2. Copyright and Credits
    1. MicroPython Cookbook
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Get in touch
      1. Reviews
  6. Getting started with MicroPython
    1. What is MicroPython?
    2. What is a microcontroller?
    3. What is CircuitPython?
    4. What is the Circuit Playground Express?
    5. Where to buy
    6. References
    7. Flashing the microcontroller firmware
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Executing your first program
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Using screen to access the REPL
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Using Mu to access the REPL
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Executing commands in the REPL
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Using the auto-reload feature
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    13. Updating the CircuitPython Library
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  7. Controlling LEDs
    1. The Adafruit Circuit Playground Express layout
    2. Turning on the pin 13 LED
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Setting the brightness of the NeoPixel
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Controlling the color of a single NeoPixel
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Displaying an LED color using the RGB and hex code
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Setting an LED color using color names
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Setting all NeoPixels to the same color
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Setting a range of NeoPixels to one color
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Generating random NeoPixel LED colors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Creating LED animations with random colors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Creating LED animations with rainbow colors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  8. Creating Sound and Music
    1. The Adafruit Circuit Playground Express layout
    2. Making a beeping sound
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Controlling tone, frequency, and duration
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Playing a musical note
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Playing a melody
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Sounding the alarm
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Playing WAV files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Converting MP3 files to WAV files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Starting and stopping tones
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  9. Interacting with Buttons
    1. The Adafruit Circuit Playground Express layout
    2. Detecting push button presses
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Controlling LEDs with push buttons
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Reading a slide switch
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Calling functions on the button state change
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Moving active LEDs with push buttons
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Playing a beep on the button press
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Detecting touch on a touchpad
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Monitoring the touchpad raw measurements
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Adjusting the touch threshold
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  10. Reading Sensor Data
    1. Circuit Playground Express sensors
    2. Reading temperature readings
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Reading brightness levels from the light sensor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Creating a light meter
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Reading data from the motion sensor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Detecting a single or double tap
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Detecting a shake
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Beeping on a shake
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  11. Button Bash Game
    1. Technical requirements
    2. Circuit Playground Express power
    3. Creating a class to detect a button state change
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Creating your own Python modules
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Adding button interactions to the event loop
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Creating a generator to get pixel colors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Showing scores with the ScoreBoard class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Detecting winners with the ScoreBoard class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Adding the ScoreBoard class to the event loop
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  12. Fruity Tunes
    1. Technical requirements
    2. Circuit Playground Express touchpads
    3. Creating a class to react to touch events
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Creating a function to enable speaker output
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating a function to play audio files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Using the NeoPixel object to control pixels
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Creating a touch handler to play sounds
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Creating a touch handler to light up pixels
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Creating an event loop to handle all touch events
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  13. Let's Move It, Move It
    1. Technical requirements
    2. DC motors
    3. Servos
    4. Adafruit CRICKIT
      1. Where to buy
    5. Tuning a servo to the correct pulse width
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Setting the actuation range of a servo
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Setting the angle of a servo
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Sweeping a servo
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Controlling servos with buttons
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Controlling multiple servos
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Turning on a DC motor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Setting the speed and direction of a DC motor
      1. Getting ready
      2. How to do it....
      3. How it works...
      4. There's more...
      5. See also
    13. Controlling a DC motor with buttons
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  14. Coding on the micro:bit
    1. Technical requirements
    2. The micro:bit
    3. Using Mu to flash code onto the micro:bit
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using Mu to get a REPL on the micro:bit
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Displaying a single character on the LED display
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Displaying a built-in image
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Displaying scrolling text
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Showing which button has been pressed
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Creating a countdown timer
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  15. Controlling the ESP8266
    1. Technical requirements
    2. The Adafruit Feather HUZZAH ESP8266
      1. Where can you buy these?
    3. Using the REPL over a serial connection
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Scanning for available Wi-Fi networks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Configuring settings for AP mode
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Connecting to an existing Wi-Fi network
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Using the WebREPL over Wi-Fi
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Transferring files with the WebREPL CLI
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Controlling the blue and red LEDs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  16. Interacting with the Filesystem
    1. Technical requirements
    2. Remounting the filesystem
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Listing files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Removing files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating a directory
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Reading a file's contents
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Writing a file's contents
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Calculating disk usage
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  17. Networking
    1. Technical requirements
    2. Performing a DNS lookup
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Creating a function to wait for internet connectivity
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Performing an HTTP request using raw sockets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Performing an HTTP request using the urequests library
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Fetching JSON data from a RESTful web service
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Creating an HTTP server
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Creating a web handler module
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Controlling LEDs through the web server
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Developing a RESTful API to control the LEDs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  18. Interacting with the Adafruit FeatherWing OLED
    1. The Adafruit FeatherWing OLED
      1. Where to buy it
    2. Technical requirements
    3. Detecting button presses with GPIO pins
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Connecting to the SSD1306 display
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Filling and clearing the display
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Setting pixels on the display
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Drawing lines and rectangles on the display
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Writing text on the display
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Inverting colors on the display
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  19. Building an Internet of Things (IoT) Weather Machine
    1. Technical requirements
    2. Retrieving weather data from the internet
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Creating a function to get a city's weather
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Randomly selecting cities
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating a Screen object for text handling
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Creating a function to show a city's weather
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Providing visual feedback when fetching data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Creating a function to display the weather for a random city
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Creating an IoT button to show the weather around the world
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  20. Coding on the Adafruit HalloWing Microcontroller
    1. The Adafruit HalloWing M0 Express
      1. Where to buy
    2. Technical requirements
    3. Discovering I2C devices
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Reading data from the accelerometer using I2C
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Detecting board-flipping with the accelerometer
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Controlling screen brightness
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Displaying a bitmap image
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Listing all image files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Creating a joke-telling machine
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: MicroPython Cookbook
  • Author(s): Marwan Alsabbagh
  • Release date: May 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838649951