Skip to main content
shopping_basket Basket 0
Log in

Playing GarageBand with Arduino

GarageBand and Arduino Logo's

Hi guys, I built an 88-key piano using a Raspberry Pi Pico in a previous series. However, even though I added filters and amps, the sound was still too Midi. Therefore, I came up with an idea why I can’t use some extended software like GarageBand to output the sound and make it better. Then, let’s try it out!

If you are a music or Apple lover, I believe that you must hear GarageBand before. If this is the first time you hear this name, it doesn't matter. According to the Apple webpage, the claim that GarageBand is a fully equipped music creation studio right inside your Mac. Being a music amateur, I also agree that GarageBand is an excellent music software, and the most significant advantage is that it is free software. Hence, in this share, I will take it as an example to share how to play GarageBand with Arduino.

If you tried to make a keyboard using Arduino, you might face that the Arduino Uno can't use the Keyboard.h library. For that library, it only allows the 32u4 and SAMD-based boards (Leonardo, Esplora, Zero, Due, and MKR Family) to appear as a native Keyboard to a connected computer. Hence, this time, I used Arduino Nano 33 IOT to do this project, which used the SAMD21 Cortex®-M0+ 32bit low power ARM MCU.

GarageBand

Before we move on to the program parts, let's discuss how to play the GarageBand with Arduino. In GarageBand, there has a function call “Musical Typing”, you can play your selected software instrument using musical typing or the onscreen keyboard. In GarageBand, choose Window > Show Musical Typing. Then you can use this musical typing function.

Below, you can see an 18-key shown on the onscreen keyboard, and here it shows the layout of keys used for playing notes. For example, the note "C" is related to the character in the "A" keyboard.

Onscreen keyboard

Hence, we can try to make a keyboard that matches the appropriate characters through the Arduino Nano 33 IOT.

Keyboard.h

First, let’s talk about the Keyboard.h library. There are 8 functions included in the library. And in his project, I’ll mainly use 4 of them.

Keyboard.h functions

Keyboard.write() is very similar to Keyboard.press() and Keyboard.release() . But Keyboard.write() just keeps writing characters as PWM, if you press and hold the button, it will receive keep pressing the button instead of holding the button from GarageBand.

Program

This is the pin map of the Arduino Nano 33 IOT. Also, as you can see, there are characters "x" and "z" for adjusting the octave in GarageBand.

#include "Keyboard.h"

const int c = 2;
const int cs = 9;
const int d = 3;
const int ds = 10;
const int e = 4;
const int f =5;
const int fs = 11;
const int g = 6;
const int gs = 12;
const int a = 7;
const int as = A0;
const int b = 8;
const int x = A1;
const int z = A2;

This is the setup of the MCU.

void setup() {
  Serial.begin(9600);
  Keyboard.begin();
  
  pinMode(c, INPUT);
  digitalWrite(c,HIGH);

  pinMode(cs, INPUT);
  digitalWrite(cs,HIGH);
  
  pinMode(d, INPUT);
  digitalWrite(d,HIGH);

  pinMode(ds, INPUT);
  digitalWrite(ds,HIGH);

  pinMode(e, INPUT);
  digitalWrite(e,HIGH);
  
  pinMode(f, INPUT);
  digitalWrite(f,HIGH);

  pinMode(fs, INPUT);
  digitalWrite(fs,HIGH);
  
  pinMode(g, INPUT);
  digitalWrite(g,HIGH);

  pinMode(gs, INPUT);
  digitalWrite(gs,HIGH);
  
  pinMode(a, INPUT);
  digitalWrite(a,HIGH);

  pinMode(as, INPUT);
  digitalWrite(as,HIGH);
  
  pinMode(b, INPUT);
  digitalWrite(b,HIGH);

  pinMode(z, INPUT);
  digitalWrite(z, HIGH);

  pinMode(x, INPUT);
  digitalWrite(x,HIGH);

  pinMode(TAB, INPUT);
  digitalWrite(TAB,HIGH);
  Keyboard.end();
  }

Then, in the main loop, which is very easy to understand. When the button is pressed, the keyboard is pressed and prints the characters.

void loop() {
  if (digitalRead(c) == HIGH) {
    Keyboard.press('a');
  }else{
    Keyboard.release('a');
  }
  if (digitalRead(cs) == HIGH) {
    Keyboard.press('w');
  }else{
    Keyboard.release('w');
  }
  if (digitalRead(d) == HIGH) {
    Keyboard.press('s');
  }else{
    Keyboard.release('s');
  }
  if (digitalRead(ds) == HIGH) {
    Keyboard.press('e');
  }else{
    Keyboard.release('e');
  }
  if (digitalRead(e) == HIGH) {
    Keyboard.press('d');
  }else{
    Keyboard.release('d');
  }
  if (digitalRead(f) == HIGH) {
    Keyboard.press('f');
  }else{
    Keyboard.release('f');
  }
  if (digitalRead(fs) == HIGH) {
    Keyboard.press('t');
  }else{
    Keyboard.release('t');
  }
  if (digitalRead(g) == HIGH) {
    Keyboard.press('g');
  }else{
    Keyboard.release('g');
  }
  if (digitalRead(gs) == HIGH) {
    Keyboard.press('y');
  }else{
    Keyboard.release('y');
  }
  if (digitalRead(a) == HIGH) {
    Keyboard.press('h');
  }else{
    Keyboard.release('h');
  }
  if (digitalRead(as) == HIGH) {
    Keyboard.press('u');
  }else{
    Keyboard.release('u');
  }
  if (digitalRead(b) == HIGH) {
    Keyboard.press('j');
  }else{
    Keyboard.release('j');
  }
  if (digitalRead(x) == HIGH) {
    Keyboard.press('x');
  }else{
    Keyboard.release('x');
  }
  if (digitalRead(z) == HIGH) {
    Keyboard.press('z');
  }else{
    Keyboard.release('z');
  }
  if (digitalRead(TAB) == HIGH) {
    Keyboard.press(KEY_TAB);
  }else{
    Keyboard.release(KEY_TAB);
  }
}

Before you download the program into the MCU, in “Device Manager” you can only see the “Arduino NANO 33 IOT” in the Ports.

Device Manager showing “Arduino NANO 33 IOT” in the ports

After you download the program into the Arduino Nano 33 IOT, you will find that you still can’t use the Arduino to type anything. Then you can open the “Device manager” again, and you will see that there has an alert next to the Arduino Nano 33 IOT.

Device Manager now with Alert

Then you can right-click the “Arduino Nano 33 IOT”, and click “Update driver”.

Click and update driver for “Arduino NANO 33 IOT”

Choose "Browse my computer for driver software", and click Next.

Browse my computer for driver software

Click “Let me pick from a list of available drivers on my computer”, then click “next”.

Let me pick from a list of available drivers

Then, select “USB Input Device”, and click “next”.
select “USB Input Device”

Finally, click “close”. And you can open “Notepad” or anything else to test whether your MCU types the characters correctly.

open “Notepad” to test your MCU types the characters correctly

Here is the demonstration of the project.

Finally, I hope you enjoy this project. Last but not least, in this sharing, I take GarageBand as an example. If you use any other music software or composition software, you can use your PC's keyboard to first check the corresponding button for each note and change the pin mapping to the MCU.

JulianWong has not written a bio yet…
DesignSpark Electrical Logolinkedin