Problem with ADC on Teensy 3.0

Status
Not open for further replies.

JeremyW

New member
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);
int RsensorValue = analogRead(A0);
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*<[color=#CC6600]Firmata[/color].h>


[color=#7E7E7E]//BLE*Mini*is*connected*to*pin*0*and*1[/color]
SoftwareSerial*BLEMini(0,*1);**[color=#7E7E7E]//RX, TX[/color]
[color=#CC6600]int[/color] redPin = 2; [color=#7E7E7E]//red LED to come on when the UV lights are on.[/color]
[color=#CC6600]int[/color] TwoSixtynmDetect = A0; [color=#7E7E7E]//260nm detector pin 14[/color]
[color=#CC6600]int[/color] TwoSixtynmLED = 3; [color=#7E7E7E]// 260nm UV LED[/color]
[color=#CC6600]int[/color] TwoEightynmDetect = A1; [color=#7E7E7E]//280nm detector pin 15[/color]
[color=#CC6600]int[/color] TwoEightynmLED = 4;[color=#7E7E7E]//280nm UV LED[/color]
[color=#CC6600]byte[/color] incomingByte = 0; [color=#7E7E7E]//incoming data from iPad[/color]



[color=#7E7E7E]//*Initialize*all*unused*pins*to*output,*for*minimum*battery*useage.[/color]
[color=#CC6600]int[/color] emptyoutPin1 = 5;
[color=#CC6600]int[/color] emptyoutPin2 = 6;
[color=#CC6600]int[/color] emptyoutPin3 = 7;
[color=#CC6600]int[/color] emptyoutPin4 = 8;
[color=#CC6600]int[/color] emptyoutPin5 = 9;
[color=#CC6600]int[/color] emptyoutPin6 = 10;
[color=#CC6600]int[/color] emptyoutPin7 = 11;
[color=#CC6600]int[/color] emptyoutPin8 = 12;
[color=#CC6600]int[/color] emptyoutPin9 = 13;
[color=#CC6600]int[/color] emptyoutPin10 = 17;
[color=#CC6600]int[/color] emptyoutPin11 = 18;
[color=#CC6600]int[/color] emptyoutPin12 = 19;
[color=#CC6600]int[/color] emptyoutPin13 = 20;
[color=#CC6600]int[/color] emptyoutPin14 = 21;
[color=#CC6600]int[/color] emptyoutPin15 = 22;
[color=#CC6600]int[/color] emptyoutPin16 = 23;


[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{
BLEMini.[color=#CC6600]begin[/color](57600);
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](57600); [color=#7E7E7E]//setting the serial speed[/color]
[color=#CC6600]pinMode[/color](redPin, [color=#006699]OUTPUT[/color]);[color=#7E7E7E]// configuring the LED pins for output[/color]
[color=#CC6600]pinMode[/color](TwoSixtynmLED, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](TwoEightynmLED, [color=#006699]OUTPUT[/color]);     
[color=#CC6600]pinMode[/color](TwoSixtynmDetect, [color=#006699]INPUT[/color]); [color=#7E7E7E]//initialize the three detectors[/color]
[color=#CC6600]pinMode[/color](TwoEightynmDetect, [color=#006699]INPUT[/color]); [color=#7E7E7E]//initialize the three detectors[/color]
[color=#7E7E7E]//**pinMode(blueDetect,*INPUT);*//initialize*the*three*detectors[/color]
[color=#CC6600]pinMode[/color](emptyoutPin1, [color=#006699]OUTPUT[/color]); [color=#7E7E7E]//Set all empty pins as output to save battery power[/color]
[color=#CC6600]pinMode[/color](emptyoutPin2, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin3, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin4, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin5, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin6, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin7, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin8, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin9, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin10, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin11, [color=#006699]OUTPUT[/color]);  
[color=#CC6600]pinMode[/color](emptyoutPin12, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin13, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin14, [color=#006699]OUTPUT[/color]);  
[color=#CC6600]pinMode[/color](emptyoutPin15, [color=#006699]OUTPUT[/color]);
[color=#CC6600]pinMode[/color](emptyoutPin16, [color=#006699]OUTPUT[/color]);

[color=#CC6600]digitalWrite[/color](redPin, [color=#006699]HIGH[/color]); [color=#7E7E7E]// turn off all LEDS to reset them from any earlier configuration[/color]
[color=#CC6600]digitalWrite[/color](TwoSixtynmLED, [color=#006699]HIGH[/color]);
[color=#CC6600]digitalWrite[/color](TwoEightynmLED, [color=#006699]HIGH[/color]);
}

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;
      }
 
Try using analogReference(INTERNAL) and set the conversion for 1.2 volts. The internal reference should be much more stable.
 
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.
 
Status
Not open for further replies.
Back
Top