Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nina Zakharenko - Light Up Your Life -- With Python and LEDs! PyCascades 2019

Nina Zakharenko - Light Up Your Life -- With Python and LEDs! PyCascades 2019

Python opens a whole new world of working with wearable electronics. MicroPython is a Python variant can run with just 256k of storage space and 16k of RAM. CircuitPython is a fork of MicroPython focused on teaching. Learn how to program LEDs with Python, to light up your life with code and creativity.

--

Nina Zakharenko is a Senior Cloud Developer Advocate at Microsoft, focusing on Python. Before joining Microsoft, she was a Senior Software Engineer with over a decade of experience writing software for companies like Reddit, Meetup, and HBO. In her spare time, she enjoys snowboarding, hiking, and tinkering with wearable electronics from her home base in Portland, OR.

Nina Zakharenko

February 24, 2019
Tweet

More Decks by Nina Zakharenko

Other Decks in Programming

Transcript

  1. Light Up Your Life --
    With Python & LEDs!
    bit.ly/pyc_leds
    (download for clickable links)
    Code at: git.io/techtalks
    Nina Zakharenko - @nnja

    View Slide

  2. Livetweet!
    use #PyCascades2019
    @nnja
    Slides: bit.ly/pyc_leds

    View Slide

  3. Where To Start
    When You're Just Getting Started
    » Hardware Options
    » Programming LEDs
    » Debugging with print() statements
    » Tools
    » Libraries
    » Projects
    @nnja

    View Slide

  4. some of my projects

    View Slide

  5. Hardware Options
    » Raspberry Pi Zero W
    » Small Development Kit w/ wireless and bluetooth
    connectivity
    » BBC micro:bit
    » computing device aimed at learning
    » Adafruit - m0 and m4 line of devices
    » & lots more...
    @nnja

    View Slide

  6. @nnja

    View Slide

  7. my

    : The HalloWing m0
    a spooky development kit
    @nnja

    View Slide

  8. @nnja

    View Slide

  9. Adafruit
    Circuit
    PlayGround
    Express
    Learning Platform -
    Cost: $25->
    @nnja

    View Slide

  10. Python's Batteries Included
    Philosophy
    “The Python source distribution has long maintained
    the philosophy of "batteries included" -- having a
    rich and versatile standard library which is
    immediately available, without making the user
    download separate packages.
    This gives the Python language a head start in many
    projects.”
    PEP 206
    @nnja

    View Slide

  11. CircuitPlaygroundExpress Tour

    View Slide

  12. MicroPython &
    CircuitPython
    » MicroPython is a variant of
    Python 3 designed to run of
    microcontrollers.
    » Runs within just 256k of
    code space and 16k of RAM.
    » CircuitPython is an
    education friendly Adafruit
    fork of MicroPython
    » Both are open source!
    @nnja

    View Slide

  13. Libraries
    » Many available
    » Need to be in the lib folder
    » Download from: git.io/circuitpythonlib
    »
    !
    We're interested in
    adafruit_circuitplayground.express, so we can
    interact with the board.
    @nnja

    View Slide

  14. from adafruit_circuitplayground.express import cpx
    cpx.button_a # Is Button A Pressed? True or False
    cpx.button_b # Is Button B Pressed? True or False
    learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/

    View Slide

  15. RGB LEDs
    RGB LEDs have one LED of each
    color - red, green, and blue.
    By combining the amount and
    intensity of these colors
    (255 * 255 * 255) you can
    produce over 16 million color
    variations!
    image source: randomnerdtutorials.com/electronics-basics-how-do-rgb-leds-work/

    View Slide

  16. Neopixels in action
    @nnja

    View Slide

  17. # Cycle between Red, Green, Blue each time a button is pressed
    from adafruit_circuitplayground.express import cpx
    import time
    RED = (255, 0, 0) # 255 Red, 0 Green, 0 Blue
    GREEN = (0, 255, 0) # 0 Red, 255 Green, 0 Blue
    BLUE = (0, 0, 255) # 0 Red, 0 Green, 255 Blue
    cpx.pixels.brightness = 0.1
    my_colors = [RED, GREEN, BLUE]
    color_pos = 0
    while True: # Keep looping forever...
    cpx.pixels.fill(my_colors[color_pos])
    if cpx.button_a or cpx.button_b: # Button A or B was pressed
    color_pos = (color_pos + 1) % len(my_colors)
    time.sleep(0.2) # Sleep, to make the button less sensitive

    View Slide

  18. @nnja

    View Slide

  19. How to program it?
    » Plug it in with a data + charge USB cable
    » See a CircuitPython drive?
    » on Mac OS in /Volumes/CIRCUITPY
    » Otherwise, install CircuitPython
    » Next, edit & save the code.py (or main.py) file.
    Your code runs instantly!
    !
    @nnja

    View Slide

  20. @nnja

    View Slide

  21. I
    !
    Programming
    with Visual Studio
    Code
    + The Python
    Extension
    $ # To open on Mac OS
    $ code /Volumes/CIRCUITPY/
    @nnja

    View Slide

  22. Programming with Mu Editor:
    a simple Python editor for beginner programmers

    View Slide

  23. Programming
    with EduBlocks
    (in Python3!
    !
    )
    » Newly announced integration
    » Drag n Drop scratch-like
    code editor
    » It's Python3 under the
    hood!
    » Switch between code +
    blocks
    @nnja

    View Slide

  24. Investigating with print statements
    my_colors = [("Red", RED), ("Green", GREEN), ("Blue", BLUE)]
    color_pos = 0
    while True: # Keep looping forever...
    name, color = my_colors[color_pos]
    cpx.pixels.fill(color)
    if cpx.button_a or cpx.button_b: # Button A or B was pressed
    color_pos = (color_pos + 1) % len(my_colors)
    next_name, _ = my_colors[color_pos]
    print("Changing color from %s to %s" % (name, next_name))
    time.sleep(0.2) # Sleep, to make the button less sensitive
    @nnja

    View Slide

  25. View print statements & errors
    via the serial console
    » To see print() statements or errors, you need to
    receive output from the Circuit Play Ground
    Express by opening a connection to the serial
    console.
    »
    !
    You won't see any output without it.
    @nnja

    View Slide

  26. easy serial console:
    with mu editor
    The easiest way to connect to the serial console is
    the serial button in the my editor toolbar.
    @nnja

    View Slide

  27. @nnja

    View Slide

  28. advanced serial console:
    the terminal
    On Mac OS, can use the terminal and screen program to
    connect to the serial console on the Circuit
    Playground Express.
    $ ls /dev/tty.* # Select the usbmodem
    $ screen /dev/tty.board_name 115200 # `115200` is the baud rate.
    To quit screen, press ctrl+a followed by k.
    Or, learn how to connect via the terminal in other OSes

    View Slide

  29. View Slide

  30. Powering Your
    Projects [1/2]
    » micro USB (same port for
    power & programming)
    » from a computer, portable
    phone charger, or wall
    » battery packs (JST
    connector)
    » need minimum 3.5V DC.
    Power with 3 household AAA
    batteries + enclosure with
    correct connector.

    View Slide

  31. Powering Your Projects [2/2]
    » lithium polymer (li-poly)
    batteries
    » warning: only for advanced
    users
    » small, lightweight, energy
    dense, fragile
    » needs special care!
    » cannot be heated,
    punctured, or bent
    »
    !
    for kids w/o supervision
    » requires additional charger

    View Slide

  32. Putting it all together
    » Adafruit demo code: bit.ly/initial_cpe
    » Initial directory: bit.ly/initial_cpe_dir
    » Demo covers a majority of the features, prints out
    sensor readings, etc.
    » There's even a flag you can set to turn the
    Circuit Playground Express into a little piano
    using the capacitive touch pads!
    @nnja

    View Slide

  33. Microsoft Make Code

    Coding not
    required

    makecode.adafruit.com
    incudes built in emulator
    learn.adafruit.com/circuit-playground-express-con-badge/

    View Slide

  34. Find projects,
    or Share yours.
    · Adafruit Guides
    · Microsoft Make Code
    · Instructables
    ← solder-free Circuit
    PlayGround Express Jack-o-
    Lantern
    learn.adafruit.com/circuit-playground-jack-o-lantern

    View Slide

  35. Next Steps
    » Buy (or borrow) a device of your choice or use the
    MakeCode emulator
    » Pick your editor (Visual Studio Code, Mu, etc..)
    » Write your own code
    » Use a library
    » Or start with a shared project
    ... start with Python on hardware in no time!
    @nnja

    View Slide

  36. github.com/nnja/pyearrings

    View Slide

  37. news &
    announcements
    1.Hack on CircuitPython with
    Scott →
    2.CPE in PyCon US Swag Bags
    !
    3.CPE Lunchbox Kits @
    Microsoft PyCon US Booth

    View Slide

  38. Thank You!
    @nnja
    Python @
    Microsoft:
    bit.ly/pyc_msft
    Code: git.io/techtalks
    *Additional resources on the next slide

    View Slide

  39. Resources
    » Circuit PlayGround Express Library -
    learn.adafruit.com/circuitpython-made-easy-on-
    circuit-playground-express/
    » Circuit Python Essentials -
    cdn-learn.adafruit.com/downloads/pdf/circuitpython-
    essentials.pdf
    » AdaFruit Python Compatible m0
    www.adafruit.com/?q=m0 and m4 www.adafruit.com/?q=m4
    @nnja

    View Slide