PDA

View Full Version : Analog Input not working



mikevsail
01-09-2013, 10:27 PM
Hey I'm trying to read a voltage on any of the analog pins on the teensy 3.0
I have a voltage of say 2.45V at the pin from a sensor.
If I connect the A1 pin to the sensor signal
and A2 to the sensor ground A1=1024; and A2=0;

I am only able to get 1024 or 0 no mater what the voltage. At first my sensor was scaled to have a max signal voltage of 5volts..
I thought I saturated the pin.. maybe it was running at 3.3... so I scaled down my sensor max voltage.

Im reading two AD590 sensors

12V supply to the sensors, 25kohm sensing resistor to ground. I had a more complex version of the code working fine on an arduino. Is there something I have to do to set the pin??

I have seen a couple ways to do this.
------------------------------------
int analog1Pin = 1;
pinMode(analog1Pin,INPUT);

------------------------------------

int analog1Pin;
anaog1Pin=analogRead(1);

------------------------------------
Whats the difference?!

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
}

int val1;
int val2;
int val3;
int val4;
int val5;
float temp1;
float temp2;
float sensorV1max=3.264;
float sensorV2max=3.32;
float maxT=350;

void loop()
{
val1 = analogRead(1);
val2 = analogRead(2);
val3 = analogRead(3);
val4 = analogRead(4);
val5 = analogRead(5);

temp1=(((5*(val1-val2)/1024))/sensorV1max)*maxT;
temp2=(((5*(val3-val4)/1024))/sensorV2max)*maxT;

Serial.print("analog 1 is: ");
Serial.println(val1);
Serial.print("analog 2 is: ");
Serial.println(val2);
Serial.print("analog 3 is: ");
Serial.println(val3);
Serial.print("analog 4 is: ");
Serial.println(val4);
Serial.print("analog 5 is: ");
Serial.println(val5);
Serial.println(" ");
Serial.print("Temp1: ");
Serial.println(temp1);
Serial.print("Temp2: ");
Serial.println(temp2);
Serial.println(" ");
delay(250);
}

mikevsail
01-09-2013, 10:33 PM
Tried adding pinMode to the setup... same results....

PaulStoffregen
01-09-2013, 10:41 PM
Connect the sensor ground to AGND, not A2.

Nantonos
01-09-2013, 10:43 PM
Hey I'm trying to read a voltage on any of the analog pins on the teensy 3.0
I have a voltage of say 2.45V at the pin from a sensor.
If I connect the A1 pin to the sensor signal
and A2 to the sensor ground A1=1024; and A2=0;
}

To read from A1, connect the sensor signal to A1 and the sendor ground to GND.
To read from A2, connect the sensor signal to A2 and the sendor ground to GND.

Your current wiring is not reading from A1 or A2, because neither input has a ground reference.

mikevsail
01-09-2013, 11:18 PM
Is there any other problem it could be?
The pins are giving wacky readings.
Removing all code and wires.

I simply have the teensy plugged into my computer
The sensor hooked to A1 and Agnd
The voltage across the sensor is 3.74V

----------------------------------------------------------------------------

void setup()
{
Serial.begin(9600);
pinMode(1, INPUT);
}

int val1;
float temp1;
float sensorV1max=3.264;
float maxT=350;

void loop()
{
val1 = analogRead(1);

temp1=(((5*(val1)/1024))/sensorV1max)*maxT;

Serial.print("analog 1 is: ");
Serial.println(val1);
Serial.print("Temp1: ");
Serial.println(temp1);
Serial.println(" ");
delay(250);
}

---------------------------------------
My output is:
analog 1 is: 1023
Temp1: 428.92
--------------------------------------





from before---
The reason I put the signal ground to A2 was just to measure differential
The voltage I used was the difference between A1 and A2

PaulStoffregen
01-09-2013, 11:22 PM
I simply have the teensy plugged into my computer
The sensor hooked to A1 and Agnd
The voltage across the sensor is 3.74V

Teensy 3.0 can not measure more than 3.3 volts.

Actually, the maximum is determined by the reference voltage. By default, the 3.3 volt power is used as a reference. You can configure it to use the 1.2 volt internal reference, or you can connect a shunt-type reference chip, but that must be less than 3.3 volts.

It's not possible to measure 3.74 volts. You need to reduce the voltage to a range Teensy 3.0 can measure.

mikevsail
01-10-2013, 12:02 AM
Arggg! thanks for being patient. That worked.

mikevsail
01-10-2013, 12:59 AM
Is there a more complete way of defining pins for analog or digital read/write
I saw the older teensy had specific pin IDs

Its strange:
If i have

int mypin=2;

int val;
val=digitalRead(2);
___________________
vs.
__________________
val=analogRead(2);


These two will read from two different pins?!

im trying to now write to a pin and its not clear to me how to ensure i am defining the pins i think I am.

Im actually trying to write an analog voltage signal and read it with another pin for debugging.
the PWM signal is 0-255? correct? 255 being 100% duty cycle at the available output voltage (3.3volts also?)

I realize these are seemingly straight forward questions, but I cant seem to find the answers.

Thanks

Jp3141
01-10-2013, 02:52 AM
Connecting a pin with a PWM 'analog' signal [analogWrite(.) ] directly to another pin used to read [analogRead(.) ] the level won't give expected results -- the PWM is a varying duty cycle of 0 and 3.3 V -- the ADC will generally read (with different probabilities) one of these two values.

If you connect the PWM to the reading channel with a resistor (say 10 kohm), and also connect a capacitor from the reading channel to ground (say 0.1 uF), it will 'smooth' the PWM and you will get more predictable results.

