I wanted to build a desktop widget device, using a single RGB LED placed in a ping pong ball acting as a diffuser. Make sure the baud rate is set 9600 to correctly read the messages.This is a yet another contribution to the already extensive literature that surrounds RGB LEDs and changing colors, but in all my research I have not seen the method I have implemented, so here it is in case someone else can use it or similar algorithm. To check the messages in the serial monitor press Ctrl Shift M in the Arduino IDE, or click the magnifying glass icon. Serial.print("\\tB: ") Serial.println(blueVal) Serial.print("\\tG: ") Serial.print(greenVal) Serial.print("R: ") Serial.print(redVal) Here I print the letter corresponding to each component in the RGB LED and its written value.I used escape character \t to create a tab spacing between each of the colors, escape characters follow a backslash and don’t show up on the printed string, as they perform special functions.Īlso, the last serial function is Serial.println(), this ensures that the next serial message appears in a new line. For this we will use the Serial.print() function. delay(50) įinally, to check on what RGB values are being shown on the LED we will print them in the serial monitor.įor the sake of clarity, these will be printed in a single line. You can adjust this delay as you like, just note that too big of a delay will make the RGB values update slower too. This delay last 50 milliseconds: 50 thousandths of a second or 0.05 seconds. Next I introduce a small delay to the code, I do this to prevent the serial monitor from scrolling too fast while still letting the RGB LED feel responsive to inputs. Sets the individual colors with the pot values In this step I use the PWM outputs of the Arduino to light up the red, green and blue pins on the RGB led with the integer values obtained from the potentiometers, you just need to specify the pin and the number between 0 and 255. When the pot is in the leftmost position, the voltage reading is 0V Unfortunately, to control the LEDs with the analogWrite() function we need to feed it values between 0 and 255, to do this we divide the read value by 4 and assign it to our integers. The first step is to define the relevant pins, this is done through the #define statement which will replace the defined keyword with our desired value when the program compiles. > Read our guide about What You Can Build with Adruino. Serial.print("\tB: ") Serial.println(blueVal) Final value is Serial.println to create a new line between readings Serial.print("\tG: ") Serial.print(greenVal) The \t character creates a tab space between colors Printing the values of Red, Green and Blue in a single line When the pot is in the leftmost position, the voltage reading is 0 Int redVal, greenVal, blueVal //RGB component values *Example sketch to control an RGB LED with Arduino using potentiometers You can upload the following code through the Arduino IDE, you can copy the code by clicking on the button in the top right corner of the code field. You can also find RGB LEDs in KY-016 modules, which have the LED and necessary resistors in a single PCB. is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on. I’ll show you step-by-step wiring diagrams and explain the code so you can fully understand how to use this component.Īfter this tutorial, you should have a circuit that shines a light in your preferred color using RGB values, which are controlled by potentiometers and shown in the Serial Monitor. This article will guide you through the basics of using an RGB LED in Arduino, so you can create your own colors with the help of potentiometers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |