Está en la página 1de 13
1ar2020 Lab: MIDI Output using an Arduino —ITP Physical Computing ITP Physical Computing Lab: MIDI Output using an Arduino Introduction This lab covers only the details of MIDI communication on the Arduino module. For a more general introduction to MIDI on a microprocessor, see the the MID| notes. MI sequencers, and other musical devices. MIDI devices are generally grouped in to two broad classes: controllers (i.e. devices that generate MIDI signals based on human actions) and synthesizers (including samplers, sequencers, and so forth). The latter take MIDI data in and make 1, the Musical Instrument Digital Interface, is a useful protocol for controlling synthesizers, sound, light, or some other effect. What You'll Need to Know To get the most out of this lab, you should be familiar with the following concepts. You can check how to do so in the links below: + How to Solder a connector (https://itp.nyu.edu/physicalcomputing/labs/labs- electronies/soldering/) * Digital Input with Arduino (https://itp.nyu.edu/physicaleomputing/labs/labs-arduino-dig and-analog/digital-input-and-output-with-an-arduino/) * Analog Input with Arduino (https://itp.nyu.edu/physicalcomputing/labs/analog-in-with-an- arduino/) + What is an Arduino Library (http://arduino.cc/en/Reference/Libraries) + How to communicate serially with the Arduino (https://itp.nyu.edu/physicaleomputing/labs/serial-output-from-an-arduino/) and how to send values serially in different formats (https://itp.nyu.edu/physicalcomputing/labs/serial-output-from-an-arduino/two-way- duplex-serial-communication-using-an-arduino/), Things You'll Need A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal wa 14/2020 Lab: MIDI Output using an Arduino —ITP Physical Computing (https://itp.nyu.edu/p! /ehyscomp, p/wp- content/uploads/peomp- kit-f2019- breadboard jpg) Figure 1. A solderless breadboard, (https://itp.nyu.edu/physcomp/wp- content/uploads/pcomp- kit-f2019-jumper- wires,jpa) Figure 2. 22 AWG hookup wire (https://itp.nyu.edu/physcomp/wp- content/uploads/arduino1- 21408455435943 jpg) Figure 3. An Arduino Uno or. A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal 23 1ar2020 Lab: MIDI Output using an Arduino —ITP Physical Computing (https://itp.nyu.edu/physcomp/wp- content/uploads/pcomp- kit-f2019-arduino- nano-33-iot jpg) Figure 4. Arduino Nano 33 loT ~ (https://itp.nyu.edu/physcomp/wp- content/uploads/midi jack.png), Figure 5. A 5-pin MIDI socket (for the Uno version only) (https://itp.nyu.edu/physcomp/wp- content/uploads/pcomp- Figure 6. 220-ohm resistors (for the Uno version only) A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal ana 1ar2020 Lab: MIDI Output using an Arduino —ITP Physical Computing oO (https://itp.nyu.edu/physcomp/wp- content/uploads/fsr.jp9) Figure 7. Force-sensing resistor (https://itp.nyuedu/physcomp/wp- content/uploads/pcomp- kit-f2019-10k- resistor.jpg) Figure 8. 10-kilohm resistors, eo ev (https://itp.nyu.edu/physcomp/wp- content/uploads/pcomp- kit-f2019- pushbuttons.jpg) Figure 9. Pushbuttons Serial SoftwareSerial or MIDILISR A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal ans sus2020 Lab: Mo! Output using an Arouno =ITP Physical Computing There are three approaches you can take to MIDI output, depending on the board you're using and the application you have in mind, If you're communicating with a MIDI sound module like a synthesizer or sample, you'll need to use either Serial or SoftwareSerial output. On the Uno, SoftwareSerial is best. On most other Arduino models, there is a second hardware serial port, Serial1, that you can use for MIDI output. If you're communicating with a MIDI program like Ableton, GarageBand, or a soundFont synth like Sforzando, either on a laptop or mobile device, then MIDIUSB is the way to go. The Uno can't communicate using MIDIUSB, but the Nano 33 IoT, the MKR series, the Leonardo, Micro, or Due can SoftwareSerial Approach If you're using an Uno or any board with only one serial port, the SoftwareSerial library is your best bet. This section describes how to wire and program your board for SoftwareSerial. Prepare the breadboard Connect power and ground on the breadboard (https://vimeo.com/86534049#t=1m41s) to power and ground from the microcontroller. On the Arduino module, use the SV and any of the ground connections: (httos://itp.nyu.edu/physcomp/wi content/uploads/LabTemplate_b! Figure 10. An Arduino Uno on the left connected to a solderless breadboard, right. Made with Fritzing (http://fritzing.org/home/) For the Nano 33 loT version or any of the MIDIUSB versions (see below), you won't need a MIDI jack. A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal 513 182020 Lab: MIOI Output using an Arduino —ITP Physical Computing Connect the sensors Connect an analog sensor to analog pins 0 like you did in the analog lab (https:/itp.nyu.edu/physicalcomputing/labs/analog-in-with-an-arduino/). Connect a switch to digital pin 10 like you did in the digital lab (https://itp.nyu.edu/physicalcomputing/labs/labs- arduino-digital-and-analog/digital-input-and-output-with-an-arduino/) (https://itp.nyu.edu/physcomp/wp- content/uploads/LabMidiOut_sensors_schem.png) Figure 11. Schematic view of an Arduino connected to a voltage divider and a switch. (https://itp.nyu.edu/physcomp/wp- content/uploads/LabMidiOut_sensors_bb- €1409511982839.png) Figure 12, Breadboard view of an Arduino connected to a voltage divider and a switch. Build the MIDI Circuit Add the MIDI out jack and a 220-ohm resistor to digital pin 3: A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal 63 1ar2020 Lab: MIDI Output using an Arduino —ITP Physical Computing (bttps:/itp.nyu.edu/physcomp/wp- content/uploads/LabMidiOut_schem.png) Figure 13. Schematic view of an Arduino. connected to a voltage divider and a switch, with a MIDI connector as well (https://itp.nyu.edu/physcomp/wp- content/uploads/LabMidiOut_bb_components.png) Figure 14. Breadboard view of an Arduino connected to a voltage divider, a switch, and a MIDI connector, This circuit doesn’t actually match the MID! specification, but it works with all the MIDI devices we've tried it with. This circuit includes an analog and a digital sensor to allow for physical interactivity, but those aren't necessary to send MIDI data. Play Notes Once you're connected, sending MIDI is just a matter of sending the appropriate bytes. In the code below, you'll use the SoftwareSerial library to send data on digital pin 3, so that you can keep the hardware serial available for debugging purposes. The bytes have to be sent as binary values, but you can format them in your code as decimal or hexadecimal values. The example below uses hexadecimal format for any fixed values, and a variahle far ehanaina valiies All valuias ara sent ceriallv as raw hinarv valuias usina the RVTE A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal m3 sus2020 Lab: Mo! Output using an Arouno =ITP Physical Computing modifier to .print() (Many MIDI tables give the command values in hex, so this was done in hex for the sake of convenience): #include // Variables: byte note // The MIDI note value to be played //software serial SoftwareSerial midiSerial(2, 3); // digital pins that we'll use for soft serial void setup() { // Set MIDI baud rate: Serial. begin(9600) ; midiSerial .begin(31250) ; ? void loop() { // play notes from F#-@ (3@) to F#-5 (98): for (note = 30; note < 90; note ++) { //Note on channel 1 (@x9@), some note value (note), middle velocity (0x45) noteOn(@x98, note, x45); delay(1@8) ; //Note on’ channel 1 (@x9@), some note value (note), silent velocity (@xea) noteOn(@x98, note, @xd@); delay(102) ; } ? // plays a MIDI note. Doesn't check to see that //_ cmd is greater than 127, or that data values are less than 127: void noteOn(byte cmd, byte datal, byte data2) { midiSerial .write(cmd); midiSerial .write(datai) ; midiSerial .write(data2) ; //prints the values in the serial monitor so we can see what note we're ple Serial.print("cmd: "); Serial.print(cmd) ; Serial.print(", datal: "); Serial.print(data1) ; Serial.print(", data2: "); Serial.printIn(data2); Alternatives to SoftwareSerial There are two alternatives to SoftwareSerial, but they are only available on boards other than the Uno. You could use the second hardware serial port if you have one. Or, if you're using one of the MR series boards or a Nano 33 loT or an M0-based derivative board, you can use MIDIUSB. A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal ans 182020 Lab: MIOI Output using an Arduino —ITP Physical Computing Second Hardware Serial Port If you're using any board that has two hardware serial ports, you don't have to use SoftwareSerial. That includes almost any of the official Arduino boards except the Uno: the Nano 33 IoT, the Due, the Mega, the MKR series, the Leonardo, the Micro and the 101 all have two hardware serial ports. You could also use any MO- or 32U4-based derivative board like Adafruit's Feather and Trinket boards. If you are using one of those boards, copy the circuits above but move the MIDI connector’s pin 4 to the TX1 pin of your board. Then change your code as follows. First, remove the first line that includes the SoftwareSerial library. Then remove the line that initializes SoftwareSerial. Then change midiserial.begin() to Serial1.begin() . Then change all midiserial calls to seriala calls. Here's the changed code: // Variables byte note = @; // The MIDI note value to be played void setup() { 7/ Set MIDI baud rate: Serial.begin(9600) ; Serial. begin(31250) ; } void loop() { // play notes from F#-0 (3@) to F#-5 (98): for (note = 30; note < 99; note ++) { //Note on channel 1 (8x30), some note value (note), middle velocity (@x45): noteOn(@x98, note, @x45); delay(102); 7/Note on channel 1 (8x98), some note value (note), silent velocity (@xe@): noteOn(@x98, note, @x@e); delay(100); } + // plays a MIDI note. Doesn't check to see that //_ cmd is greater than 127, or that data values are less than 127: void noteOn(byte cmd, byte datal, byte data2) { Serial1.write(cmd) ; Serial1.write(datal) ; Seriall.write(data2); //prints the values in the serial monitor so we can see what note we're play Serial.print("cmd: "); Serial.print (cmd) ; Serial.print(", datal: "); Serial.print(data1) ; Serial.print(", data2: "); Serial.print1n(data2); Using MIDIUSB A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal ona s1a2020 Lab: MIO! Output using an Arai —ITP Physial Computing If you're using an ARM board like the MKR series, the Nano 33 IoT, the Due, or any of the MO-based derivatives, you can also use MIDIUSB. When you do this, your microcontroller shows up to your computer like a USB MIDI device. This is handy for when you're connecting to a laptop. It's less handy for connecting to dedicated synthesizers or samplers. To do this, dispose of the MIDI socket. You'll be connecting through your USB connector, and yes, the serial connection to the Serial Monitor will still work as well. Change your code as follows. First include the MIDIUSB (https://www.arduino.cc/en/Reference/MIDIUSB) library (you might need to install it using the Library Manager) at the top of your code. Then remove ‘the serial1.begin() line in the setup. Then replace the Serial1.write() lines in the noteOn function with the MIDI code shown below: #include ; // Variables: byte note // The MIDI note value to be played void setup() { Serial. begin(960@) ; } void loop() { // play notes from F#-@ (30) to F#-5 (90): for (note = 30; note < 98; note ++) { //Note on channel 1 (0x38), some note value (note), middle velocity (@x45): noteOn(@x98, note, 6x45); delay(1@@); //Note on channel 1 (@x3@), some note value (note), silent velocity (@xe0): noteOn(@x98, note, @xe@); delay(1@@); } > // plays a MIDI note. Doesn't check to see that 7/ cmd is greater than 127, or that data values are less than 127: void noteOn(byte cmd, byte datal, byte data2) { /* First parameter is the event type (top 4 bits of the command byte). Second parameter is command byte combined with the channel. Third parameter is the first data byte Fourth parameter second data byte, if there is one: */ midi€ventPacket_t midiMsg = {cmd >> 4, cmd, datal, data2}; MidiUSB. sendMIDI (midimsg) ; //prints the values in the serial monitor so we can see what note we're playir Serial.print("cmd: "); Serial.print (cmd); Serial.print(", datal: "); Serial.print(datat) ; Serial.print(", data2: Serial. printIn(data2) ; } A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal 1013 14/2020 Lab: MIDI Output using an Arduino —ITP Physical Computing Allow a Person to Play Notes The previous example will just play notes, no interactivity. The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note: #include const int switchPin = 10; // The switch is on Arduino pin 1@ const int LEDpin = 13; // Indicator LED // Variables: byte note = @; // The MIDI note value to be played int Analogvalue = 0; // value from the analog input int lastNotePlayed = 0; 7/ note turned on when you press the switch int lastSwitchState = @; // state of the switch during previous time througt int currentSwitchState //software serial SoftwareSerial midiSerial(2, 3); // digital pins that we'll use for soft serial void setup() { // set the states of the I/O pins: pinMode(switchPin, INPUT) ; pinMode(LEDpin, OUTPUT); // Set MIDI baud rate: Serial. begin(960@) ; midiSerial .begin(3125@) ; + void loop() { // My potentiometer gave a range from @ to 1023: ‘AnalogValue = analogRead(@) ; // convert to a range from @ to 127: note = Analogvalue/8; currentSwitchState = digitalRead(switchPin); // Check to see that the switch is pressed: if (currentswitchState == 1) { // check to see that the switch wasn't pressed last time 7/ through the main loop: if (lastSwitchState == @) { // set the note value based on the analog value, plus a couple octaves: // note = note + 60; // start a note playing: noteOn(@x9@, note, @x40); // save the note we played, so we can turn it off: lastNotePlayed = note; digitalWrite(LEDpin, HIGH); + else { // if the switch is not pressed: 7/ but the switch was pressed last time through the main loop: if (lastSwitchState == 1) { // stop the last note played: noteOn(@x98, lastNotePlayed, @x@0); digitalWrite(LEDpin, LOW); A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal nis 1ar2020 Lab: MIDI Output using an Arduino —ITP Physical Computing } // save the state of the switch for next time 7/ through the main loop: lastSwitchState = currentSwitchState; + // plays a MIDI note. Doesn't check to see that //_ cmd is greater than 127, or that data values are void noteOn(byte cmd, byte datal, byte data2) { midiSerial .write(cmd) ; midiSerial.write(data1) ; midiSerial.write(data2) ; less than 127: //prints the values in the serial monitor so we can see what note we're playir Serial.print("cmd: "); Serial. print (cmd) ; Serial.print(", datal: "); Serial. print (data1) ; Serial.print(", data2: ")5 Serial. print1n(data2); Make an Instrument This is a suggestion. You can do any project you wish as long as it demonstrates your mastery of the lab exercises and good physical interaction. This is just one suggestion. Now that you've got the basics, make a musical instrument. Consider a few things in designing your instrument: * Do you want to play discrete notes (like a piano), or sliding pitches (like a theremin)? How do you program to achieve these effects? + Do you want to control the tempo and duration of a note? * Do you want the same physical action to set both the pitch and the velocity (volume) of a note? * Do you want to be able to play more than one note at a time (e.g. chords)? All of these questions, and many more, will affect what sensors you use, how you read them, and how you design both the physical interface and the software. Originally written on August 23, 2014 by Benedetta Piantella Simeonidis Last modified on August 15, 2019 by David Rios A hitpsitp.nyu.edulphyscomplabelabe-seral-communcationlab-mid-ouiputusing-an-ardunal ra ar2020 Lab: MIDI Output using an Arduino —ITP Physical Computing Copyright © 2020 ITP Physical Computing. Powered by WordPress and Themelia. hitpsaitp.nyu.edulphyscomplabsiabs-soral- communicator smid-outputusing-an-aréunal san