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

Thread: teensy2 ADC weired berhaviour

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    11

    teensy2 ADC weired behaviour

    I have an app which uses 2 ADC channels on differnet Ports because of layout linitaions. Reading channel 1 from Port F on Pin PF1 works fine, but reading channel 11 from Port C on Pin PB4 returns garbage.
    I reduced the code to reading the same value on both Pins every second in a loop. Teensy is on a breadboard with a voltage divider and a capacitor for the value to read. Code for ADC is adc_read, copied from the pjrc site. Settings for the channels is:
    DDRB &= ~(1<<4); // ADC 11
    DDRF &= ~(1<<1); // ADC1

    Call is:
    uint8_t Tastenwert=0;
    Tastenwert=(adc_read(1)>>2); // Read channel 1
    lcd_putint12(Tastenwert); // Display a 12Bit number on the LCD

    Tastenwert=(adc_read(11)>>2); // Read channel 11
    lcd_putint12(Tastenwert); // Display a 12Bit number on the LCD

    Result of channel 1 is correct, channel 11 gives 127

    Changing the order of the calls changes nothing. Same behaviour on two teensy2, oneof them out of the box. Reading on other channels on Port F is OK, on other channels of Port C is not. There are no timers or other interrupts in the code.

    I think there is a very basic mistake in the code, but no idea what. I made several apps with teensy2++ with no such problems.
    Last edited by heimi; 12-03-2017 at 12:28 PM. Reason: wrong click while writing

  2. #2
    Senior Member manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,258
    As the forum rule suggests, you need to provide a sketch that someone could actually run and test. There are no ADCs on Port C.

    You should use analogRead() not adc_read(). The mux values for adc_read() are not linear. See https://www.pjrc.com/teensy/adc_simple.zip from https://www.pjrc.com/teensy/adc.html

    this simple sketch reads all the ADC pins on Teensy 2
    Code:
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
      int i, val;
    
      for (i = 0; i < 12; i++) {
        Serial.print(i); Serial.print(" "); Serial.println(analogRead(i));
      }
      delay(2000);
    }
    You can jumper GND to each of the ADC pins and see that the corresponding value changes.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    11

    ADC weired behaviour

    Quote Originally Posted by manitou View Post
    As the forum rule suggests, you need to provide a sketch that someone could actually run and test. There are no ADCs on Port C.

    You should use analogRead() not adc_read(). The mux values for adc_read() are not linear. See https://www.pjrc.com/teensy/adc_simple.zip from https://www.pjrc.com/teensy/adc.html

    this simple sketch reads all the ADC pins on Teensy 2
    Code:
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
      int i, val;
    
      for (i = 0; i < 12; i++) {
        Serial.print(i); Serial.print(" "); Serial.println(analogRead(i));
      }
      delay(2000);
    }
    You can jumper GND to each of the ADC pins and see that the corresponding value changes.

    Thanks and sorry for wrong format, safari did not show anything about it. And I misspelled Port C instead of Port B while not copy-pasting.
    This code snippet is part of a longer project with a dozen files and more than thousend lines, written in C. It loggs data via nRF24L01-Modules and saves it to a sd-card.
    I used the adc_read() in several projects from ATiny13 to Atmega644 and teensy2++ with adopted macros without problems.
    The gcc compiler shows no overrun of RAM or things like that. I only wonder why the teensy2 behaves in this manner for the channels on Port B.
    I modified your code snippet to read only channel 1 and 11.
    Code:
     int i, val;
          Serial.print(1); Serial.print(" "); Serial.print(analogRead(1));
          Serial.print(" ");
         
          Serial.print(11  ); Serial.print(" "); Serial.print(analogRead(11));
          Serial.println();
    The result is rather strange.
    Both pins are connected to the same value (output of an analog 3x4 keyboard)
    Channel 1 reads as expected. Channel 11 reads another value.
    inserting a delay between the readings changes the return value of channel 11.
    Code:
         Serial.print(1); Serial.print(" "); Serial.print(analogRead(1));
          Serial.print(" ");
          delay(1000);
          Serial.print(11  ); Serial.print(" "); Serial.print(analogRead(11));
          Serial.println();
    Attatched is an excel sheet with the results. (val 11 A with delay, val 11 B without delay)

    Click image for larger version. 

Name:	teensy2.jpg 
Views:	7 
Size:	59.1 KB 
ID:	12135

    Connecting channel 11 to ground changes nothing, the reading of the channel is the same as when connected to the input voltage. Connecting channel 1 to ground fixes the reading of channel 11 to a constant value for every input value. Connecting all open ADC channels to ground gives another serie of data on channel 11.
    The oscilloscope shows proper values.
    Somewhat is going wrong.

  4. #4
    Senior Member manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,258
    Hmmm, sounds like a wiring problem. check your soldering on A11, since it's an end pin and not an edge pin. Maybe attach a photo of your setup.
    You could disconnect everything from teensy 2.0 and run the analogRead() sketch above checking whether you get 0 when jumpered to GND, and 1023 when jumpered to 5v.

    maybe the EE guys will have more insights ...

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    Quote Originally Posted by manitou View Post
    Hmmm, sounds like a wiring problem. check your soldering on A11, since it's an end pin and not an edge pin. Maybe attach a photo of your setup.
    You could disconnect everything from teensy 2.0 and run the analogRead() sketch above checking whether you get 0 when jumpered to GND, and 1023 when jumpered to 5v.

    maybe the EE guys will have more insights ...
    Teensyduino problem solved. I had in mind the pinout and schematics from the pjrc-website and did not mention that in teensyduino the numbering of the channels is different.
    Teensyduino:
    Name:  wiring_pinout2h_analog.png
Views: 42
Size:  94.5 KB
    teensy2 from pjrc:
    Click image for larger version. 

Name:	pinout2a.png 
Views:	5 
Size:	245.2 KB 
ID:	12138
    sorry and thanks for your help. Channel 8 in the sketch runs fine now, but not channel 11 in the C code. Looking further.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    11
    I found a useful site about the ADC of the Atmega32u:
    http://medesign.seas.upenn.edu/index...es/MaEvArM-adc
    Dealing with channel numbers above 7 requieres setting the MUX5 bit in ADCSRB.

  7. #7
    Senior Member manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,258
    Quote Originally Posted by heimi View Post
    I found a useful site about the ADC of the Atmega32u:
    http://medesign.seas.upenn.edu/index...es/MaEvArM-adc
    Dealing with channel numbers above 7 requieres setting the MUX5 bit in ADCSRB.
    analogRead(pinnumber) takes care of all of that for you.

Tags for this Thread

Posting Permissions

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