Go Down

Topic: 10 LDR's to 5 RGB LEDs using Arduino MEGA (Read 430 times) previous topic - next topic

EddixNL

Hy All,

Currently, I am doing an architectural project on the TU Delft and for the first time ever I'm investigating the possibilities of electronics (Arduino) to create interactive architecture. The project that I am doing involves creating an urban furniture with interactive elements. The concept is to have multiple LDR sensors detect the movement of people by integrating them into the surface and then have the values translate into a certain color of light. The color of light will give feedback to the user about how crowded it is (or how high the density of users is). Since we will make a small 1 to 1 prototype I don't need hundreds of sensors nor LEDs, but I would like to use, let's say 5 RGB LEDs in combination with 10 or so LDR sensors.

I've purchased the Arduino MEGA since it has 15 PWM pins, which would in theory suit 5 RGB LEDs just fine. But because I can't find any examples of this I'm a little bit worried that it is not that easy. So, can I, without problem hookup 5 RGB LEDs to the Arduino MEGA?

- I did some research and noticed that creating more PWM pins on a standard Arduino by SoftWrite will cause problems when there is a delay in the code (which there will in my case).
- Secondly, I would like to avoid having to use an RGB strip, since I want more control on the placements of the LEDs. (The same goes for an LED matrix or NEO-pixel.
- If possible I also want to avoid having to use something like a multiplexing chip, I'm a little bit worried that I would make things overly complicated. But of course, if I have no other option then please tell.
- I've seen a lot of examples using transistors, can I avoid them with just 5 RGB LEDs? If not, which one should I use?

This is the list of parts I have at my disposal, as of now:
- Arduino MEGA
- 10 RGB LEDs common cathode (I already found that this would make thing complicated if I would use a multiplexer since most of these are sinking devices.)
- 10 LDR sensors
- Jumper wires (all kinds)
- multiple 1K resistors
- multiple 10K resistors
- multiple 220 resistors
- some 220k resistors
- Breadboard (both small and large)

Currently my setup uses 5 LDR sensors and 2 RGB LEDs (on a normal Arduino), it works just fine, but now, how would I expand my RGB LEDs the best way?

And this is my current code:
Code: [Select]

int LDRpin1 = A1;
  int LDRpin2 = A2;
  int LDRpin3 = A3;
  int LDRpin4 = A4;
  int LDRpin5 = A5;

  int LDRvalue1 = 1;
  int LDRvalue2 = 2;
  int LDRvalue3 = 3;
  int LDRvalue4 = 4;
  int LDRvalue5 = 5;

  int BlueLED = 9;
  int GreenLED = 10;
  int RedLED = 11;
  int BlueLED2 = 3;
  int GreenLED2 = 5;
  int RedLED2 = 6;

void setup()
{
  pinMode(BlueLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(RedLED, OUTPUT);
  pinMode(BlueLED2, OUTPUT);
  pinMode(GreenLED2, OUTPUT);
  pinMode(RedLED2, OUTPUT);
 
  int LDRpin1 = analogRead(A1);
  int LDRpin2 = analogRead(A2);
  int LDRpin3 = analogRead(A3);
  int LDRpin4 = analogRead(A4);
  int LDRpin5 = analogRead(A5);
  Serial.begin(9600);
}

void loop()
{
  LDRvalue1 = analogRead(LDRpin1);
  delay (100);
  Serial.print("Value LDR1 = ");
  Serial.println(LDRvalue1);
  int color = map(LDRvalue1, 0, 1023, 0, 255);
  Serial.print("value Map1 = ");
  Serial.println(color);
  LDRvalue2 = analogRead(LDRpin2);
  delay (100);
  Serial.print("Value LDR2 = ");
  Serial.println(LDRvalue2);
  int color2 = map(LDRvalue2, 0, 1023, 0, 255);
  Serial.print("value Map2 = ");
  Serial.println(color2);
  LDRvalue3 = analogRead(LDRpin3);
  delay (100);
  Serial.print("Value LDR3 = ");
  Serial.println(LDRvalue3);
  int color3 = map(LDRvalue3, 0, 1023, 0, 255);
  Serial.print("value Map3 = ");
  Serial.println(color2);
  LDRvalue4 = analogRead(LDRpin4);
  delay (100);
  Serial.print("Value LDR4 = ");
  Serial.println(LDRvalue4);
  int color4 = map(LDRvalue4, 0, 1023, 0, 255);
  Serial.print("value Map4 = ");
  Serial.println(color4);
  LDRvalue5 = analogRead(LDRpin5);
  delay (100);
  Serial.print("Value LDR5 = ");
  Serial.println(LDRvalue5);
  int color5 = map(LDRvalue5, 0, 1023, 0, 255);
  Serial.print("value Map5 = ");
  Serial.println(color5);
  delay(1000);
 
 

  if((LDRvalue1 >= 500) and (LDRvalue2 >= 500) and (LDRvalue3 >= 500) and (LDRvalue4 >= 500) and (LDRvalue5 >= 500))
  {
    analogWrite(GreenLED, 0);
    analogWrite(BlueLED, 10);
    analogWrite(RedLED, 0);
    analogWrite(GreenLED2, 0);
    analogWrite(BlueLED2, 10);
    analogWrite(RedLED2, 0);
  }
 
  if((LDRvalue1 <= 500) and (LDRvalue2 <= 500) and (LDRvalue3 <= 500) and (LDRvalue4 <= 500) and (LDRvalue5 <= 500))
  {
    analogWrite(GreenLED, 0);
    analogWrite(BlueLED, 0);
    analogWrite(RedLED, 50);
    analogWrite(GreenLED2, 0);
    analogWrite(BlueLED2, 0);
    analogWrite(RedLED2, 50);
    delay(200);
    analogWrite(GreenLED, 0);
    analogWrite(BlueLED, 0);
    analogWrite(RedLED, 0);
    analogWrite(GreenLED2, 0);
    analogWrite(BlueLED2, 0);
    analogWrite(RedLED2, 0);
    delay(200);
  }
 
  }


Thanks in advance!

Edwin

GrooveFlotilla

#1
Jan 26, 2018, 09:59 pm Last Edit: Jan 26, 2018, 10:11 pm by GrooveFlotilla
Why did you start out by assuming there would be delays in your code?

You best approach to expanding your code would be to learn about arrays, and forgetting that you ever heard about the delay function.
Edit: (after reading code) and also learn about the scope rules for C/C++
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

Wawa

...my setup uses 5 LDR sensors and 2 RGB LEDs (on a normal Arduino), it works just fine, but now, how would I expand my RGB LEDs the best way?
Replace the RGB LEDs with addressable LEDs.
Hundreds of them only use one Arduino pin.
Leo..

EddixNL

Why did you start out by assuming there would be delays in your code?
Thanks for the quick reply. I assumed the delays for multiple analog readings. I read that the readings can be off without delay. Furthermore, I want max density to result in a blinking led, therefore delay between on and off. I will try to avoid the delays as much as can. Second to that, thanks for the advice on C/C++ code, I will need to study about it a bit more, like I said, no experience in this yet.

Replace the RGB LEDs with addressable LEDs.
To establish a good feedback loop I would like to make the colors change gradually (fading) and use different colors all together. It can be my mistake to believe that this is only achievable with RGB LEDs but please tell if I'm incorrect.

Apart from these things please answer the initial question, can I, or can I not, hookup 5 RGB LEDs to an Arduino MEGA?

Thanks

Wawa

To establish a good feedback loop I would like to make the colors change gradually (fading) and use different colors all together. It can be my mistake to believe that this is only achievable with RGB LEDs but please tell if I'm incorrect.

can I, or can I not, hookup 5 RGB LEDs to an Arduino MEGA?
Addressable LEDs are just RGB LEDs with a control chip inside.
The result (light/colour/dimming/mixing) is exactly the same as connecting common RGB LEDs to PWM pins.

Yes, but there are pin current limitations for the Mega chip.
That could be 100mA per port (8 pins) or 200mA for a combination of ports.
See page 356 of the datasheet.
I think you're safe if you calculate the CL resistors for the LEDs for ~10mA max per colour.
Leo..

Go Up