Lecture 02

E21 Computer Engineering Fundamentals

Author

Emad Masroor

Published

September 3, 2026

Lab Section Assignments

See Google Sheet sent out by Prof. Zucker!

Materials you should have

Circuit Playground Express

Thonny Code Editor

Install CircuitPython*

* Drag-and-drop UF2 file onto CPLAYBOOT drive; if you see CIRCUITPY as an external drive, you have succeeded. Find instructions on Lecture 1 page.

Also:

USB-A to C adapter

Integrated Development Environment (IDE)

  • A ‘development environment’ is the program (or programs) you use to write code.
  • It’s possible to simply use Notepad as your Development Environment, but that’s kinda lame
  • Some options
    • VS Code (v. popular among Swat students)
    • A simple text editor with syntax highlighting (e.g. Sublime Text)
    • Thonny, Mu, other ‘lightweight IDEs’
    • IDLE (official Python IDE)

Two ways to run code on your board

Editing code.py on board the file

Click ‘Stop’ and code interactively
  • Once you’re sure what you want to code
  • Thoughtful, long-form coding
  • Runs if and when you save code.py
  • Persistent
  • Trying things out
  • Instant feedback if works
  • Runs when you hit enter
  • No memory

Let’s start with the CPX in interactive mode

  1. Disconnect and reconnect CPX.
  2. Open Thonny Code Editor. You should see “CircuitPython (generic)” here.

if not, click

  1. Click . You should now see white lights

Using your Circuit Playground Express

The CPX can do some things without loading any libraries. e.g., try:

This uses the computer on board the CPX to calculate 2+2.

NotePython features

Unlike MATLAB, semicolons are irrelevant. Don’t use them!

The Adafruit Library

The CPX comes with a library of programs to control it, but you have to load it. Enter:

from adafruit_circuitplayground.express import cpx

Find the documentation for this library here.

NoteExpress vs. non-Express

Much of the code we use is shared between the ‘express’ and ‘non-express’ versions. You can often do

from adafruit_circuitplayground import cp

instead of

from adafruit_circuitplayground.express import cpx
Tip

Keep this snippet of code handy if you’re slow to type; you’ll have to copy-and-paste it many times!

Reading Sensors on the CPX

  1. Light Sensor: cpx.light
  2. Acceleration: cpx.acceleration
  3. Temperature: cpx.temperature
  4. Touch sensors: cpx.touch_A4, cpx.touch_A3 etc.

Output with the CPX

  1. Play a tone at x Hertz for y seconds: cpx.play_tone(x,y)

  2. Light up pixel number N with a light made up of Red, Green, and Blue elements:

    cpx.pixels[4] = (30,100,20)

    This will produce light on pixel number 4 with the following components:

    Red: 30, Green: 100, Blue: 20

Python preliminaries

  • Like MATLAB, Python is an interpreted language.
    • by contrast, C is a ‘compiled’ language
  • Unlike MATLAB, indentation matters!
NoteVariable Assignment

Assigning a value to a variable in Python occurs with =. Thus a = 5 takes the value 5 and assigns it to the variable named a.

Conditionals

A conditional is a statement that evaluates to True or False.

TipConditional operators — comparing two numbers
  • >: Greater than
  • <: Less than
  • ==: Equal to
  • !=: Not equal to
  • >=: Greater than or equal to
  • <=: Less than or equal to

Trying out conditional operators on CPX

Try the following code:

cpx.light > 100

Next, try this with a cell phone flashlight shining on the CPX.

Practice with conditionals

Try the following lines of code inside Thonny Code Editor in interactive mode.

>>> True == 'True'
>>> 3 ==3
>>> None == 0
>>> None == None
>>> 0.1 + 0.2 == 0.3
>>> 1 == 0.9999
>>> 1 == 0.999999
>>> (3 + 2j) > (2 + 1j)
>>> [] == []
>>> ‘’ == ‘’
>>> [] == ‘’
>>> [1,2] < [3,4,5]
>>> True == 1
>>> False == 0
>>> (3 > 2) != (5 < 4)
>>> 1.0 == 1

