Circuit Python at PyConf Hyderabad

Introduction

Coding in/with hardware has become my biggest stress buster for me ever since I have been introduced to it in PyCon Pune 2017 by John. Coding with hardware provides a real-life interaction with the code you write. It flourishes creativity. I can do all of this while I learn something new. Now I look for auctions to offer me a chance to code in/with Hardware. It gives the chance to escape the muggle world.

Diwali and Circuit Python

Diwali is the festival of flowers, food, and lights. So why not I take Diwali as an opportunity to create some magic. Since 2017 I try to lit my house with hardware, which I operate with coding. This year in PyCon US, all the participants got a piece of Adafruit Circuit Playground Express. For 2019 Diwali, I chose Circuit Playground Express as my wand and spell is abracadabra Circuit Python.

Circuit Python in PyConf Hyderabad

A week before, I got a call from the organizers of PyConf Hyderabad 2019 that if I want to deliver a talk there. Initially, I thought of a talk titled " Limiting the legal risk of your open source project”, this was a talk selected for PyCon India 2019. Unfortunately, I could not deliver it due to my grandfather's demise. I went for this as the talk was ready. But an afterthought invoked an idea that why shouldn’t I go for a talk on Circuit Python? And share what I did in Diwali with it. The organizers also liked it. (Of course, talk about/on hardware is more interesting than a legal talk in a Python conference, right?). In hindsight, it meant much work within 6 days, but I dove for it.

The day of the talk

My morning of the day of the conference started with a lovely surprise. My work has been featured on a blog by Adafruit. It was a fantastic feeling when your work gets recognition from the organization you admire the most. Thank you, Adafruit.

Mine was the 4th talk of the day and the talk before lunch. It was my first talk on/about hardware/hardware projects. This is a talk I am giving after 1 and a half years after my battle with depression. So in a word, I was nervous.

The talk

I started with why programming with/on hardware is essential for me. Then moving to what is Circuit Python and Circuit Playground Express. I use mu as my editor for all my hardware projects. This is no exception. I opened up the editor and started to code. Then came the time to showcase my work/projects with Circuit Python.

In the first example, I turned on the first NeoPixel of the CPX into Red.

from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.5

# COLOUR

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0,one_led_red.py 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
MAROON = (128,0,0)
PURPLE =(128,0,128)
TEAL = (0,128,128)
OFF = (0, 0, 0)


while True:
    cpx.pixels[0] = RED

In CPX, we have 10 individually addressable LEDs. Now, we are importing the adafruit_circuitplayground.express module as cpx so that it becomes easier to type. If you remember, there are 10 neopixels on the board, and we can access them as cpx.pixels. We can set the brightness of pixels on the board. Instead of full, I am setting it as half as the lights are really bright. Then I have defined a few colours.

I want my light to lit forever so,' while True' and I am setting the first neopixel's color as red.

all_led_rgb.py

One led is a bit dull; let us lighten up the room. Here we will light up all the neopixels of cpx. With cpx.pixels.fill I am filling up the color red, green, and blue. I am giving a 1-second gap to see the change of colors.

from adafruit_circuitplayground.express import cpx
import time

cpx.pixels.brightness = 0.5

#COLOUR

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
MAROON = (128,0,0)
PURPLE =(128,0,128)
TEAL = (0,128,128)
OFF = (0, 0, 0)


while True:
    cpx.pixels.fill(RED)
    time.sleep(1)

For the third example, I showed the code where I was

  • switching on all neopixels on cpx one by one
  • then I was switching it off in the same order.
import time
from adafruit_circuitplayground.express import cpx
from random import randint

cpx.pixels.brightness = 0.5

#  COLOUR

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
MAROON = (128,0,0)
VIOLET =(128,0,128)
TEAL = (0,128,128)
OFF = (0, 0, 0)

COLOURS = [RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE, MAROON, VIOLET, TEAL]

i = 0
complete = False

while True:
    if complete:
        c = (0,0,0)
    else:
        r = randint(0,9)
        c = COLOURS[r]
    cpx.pixels[i] = c
    time.sleep(1)
    i += 1
    if i == 10:

        i = 0
        if complete:
            complete = False
        else:
            complete = True

