Having a weird issue with two LEDs and some light sensors on a Teensy LC

Status
Not open for further replies.
Hey. I made a quick thing in the office today that turns on an LED when a light sensor goes high, turns on another LED when another light sensor goes high, and turns on a relay when both light sensors go high.

The board is a Teensy LC. The sensors are ALSPT19 Light sensors from Adafruit (adafru.it/2748). The LEDs are 5v LEDs with built in resistors for pinball machines.

The issue is, the LEDs are not getting the full 3.3v. The relay gets 3.3v when activated. The LEDs get between 1.5 and 1v. They also seem to change brightness based on whether they're both on or not.

Here's my code. Everything is working as expected except for the brightness/voltage issues. Let me know what you think.

LEDs are "doorA" and "doorB", relay is "StpLght".

Code:
const int sensorA = A0;
const int sensorB = A1;
const int doorA = 12;
const int doorB = 11;
const int StpLght = 10;

void setup() {
  Serial.begin(9600);
  pinMode(sensorA, INPUT);
  pinMode(sensorB, INPUT);
  pinMode(doorA, OUTPUT);
  pinMode(doorB, OUTPUT);
  pinMode(StpLght, OUTPUT);

}

void loop() {
  if (analogRead(sensorA) >= 25){
    digitalWrite(doorA, HIGH);
  }
  if (analogRead(sensorB) >= 25){
    digitalWrite(doorB, HIGH);
  }
  if (analogRead(sensorA) >= 25 && analogRead(sensorB) >= 25){
    digitalWrite(StpLght, HIGH);
  }
  else {
    digitalWrite(doorA, LOW);
    digitalWrite(doorB, LOW);
    digitalWrite(StpLght, LOW);
  }
}
 
They are pulsing from the order of the code - try this:
Code:
void loop() {
  if (analogRead(sensorA) >= 25){
    digitalWrite(doorA, HIGH);
  }
  else {
    digitalWrite(doorA, LOW);
  }
  if (analogRead(sensorB) >= 25){
    digitalWrite(doorB, HIGH);
  }
  else {
    digitalWrite(doorB, LOW);
  }
  if (analogRead(sensorA) >= 25 && analogRead(sensorB) >= 25){
    digitalWrite(StpLght, HIGH);
  }
  else {
    digitalWrite(StpLght, LOW);
  }
}
 
Unrelated to LED problem, but I’d use two local variables to eliminate unnecessary calls to analogRead().

Not sure if this answers the question - but for example the above code could be written as:
Code:
void loop() {
  uint32_t SensorA_val = analogRead(sensorA);
  uint32_t SensorB_val = analogRead(sensorB);
  if (SensorA_val >= 25){
    digitalWrite(doorA, HIGH);
  }
  else {
    digitalWrite(doorA, LOW);
  }
  if (SensorB_val >= 25){
    digitalWrite(doorB, HIGH);
  }
  else {
    digitalWrite(doorB, LOW);
  }
  if (SensorA_val >= 25 && SensorB_val >= 25){
    digitalWrite(StpLght, HIGH);
  }
  else {
    digitalWrite(StpLght, LOW);
  }
}
 
If code suggestions don’t fix, I am guessing that the led draw is too much for the teensy to sink/source properly. I recall that pinball leds with built in resistors is to prevent lighting when the circuit leaks out a low voltage that incandescent bulbs don’t show but the LEDs do (hence the resistor to fix the issue) . The LED Pinball Bulbs I am referring to is non ghosting bulbs. One way to fix this is to add a transistor circuit per led. (see post below for ideas).

https://forum.pjrc.com/threads/36766-Three-quick-questions-from-a-beginner-ish
 
If code suggestions don’t fix, I am guessing that the led draw is too much for the teensy to sink/source properly. I recall that pinball leds with built in resistors is to prevent lighting when the circuit leaks out a low voltage that incandescent bulbs don’t show but the LEDs do (hence the resistor to fix the issue) . The LED Pinball Bulbs I am referring to is non ghosting bulbs. One way to fix this is to add a transistor circuit per led. (see post below for ideas).

https://forum.pjrc.com/threads/36766-Three-quick-questions-from-a-beginner-ish

That was my first wonder - too much current esp. depending on the 'relay' - hopefully it won't come to that - though it seems possible.

Looking at the code - if only one door then its LED was set LOW right after it was set HIGH at loop() end - so half value makes sense.
 
These are all awesome ideas. Thank you so much! I'll give them a try when I'm back in the office on Monday and see how that goes.
 
The re-arranged code was exactly the right fix! I just loaded it up and it all seems to be working as intended now. This is a system of lights at our office that tells people when the two single-user bathrooms are in use, so they don't have to get up from their desk or walk across the warehouse and rattle the knobs to see if they can use the room. It really helps. Thank you so much for helping me with the code!
 
Very practical - glad it helped and was the right answer.

Now you can keep stats and see if a 3rd restroom is called for :)
 
Status
Not open for further replies.
Back
Top