HW 1

ENGR 21, Fall 2026.

Published

September 10, 2026

Due Date Thu, Sep 10
Turn in link Gradescope
URL emadmasroor.github.io/E21-F26/Homework/HW1

What to turn in

For this assignment, turn in a single PDF file, even though you will be writing code for some parts. Make sure your code is legible, indented, and syntax-highlighted; a screenshot of code is usually a bad idea, but OK for this homework.

You may use the text editor in Thonny Code Editor and then go to File > Print. If you use this option to make a printed copy of code for submission, please increase the font size using the ‘scale’ option when printing.

1 Writing simple programs for the Circuit Playground Express

Using only the features of Python that have been discussed so far,

  1. Write a program for the Circuit Playground Express that plays a tone of 440 Hz for one-second intervals repeatedly until a cell phone light is shined on it, at which point the program should terminate.
  1. Write a program for the Circuit Playground Express that plays a tone of 440 Hz for one-second intervals repeatedly. When it is brought close to a warm object (warmer than room temperature), it stops playing this tone and instead plays a high-frequency tone of 880 Hz for a half second. After this, the program should terminate.

2 Truth Tables

We have seen that the truth table for the and and or operators was as given in lecture.

Note

It is possible to use logic to determine the truth tables for more complicated operators than and and or, but here you should simply check all possibilities using Python.

2.1 For a binary operator

Use this info to construct a truth table for the following binary operator.

(a and b) or a
Note

To do this, make a table with the following entries.

Variable a Variable b Result of (a and b) or a
True True
True False
False True
False False

2.2 For an operator with three inputs

Construct a truth table for the following operator.

(a or b) and c
Note

One row has been filled out for you.

a b c (a or b) and c
True True False False
NoteHow many rows do you need?

The rows of a truth table should cover every possible combination of True and False values. If binary operators required 4 rows, how many rows do you need for a logical operator with three inputs, such as (a or b) and c?

3 While loops

Determine the behavior of the Circuit Playground Express when the following code is run on it. Give your answer in words.

3.1 No inputs

  1. while True:
      cpx.play_tone(440,1)
    cpx.play_tone(880,0.5)
  2. while True:
      cpx.play_tone(880,0.5)
    cpx.play_tone(440,1)
  3. while True:
      cpx.play_tone(880,0.5)
      cpx.play_tone(440,1)
  4. while True:
      cpx.play_tone(440,1)
      cpx.play_tone(880,0.5)
  5. cpx.play_tone(440,1)
    while True:
      cpx.play_tone(880,0.5)
  6. cpx.play_tone(440,1)
    while False:
      cpx.play_tone(880,0.5)
  7. cpx.play_tone(440,1)
    while False:
      cpx.play_tone(880,0.5)
    cpx.play_tone(440,1)

3.2 With inputs

For the following questions, determine what the board will do under the different relevant possibilities. For example,

  • if a flashlight is shining on it vs. not
  • if the temperature sensor is above/below a threshold
  • if a certain pin is touched or not touched.
  1. while not (cpx.touch_A1 and cpx.touch_A5):
      cpx.play_tone(440,1)
    cpx.play_tone(880,0.5)
  2. while not (cpx.touch_A1 and cpx.temperature > 30):
      cpx.play_tone(440,1)
    cpx.play_tone(880,0.5)
  3. while not (cpx.touch_A1 or cpx.temperature > 30):
      cpx.play_tone(440,1)
    cpx.play_tone(880,0.5)
  4. cpx.play_tone(440,1)
    while cpx.light < 100:
      cpx.play_tone(880,0.5)
    cpx.play_tone(440,1)

4 Nested while loops

  1. Explain the difference between the following programs.

    while True:
        cpx.play_tone(880,0.5)
        while True:
            cpx.play_tone(440,1)
    
    while True:
        cpx.play_tone(880,0.5)
    while True:
        cpx.play_tone(440,1)
    
  2. For each of the following parts, assume that the Circuit Playground Express has been running the following code for a few seconds before you carry out your intervention. (i.e., a, b, c and d are not applied sequentially.)

    while not cpx.touch_A7:
     cpx.play_tone(880,0.5)
     while not cpx.touch_A5:
         cpx.play_tone(440,1)
  1. What happens if pin A5 is tapped ?
  2. What happens if pin A5 is touched for ten seconds ? Then, when you release it, what happens?
  3. What happens if pin A7 is touched while the speaker is beeping at 440 Hz ?
  4. What happens if both pin A5 and A7 are pressed for a few seconds?
  1. If the Circuit Playground Express has been running the following code for a few seconds, what happens if pin A5 is touched?

    while not cpx.touch_A7:
       cpx.play_tone(880,0.5)
       while not cpx.touch_A5:
           cpx.play_tone(440,1)
       while True:
           cpx.play_tone(220,1)