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

Thread: How to analogRead() and use adc?

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Location
    Bristol UK
    Posts
    4

    How to analogRead() and use adc?

    Hi there,

    Please can someone tell me how to use anaolgRead() while also using adc. Does not work on 3.2 Maybe on 4.0?

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,953
    If using on the same pins there will be a conflict as the ADC library needs to be in control of those pins. Is that the desired use case?

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Location
    Bristol UK
    Posts
    4
    Obviously I'm not using the same pins.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,953
    Quote Originally Posted by AlexisJohn View Post
    Obviously I'm not using the same pins.
    It is now ... with @AlexisJohn a first time poster with no example code or specifics posted it seemed like a good starting point to clarify

    ADC was just updated to support T_4.0 in latest TD 1.49. Unclear if the same hardware limitation exists.

    If a simple usage sample were provided it would be easy to test.

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    6,347
    Sorry, I really don't fully understand what you are asking?

    What do you mean by "using adc"? Are you saying that you are using the ADC library? or using some external adc? or...

    Might help you gave a little more explanation on what you are trying to do.

    Note: both the system function analogRead and the ADC both setup the registers associated with the internal analog subsystem.

    With T4, there are two ADC units on the chip. The majority of the Analog pins on the T4 can be serviced by the first ADC object (ADC1) (ADC_0 object in ADC library) and as such in most cases analogRead will use this ADC object.
    Note: Most of the pins can also be serviced by the second ADC hardware ADC2 (ADC_1 in ADC library). But unless the Analog pins are not supported on ADC1 (A12 A13) Analog read will always use the ADC1 object.

    So potentially if you are desiring to use ADC library for some pin/pins, and they can be serviced by ADC2 (all but A10, A11) then maybe you could configure up ADC->adc1-> in ADC library and then see if analogRead works sufficiently.
    My guess it will run, but you may not have the normal default settings for analog...

    But you can always experiment and see if it works for you.

    As for it not working for you an T3.2? again it may really depend on exactly what you are trying to do and exactly how and what order you have done things.

  6. #6
    Junior Member
    Join Date
    Feb 2019
    Location
    Bristol UK
    Posts
    4
    Thanks guys especially KurtE

    I'm afraid a lot of your information is a bit over my head but basically I am using the inbuilt adc1 to analyse an audio stream (both mono and stereo from pins A2 abd A3 on Teensy 3.2) AND I would like to read a potentiameter (or 2) ? Is there ay simple way?

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,953
    It looks like the ADC library can be used for single reads see this example :: ...\hardware\teensy\avr\libraries\ADC\examples\ana logRead\analogRead.ino
    after setup() has this code:
    Code:
    void loop() {
    
        // Single reads
    
        value = adc->analogRead(readPin); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.

  8. #8
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    6,347
    Sorry, I know some of this stuff is confusing. Also please note I am not a big user of the Analog stuff, but do use it some, and helped a bit to get the ADC library working on the T4.


    But it is still not clear to me on how you are using adc1? Are you using the ADC library by @pedvide? As talked about in this thread: https://forum.pjrc.com/threads/25532...ht=adc+library


    Or some other mechanism?

    It would probably really help if you posted more of what you already have, so we might get a better feel of what you have...
    In some continuous reads or the like? Are you only using ADC_1? or do you also use ADC_0?

    As @defragster mentioned you can do single reads using ADC library like he shows above...
    Or you can specify which of the two ADCs to use like:
    Code:
    value = adc->adc0->analogRead(readPin);
    value = adc->adc1->analogRead(readpin);
    This form only works if the ADC unit you specify actually supports that specific PIN. The first post in the thread I linked to here has images of the different teensy cards and shows which analog pin is supported by which ADC

    EDIT-Warning - The images shown of the PINS for the T4 on that posting can be sort of confusing. That is
    it shows ADC0 and ADC1 which are actually ADC1 and ADC2 (IMXRT numbering on these different than T3.x processors) But we integrated it, decide to try to keep code sort of consistent...

  9. #9
    Junior Member
    Join Date
    Feb 2019
    Location
    Bristol UK
    Posts
    4
    Thank you to everyone for your help.

    So I dont really know what I'm doing here but basically once I switched library from adcs1 to adc1 I suddenly got access to loads of analog pins!!!! Problem solved!!!

Posting Permissions

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