Hardware Week 1: Intro to On-Board IO

Day 1

GA CCI Hardware Week 1
GA CCI Hardware Week 1
GA CCI Hardware Week 1

Class Overview

  • intro to microcontrollers
  • talking to your board
  • inputs and outputs
GA CCI Hardware Week 1

Ground Rules

  • DO NOT PUT THESE ON METAL1
  • Don't force a plug into a socket
  • Don't struggle alone
  1. Static electricity is the circuit's worst enemy ↩︎

GA CCI Hardware Week 1

What's a microcontroller?

Microcontrollers are sometimes called circuit boards or just boards.

GA CCI Hardware Week 1

The Circuit Playground Express

GA CCI Hardware Week 1
  • learning platform with all the bells & whistles
  • ATSAMD21 ARM Cortex M0 Processor
  • literally so much on this thing
GA CCI Hardware Week 1
GA CCI Hardware Week 1
GA CCI Hardware Week 1
GA CCI Hardware Week 1

So what's on your board?

  • 10 x mini NeoPixels, each one can display any color
  • 1 x Motion sensor (LIS3DH triple-axis accelerometer with tap detection, free-fall detection)
  • 1 x Temperature sensor (thermistor)
  • 1 x Light sensor (phototransistor). Can also act as a color sensor and pulse sensor.
  • 1 x Sound sensor (MEMS microphone)
  • 1 x Mini speaker with class D amplifier (7.5mm magnetic speaker/buzzer)
  • 2 x Push buttons, labeled A and B
  • 1 x Slide switch
  • Infrared receiver and transmitter - can receive and transmit any remote control codes, as well as send messages between Circuit Playground Expresses. Can also act as a proximity sensor.
  • 8 x alligator-clip friendly input/output pins
  • Includes I2C, UART, 8 pins that can do analog inputs, multiple PWM output
  • 7 pads can act as capacitive touch inputs and the 1 remaining is a true analog output
  • Green "ON" LED so you know its powered
  • Red "#13" LED for basic blinking
  • Reset button
  • ATSAMD21 ARM Cortex M0 Processor, running at 3.3V and 48MHz
  • 2 MB of SPI Flash storage, used primarily with CircuitPython to store code and libraries.
  • MicroUSB port for programming and debugging
  • USB port can act like serial port, keyboard, mouse, joystick or MIDI!
GA CCI Hardware Week 1

So how do we talk to these things?

GA CCI Hardware Week 1
GA CCI Hardware Week 1

How does CircuitPython work?

GA CCI Hardware Week 1

Download & Open MU12

https://codewith.mu/

  1. If you want to work a different way with your code, you can! Check the class resources for more info. ↩︎

  2. Also, yeah this is being sunsetted ... but it's still the best learning platform for CircuitPython at the moment ↩︎

GA CCI Hardware Week 1

Ground Rules:

  • your code lives on your microcontroller
  • code lives in code.py
  • runs when you save
  • formatting matters
GA CCI Hardware Week 1
print('Hello, World')
GA CCI Hardware Week 1

Find a partner, and get your board to say hello to them.

print('Hello, World')
GA CCI Hardware Week 1

Now let's add some structure to it.

GA CCI Hardware Week 1
import time 

while True: 
    print("hello")
    time.sleep(1)

print("I never print")
GA CCI Hardware Week 1
GA CCI Hardware Week 1

Okay, let's control some hardware.

GA CCI Hardware Week 1
import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.1)
    led.value = False
    time.sleep(0.5)
GA CCI Hardware Week 1

Onboard I/O

Input & output

GA CCI Hardware Week 1

Output and Input

Output can be:

  • Lights
  • Speakers
  • Motors

Input can be:

  • Buttons
  • Sensors
  • Microphones
GA CCI Hardware Week 1

That was output. Let's do some input.

GA CCI Hardware Week 1
GA CCI Hardware Week 1
import time
import board
import digitalio

led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

button = digitalio.DigitalInOut(board.BUTTON_A)
button.switch_to_input(pull=digitalio.Pull.DOWN)

while True:
    if button.value:  # button is pushed
        led.value = True
    else:
        led.value = False

    time.sleep(0.01)

Example Code

GA CCI Hardware Week 1

Some exercises to try ...

  • Try using the second button. Can you make the LED only light up when both buttons are pressed?
  • Can you make a light that turns on if one button is pressed, and turns off if the other one is?
  • Can you make the board say "hello" when a button is pressed?
GA CCI Hardware Week 1

Did you make the board say hello? What happened?

GA CCI Hardware Week 1
import time
import board
import digitalio

button = digitalio.DigitalInOut(board.BUTTON_A)
button.switch_to_input(pull=digitalio.Pull.DOWN)

while True:
    print(button.value)
    time.sleep(0.1)
GA CCI Hardware Week 1

Pair up with someone and see if you can solve that issue.

(5 mins)

GA CCI Hardware Week 1

We have to do something only when the button value changes.

button = digitalio.DigitalInOut(board.BUTTON_A)
button.switch_to_input(pull=digitalio.Pull.DOWN)

lastValue = button.value

while True:
    if (button.value == True) and (lastValue == False):
        print("Only prints once per button press")
    lastValue = button.value
    
    time.sleep(0.1)

This is called edge detection. Example code

GA CCI Hardware Week 1

We'll get to more output in a minute! But first ...

Libraries

GA CCI Hardware Week 1
GA CCI Hardware Week 1

To use external libraries, you need to copy the library files into the CIRCUITPY/lib folder.

GA CCI Hardware Week 1

Libraries in practice: Neopixels

GA CCI Hardware Week 1
GA CCI Hardware Week 1

Example Package

Download the package above. You should see the following: this is just like what's on your CIRCUITPY. Copy these over; put these libraries in /lib and replace your code.py with this one.

GA CCI Hardware Week 1
import time
import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1, auto_write=False)
RED = (255, 0, 0)
OFF = (0, 0, 0)

while True:
    pixels.fill(RED)
    pixels.show()
    time.sleep(1)
GA CCI Hardware Week 1

Neopixel Challenges:

  • Turn the whole board your favorite color
  • Make every other pixel a different color
  • Make the whole board flash 2 different colors
GA CCI Hardware Week 1

Challenges Pt 2

  • Animate a pixel in a circle around the board
  • Use a button press and your neopixels together
  • Use your neopixels to count the number of button presses
GA CCI Hardware Week 1

Addressing multiple pixels at a time:

    indices = [0, 2, 4]
    for i in indices:
        pixels[i] = CYAN
GA CCI Hardware Week 1

Exercise for Tuesday April 14

Take a library from this class (or another!) and make something with it.

GA CCI Hardware Week 1

What we learned:

  • What's a microcontroller
  • Intro to Mu
  • Intro to CircuitPython
  • Buttons, I/O, Serial Monitor
  • Edge Detection
  • Libraries
  • Intro to Neopixel
GA CCI Hardware Week 1

Resources:

  • Check out the Adafruit CircuitPython Examples, specifically the Light Sensor, Sound Meter, and Temperature Sensors.
GA CCI Hardware Week 1