teensy2 ADC weired berhaviour

Status
Not open for further replies.

heimi

Active member
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:
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.
 
ADC weired behaviour

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)

teensy2.jpg

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.
 
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 ...
 
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:
wiring_pinout2h_analog.png
teensy2 from pjrc:
pinout2a.png
sorry and thanks for your help. Channel 8 in the sketch runs fine now, but not channel 11 in the C code. Looking further.
 
Status
Not open for further replies.
Back
Top