The next was the code which I used to lit my Diwali Kandel. I needed cpx to be continuously lighted. So I wrote this code.

import time
from adafruit_circuitplayground.express import cpx
from random import randint

cpx.pixels.brightness = 0.5

# COLOUR

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
MAROON = (128,0,0)
VIOLET =(128,0,128)
TEAL = (0,128,128)
OFF = (0, 0, 0)

COLOURS= [RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE, MAROON, VIOLET, TEAL]

i = 0


while True:
    r = randint(0,9)
    c = COLOURS[r]
    cpx.pixels[i] = c
    time.sleep(1)
    i += 1
    if i == 10:
        i = 0

It is customary that we play a game on Diwali. I have replaced card games with a game on cpx. It is a guessing game on CPX.

import time
from adafruit_circuitplayground.express import cpx
from random import randint

cpx.pixels.brightness = 0.5

# COLOUR

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
MAROON = (128,0,0)
VIOLET =(128,0,128)
TEAL = (0,128,128)
OFF = (0, 0, 0)

COLOURS = [RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE, MAROON, VIOLET, TEAL]

i = 0


while True:
    if cpx.button_a:
        r = randint(0,9)
        c = COLOURS[r]
        cpx.pixels[i] = c
        time.sleep(0.5)
        i += 1
    elif cpx.button_b:
        cpx.pixels.fill( (0,0,0))
        i = 0

    if i == 10:
        i = 0

What is Diwali without music? Circuit Python and Adafruit have options for that also. I demonstrated Adafruit NeoTrellis M4 for creating some beats.
It is a backlight keypad driver system. We can use the Adafruit NeoTrellis with

  • Python or
  • CircuitPython, and the
  • CircuitPython Trellis module, provided by Adafruit.

The Adafruit module enables one to write Python code controlling the neopixel and read button presses on a single Trellis board or with a matrix of up to eight Trellis boards.

This board can be used with any

  • CircuitPython microcontroller board or
  • with a computer that has GPIO and Python.

Coloring the NeoPixel strip

At PyCon US 2019, while discussing about projects on my previous Diwali with Nina, she got excited and gifted me with a neopixel strip. And as the lady commanded, I lit my Diwali rangoli with neopixel strip connecting it with the alligator clip at cpx :

  • GND - black
  • A1 (data) - white
  • VOUT - red

and ran the code :

import time
from adafruit_circuitplayground.express import cpx
import board
import neopixel
pixels = neopixel.NeoPixel(board.D6, 30)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)


while True:
    for i in range(0,30):
        pixels[i] = red
        time.sleep(0.1)
    for i in range(29,-1, -1):
        pixels[i] = green
        time.sleep(0.1)
        

Finally, it was time to light the Diwali diya. I have placed 3 diyas, (color by my toddler) and then fixed the neo pixel strip taping it on the diyas and then ran this code :

import time
from adafruit_circuitplayground.express import cpx
import board
import neopixel
pixels = neopixel.NeoPixel(board.D6, 30)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

cpx.pixels[1] = red
cpx.pixels[2] = red
cpx.pixels[9] = green
cpx.pixels[10]= green
cpx.pixels[18] = red
cpx.pixels[19]= red
cpx.pixels[27] = green
cpx.pixels[26]= green
time.sleep(30)

I never showed this code during the talk itself for the time constraint.

The slide :

My slides are public at https://slides.com/dascommunity/my-diwali-with-circuit-python#/

My Gratitude:

I would like to show my deepest gratitude to Nina Zakharenko, Kattni and Carol Willing for the hardware, Scott Shawcroft for giving me guidance into Circuit Python, Nicholas Tollervey for giving us mu and John Hawley for dragging me into hardware. Moreover thank you everyone for helping me, supporting me, standing by me and inspiring me when I broke down.

Conclusion

I really enjoyed giving the talk on/with/in Circuit Python. I will be here, coding simple, fun, useless and creative stuff.

Show Comments