From Light to Sound


type: individual
date: Oct 1, 2019



1. Testing Phototransistor as a Analog Input








This circuit is to use phototransistor as a analog input, and directly use it to change frequency of a sound output.

As written in previous post, I had a trouble making phototransistor work.
I worked it out finally, which was to put the pin on the flipside. Unlike LED pins, the longer side should be connected to ground and the shorter side to power. 

Also, eventhough phototransistor itself has high resistance, I had to put resistor before connecting to ground to have a clear output value.

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

void loop() {
int ptState;
ptState = analogRead(A1);
int pitch = map (ptState,0,1023,200,1800);
Serial.println(pitch);
tone(8,pitch,10);
delay(1000);
}





2. Phototransistor as a Keyboard









After testing phototransistor, I wanted to develop it into a multi-sensor input, working like a “keyboard”. 


How to:

a. The size of resistant is direct proportion to the maximum of the value range. In this circuit I used 30k ohm resistor for each phototransistor. 

b. With 30k ohm resistor, I was able to get a range from 60(no light) to 1000 (direct flash light).

c. Then I grouped some of the ranges into four. While each phototransistor gives each value, it will fall into one of the ranges and play the designated note, which is pulled from “pitches.h” library.








code

#include "pitches.h"

const int threshold0 = 750;
const int threshold1 = 150;
const int threshold2 = 75;

int notes0[ ] = {
NOTE_A4, NOTE_B4, NOTE_C3
};
int notes1[ ] = {
NOTE_F7, NOTE_G3, NOTE_C5
};
int notes2[ ] = {
NOTE_D5, NOTE_G5, NOTE_E5
};
int notes3[ ] = {
NOTE_F6, NOTE_G6, NOTE_A6
};

void setup() {

}

void loop () {
for (int thisSensor = 0; thisSensor < 3; thisSensor++){

int sensorValue = analogRead (thisSensor);

if (sensorValue > threshold0) {
tone(8, notes0[thisSensor], 20);
}
if (sensorValue > threshold1 && sensorValue < threshold0) {
tone(8, notes1[thisSensor], 20);
}
if (sensorValue > threshold2 && sensorValue < threshold1) {
tone(8, notes2[thisSensor], 20);
}
if (sensorValue < threshold2) {
tone(8, notes3[thisSensor], 20);
}

Serial.print target="_blank">Serial.print(thisSensor);
Serial.print(" : ");
Serial.println(sensorValue);
delay(100);
}
}

Serial monitor




Soldering the speaker




© ymchoi 2019