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

Thread: A problem using the Audio library and ADC library

  1. #1

    A problem using the Audio library and ADC library

    Hey guys,
    I am experiencing a strange problem using Pedvide's (great) ADC library and the audio library :
    I am trying to read a (10k linear) pot while reading an audio input through the adc,
    here is my code :

    Code:
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <ADC.h>
    
    ADC *adc = new ADC();
    const int readPin = A3;
    
    // GUItool: begin automatically generated code
    AudioInputAnalog         adc1;           //xy=161,80
    AudioOutputAnalog        dac1;           //xy=329,47
    AudioConnection          patchCord1(adc1, dac1);
    // GUItool: end automatically generated code
    
    void setup() {
      AudioMemory(12);
      pinMode(17, INPUT);
    
      Serial.begin(57600);
      adc->setAveraging(32, ADC_1); // set number of averages
      adc->setResolution(16, ADC_1); // set bits of resolution
      adc->setConversionSpeed(ADC_HIGH_SPEED, ADC_1); // change the conversion speed
      adc->setSamplingSpeed(ADC_HIGH_SPEED, ADC_1); // change the sampling speed
      adc->setReference(ADC_REF_3V3, ADC_1);
    }
    
    int value = 0;
    
    void loop() {
      value = adc->analogRead(readPin, ADC_1); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
      Serial.print("Pin: ");
      Serial.print(readPin);
      Serial.print(", value ADC1: ");
      Serial.println(value, DEC);
    }
    audio input is fed to pin 16 (A2) and the pot is connected to 3.3v, GND and pin 17(A3).
    While the audio goes through very nicely, the reading of the pot is quite strange :
    I get a very precise reading for only half of the turn of the pot and for the other half
    it only gives the highest value, for example for the 16 bit resolution half of the dial
    gives all the range (0..65536 and very precisely so) and when I turn the knob further
    it only stays on the value 65536. My initial thought was that this is a faulty pot but then
    I ran this code :

    Code:
    #include <ADC.h>
    
    ADC *adc = new ADC();
    const int readPin = A3;
    
    void setup() {
      
      //pinMode(17, INPUT);
    
      Serial.begin(57600);
      adc->setAveraging(32, ADC_1); // set number of averages
      adc->setResolution(16, ADC_1); // set bits of resolution
      adc->setConversionSpeed(ADC_HIGH_SPEED, ADC_1); // change the conversion speed
      adc->setSamplingSpeed(ADC_HIGH_SPEED, ADC_1); // change the sampling speed
      adc->setReference(ADC_REF_3V3, ADC_1);
    }
    
    int value = 0;
    
    void loop() {
      value = adc->analogRead(readPin, ADC_1); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
      Serial.print("Pin: ");
      Serial.print(readPin);
      Serial.print(", value ADC1: ");
      Serial.println(value, DEC);
      //value*5.0/adc->getMaxValue(ADC_1)
    }
    which is the same, only without the audio library and the readings of the pot are perfect!
    I can't figure this out, help anyone ?
    Thanks in advance

  2. #2
    Senior Member Davidelvig's Avatar
    Join Date
    Aug 2015
    Location
    Wisconsin
    Posts
    268
    I could look deeper later this week. If you're interested in a quick glance now
    -you don't have "audio.h" at the top of your source.
    - Also line 19 appears to have an "ADC 1" instead of "ADC_1"
    Also, I recall trouble with this in the last year - not the same, but trouble regarding sharing ADC0 between Audio (which dominates it) and the ADC lib.
    Something reminds me that the order of the "includes" might matter (though I'd prefer it did't).
    Try ADC.H at the top, and make sure you add Audio.h as well later, and fix the "ADC 1" item as see if it compiles and runs.
    Good luck.

  3. #3
    Member
    Join Date
    Oct 2014
    Location
    World
    Posts
    31
    Did anyone make any progress on this? I just updated my Teensy+Arduino package and noticed a similiar issue emerge.
    Previously, I believe I had AudioInputAnalog and adc->analogRead(readPin, ADC_1) functioning together.

    I was trying to do some research and code-tinkering to get this working, but my log-scale light sensor only responds in very dark settings and is otherwise maxed out.

    I'm wondering if AudioInputAnalogStereo implementation is part of the cause?

    I also saw: (line 326) analogReadADC1(uint8_t pin) -- Is standalone analog input support coming to the Audio Library? I didn't find any documentation on this.
    Last edited by mcivan; 01-14-2017 at 09:58 PM.

  4. #4
    Member
    Join Date
    Oct 2014
    Location
    World
    Posts
    31
    I believe I found it. While this is still a feature request, you'll need to edit the Audio library to disable the Internal 1.2V reference that the Audio Library calls for. If you update your Teensy libraries... You'll need to make this edit again. Many other forums say to edit the files, in any txt editor but on my Mac this crashed the ruined the library. Here's what worked for me.

    1. Install or update your Audio Library.
    2. Start a new Arduino file... (I'd save it as something for future ref)
    3. Add the library file input_adc.cpp found at .../Arduino.app/Contents/Java/hardware/teensy/avr/libraries (or similar)
    4. Comment out analogReference(INTERNAL);
    5. Save and Quit all Arduino instances.
    6. Copy and replace the edited .cpp files (from the saved file) into the Library file above.
    7. Open Arduino, recompile, test you're ADC.

    Be aware this change also affects the Audio Input. (Which maybe good if you are using an Adafruit or similar mic amp.)

    In my testing adc->setReference(ADC_REF_3V3, ADC_1) has no effect, likely overwritten by Audio. Also setting the resolution to 10bits returned a 16bit value (though I only saw the max value).

    Hope the library supports changing the voltage reference in future releases!
    https://github.com/PaulStoffregen/Audio/issues/89
    Last edited by mcivan; 01-15-2017 at 06:08 AM.

Posting Permissions

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