# Problem with ADC on Teensy 3.0

• 12-21-2013, 07:20 PM
JeremyW
Problem with ADC on Teensy 3.0
I am working on building a ADC conversion board to read a voltage from a photodiode to an input pin on the teensy 3.0 and send it out over serial connection to an output.
My goal is that the value it sends out is as close of a reading as the voltmeter reads. The voltmeter has been extremely accurate so far R2 of .9998. but the teensy is still giving me problems.

void TwoSixtynmP0()
{
digitalWrite(redPin, HIGH); // turn on the red LED
double rSum0 = 0;
double rlSum0 = 0;
int RVoltage0 = 0;
delay(100);
for (int r = 0; r < 5; r++)
{
for (int rl = 0; rl < 10; rl++)
{
delay(50);
double RVoltage0 = RsensorValue * (3.3 / 1023.0); // is this conversion correct?
if (RVoltage0<0)
{
RVoltage0 = 0;
}
rlSum0 += RVoltage0;
}
delay(10);
}
double rAvg0 = rlSum0 / 50;
Serial.print("R0:");Serial.println(rAvg0, 5); //Serial.println(rAvg, 5);
BLEMini.print("R:");BLEMini.println(rAvg0, 5); // BLEMini.println(rAvg, 5);
dimAll();
rAvg0=0;
return;
}

Here is what I'm working with:

External LEDs that are controlled via switch and external battery power. I only have two photodiodes connected to the board. in pins 14 (A0) and 15(A1)
I also have a bluetooth board connected to pins 0,1
and a single LED to pin 2 to let me know the board is turned on.

Teensy Voltage Volt meter Difference % Error
0.17839 0.155 0.02339 0.1311172151
0.16194 0.145 0.01694 0.1046066444
0.13445 0.123 0.01145 0.0851617702
0.09058 0.043 0.04758 0.5252815191
0.06413 0.018 0.04613 0.719320131

I've got much more sample data if needed. I'm at a loss as to where I'm going wrong. Any help is appreciated! if you need more information just ask.

the setup

Code:

```#include*<stdio.h> #include*<Arduino.h>* #include*<SoftwareSerial.h> #include*<Firmata.h> //BLE*Mini*is*connected*to*pin*0*and*1 SoftwareSerial*BLEMini(0,*1);**//RX, TX int redPin = 2; //red LED to come on when the UV lights are on. int TwoSixtynmDetect = A0; //260nm detector pin 14 int TwoSixtynmLED = 3; // 260nm UV LED int TwoEightynmDetect = A1; //280nm detector pin 15 int TwoEightynmLED = 4;//280nm UV LED byte incomingByte = 0; //incoming data from iPad //*Initialize*all*unused*pins*to*output,*for*minimum*battery*useage. int emptyoutPin1 = 5; int emptyoutPin2 = 6; int emptyoutPin3 = 7; int emptyoutPin4 = 8; int emptyoutPin5 = 9; int emptyoutPin6 = 10; int emptyoutPin7 = 11; int emptyoutPin8 = 12; int emptyoutPin9 = 13; int emptyoutPin10 = 17; int emptyoutPin11 = 18; int emptyoutPin12 = 19; int emptyoutPin13 = 20; int emptyoutPin14 = 21; int emptyoutPin15 = 22; int emptyoutPin16 = 23; void setup() { BLEMini.begin(57600); Serial.begin(57600); //setting the serial speed pinMode(redPin, OUTPUT);// configuring the LED pins for output pinMode(TwoSixtynmLED, OUTPUT); pinMode(TwoEightynmLED, OUTPUT);    pinMode(TwoSixtynmDetect, INPUT); //initialize the three detectors pinMode(TwoEightynmDetect, INPUT); //initialize the three detectors //**pinMode(blueDetect,*INPUT);*//initialize*the*three*detectors pinMode(emptyoutPin1, OUTPUT); //Set all empty pins as output to save battery power pinMode(emptyoutPin2, OUTPUT); pinMode(emptyoutPin3, OUTPUT); pinMode(emptyoutPin4, OUTPUT); pinMode(emptyoutPin5, OUTPUT); pinMode(emptyoutPin6, OUTPUT); pinMode(emptyoutPin7, OUTPUT); pinMode(emptyoutPin8, OUTPUT); pinMode(emptyoutPin9, OUTPUT); pinMode(emptyoutPin10, OUTPUT); pinMode(emptyoutPin11, OUTPUT);  pinMode(emptyoutPin12, OUTPUT); pinMode(emptyoutPin13, OUTPUT); pinMode(emptyoutPin14, OUTPUT);  pinMode(emptyoutPin15, OUTPUT); pinMode(emptyoutPin16, OUTPUT); digitalWrite(redPin, HIGH); // turn off all LEDS to reset them from any earlier configuration digitalWrite(TwoSixtynmLED, HIGH); digitalWrite(TwoEightynmLED, HIGH); }```
Here is the code for my ADC:

Code:

```  void TwoSixtynmP0()     {         digitalWrite(redPin, HIGH); // turn on the red LED         digitalWrite(TwoSixtynmLED, HIGH);         digitalWrite(TwoEightynmLED, HIGH);         double rSum0 = 0;         double rlSum0 = 0;         int RVoltage0 = 0;                delay(100); // give time for stray light and signal to steady.         for (int r = 0; r < 5; r++)           {           for (int rl = 0; rl < 10; rl++)               {                   delay(50);                   int RsensorValue = analogRead(A0);                   double RVoltage0 = RsensorValue * (3.3 / 1023.0);                     if (RVoltage0<0)                         {                           RVoltage0 = 0;                         }                   rlSum0 += RVoltage0;               }         delay(10);           }         double rAvg0 = rlSum0 / 50;         Serial.print("R0:");Serial.println(rAvg0, 5);    //Serial.println(rAvg, 5);         BLEMini.print("R:");BLEMini.println(rAvg0, 5); // BLEMini.println(rAvg, 5);         dimAll();         rAvg0=0;         return;       }```
• 12-22-2013, 02:02 PM
PaulStoffregen
Try using analogReference(INTERNAL) and set the conversion for 1.2 volts. The internal reference should be much more stable.
• 12-22-2013, 02:32 PM
Jp3141
Also, if you look at your (limited) data, the Teensy has an error of ~ 28 % and there is an offset of ~ -65 mV. You should check your ground connections also.