Variable Types: Boolean

  • A conditional statement of the form cpx.light > 100 checks whether the variable cpx.light is more than 100 and returns an answer: True or False

  • This is a special type of variable called a Boolean

TipBoolean variables

are either True or False. There are no maybe’s!

  • Some examples of Boolean variables:
    • True (without quotes)
    • False (without quotes)
    • (2 > 3) with or without parantheses
    • (cpx.light < 100)

Logical Operations on conditionals

Python has two main binary logical operators that operate on two Boolean variables on either side:

  1. x and y
  2. a or b

There is also a logical operator that operates on only one Boolean variable:

  1. not c

Try the following code line by line:

x = True

y = False

z = True

Then try using and, or and not on the three variables x, y and z and observe the results.

Truth Tables for binary logical operators

for the and operator

Variable A Variable B Result of A and B
True True True
True False False
False True False
False False False

for the or operator

Variable A Variable B Result of A or B
True True True
True False True
False True True
False False False

Common variable types in Python

Type Name Purpose
String str String of text
Integer int Whole numbers
Floating-point numbers float Decimals
Boolean bool Truth value
List list Ordered list of variables
Tuple tuple Un-alterable list
Range range A sequence of numbers

Check the type of a variable x using type(x)

NotePython features

Parantheses around a single variable don’t do anything.

Numbers in Python: float and int

  • In Python, it is important to distinguish between ints and floats.
  • In MATLAB, numbers default to being floating-point.
ImportantWhat’s a floating-point number?

We will get to this later in E21!

int is a whole number.
float is a decimal number.

>>> a = 5     # this is an int
>>> a = 5.0   # this is a float

>>> type(6)
<class 'int'>
>>> type(6.0)
<class 'float'>

The list and tuple types

Lists and tuples in Python are zero-indexed.
Lists can be modified.
Tuples, once set, cannot be changed.

>>> a = [1,3,5,7,10]
>>> type(a)
<class ‘list’>
>>> a[0]
1
>>> a[3]
7
>>> b = [4, 2.4, 2+4j]
>>> type(b[0])
<class ‘int’>
>>> type(b[1])
<class ‘float’>
>>> type(b[2])
<class ‘complex’>
>>> s = (3,4,5)
>>> s[1]
4

The range type

Refers to a range of whole numbers.
You can access different elements of a range the same way you would access elements of a list.

Your computer saves memory if you use range instead of list.

>>> a = [1,3,5,7,9,11,13]
>>> b = range(1,13,2)
>>> a[3]
7
>>> b[3]
7
>>> c = range(13)
>>> c[9]
9
>>> d = range(5,13)
>>> d[0]
5
>>> d[7]
12
Usage Code
From 0 to N-1 range(N)
From a to b-1 range(a,b)
From a to b in steps of c but excluding b range(a,b,c)

While Loops

  • A while loop makes code run for ever… while the loop’s condition is True
  • In words: “While condition is true, keep doing thing
# This program makes the Circuit Playground Express 
# play middle A (440 Hz) and then a higher note (880 Hz)
# until you touch pin A4. Then, it plays a low note once.
while not cpx.touch_A4:
    cpx.play_tone(440,1)
    cpx.play_tone(880,0.5)
cpx.play_tone(220,1)
  • Some things to notice about while loops:
    • What happens when the condition is not true?
    • Indentation and colon symbol
  1. Check if the condition is true. If yes:
  2. Execute cpx.play_tone(440,1)
  3. Execute px.play_tone(880,0.5)
  4. Check if the condition is (still) true:
    • If yes: return to step 2.
    • If no: exit the loop

You can indent as much as you want

But be consistent.


while True:
  cpx.play_tone(440,1)
  cpx.play_tone(880,0.5)

while True:
      cpx.play_tone(440,1)
      cpx.play_tone(880,0.5)

while True:
              cpx.play_tone(440,1)
              cpx.play_tone(880,0.5)

You can nest one while loop inside the other

Nested Loops

while not cpx.touch_A4:
  cpx.play_tone(440,1)
  while True:
    cpx.play_tone(220,1)

Two loops at the same indentation level are not nested:

while not cpx.touch_A4:
  cpx.play_tone(440,1)
while True:
    cpx.play_tone(220,1)