I am trying to read and use values retrieved from a TSL230R (light to frequency converter) as a kind of a digital nightlight. The circuit seems to be working and the code too. However, if the TSL230R gets too dark, the Uno returns 0 with the pulseIn instruction. pulseIn should not be timing out as the waiting times for the TSL are: 100us for power up and 150ns max (50ns typical) for output enable. Well below the Arduino default timeout of 1000ms.
//aliases so we don't have to remember numbers
int S0 = 2;
int OE = 3;
int In = 4;
int LED = 7;
long x; //storage for chipRead()
int light = 10000; //fill in with experiment data
int dark = 25000; //fill in with experiment data
bool LEDstat = 0; //act as flipflop
void setup() {
//1=pin 2 (S0)
//2=GND (S1)
//3=pin 3 (!OE)
//4=GND
//5=5 Vcc
//6=pin 4 (Output)
//7=Vcc (S2)
//8=GND (S3)
//set pins
pinMode(2,OUTPUT); //power control (0=off 1=on)
pinMode(3,OUTPUT); //output control (active low)
pinMode(4,INPUT); //input (read high)
pinMode(7,OUTPUT); //LED pin
//initialization
digitalWrite(OE,1); //disable output
delay(1); //let the chip respond
digitalWrite(S0,0); //turn chip off
digitalWrite(LED,0); //disable LED
Serial.begin(9600); //debugging
}
void loop() {
digitalWrite(LED,0);
turnOn();
x = chipRead();
turnOff();
if(x>light && x<dark){
//do nothing
}
if(x<=light){
LEDstat=0;
}
if(x>=dark){
LEDstat=1;
}
Serial.println(x); //debugging
digitalWrite(LED,LEDstat);
delay(1000); //1000 for debugging. set to 60000 for final code
}
void turnOff(){
digitalWrite(OE,1); //disable output
delay(1); //let the chip respond
digitalWrite(S0,0); //turn chip off
}
void turnOn(){
digitalWrite(S0,1); //turn chip on
delay(1); //let the chip respond
digitalWrite(OE,0); //enable output
}
unsigned int chipRead(){
return pulseIn(In,1); //read a pulse in the train
}
Nope. I did notice a little error in the code. I had x as a long, but I called the function as an unsigned int. I fixed it and nothing changed. I also shined a flashlight onto the chip and the Uno isn't reporting numbers over 1000, which it had no problems with before. Actually, the largest number it reports is <750.
It was working when I initially tested it. The only thing that changed is what room it was located in. I am back in that room and it still isn't working.
It responds incorrectly to any light levels. It could be a bad connection with a wire itself. Yes, breadboard. It's starting to get a bit late for me, though, so I'll do further tests tomorrow.
I tested the wires with a multimeter and moved them around while testing. They all seem to be fine. I finally dug up my oscope and it appears that the Arduino is fine. The problem seems to be that the TSL simply isn't putting out a high signal. If I shine a flashlight directly onto the chip, it works. So perhaps I have a bad TSL? So I replaced the TSL with one I almost destroyed once. I'm not getting zero's any more. But I am getting two's and three's and an occasional four and five. It's still wrong, but it's not zero. And here is my current code:
//aliases so we don't have to remember numbers
int S0 = 2;
int OE = 3;
int In = 4;
int LED = 7;
long x; //storage for chipRead()
int light = 10000; //fill in with experiment data
int dark = 25000; //fill in with experiment data
bool LEDstat = 0; //act as flipflop
void setup() {
//1=pin 2 (S0)
//2=GND (S1)
//3=pin 3 (!OE)
//4=GND
//5=5 Vcc
//6=pin 4 (Output)
//7=Vcc (S2)
//8=GND (S3)
//set pins
pinMode(2,OUTPUT); //power control (0=off 1=on)
pinMode(3,OUTPUT); //output control (active low)
pinMode(4,INPUT); //input (read high)
pinMode(7,OUTPUT); //LED pin
//initialization
digitalWrite(OE,1); //disable output
delay(1); //let the chip respond
digitalWrite(S0,0); //turn chip off
digitalWrite(LED,0); //disable LED
Serial.begin(9600); //debugging
}
void loop() {
digitalWrite(LED,0);
turnOn();
x = chipRead();
turnOff();
if(x>light && x<dark){
//do nothing
}
if(x<=light){
LEDstat=0;
}
if(x>=dark){
LEDstat=1;
}
Serial.println(x); //debugging
digitalWrite(LED,LEDstat);
delay(1000); //1000 for debugging. set to 60000 for final code
}
void turnOff(){
digitalWrite(OE,1); //disable output
delay(1); //let the chip respond
digitalWrite(S0,0); //turn chip off
}
void turnOn(){
digitalWrite(S0,1); //turn chip on
delay(1); //let the chip respond
digitalWrite(OE,0); //enable output
}
long chipRead(){
return pulseIn(In,1,3000); //read a pulse in the train
}
EDIT
Farther testing concludes that the issue is with the chipRead() function. I wrote a little script that read:
I also hardwired the OE to ground and S0 to Vcc. It worked, so I changed my code to not use a function by changing "x = readChip();" to "x = pulseIn(4,1);" and commenting out the entire readChip() function and it's now printing the numbers correctly. I rewired the chip to how it's supposed to be and it is working 100%.
EDIT
Just to clarify, it wasn't working incorrectly because I wired it incorrectly. I wired it incorrectly to keep the chip on during testing to see if that was the problem. It wasn't. The problem had to do with using a function to store a value. I still don't know why the function didn't work, but I got the code working and that's all that matters.
Getting ready for bed. I plugged it in and the LED lit up. Hmm. "I'll wait until it updates and see what happens." It updated and the LED stayed lit. "Maybe the wires are casting too much shade on the photodiode array. I'll shine a brighter light on it and wait for the next update." It updated and the LED turned off. I turned the flashlight off and waited some more. It updated (I think) and stayed off.
"Okay, cool, it's finally working." I start finishing getting ready for bed and it updates again and the LED comes back on.
"Let's see what happens when I turn the (bedroom) light off." I turn the light off and get in bed. It updates and the LED turns off. It has yet to turn back on.
EDIT
It turned back on after I turned my light back on.