1. Keyboard Instrumental

Introduction

This project involves creating a simple musical keyboard using Arduino, pushbuttons, and a resistor ladder network. The resistor ladder network allows you to connect multiple pushbuttons to a single analog input pin on the Arduino. When a pushbutton is pressed, it completes a circuit and sends a different voltage level to the Arduino, which can then be interpreted to identify the pressed button and generate the corresponding musical note.

Necessary Components

  1. Arduino Uno: The microcontroller board that will process the sensor data and generate the sounds.
  2. Pushbuttons (4): These will be used as the keys of the musical keyboard.
  3. Resistors (4): Different resistors will be used to create the resistor ladder network.
  4. Piezo buzzer: This will be used to generate the musical tones.
  5. Jumper wires: These will be used to connect the components on the breadboard.
  6. Breadboard: This will provide a platform for prototyping the circuit.

Construction

  1. Connect the Breadboard to Power and Ground:

    1. Insert the red power rail (usually labeled "+") from the Arduino power supply into the top red rail of the breadboard.
    2. Insert the black ground rail (usually labeled "-") from the Arduino power supply into the bottom blue rail of the breadboard.
  2. Connect the Piezo Buzzer:

    1. Insert one leg of the piezo buzzer into the ground rail (blue) on the breadboard.
    2. Insert the other leg of the piezo buzzer into analog pin 8 on the Arduino.
  3. Connect the Pushbuttons and Resistor Ladder Network:

    1. Place the four pushbuttons on the breadboard in a row, representing the keys of the keyboard.
    2. Connect one end of each pushbutton to a common ground point on the breadboard (usually the bottom blue rail).
    3. Connect the other end of each pushbutton to a different resistor. Use resistors with different values, such as 100 ohms, 220 ohms, 470 ohms, and 1k ohm.
    4. Connect the remaining ends of the resistors to analog pin A0 on the Arduino. This creates the resistor ladder network.

Explanation of the Resistor Ladder Network

The resistor ladder network is a circuit that allows you to connect multiple sensors or inputs to a single analog input pin on the Arduino. Each resistor in the ladder network creates a different voltage divider, resulting in different voltage levels at the analog pin when different buttons are pressed. The Arduino can then read these voltage levels and determine which button was pressed.

IMG_4715 1.jpg

Code explanation

The provided code is written in C++ for an Arduino project. It utilizes analog input to control the generation of different musical tones.

int notes[] = {262, 294, 330, 349};

void setup() {
  Serial.begin(9600);
}

void loop() {
  int keyVal = analogRead(A0);

  if(keyVal == 1023){
    tone(8, notes[0]);
  }else if(keyVal >= 990 && keyVal <= 1010){
        tone(8, notes[1]);
  }else if(keyVal >= 500 && keyVal <= 990){
        tone(8, notes[2]);
  }else if(keyVal >= 5 && keyVal <= 10){
        tone(8, notes[3]);
  }else{
    noTone(8);
  }
}

Key Components:

  1. Variables:
    • notes array stores frequency values (Hz) for different tones.
    • keyVal holds the analog input value from pin A0.
  2. Setup Function:
    • Serial.begin(9600) initializes serial communication for debugging.
  3. Loop Function:
    • Reads the analog value from pin A0 (keyVal).
    • Uses if statements to check keyVal ranges and generate corresponding tones:
      • If keyVal == 1023, plays the first note (notes[0]).
      • If keyVal is within specific ranges, plays corresponding notes from the notes array.
      • Otherwise, stops any ongoing sound (noTone(8)).

Overall, the code demonstrates how to map analog sensor input to control sound generation using Arduino.