# The Scream: Interactive Screaming Painting

## Overview

https://youtu.be/J3U4hcvJnmI

When Edvard Munch painted his most famous painting, _The Scream_, in 1893 he perfectly captured the existential angst of modern humanity. He did not, however, have access to a microcontroller board with built-in sound sensing connected to a servo motor and speaker, so he failed to make his masterpiece interactive.

Today, we’ll change all of that! By using the Crickit with&nbsp;Circuit Playground Express, and a bit of code in CircuitPython, we can make _The Scream_ scream back at you when you scream at it!

![](https://cdn-learn.adafruit.com/assets/assets/000/058/068/medium800/express_Edvard_Munch__1893__The_Scream__oil__tempera_and_pastel_on_cardboard__91_x_73_cm__National_Gallery_of_Norway.jpg?1532540904 By Edvard Munch - National Gallery of Norway, Public Domain, https://commons.wikimedia.org/w/index.php?curid=69541493)

## Parts & Materials
### Part: Adafruit CRICKIT
quantity: 1
for Circuit Playground Express
[Adafruit CRICKIT](https://www.adafruit.com/product/3093)

### Part: Circuit Playground Express
quantity: 1
Round, Awesome Microcontroller Board
[Circuit Playground Express](https://www.adafruit.com/product/3333)

### Part: Micro Servo
quantity: 1
180 degree rotation servo
[Micro Servo](https://www.adafruit.com/product/169)

### Part: Mini Metal Speaker w/ Wires
quantity: 1
8 ohm 0.5W
[Mini Metal Speaker w/ Wires](https://www.adafruit.com/product/1890)

### Part: 3 x AA Battery Holder
quantity: 1
with 2.1mm Plug
[3 x AA Battery Holder](https://www.adafruit.com/product/3842)

### Part: Alkaline AA batteries
quantity: 1
3 pack
[Alkaline AA batteries](https://www.adafruit.com/product/3521)

### Optional
### Part: 5V 2A switching power supply
quantity: 1
UL listed
[5V 2A switching power supply](https://www.adafruit.com/product/276)

In addition to the parts above, you’ll need:

- Two copies of the painting printed on a color printer
- Corrugated cardboard
- Cardstock
- Chipboard (thin cardboard)
- Frame and mat with cardboard backing
- Scrap wood for standoffs
- Glue stick for paper
- High temp hot melt glue and glue gun, or wood glue and clamps
- Frame hanging hooks and hardware
- Hobby knife
- Straight edge

# The Scream: Interactive Screaming Painting

## Build the Interactive Painting

![](https://cdn-learn.adafruit.com/assets/assets/000/058/168/medium800/express_screamPaint_0002_2k.jpg?1532827105)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/169/medium800/express_screamPaint_0003_2k.jpg?1532827116)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/183/medium800/express_screamPaint_0001-2_2k.jpg?1532827569)

Let's build the interactive painting! First, download the image below and print two copies of it on a color printer.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/185/medium800/express_The_Scream_bright.jpg?1532827740)

## Create the Moving Head
- Use a hobby knife or scissors to cut out the head from one of the prints
- To strengthen the head, we'll glue it to a piece of cardstock. Trace the head onto a piece of cardstock and cut it out
- Glue the head to the cardstock
- Use hot melt glue to attach a servo horn to the back of the head

![express_screamPaint_0039_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/177/medium640/express_screamPaint_0039_2k.jpg?1532827491)

![express_screamPaint_0040_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/178/medium640/express_screamPaint_0040_2k.jpg?1532827507)

![express_screamPaint_0041_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/179/medium640/express_screamPaint_0041_2k.jpg?1532827515)

![express_screamPaint_0042_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/180/medium640/express_screamPaint_0042_2k.jpg?1532827522)

![express_screamPaint_0044_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/181/medium640/express_screamPaint_0044_2k.jpg?1532827529)

![express_screamPaint_0046_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/182/medium640/express_screamPaint_0046_2k.jpg?1532827537)

![express_screamPaint_0050_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/187/medium640/express_screamPaint_0050_2k.jpg?1532827876)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/186/medium800/express_screamPaint_0051_2k.jpg?1532827863)

## Prepare the Background

The second print will be the background of our interactive painting. Let's prep it for use.

- Measure and mark the print's dimensions onto a piece of thin cardboard, a.k.a. chipboard, using a straight edge and pencil
- Cut out the cardboard with a hobby knife and straight edge or scissors
- Use a glue stick to adhere the backside of the print to the chipboard
- Smooth out the print to avoid any bubbles
- Center the print on the cardboard backing that came with the frame, then affix it with a strip of tape at the top (the mat will hide this)
- Center the head and servo horn over the background head, then press down a bit to make an impression -- you'll use this mark as a guide for cutting out a hole to allow the horn to fit. The servo will be on the other side of the cardboard
- Cut out the hole using a hobby knife or awl so that the collar of the servo horn fits in place. Make sure the hole goes all the way through so we can connect the servo on the other side

![express_screamPaint_0004-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/188/medium640/express_screamPaint_0004-2_2k.jpg?1532827969)

![express_screamPaint_0005-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/189/medium640/express_screamPaint_0005-2_2k.jpg?1532828019)

![express_screamPaint_0007-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/190/medium640/express_screamPaint_0007-2_2k.jpg?1532828063)

![express_screamPaint_0008-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/191/medium640/express_screamPaint_0008-2_2k.jpg?1532828164)

![express_screamPaint_0010-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/192/medium640/express_screamPaint_0010-2_2k.jpg?1532828183)

![express_screamPaint_0013-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/195/medium640/express_screamPaint_0013-2_2k.jpg?1532828539)

![express_screamPaint_0015-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/196/medium640/express_screamPaint_0015-2_2k.jpg?1532828569)

![express_screamPaint_0014-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/197/medium640/express_screamPaint_0014-2_2k.jpg?1532828608)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/199/medium800/express_screamPaint_0030_2k.jpg?1532829754)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/200/medium800/express_screamPaint_0028_2k.jpg?1532829787)

## Mount the Servo

Now, we'll attach the servo to the cardboard frame backing.

- Increase the size of the hole a bit so the servo's keyed circular portion fits. The flat housing will lie flush with the cardboard
- Put the servo in place
- Use hot glue to secure the servo

![express_screamPaint_0016-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/201/medium640/express_screamPaint_0016-2_2k.jpg?1532829919)

![express_screamPaint_0017-2_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/202/medium640/express_screamPaint_0017-2_2k.jpg?1532830042)

![express_screamPaint_0034_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/203/medium640/express_screamPaint_0034_2k.jpg?1532830073)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/205/medium800/express_screamPaint_0027_2k.jpg?1532830522)

## Mount and Frame Backing

Now that the servo is mounted to the frame backing, with the print on the front, we can place it all inside the frame. Lay the mat into the frame, and then place the cardboard.

You can then fold down the metal tabs and get ready to mount the rest of the hardware.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/204/medium800/express_screamPaint_0025_2k.jpg?1532830507)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/206/medium800/express_screamPaint_0023_2k.jpg?1532830608)

## Mount and Wire the Crickit
- Use four nylon M2.5 standoffs to mount the Crickit. You can attach four of them to the Crickit's mounting holes with four nylon M2.5 screws to get the position right, then glue them in place with hot glue
- You can leave the Crickit attached -- it is shown here detached so you can see the mounted standoffs

![express_screamPaint_0020_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/208/medium640/express_screamPaint_0020_2k.jpg?1532830729)

![express_screamPaint_0022_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/209/medium640/express_screamPaint_0022_2k.jpg?1532830761)

![express_screamPaint_0021_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/210/medium640/express_screamPaint_0021_2k.jpg?1532830770)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/106/medium800/express_screaming_painting_fritzing.jpg?1532618812)

For the remaining parts:

- Use hot glue to affix the battery pack in place next to the Crickit so it can plug into the power jack
- Connect the two speaker wires to the Crickit's speaker port and screw them in place, then tape the speaker to the cardboard as shown. This provides good resonance for the sound and it will be plenty loud!
- Plug the micro servo into the Crickit's Servo 1 port with the yellow signal wire nearest the outside edge of the board

![](https://cdn-learn.adafruit.com/assets/assets/000/058/211/medium800/express_screamPaint_0018_2k.jpg?1532831063)

The circuit is complete!

## Add Frame Standoffs

You'll notice that the parts on the back of our painting will prevent the frame from hanging normally. So, we'll attach some scrap wood to push the frame out away from the wall a bit and provide clearance.

![](https://cdn-learn.adafruit.com/assets/assets/000/058/212/medium800/express_screamPaint_0017_2k.jpg?1532831123)

## Wood Blocks

&nbsp;

I used a couple of piece of scrap 1/2" thick plywood roughly 1-1/2" x 6". The 1-1/2" height is enough to allow all of the parts to clear the wall when hung

&nbsp;

I glued them in place with high temperature hot melt glue, which works well -- an even stronger method would be to use wood glue and clamps.

&nbsp;

I also added a small block of wood for extra support at the top as I used the top piece for hanging.

![express_screamPaint_0016_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/213/medium640/express_screamPaint_0016_2k.jpg?1532831543)

![express_screamPaint_0011_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/214/medium640/express_screamPaint_0011_2k.jpg?1532831559)

![express_screamPaint_0010_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/058/215/medium640/express_screamPaint_0010_2k.jpg?1532831578)

![](https://cdn-learn.adafruit.com/assets/assets/000/058/216/medium800/express_screamPaint_0009_2k.jpg?1532831614)

While the glue cures, let's program the Circuit Playground Express using CircuitPython!

# The Scream: Interactive Screaming Painting

## Code with CircuitPython

The key to the painting's interactivity will be sound sensing. When you scream, the microphone built into the Circuit Playground Express will detect it and then instruct a wave file we load on the board to play over the amp and speaker, as well as run the servo back and forth.

## CircuitPython Setup

To get started, you'll want to set up the CPX for use with CircuitPython by [following this guide](https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart). When you're ready, and can upload code to the board return here.

To use the Crickit with the CPX, follow the [steps listed here](https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit/circuitpython-code) to install the special build of CircuitPython, as well as the latest library bundle.

Adafruit really likes using the Mu editor to edit the CircuitPython code. [See this guide on loading and using Mu](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor).

## Playing Audio

The Circuit Playground Express plays back .wav files. If you would like to prepare your own files, [follow the instructions in this guide](https://learn.adafruit.com/microcontroller-compatible-audio-file-conversion?view=all). Download the following files and then uncompress the .zip file. Copy the .wav file to your CPX, which shows up as **CPLAYBOOT** on your computer.

[scream_low.wav](https://cdn-learn.adafruit.com/assets/assets/000/058/232/original/scream_low.wav?1532834373)
## Code

You can copy the code here and then paste it into Mu. Save it to your Circuit Playground Express as **code.py**

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Crickits/Screaming_Painting/code.py

Here's how the code works. First, we'll import some libraries that add capabilities to our CircuitPython code.

Then, we'll create a variable` NUM_SAMPLES` to represent the number of samples we'll use when testing the microphone for loud sounds.

```python
import time
import math
import array
import audiobusio
import audioio
import audiocore
import board
from adafruit_crickit import crickit

# Number of samples to read at once.
NUM_SAMPLES = 160
```

## Helper Proceedures

Next, we define a procedure named `normalized_rms` that will be used for removing DC bias from our mic readings before computing the root mean square (a type of average).

```auto
def normalized_rms(values):
    minbuf = int(mean(values))
    samples_sum = sum(
        float(sample - minbuf) * (sample - minbuf)
        for sample in values
    )

    return math.sqrt(samples_sum / len(values))

```

The mean procedure is also defined -- this is used to calculate the mean of our sampled values.

```auto
def mean(values):
    return sum(values) / len(values)
```

## Microphone

Now we'll set up the microphone object to use the Circuit Playground Express's microphone at a sample rate of 16,000 and a bit depth of 16.

And, we'll immediately take a sample of the room's ambient sound to provide a baseline.

```auto
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
                       sample_rate=16000, bit_depth=16)

# Record an initial sample to calibrate. Assume it's quiet when we start.
samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))

```

## Servo Setup

Here we'll instantiate a servo object named `head_servo` on the Crickit's Servo 1 port with pulse width ranging from 500 to 2500 microseconds. We'll then set the servo to its center position of 90° (the servo rotational range is from 0° to 180°).

```auto
head_servo = crickit.servo_1
head_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
head_servo.angle = 90  # center the head.

```

## Audio Wave File Playback

We'll prepare for our audio wave file playback by setting the audio output on the speaker connected to the Crickit's sound port -- this is driven by the Circuit Playground Express board's A0 pad.

Then, we define a procedure named `play_file` that will have two key jobs -- it will play back wave files stored on the Circuit Playground Express and, while playing, it will turn the head servo back and forth 30° in either direction with a very, very short pause in between.

```python
# Set audio out on speaker.
a = audioio.AudioOut(board.A0)

# Start playing the file (in the background).
def play_file(wavfile):
    print("Playing scream!")
    with open(wavfile, "rb") as f:
        wav = audiocore.WaveFile(f)
        a.play(wav)
        while a.playing:
            head_servo.angle = 60
            time.sleep(.01)
            head_servo.angle = 120
time.sleep(.01)
```

## Main Loop

Now we are at the main loop of the program, the part that runs over and over and over again endlessly.

Here, the microphone is sampled, and the normalized root mean square is calculated and assigned to a variable named magnitude.

Then, the magnitude value is printed to the serial port (it is formatted funny with lots of parenthesis so that it is a tuple and can be graphed in the Mu plotter). This is useful to watch when you first are setting it up.

Try yelling at the painting and watch the values change. You want a number that is below the yelling magnitude that you can use in the next section.

```auto
while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
    print(((magnitude),))  # formatting is for the Mu plotter.

```

If the yelling volume caused a magnitude reading of 2000, say, and normal room noise is below 400, then 1000 is a good magnitude to use as your scream threshold.

This `if` statement checks to see if the current magnitude is below 1000. If it is, nothing happens. `Else`, if the reading is higher, the serial monitor will print "LOUD" and then the head will wiggle a couple of times and then play back the wave file!

After this is done, the head returns to resting position, and the system waits a couple of seconds before the main loop runs again -- this helps prevent any self screaming feedback loops, and people who think they can just get The Scream to scream constantly!

```auto
if magnitude < 1000:  # it's quiet, do nothing.
    pass
else:
    print("LOUD")
    head_servo.angle = 60
    time.sleep(.05)
    head_servo.angle = 120
    time.sleep(.05)
    head_servo.angle = 90
    time.sleep(.02)
    play_file("scream_low.wav")
    head_servo.angle = 90
    time.sleep(2)
```

# The Scream: Interactive Screaming Painting

## Scream at The Scream

![](https://cdn-learn.adafruit.com/assets/assets/000/058/222/medium800/express_wallScream.jpg?1532833955)

Now, you can hang you painting, turn on the Crickit, and invite your guests to give The Scream a good scream!

https://youtu.be/J3U4hcvJnmI


## Featured Products

### Adafruit CRICKIT for Circuit Playground Express

[Adafruit CRICKIT for Circuit Playground Express](https://www.adafruit.com/product/3093)
Sometimes we wonder if robotics engineers ever watch movies. If they did, they'd know that making robots into servants always ends up in a robot rebellion. Why even go down that path? Here at Adafruit, we believe in making robots our&nbsp; **friends!**

So if you find...

In Stock
[Buy Now](https://www.adafruit.com/product/3093)
[Related Guides to the Product](https://learn.adafruit.com/products/3093/guides)
### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### Micro servo

[Micro servo](https://www.adafruit.com/product/169)
Tiny little servo can rotate approximately 180 degrees (90 in each direction) and works just like the standard kinds you're used to but _smaller_. You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without...

Out of Stock
[Buy Now](https://www.adafruit.com/product/169)
[Related Guides to the Product](https://learn.adafruit.com/products/169/guides)
### Mini Metal Speaker w/ Wires - 8 ohm 0.5W

[Mini Metal Speaker w/ Wires - 8 ohm 0.5W](https://www.adafruit.com/product/1890)
Listen up! This tiny 1" diameter speaker cone is the perfect addition to any small audio project where you need an 8 Ω impedance and will be using 0.5W or less of power. We particularly like this cone as it's very simple and its metal body is extremely lightweight. <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/1890)
[Related Guides to the Product](https://learn.adafruit.com/products/1890/guides)
### 3 x AA Battery Holder with 2.1mm Plug

[3 x AA Battery Holder with 2.1mm Plug](https://www.adafruit.com/product/3842)
Here's another addition to our growing family of&nbsp;[AA battery holders](https://www.adafruit.com/category/563).&nbsp;A&nbsp;holder for three (3) AA batteries! It's got&nbsp;an 8" long power cable with a 2.1mm DC jack at the end, and, oh yes, it's in classy...

In Stock
[Buy Now](https://www.adafruit.com/product/3842)
[Related Guides to the Product](https://learn.adafruit.com/products/3842/guides)
### Alkaline AA batteries (LR6) - 3 pack

[Alkaline AA batteries (LR6) - 3 pack](https://www.adafruit.com/product/3521)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAs. This is a pack of **3 AA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a voltage...

In Stock
[Buy Now](https://www.adafruit.com/product/3521)
[Related Guides to the Product](https://learn.adafruit.com/products/3521/guides)
### White Nylon Machine Screw and Stand-off Set – M2.5 Thread

[White Nylon Machine Screw and Stand-off Set – M2.5 Thread](https://www.adafruit.com/product/3658)
Totaling 420 pieces, this **White Nylon&nbsp;M2.5 Screw Set** &nbsp;is a must-have smörgåsbord for your workstation.&nbsp;You'll have more than enough screws, nuts, and hex standoffs to construct&nbsp;your maker projects&nbsp;for days on end! M2.5 screws fit nearly...

In Stock
[Buy Now](https://www.adafruit.com/product/3658)
[Related Guides to the Product](https://learn.adafruit.com/products/3658/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [TFT Gizmo Animated Eye](https://learn.adafruit.com/tft-gizmo-animated-eye.md)
- [Porting an Arduino library to CircuitPython: VL6180X Distance Sensor](https://learn.adafruit.com/porting-an-arduino-library-to-circuitpython-vl6180x-distance-sensor.md)
- [Paper-Craft Crystal Gem Lantern](https://learn.adafruit.com/paper-craft-crystal-gem-lantern.md)
- [Stroboscopic Zoetrope](https://learn.adafruit.com/strobe-zoetrope.md)
- [Drama Piñata](https://learn.adafruit.com/customizable-reusable-pinata.md)
- [Adabot Operation Game](https://learn.adafruit.com/adabot-operation-game.md)
- [Using the Android CircuitPython Editor](https://learn.adafruit.com/using-the-android-circuitpython-editor.md)
- [Halloween Sentry-Bot with CRICKIT for CPX](https://learn.adafruit.com/halloween-sentry-bot.md)
- [Paper Airplane Launcher](https://learn.adafruit.com/paper-airplane-launcher-with-crickit.md)
- [Hacking Ikea Lamps with Circuit Playground Express](https://learn.adafruit.com/hacking-ikea-lamps-with-circuit-playground-express.md)
- [Robotic Creatures ](https://learn.adafruit.com/robotic-creatures.md)
- [Bunny Ears with MakeCode](https://learn.adafruit.com/bunny-ears-with-makecode.md)
- [Stand-alone programming AVRs using CircuitPython](https://learn.adafruit.com/stand-alone-programming-avrs-using-circuitpython.md)
- [Archimedes' Boat](https://learn.adafruit.com/archimedes-boat.md)
- [Sensors in MakeCode](https://learn.adafruit.com/sensors-in-makecode.md)
