teaching machines

The Monodrone, Part I

June 26, 2018 by . Filed under electronics, music, public.

This post is part of a series of notes and exercises for a summer camp on making musical instruments with Arduino and Pure Data.

By the end of the day, we’ll have an instrument that behaves like a piano, but with many fewer keys. In fact, this first incarnation of that instrument will have just a single key. Let’s call it the monodrone. It sports a single push button that always plays the same note when it’s pressed.

We cover the hardware in this exercise, and the software in the next.

Hardware

Start by building this circuit:

See those four pins on the button? We can ignore the top pins; they are only being used to anchor the button in place. When the button is not pressed, no electrons flow from ground to pin 7. When it is pressed, electrons do flow.

Firmware

We want to program the Arduino to send a number across the serial port that describes the state of the button. Our setup function will open the serial port and allow pin 7 to be read from:

void setup() {
  Serial.begin(9600);
  pinMode(7, INPUT_PULLUP);
}

Note the _PULLUP in the pin mode. To prevent short circuits, we use something inside the Arduino called a pullup resistor. This mode, unlike the regular INPUT, activates an electrical component called a resistor, which keeps too many electrons from zooming through and starting a fire.

In loop, we want to continuously read the state of pin 7 and send it across the serial port:

void loop() {
  int button = digitalRead(7);
  Serial.println(button);
}

Upload this code to your Arduino. Open the serial monitor and examine the output.

Challenges

After you get your circuit assembled and programmed, answer the following questions on a piece of scratch paper.

  1. What output do you see when the button is pressed? When not pressed? How is the output here different from what we saw with the potentiometer?
  2. Add the line button = !button; right before printing to the serial port. Upload and test your circuit. What does the ! operator do?
  3. What’s another way to achieve the effect of ! here using just arithmetic?

Before leaving this exercise, do two things. First, ensure that your Arduino issues a 1 when the button is pressed, and a 0 when it’s not pressed. Second, to prepare for working with Pure Data, switch your Arduino to use binary output. Replace Serial.println with Serial.write and upload. Close the serial monitor.