This tutorial tells about how to light an RGB LED in accordance with a PIR motion sensor. The RGB LED can display a vast array of colors. By following this tutorial, you can realize the scenario that if the PIR detects nobody around, the LED will be on in a smooth and colorful pattern. When movement is reported by the PIR, the LED will change into a lively pattern. This is part of the project our group are working on, which is to convert a passive and invisible-at-night trash bin into a responsive one.
To begin with, you need
- Arduino Kit
- Breadboard
- Jumper Wires
- 3 * 330 Ohm Resistors
- 1 * RGB LED (or more)
- 1 * PIR Motion Sensor (or more)
Then make sure you have assembled them correctly.
Note that the pins of an RGB LED are ordered RED, COMMON (the longest), GREEN, BLUE. In the circuit, connect COMMON pin to GND. Connect each of the RED, GREEN and BLUE to a 330 Ohm resistor. Connect the other end of each resistor to Arduino digital pin 9, 10, and 11 respectively.
The PIR has three pins: UCC, OUT, and GND. Connect UCC to 5V, GND to GND, and OUT to a digital pin. Here we use pin 2.
Now let’s look at the code:
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 |
unsigned long pausetime = 1000; //the number of milliseconds the sensor has to be low before //we assume that the detected human being is not just a passer-by but a litterer. unsigned long starttime; const int pirPin = 2; //the digital pin connected to the PIR sensor's output const int RED_PIN = 9; const int GREEN_PIN = 10; const int BLUE_PIN = 11; int pirState = LOW; //to begin with, assume that no motion is detected. int val = 0; // use this variable to read input value void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); digitalWrite(pirPin, LOW); starttime = millis(); //get the current time } void loop() { val = digitalRead(pirPin); if (val == HIGH) { if ((millis()-starttime) > pausetime) { if (pirState == LOW) { // check if the input is HIGH Serial.print("Motion detected at "); Serial.print(millis()); Serial.println("ms"); // Print out the change and the time it happens. pirState = HIGH; } SBhere(); // Change the RGB lighting pattern to "Somebody is Here" starttime = millis(); } } else { if ((millis()-starttime) > pausetime) { if (pirState == HIGH){ Serial.print("Motion ended at "); Serial.print(millis()); Serial.println("ms"); pirState = LOW; } NBhere(); // Change the RGB lighting pattern to "Nobody is Here" starttime = millis(); } } |
Here I use the “pausetime” to distinguish those who approach the trash bin intentionally from other passers-by. Only if the movement has been detected for a while, which means the person has been staying around for a while, that the movement will be reported, and the light pattern be changed.
Below are the two functions we called in “loop()”. Each of the functions instruct the RGB LED to display colors in a specific way.
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 |
void NBhere() { int x; for (x = 0; x < 768; x++) // Each time we loop (with a new value of x), do the following: { showRGB(x); // Call RGBspectrum() with our new x delay(50); // Delay for 50 ms } } void showRGB(int color) { int redIntensity; int greenIntensity; int blueIntensity; if (color <= 255) { redIntensity = 255 - color;// red goes from on to off greenIntensity = color;// green goes from off to on blueIntensity = 0;// blue is always off } else if (color <= 511) { redIntensity = 0;// red is always off greenIntensity = 255 - (color - 256);// green on to off blueIntensity = (color - 256);// blue off to on } else //color > = 512 { redIntensity = (color - 512);// red off to on greenIntensity = 0;// green is always off blueIntensity = 255 - (color - 512);// blue on to off } analogWrite(RED_PIN, redIntensity); analogWrite(BLUE_PIN, blueIntensity); analogWrite(GREEN_PIN, greenIntensity); } |
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 |
void SBhere() { int delaytime = 500; // Off (all LEDs off): digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, LOW); delay(delaytime); // Red (turn just the red LED on): digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, LOW); delay(delaytime); // Purple (turn red and blue on): digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, HIGH); delay(delaytime); // Cyan (turn green and blue on): digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); digitalWrite(BLUE_PIN, HIGH); delay(delaytime); // Blue (turn just the blue LED on): digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, HIGH); delay(delaytime); } |
When it is dark in the street, the LED will be on. Without anyone approaching the bin to throw trash, the LED smoothly displays a rainbow of colors. When somebody is approaching, it will change into blinking.
2 replies on “Changing RGB LED Display In Accordance with PIR Sensor”
Hello!
I am an artist working on a project with a colleague of mine and we are interested in using this Arduino/LED/PIR combo in an installation. However, we are complete novices in this field and thus are having trouble getting the code to work.
Basically, we don’t know how to combine the main piece of code with the two functions that follow it. When we copy and paste them all into the Arduino program we get an error saying “SBhere (); was not declared in this scope.” Is there something we are missing? Would you be able to help us with our project?
Thank you for taking the time to read this email!
Sutton
Hi!
I’m a student new to coding and trying out in this project as I’m planning to make a small artwork with it. But I’m also having issues with the SBhere() and NBhere(); not declared error, even though I followed the instructions. I hope you would be able to help!
Thanks!!