Jp3141
01-10-2013, 03:03 AM
It sounds like you don't quite know what you are doing. The AD590 is a 2-terminal device and needs at least 4 V to operate. Typically you would connect its positive pin to a supply and connect the negative pin to ground via a resistor. The current in the R will be proportional to temperature in Kelvin -- it will be about 300 uA at room temperature and from -40 to +105 C will vary from about 230 uA to 400 uA.

Given that you can't put more than 3.3 V on the ADC input, you need the hot (400 uA) value to not generate more than 3.3 V, so the R should be less than 3.3/400u = 8k -- use 6.8kohm for margin. Then the ADC reading will vary by 1u*6.8k = 6.8 mV per degree centigrade. Note than then at room temperature, you will still get a pin voltage of 300u*6.8k= 2.04 V.

You will also need a supply of greater than 3.3+4 = 7.3 V to operate this.

A better way is to use an I2C temperature sensor -- e.g. LM75A or similar. If you really need an analog measurement, then perhaps an LM35-type would be more suitable ?

mikevsail
01-10-2013, 03:09 AM
If I put the pwm value to 255 shouldnt the analog read signal be full scale 3.3V at 100% duty cycle..

analogWrite(255,3);

When I try this the value i get for output is 0 Volts, read with a multimeter.
Im using Digital Pin 3, as the PWM output pin.

Jp3141
01-10-2013, 03:36 AM
do you have
pinMode(3, OUTPUT);
in your setup ?

Also, the way the PWM works is that a code of 255 puts out a duty cycle of 255/256 -- not quite 100 % of 3.3 V:
http://forum.pjrc.com/threads/8-analogWrite(pin-255)-outputs-a-lower-voltage-than-digitalWrite(pin-HIGH)

mikevsail
01-10-2013, 03:41 AM
I don't have any issues with my ad590 circuit. I'm using a 12v supply. And 3.3v max across the sensing resistor. And a 350uA current max. 350k

I originally thought the pin could read 0-5volts.

After fixing the analog ground. And changing the resistor so that the sensing voltage max was 3.3 all of the read functionally worked.

Now I'm having trouble writing.
That's why I asked about pin definition.
If I did an analogread and analogwrite on pin #3. They are or are not the same pin 3. So far

I think they are not the same.

Thanks
Mike

mikevsail
01-10-2013, 04:05 AM
pinMode(3,INPUT); //analog pin #3
pinMode(3,OUTPUT); //digital pin #3

How is this possible. The read or write come later. I just defined the pin then redefined it. I feel I'm missing something really easy. Like um... The pin numbers! I have the sheet.

Jp3141
01-10-2013, 04:15 AM
pinMode(3,INPUT); //analog pin #3
pinMode(3,OUTPUT); //digital pin #3

How is this possible. The read or write come later. I just defined the pin then redefined it. I feel I'm missing something really easy. Like um... The pin numbers! I have the sheet.

Be sure you are using the correct pin numbers (and reading the correct pin with your meter !). For example, the LED is on pin 13 (botom right corner when the USB connector is at the top). Next to (above) that is a pin named 'A0', but in the software this is pin 14 -- so you would use pinMode(14, OUPUT) to configure this physical pin.

Jp3141
01-10-2013, 04:23 AM
I don't have any issues with my ad590 circuit. I'm using a 12v supply. And 3.3v max across the sensing resistor. And a 350uA current max. 350k


For the current to be less than 350 uA, you will only be able to go to ~ 77 deg. C. To keep the voltage less than 3.3 (actually you need some margin because the '3.3 V' supply could be as low as 3.0 from the MCU spec.), you need a R of less than 3.3/350u = 9.4k -- I suggest 8.2k or so.

mikevsail
01-10-2013, 04:36 AM
Wow the pin number thing was screwing me up...
..now that you say that. I see in the pin diagram that pinMode corresponding to the grey numbers.
Ill try that, thank you very much.

As for the AD590. im doing what I intended to do. Im testing a heater control, and before I start heating things I am only using my finger to change the signal by a couple degrees, to trigger the control logic. Thats why the max limit is only 350K.
(K is for Kelvin here! maybe that was confusing, sorry. I like my SI units. 350K not 350k, my bad.)

mikevsail
01-10-2013, 06:14 AM
I guess now Im confused why my code worked at all before?
Can you help me understand?
val=analogRead(1); gave me the correct voltage applied to pin#15
It is also Analog Pin 1.

My program worked by defining the pinMode(1,Input).

-------------------------------------------------------
i will try using the pins as you describe.

PaulStoffregen
01-10-2013, 08:54 AM
The separate numbering systems for digital and analog pins is confusing. Other than analogRead(), every function takes a pin number from 0 to 33 representing the "digital" pin. Even analogWrite() uses that digital pin number.

Inside analogRead(), there's a hidden feature... the thing that maybe made this work. It accepts either type of pin number!

On Teensy 3.0, analog pin 0 is digital pin 14. So if you use analogRead(0), or analogRead(14), either way will read the same pin. There are also constants called A0, A1, A2, etc which are equal to 14, 15, 16... which you can use as the input the analogRead(). If you write analogRead(A0), it will be the same as analogRead(14). The idea behind "A0" is it automatically changes to whatever pin happens to be analog #0 if your code is used on a different board.


Also, it was mentioned above, but probably merits repeating: the analogWrite() creates a pulse width modulated output. It rapidly pulses the pin, where the "analog" nature is the portion of the time the pin is high. If you use low-pass filter to block the high frequency pulsing, you'll have an actual analog voltage suitable for input to analogRead(). The simplest way involves connecting a resistor to the pin, and on the other side of that resistor, a capacitor with the positive side to the resistor and the negative side to ground. That other side of the resistor will be a non-pulsing analog voltage which you can read with analogRead().