Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Audio shield - Noise while using photoresistor with audio shield to generate sine

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    1

    Audio shield - Noise while using photoresistor with audio shield to generate sine

    I am trying to build a theremin where i'm controlling the amplitude of a sine wave based on input from a photoresistor (and frequency based on ultrasonic distance sensor).
    I am getting really bad noise when the photoresistor input is in the low to middle range. Input from other sensor (ultrasonic distance) does not cause this problem. Any guidance will be appreciated.

    code:
    Code:
    
    
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    //Ultrasonic sensor HC-S04 stuff
    #define trigPin 0
    #define echoPin 1
    #define photoResistorPin 8
    #define led 13
    long distance, duration;
    
    
    // GUItool: begin automatically generated code
    AudioSynthWaveform   sine1;          //xy=245,312
    AudioOutputUSB           usb1;           //xy=841,291
    AudioOutputAnalog        dac1;
    AudioOutputI2S           i2s1;           //xy=873,209
    AudioConnection          patchCord1(sine1, 0, i2s1, 0);
    AudioConnection          patchCord2(sine1, 0, i2s1, 1);
    AudioConnection          patchCord3(sine1, 0, usb1, 0);
    AudioConnection          patchCord4(sine1, 0, usb1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=469.00000762939453,382.00000762939453
    // GUItool: end automatically generated code
    
    
    float noteFrequency;
    float noteAmplitude;
    double amplitudeVal; 
    long sensorVal;
    long sensor1Val;
    int key;
    
    
    
    void setup() {
      Serial.begin(9600);
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
      pinMode(led, OUTPUT);
      sine1.begin(WAVEFORM_SINE);
      AudioMemory(20);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.85);
      
    }
    //>>>>>>>>>>>>>> Reading the ultra sonic sensor<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    long sensorReading()
      {
        digitalWrite(trigPin, LOW); 
        delayMicroseconds(2); 
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10); 
        digitalWrite(trigPin, LOW);
        duration = pulseIn(echoPin, HIGH);
        distance = duration/58.2; //for cm
        Serial.println("Distance ");
        Serial.println(distance);
        return distance;
      }
    //>>>>>>>>>>>>>>>>>>>>> Key Mapping <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Translating sensor reading to frequency>>>>>>>>>>>>>>>>>>>>>>
    float frequencyCalculator()
      {    
        if((distance>50))
        {
          noteFrequency = 0;
        }
        else 
         {
            if((distance<=5))
             {
              noteFrequency = 261;    //middle C
             } 
            else if((distance>5)&&(distance<=10))
                {
                 noteFrequency = 293;   //D
                }
                  else if((distance>10)&&(distance<=15))
                   {
                    noteFrequency = 329;    //E
                   } 
                   else if((distance>15)&&(distance<=20))
                      {
                       noteFrequency = 349;   //F
                      }
                       else if((distance>20)&&(distance<=25))
                         {
                          noteFrequency = 391;    //G
                         }
                         else if ((distance>25)&&(distance<=30))
                            {
                              noteFrequency = 440;    //A
                            }
                            else if ((distance>30))
                               {
                                 noteFrequency = 494;    //B
                               }
        }
        return noteFrequency;     
       }
    // >>>>>>>>>>>>>>>>>>>>>> Envelope altering >>>>>>>>>>>>>>>>>>>>>>>>>
    
    // >>>>>>>>>>>>>>>>>>>>>> Amplitude maping >>>>>>>>>>>>>>>>>>>>>>>>>
    float amplitudeMapping()
        {  
           
           double z;     
           z = analogRead(photoResistorPin);
           
           amplitudeVal = z/1000;
           Serial.println("aplitude is ");
           Serial.println(amplitudeVal);       
          return amplitudeVal;
        }  
      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> generating the sound<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    void soundGen()
      {sensorReading();
        noteFrequency = frequencyCalculator();
        
        noteAmplitude = amplitudeMapping();
        
        
        
         
              sine1.frequency(noteFrequency);
              sine1.amplitude(noteAmplitude);
              
              
        
           
         if(noteFrequency == 0)
          {
           //Serial.print("\nDoing nothing \n"); 
            
            digitalWrite(led,LOW);
          }
      }
    
    // >>>>>>>>>>>>>>>>>>>>>>>>>>> Main Loop <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    
    void loop()
    {
     
     soundGen();
      
        Serial.print("\n Key: ");
        Serial.print(key); 
        Serial.print("  amplitudeVal: ");
        Serial.print(amplitudeVal);
        Serial.print("  Frequency: ");
        Serial.print(noteFrequency);
        
     }

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,442
    If it's with a photoresistor, you should not call it a Theremin. As the original Theremin patent states, its primary characteristic is generating sound with continuous pitch through heterodyning two radio frequencies, one fixed and one variable.

    What you are doing is a sensor controlled oscillator. And your code does not output continuous pitch but discrete tones, and that in a very limited range...

    The photoresistor risks to pick up ambient 50 or 100Hz noise from AC driven light bulbs, which is then aliased with your sampling rate. All these products will modulate the audio signal... Thus, such a circuit can only work with advanced filtering.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,169
    Maybe the ResponsiveAnalogRead library can help you get a cleaner measurement of the photosensor signal?

    Might also be worth trying a 10 nF capacitor connected as close as reasonably possible between the analog input pin and AGND.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •