Hello everybody,
I have need to test the photodiode sensor with DUE but the playground that is here is meant for the UNO.
Would you have an idea to fix the first few lines of code that give error?
// Define various ADC prescaler:
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
I know that these are the registers that run the ADC in UNO but in DUE that changes everything!
So the Due runs on a completely different architecture, 32-bit arm in the Due vs 8-bit AVR in the Uno. Because of this change the registers are different. The ADC pre scaler determines how fast the ADC can run. Other people (such as in this thread: Analog I/O Pins and ADC Sampling Frequency - Arduino Due - Arduino Forum ) also talk about ADC speed. According to Evaluating Arduino and Due ADCs you can speed this up quite a bit by putting
REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000;
in to your project. You will need to get rid of all of the Uno pre scaler.
Here is the code you should try to use (I havent tested it since I don't have a project to use it with, but it compiles fine on my computer):
// Parallel read of the linear sensor array TSL1402R (= the sensor with 256 photodiodes)
//-------------------------------------------------------------------------------------
int CLKpin = 4; // <-- Arduino pin delivering the clock pulses to pin 3 (CLK) of the TSL1402R
int SIpin = 5; // <-- Arduino pin delivering the SI (serial-input) pulse to pin 2 of the TSL1402R
int AOpin1 = 1; // <-- Arduino pin connected to pin 4 (analog output 1)of the TSL1402R
int AOpin2 = 2; // <-- Arduino pin connected to pin 8 (analog output 2)of the TSL1402R
int IntArray[256]; // <-- the array where the readout of the photodiodes is stored, as integers
void setup()
{
// Initialize two Arduino pins as digital output:
pinMode(CLKpin, OUTPUT);
pinMode(SIpin, OUTPUT);
// Next, assert default setting:
analogReadResolution(10);
// Set all IO pins low:
for( int i=0; i< 14; i++ )
{
digitalWrite(i, LOW);
}
// Clock out any existing SI pulse through the ccd register:
for(int i=0;i< 260;i++)
{
ClockPulse();
}
// Create a new SI pulse and clock out that same SI pulse through the sensor register:
digitalWrite(SIpin, HIGH);
ClockPulse();
digitalWrite(SIpin, LOW);
for(int i=0;i< 260;i++)
{
ClockPulse();
}
Serial.begin(115200);
}
void loop()
{
// Stop the ongoing integration of light quanta from each photodiode by clocking in a SI pulse
// into the sensors register:
digitalWrite(SIpin, HIGH);
ClockPulse();
digitalWrite(SIpin, LOW);
// Next, read all 256 pixels in parallell. Store the result in the array. Each clock pulse
// causes a new pixel to expose its value on the two outputs:
for(int i=0; i < 128; i++)
{
delayMicroseconds(20);// <-- We add a delay to stabilize the AO output from the sensor
IntArray[i] = analogRead(AOpin1);
IntArray[i+128] = analogRead(AOpin2);
ClockPulse();
}
// Next, stop the ongoing integration of light quanta from each photodiode by clocking in a
// SI pulse:
digitalWrite(SIpin, HIGH);
ClockPulse();
digitalWrite(SIpin, LOW);
// Next, send the measurement stored in the array to host computer using serial (rs-232).
// communication. This takes ~80 ms during whick time no clock pulses reaches the sensor.
// No integration is taking place during this time from the photodiodes as the integration
// begins first after the 18th clock pulse after a SI pulse is inserted:
for(int i = 0; i < 256; i++)
{
Serial.print(IntArray[i]); Serial.print(";");
}
Serial.println(""); // <-- Send a linebreak to indicate the measurement is transmitted.
// Next, a new measuring cycle is starting once 18 clock pulses have passed. At
// that time, the photodiodes are once again active. We clock out the SI pulse through
// the 256 bit register in order to be ready to halt the ongoing measurement at our will
// (by clocking in a new SI pulse):
for(int i = 0; i < 260; i++)
{
if(i==18)
{
// Now the photodiodes goes active..
// An external trigger can be placed here
}
ClockPulse();
}
// The integration time of the current program / measurement cycle is ~3ms. If a larger time
// of integration is wanted, uncomment the next line:
// delay(15);// <-- Add 15 ms integration time
}
// This function generates an outgoing clock pulse from the Arduino digital pin 'CLKpin'. This clock
// pulse is fed into pin 3 of the linear sensor:
void ClockPulse()
{
delayMicroseconds(1);
digitalWrite(CLKpin, HIGH);
digitalWrite(CLKpin, LOW);
}
Thanks Themavery,
I had already found the blog of Dave Erickson, really well done!
I tried to add to your code setting the REG_ADC_MR but it generates error ...
I still try ... >:(