This is a LED, with several colors (RGB). You can make very beautiful and attractive light patterns with LEDs. The main difference between other RGB LED is that it uses just 1 PIN, thus allowing more hardware interface on your projects. City never hates dark nights, she lights them. What can you do in this lesson? 1.Learn something about the principle of WS2812, a kind of RGB LED 2.Learn how to use a library, and control the color of your LED 3.Display the state of your robot through LED
Upload your Code
Put the folder into the path the picture below shows。
1)Where to put the library
2)Check the library path
Open “rgb.ino”，upload the code:
Now check your LED, what will you see?
Import the library #include <Adafruit_NeoPixel.h> Define a class Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800); Init the LED strip.begin(); Color change function rainbow(20); Number i LED change color into Wheel（（i+i）&255）） strip.setPixelColor(i, Wheel((i+j) & 255));
RGB is a kind of standard mode of color control, it gives different color through different rate of Red, Green and Blue. And these three kinds of color can almost constitute all color we see, so this system is the most popular system about color constitution. Common RGB LEDs usually have 4 pins, 3 pins control 3 kinds of color and another pin for power supply. The picture below shows a kind of common cathode LED, you can see how a RGB LED is born.
4)How a RGB LED be born
So how to control color? Common RGB LED packaged three leds into one, so just consider that you are using 3 leds. And you control the color use the picture below:
5)Mixture of color
5-1 Color Mixture
Chart 5-1 gives several samples of how to get yellow or pink and so on, and you can get much more color with different value of different color, the mount of patterns can be 255*255*255=16777216.
6)Common RGB LED
The LED in the robot is also like this but you need a little more code. So why? Every LED we see upon needs three pins to control, if I need 2 LED shows different color, I must give 6 pins for control, but if I need 3 or 4, that seems insupportable.
7)WS2812 LED Strip
But you don’t worry about that, in the robot, there is only one LED but only occupy one pin. And if you want to control more in the future, just add the amount of LED you can also control them use one pin! That sounds wonderful. Every LED contains a chip inside, which receives commands and transmits them to the next LED, so you just need to tell which one should be which color, all the things will be OK. The chip on the LED will do the work for you.
This is the schematic of WS2812, it is so simple just need two devices outside：
The pin with “RGB” (DI) connects with Arduino, and DO is used to transmit the command to the next LED.
You may be confusing that how messages transmit in one wire. You can search “one-wire” in internet for more details. You can also learn how to use DS18B20 which is a temperature sensor, thus you can know it better.