-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTSL1402R.ino
More file actions
122 lines (99 loc) · 4.05 KB
/
Copy pathTSL1402R.ino
File metadata and controls
122 lines (99 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Parallel read of the linear sensor array TSL1402R (= the sensor with 256 photodiodes)
//-------------------------------------------------------------------------------------
// Define various ADC prescaler:
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
int CLKpin = 3; // <-- Arduino pin delivering the clock pulses to pin 3 (CLK) of the TSL1402R
int SIpin = 4; // <-- Arduino pin delivering the SI (serial-input) pulse to pin 2 of the TSL1402R
int AOpin1 = A1; // <-- Arduino pin connected to pin 4 (analog output 1)of the TSL1402R
int IntArray[128]; // <-- 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);
pinMode(5, OUTPUT);
analogWrite(5, 128);
pinMode(6, OUTPUT);
analogWrite(6, 0);
pinMode(9, OUTPUT);
analogWrite(9, 0);
// To set up the ADC, first remove bits set by Arduino library, then choose
// a prescaler: PS_16, PS_32, PS_64 or PS_128:
ADCSRA &= ~PS_128;
ADCSRA |= PS_32; // <-- Using PS_32 makes a single ADC conversion take ~30 us
// Next, assert default setting:
analogReference(DEFAULT);
// Set all IO pins low:
for( int i=0; i< 14; i++ )
{
}
// 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< 132;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);
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 < 128; 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 < 132; 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);
}