• RGB LED strip. Connection diagram and LED strip control using Arduino

    Last time we looked at how to connect an LED strip to an Arduino via the L298 driver. Color management was carried out programmatically - the Random function. Now it’s time to figure out how to control the color of the LED strip based on the readings of the DHT 11 temperature and humidity sensor.

    The example is based on connecting an LED strip via the L298 driver. Plus, the example has added an LCD 1602 display, which will display the readings of the DHT 11 sensor.

    The project will require the following Arduino elements:

    1. Arduino UNO board.
    2. Display LCD 1602 + I2C.
    3. Temperature and Humidity Sensor DHT
    4. LED strip.
    5. Driver L298.
    6. Power supply 9-12V.
    7. Housing for Arduino and display (optional).

    First of all, let's look at the circuit diagram (Fig. 1). On it you can see how to connect all of the above elements. There is nothing complicated in assembling the circuit and connecting it, but it is worth mentioning one nuance that most people forget about and end up getting incorrect results LED work– tapes from Arduino.

    Figure 1. Schematic diagram Arduino connections and LED strip with DHT 11 sensor

    To avoid correct operation LED strip (flickering, color mismatch, incomplete glow, etc.), the power supply of the entire circuit must be made common, i.e. combine the GND (ground) pins of the Arduino controller and the L298 driver (LED strip). You can see how to do this in the diagram.

    A few words about connecting a humidity sensor. If you buy a bare DHT 11, without strapping, then between the first and second contacts, 5V and Data, respectively, you need to solder a resistor with a nominal value of 5-10 kOhm. The temperature and humidity measurement range is written on back side DHT 11 sensor housing. Temperature: 0-50 degrees Celsius. Humidity: 0-80%.


    Figure 2. Correct connection humidity sensor DHT 11

    After assembling all project elements according to the diagram, you need to write program code, which will make it all work the way we need it. And we need to led strip changed color depending on the readings of the DHT 11 sensor (humidity).

    To program the DHT 11 sensor, you will need an additional library.

    Code Arduino programs and RGB – tape. Changes the color of the tape depending on humidity.

    #include #include //library for working with LCD display 1602 #include //library for working with the humidity and temperature sensor DHT 11 int chk; //the variable will store all data from the DHT11 sensor int hum; //the variable will store humidity readings from the DHT11 sensor dht11 DHT; //object of type DHT #define DHT11_PIN 4 //Data pin of the DHT11 sensor is connected to input 4 #define LED_R 9 //pin for channel R #define LED_G 10 //pin for channel G #define LED_B 11 //pin for channel B / /variables will store color values ​​//when mixing all three colors the required color will be obtained int led_r=0, led_g=0, led_b=0; //declaring a display object with address 0x27 //don’t forget to use a display in the project via an I2C board LiquidCrystal_I2C lcd(0x27,16,2); void setup() ( //create a display lcd.init(); lcd.backlight(); // declare pins as outputs pinMode(LED_R, OUTPUT); pinMode(LED_G, OUTPUT); pinMode(LED_B, OUTPUT); ) void loop () ( chk = DHT.read(DHT11_PIN);//read data from the DHT11 sensor //output data to the display lcd.print("Temp: "); lcd.print(DHT.temperature, 1); lcd.print( " C"); lcd.setCursor(0,1); lcd.print("Hum: "); lcd.print(DHT.humidity, 1); /for correct operation of the sensor, a delay is required for polling lcd.clear(); hum = DHT.humidity; //take humidity readings //in the range from 19 to 30% humidity green if ((hum >= 19) && (hum<= 30)) { led_r = 1; led_g = 255; led_b = 1; } //в диапозоне от 31 до 40% влажности выдать красный цвет if ((hum >= 31) && (hum<= 40)) { led_r = 255; led_g = 1; led_b = 1; } //в диапозоне от 41 до 49% влажности выдать синий цвет if ((hum >= 41) && (hum<= 49)) { led_r = 1; led_g = 1; led_b = 255; } // подача сигналов цвета на выхода analogWrite(LED_R, led_r); analogWrite(LED_G, led_g); analogWrite(LED_B, led_b); }

    Tags: Tags

    Hello Habr community.

    Nowadays, LED strips with changeable glow colors have become available. They look cool, are not expensive and can be well suited for decorative interior lighting, advertising, etc.

    For such tapes you can buy a power supply, a dimmer, a dimmer with a control panel. This will allow you to use LED strip for illumination. However, if you want to record a color changing algorithm, or make control from a computer, then disappointment begins. You won’t find dimmers on sale that can be controlled via a COM port or Ethernet.

    Welcome to cat.

    Theoretical part

    To implement a smooth change in the glow of all 3 channels, we will need to make our own dimer. It’s very simple to make; all you need to do is take power switches and control them using a PWM signal. Also, our dimmer must be programmable and/or externally controllable.

    Arduino is ideal as a brain. Any algorithm for changing colors can be written into its program, and it can also be controlled using Arduino modules, or remotely via Ethernet, Infrared, Bluetooth, using the appropriate modules.

    To implement my plan, I chose Arduino Leonardo. It is one of the cheapest Arduino boards and it has many PWM pins.

    PWM: 3, 5, 6, 9, 10, 11, and 13. Provide 8-bit PWM output with the analogWrite() function.

    And so, we have a PWM source, we just need to come up with power switches. If you browse online stores, you will find out that there is no Arduino module for controlling RGB tape mi. Or just universal modules with power transistors. You can also find a huge number of sites for radio amateurs who make boards with power switches themselves.

    However, there is an easier way! The Arduino module for controlling the motors will help us out. This module has everything we need - powerful 12V switches are installed on it.

    An example of such a module is “L298N Module Dual H Bridge Stepper Motor Driver Board Modules for Arduino Smart Car FZ0407”. This module is based on the L298N chip, which consists of 2 bridges. However, bridge switching is useful for the motor (this can cause it to change the direction of rotation), and in the case of RGB tape, it is useless.

    We will not use all the functionality of this chip, but only its 3 lower keys, connecting the tape as shown in the figure.

    Practical part

    For implementation you will need an Arduino Leonardo, an L298N motor control module, a 12V source (for powering the tape), the RGB tape itself, and connecting wires.
    For ease of connection, I also used Fundruino IO Expansion, but it does not carry any functional load.

    The connection diagram is shown in the figure.

    I would like to further describe the power supply of the system. In this circuit, power is supplied to the motor control module, it contains a step-down power supply of 5V, and I supply this 5V to the Vin input of the Arduino power supply. If you break this connection (of course, leaving the grounds connected), then you can power the Arduino and the power switches from different power sources. This can be useful when a lot of things are connected to the Arduino, and the source in the motor control module cannot cope (it turns off due to overheating).

    The RGB strip is controlled using analogWrite commands, which configures the output to generate a PWM signal.

    Source code for the program for arduino:
    #define GRBLED_PIN_R 9 // pin for channel R #define GRBLED_PIN_G 10 // pin for channel G #define GRBLED_PIN_B 11 // pin for channel B int rgbled_r=0, rgbled_g=0, rgbled_b=0; void setup())( //enable serial datada print Serial.begin(9600); Serial.println("RBG LED v 0.1"); // RGBLED pinMode(GRBLED_PIN_R, OUTPUT); pinMode(GRBLED_PIN_G, OUTPUT); pinMode(GRBLED_PIN_B , OUTPUT); ) void loop())( // change color rgbled_r = (rgbled_r+1)%1024; rgbled_g = (rgbled_g+2)%1024; rgbled_b = (rgbled_b+3)%1024; // Output Z1_output_rgbled() ; delay(1); void Z1_output_rgbled() ( analogWrite(GRBLED_PIN_R, rgbled_r); analogWrite(GRBLED_PIN_G, rgbled_g); analogWrite(GRBLED_PIN_B, rgbled_b); )

    In the video you can see how it works:

    Economic part






















    L298N Module Dual H Bridge Stepper Motor Driver Board Modules for Arduino Smart Car FZ0407$ 5.31 1
    Leonardo R3 Development Board for Arduino Compatiblae + USB Cable Wire FZ0437$ 10.00 1
    5050 LED Strip RGB and single color 5M DC12V/24V 60leds/m Waterproof Flexible Car auto Strip Light saving light$ 12.38 1
    Retail AC85~265V to DC 12V/6A power supply adapter transformer switching for led light$ 9.98 1

    Total $37.65 = 1,300 rubles

    Instead of a conclusion

    For those who want to repeat the circuit described here, I would like to note that the L298N driver is designed for a current of 2-3A, and RGB LED strips based on 5050 LEDs with a density of 60 LEDs per meter, sold at 5 meters, can consume up to 6A. Therefore, if you want to use long and bright tapes, you may need to upgrade the circuit (connect the tape in segments, or take a more powerful driver) or use simpler tapes.


    On the eve of the New Year, I suggest you assemble a programmable RGB Christmas tree garland with the ability to create various patterns.

    What will you need for the garland?

    WS2811 RGB Full Color 12mm LED String DC 5V can be purchased on Aliexpress for $20. The usual end of one such garland can be connected to another to increase the length. This article is designed for building light patterns, so if you have a programmable LED garland with a different protocol on hand, you will need to rewrite the program and connect the garland in accordance with the datasheet.
    5 volt current source, designed for the current consumed by your garland. Usually the seller indicates the current consumed by the garland.
    Arduino of any version. The author used a standard Arduino Uno.
    Green acrylic paint
    Insulating tape
    Wire.
    It is advisable to have JST connectors to make it easier to connect the garland to the controller

    Before you start assembling your Christmas tree garland, make sure that all LEDs are working properly. On the Internet you can find how to connect the WS2811 to Arduino.

    Determine the +5V and GND pins from the datasheet for your WS2811
    Red = +5V
    Blue = GND
    White = Data

    The connection looks like in the picture.

    Install the popular WS2811 Arduino library from Adafruit. You can download and read the installation instructions here:
    Modify the attached code according to the length of your garland in #define LED_COUNT. Download and run the program on Arduino. Please note that the pixels change color from red to green to blue to white within 5 seconds. This ensures that all 3 LEDs inside the pixel are working.

    (downloads: 1085)

    Voltage drop estimation.

    Each LED pixel and subsequent connected LED garland will cause some kind of voltage drop. So after 50 LEDs in your LED garland, the voltage of the power supply will drop by some noticeable amount. For example, from 5V to 4.7V. This means that the next garland that you connect to the first one will be powered not from 5V, but from 4.7V, and the voltage after it will drop even lower. As a result, each LED will be darker than the previous one. Ultimately, when the voltage drops to 3.3V, the microcircuit that serves the WS2811 protocol simply stops working.

    Since each pixel has 3 LEDs and the white color of the garland means that all 3 LEDs glow equally, the voltage across it will drop more than if, for example, only red LEDs were lit. When starting the testing program, did you notice strong darkening at the ends of the garland? You can connect additional 5V power there. The author did this every 100 pixels

    Painting the garland.

    Normal Christmas lights are colored green to blend in with the tree. Your LED string light has wires of different colors. Hang the garland and paint the wires green with acrylic paint, this will take a little time. Wrap the WS2811 housings in black electrical tape, it will be faster than painting them.


    Determining the X and Y position of each pixel

    Hang the assembled garland on the Christmas tree. You can then calculate the X and Y position of each pixel and insert this data into the program code. To do this, use this code file. Uncomment the first function loop(), which lights up sections of 10 LEDs. If you have more than 50 LEDs, then you can extend this section by simply copying, remembering to indicate the required number in #define LED_COUNT

    Try to overlay the grid so that the bottom left LED falls into cell 1.1. This is done so that the program can determine the middle of the tree in both X and Y directions. Entering X and Y coordinates is a manual process, you will get each coordinate by watching the video. 200 coordinates sounds, of course, intimidating, but it will take about 20 minutes.

    You can print out the grid and attach it to your computer monitor or phone display so you don't have to bother with video editors.

    The attached file, just like the previous file, is an example of code that iterates through various patterns, as in the video.

    (downloads: 1240)

    From the video you can understand the logic of the program and write your own templates or change the code to suit your needs. The other file attached is a setup file to allow the Arduino to be controlled via serial interface from another device. The author used a Raspberry Pi to control an Arduino.

    Here's a New Year's garland using Arduino and the WS2811 kit.

    (downloads: 1132)

    In this article we will talk about color LEDs, the difference between a simple RGB LED and an addressable one, and add information about the areas of application, how they work, how control is carried out with schematic pictures of connecting LEDs.

    LEDs are an electronic component capable of emitting light. Today they are widely used in various electronic equipment: flashlights, computers, household appliances, cars, phones, etc. Many microcontroller projects use LEDs in one way or another.

    They have two main purposes:

    Demonstration of equipment operation or notification of any event;
    use for decorative purposes (lighting and visualization).

    Inside, the LED consists of red (red), green (green) and blue (blue) crystals assembled in one housing. Hence the name – RGB (Fig. 1).

    2. Using microcontrollers

    With it you can get many different shades of light. The RGB LED is controlled using a microcontroller (MK), for example, Arduino (Fig. 2).

    Of course, you can get by with a simple 5-volt power supply, 100-200 Ohm resistors to limit the current and three switches, but then you will have to control the glow and color manually. In this case, it will not be possible to achieve the desired shade of light (Fig. 3-4).

    The problem arises when you need to connect hundreds of colored LEDs to the microcontroller. The number of pins on the controller is limited, and each LED needs power from four pins, three of which are responsible for color, and the fourth pin is common: depending on the type of LED, it can be an anode or cathode.

    3. Controller for RGB control

    To unload the MK terminals, special controllers WS2801 (5 volts) or WS2812B (12 volts) are used (Fig. 5).

    With the use of a separate controller, there is no need to occupy several MK outputs; you can limit yourself to only one signal output. The MK sends a signal to the “Data” input of the WS2801 LED control controller.

    This signal contains 24-bit information about color brightness (3 channels of 8 bits for each color), as well as information for the internal shift register. It is the shift register that allows you to determine which LED the information is addressed to. In this way, you can connect several LEDs in series, while still using one pin of the microcontroller (Fig. 6).

    4. Addressable LED

    This is an RGB LED, only with an integrated WS2801 controller directly on the chip. The LED housing is made in the form of an SMD component for surface mounting. This approach allows you to place the LEDs as close to each other as possible, making the glow more detailed (Fig. 7).

    In online stores you can find addressable LED strips, where up to 144 pieces fit in one meter (Fig. 8).

    It is worth considering that one LED consumes only 60-70 mA at full brightness; when connecting a strip, for example, with 90 LEDs, you will need a powerful power supply with a current of at least 5 amperes. Under no circumstances power the LED strip through the controller, otherwise it will overheat and burn out from the load. Use external power supplies (Fig. 9).

    5. Lack of addressable LEDs

    The addressable LED strip cannot operate at too low temperatures: at -15 the controller begins to malfunction; in severe frosts there is a high risk of its failure.

    The second drawback is that if one LED fails, all the others along the chain will also refuse to work: the internal shift register will not be able to transmit information further.

    6. Application of addressable LED strips

    Addressable LED strips can be used for decorative lighting of cars, aquariums, photo frames and paintings, in room design, as New Year's decorations, etc.

    An interesting solution is obtained if an LED strip is used as an Ambilight backlight for a computer monitor (Fig. 10-11).

    If you use Arduino-based microcontrollers, you will need the FastLed library to simplify working with LED strip ().

    An RGB controller is used to control these devices. But, besides it, in recent years the Arduino board has been used.

    Arduino - operating principle

    Arduino board

    An Arduino board is a device on which a programmable microcontroller is installed. Various sensors, controls or encoder are connected to it and, according to a given sketch (program), the board controls motors, LEDs and other actuators, including other Arduino boards via the SPI protocol. The device can be controlled via a remote control, Bluetooth module, HC-06, Wi-Fi, ESP or internet, and buttons. Some of the most popular boards are Arduino Nano and Arduino Uno, as well as Arduino Pro Mini - a device based on the ATmega 328 microcontroller


    Appearance of Arduino Pro Mini
    Appearance of Arduino Uno
    Appearance of Arduino micro

    Programming is carried out in the open source Arduino environment installed on a regular computer. Programs are downloaded via USB.

    The principle of load control via Arduino


    Arduino control

    The board has many outputs, both digital, having two states - on and off, and analog, controlled via a PWM controller with a frequency of 500 Hz.

    But the outputs are designed for a current of 20 - 40 mA with a voltage of 5 V. This is enough to power an RGB indicator LED or a 32x32 mm matrix LED module. For a more powerful load this is not enough.

    To solve this problem in many projects you need to connect additional devices:

    • Relay. In addition to individual relays with a supply voltage of 5V, there are entire assemblies with different numbers of contacts, as well as with built-in starters.
    • Amplifiers based on bipolar transistors. The power of such devices is limited by the control current, but you can assemble a circuit from several elements or use a transistor assembly.
    • Field-effect or MOSFET transistors. They can control loads with currents of several amperes and voltages up to 40 - 50 V. When connecting the mosfet to PWM and an electric motor or other inductive load, a protective diode is needed. When connecting to LEDs or LED lamps, this is not necessary.
    • Expansion cards.

    Connecting LED strip to Arduino


    connecting LED strip to Arduino

    Expert opinion

    Alexey Bartosh

    Specialist in repair and maintenance of electrical equipment and industrial electronics.

    Ask a question to an expert

    Arduino Nanos can control more than just electric motors. They are also used for LED strips. But since the output current and voltage of the board are not sufficient to directly connect a strip with LEDs to it, additional devices must be installed between the controller and the LED strip.

    Via relay


    Connection via relay

    The relay is connected to the device via a digital output. The strip controlled with it has only two states - on and off. To control the red-blue-green ribbon, three relays are needed. The current that such a device can control is limited by the power of the coil (a low-power coil is not able to close large contacts). To connect more power, relay assemblies are used.

    Using a bipolar transistor


    Connection using a transistor

    A bipolar transistor can be used to amplify the output current and voltage. It is selected based on load current and voltage. The control current should not be higher than 20 mA, therefore it is supplied through a current-limiting resistance of 1 - 10 kOhm.

    It is better to use a transistor n-p-n with a common emitter. For a higher gain, a circuit with several elements or a transistor assembly (amplifier microcircuit) is used.

    Using a field effect transistor

    In addition to bipolar ones, field-effect transistors are used to control the strips. Another name for these devices is MOS or MOSFET-transistor.

    Such an element, unlike a bipolar one, is controlled not by current, but by voltage at the gate. This allows the low gate current to drive large load currents—up to tens of amperes.

    The element is connected through a current-limiting resistance. In addition, it is sensitive to noise, so the controller output should be connected to ground with a 10 kOhm resistor.

    Using expansion cards


    Connecting Arduino using expansion boards

    In addition to relays and transistors, ready-made blocks and expansion boards are used.

    This could be Wi-Fi or Bluetooth, a motor control driver such as the L298N module, or an equalizer. They are designed to control loads of different power and voltage. Such devices are single-channel - they can only control a monochrome strip, and multi-channel - designed for RGB and RGBW devices, as well as strips with WS 2812 LEDs.

    Example program


    Arduino and LED strip

    Arduino boards are capable of controlling LED structures according to predefined programs. Their libraries can be downloaded from the official website, found on the Internet, or written a new sketch (code) yourself. You can assemble such a device with your own hands.

    Here are some options for using such systems:

    • Lighting control. Using a light sensor, the light in the room turns on both immediately and with a gradual increase in brightness as the sun sets. Switching on can also be done via wi-fi, with integration into the “smart home” system or connection by telephone.
    • Turning on the light on the stairs or in a long corridor. The LED lighting of each step separately looks very nice. When a motion sensor is connected to the board, its activation will cause a sequential, time-delayed switching on of the lighting of the steps or corridor, and turning off this element will lead to the reverse process.
    • Color music. By applying an audio signal to the analog inputs through filters, the output will be a color and music installation.
    • Computer modding. With the help of appropriate sensors and programs, the color of the LEDs can depend on the temperature or load of the processor or RAM. This device operates using the dmx 512 protocol.
    • Controlling the speed of running lights using an encoder. Similar installations are assembled on WS 2811, WS 2812 and WS 2812B microcircuits.

    Video instructions