Assignment 10: Musical intstrument


For this assignment, we decided to remake a piano, but add some adjustments to its stage presence. We all know that there are a lot of stage lights mechanisms when a pianist performs, but what happens when there is no light or lights are directed at people? Our piano changes its tunes and how it sounds. The main technology behind this piano is the usage of the photoresistor which sends different sensor values and with the changing frequency of the sensor, the note fluctuates.

Materials used


3 Switches (digital)

Photoresistor (analog)


Resistors – 10 K ohm and 330 ohm



Code starts off with the assignment of a variable to each component of the circuit 

The tone() function is used to generate a square wave of a certain frequency on three pins that are attached to their respective switches.  

int piezoPin = 8;
int LED = 13;
int button1 = 5; //Set up the input pins connected to the buttons.
int button2 = 4;
int button3 = 3;
int frequency = 0;
int sensor = 0;
int value = 0;
const int delayTime = 500; 
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(LED, OUTPUT);

void loop() {
 value = analogRead(sensor);
 //Map the different values of the photoresist to a set of frequencies for each of the three buttons.
 if (digitalRead(button1) == LOW) {
 frequency = map(value, 0, 1023, 400, 499);
 else if (digitalRead(button2) == LOW) {
   frequency = map(value, 0, 1023, 300, 599);
 else if (digitalRead(button3) == LOW) {
   frequency = map(value, 0, 1023, 500, 599);
 else {
   frequency = 0;
 tone(piezoPin, frequency, delayTime); //plays a tone() functions mapped frequencies.


An LED also lights up when a note is played

The photoresistor also causes the fluctuation in the tone i.e. the frequency when the sensor value is mapped.


Happy with the playfulness of the output but would like to experiment with other notes. We had some difficulties with creating more hearable differences when the light setting changed. Perhaps, it was due to the little difference photoresistor could detect, or we could play around changing the value, but it didn’t really work out as it was making the noise bad. Moreover, the LED just switches on and off when the switch is pressed perhaps its brightness could also be changed when a new note is played.

Leave a Reply