• Connecting an RGB LED to Arduino. RGB LEDs: Addressable LED Strip

    Arduino is ideal for controlling any devices. The ATmega microprocessor uses a sketch program to manipulate a large number discrete outputs, analog-digital inputs/outputs and PWM controllers.

    Due to the flexibility of the code, the ATmega microcontroller is widely used in various automation modules, including on its basis it is possible to create an LED lighting control controller.

    The principle of load control via Arduino

    The Arduino board has two types of output ports: digital and analog (PWM controller). A digital port has two possible states: logical zero and logical one. If you connect an LED to it, it will either glow or not.

    The analog output is a PWM controller, to which a signal with a frequency of about 500 Hz is supplied with an adjustable duty cycle. What a PWM controller is and the principle of its operation can be found on the Internet. Through analog port It is possible not only to turn the load on and off, but also to change the voltage (current) on it.

    Command Syntax

    Digital output:

    pinMode(12, OUTPUT);— set port 12 to be the data output port;
    digitalWrite(12, HIGH);— we apply a logical one to discrete output 12, lighting the LED.

    Analog output:

    analogOutPin = 3;– set port 3 to output an analog value;
    analogWrite(3, value);– we generate a signal at the output with a voltage from 0 to 5V. The value is the duty cycle of the signal from 0 to 255. At a value of 255, the maximum voltage.

    Ways to control LEDs via Arduino

    Only a weak LED can be connected directly through the port, and even then it is better through a limiting resistor. Trying to connect a more powerful load will damage it.

    For more powerful loads, including LED strips, an electronic switch – a transistor – is used.

    Types of transistor switches

    • Bipolar;
    • Field;
    • Composite (Darlington assembly).
    Load connection methods
    Through bipolar transistor Via field effect transistor Via voltage switch

    When a high logic level is applied (digitalWrite(12, HIGH);) through the output port to the base of the transistor, the reference voltage will flow to the load through the collector-emitter chain. This way you can turn the LED on and off.

    A field-effect transistor works in a similar way, but since instead of a “base” it has a drain, which is controlled not by current, but by voltage, a limiting resistor in this circuit is not necessary.

    The bipolar view does not allow you to regulate powerful loads. The current through it is limited to 0.1-0.3A.

    Field-effect transistors operate with more powerful loads with currents up to 2A. For even more powerful load use field effect transistors Mosfet with current up to 9A and voltage up to 60V.

    Instead of field ones, you can use a Darlington assembly of bipolar transistors on ULN2003, ULN2803 microcircuits.

    ULN2003 chip and circuit diagram of an electronic voltage switch:

    The principle of operation of a transistor for smooth control of an LED strip

    A transistor works like a water faucet, only for electrons. The higher the voltage applied to the base of the bipolar transistor or the drain of the field effect transistor, the lower the resistance in the emitter-collector circuit, the higher the current passing through the load.

    Having connected the transistor to the Arduino analog port, we assign it a value from 0 to 255, and change the voltage supplied to the collector or drain from 0 to 5V. The collector-emitter circuit will pass from 0 to 100% of the load reference voltage.

    To control LED arduino tape it is necessary to select a transistor of suitable power. The operating current for powering the LED meter is 300-500mA; a power bipolar transistor is suitable for these purposes. For longer lengths, a field effect transistor will be required.

    Scheme LED connections Arduino tapes:

    Controlling RGB strip with Andurino

    In addition to single-chip LEDs, Arduino can also work with color LEDs. By connecting the pins of each color to the analog outputs of Arduino, you can arbitrarily change the brightness of each crystal, achieving the desired color of glow.

    Connection diagram for Arduino RGB LED:

    The Arduino RGB strip control is constructed similarly:

    It is better to assemble the Arduino RGB controller using field-effect transistors.

    For smooth control brightness two buttons can be used. One will increase the brightness of the glow, the other will decrease it.

    Arduino LED strip brightness control sketch

    int led = 120; install intermediate level brightness

    void setup() (
    pinMode(4, OUTPUT); set the 4th analog port to output
    pinMode(2, INPUT);

    pinMode(4, INPUT); set the 2nd and 4th digital ports to input for polling buttons
    }
    void loop()

    button1 = digitalRead(2);

    button2 = digitalRead(4);
    if (button1 == HIGH) Pressing the first button will increase the brightness
    {
    led = led + 5;

    analogWrite(4, led);
    }
    if (button2 == HIGH) pressing the second button will decrease the brightness
    {
    led = led - 5;

    analogWrite(4, led);
    }

    When you hold down the first or second button, the voltage supplied to the control contact smoothly changes electronic key. Then there will be a smooth change in brightness.

    Arduino control modules

    To create a full-fledged LED strip control driver, you can use sensor modules.

    IR control

    The module allows you to program up to 20 commands.

    The signal radius is about 8m.

    The price of the set is 6 USD.

    By radio channel

    Four-channel unit with a range of up to 100m

    The price of the set is 8 USD.

    Allows you to turn on the lighting when approaching the apartment.

    Contactless

    The distance sensor is capable of increasing and decreasing the brightness of the lighting by moving your hand.

    Range of action up to 5m.

    Module price 0.3 USD

    LED RGB tape It is a flexible tape with conductors and RGB LEDs (full color) printed on it. IN lately LED strips are widely used in architecture, auto and motorcycle tuning, costumes, decorations, etc. There are also waterproof tapes that can be used, for example, in swimming pools.

    LED strips come in two types: analog and digital.
    In analog strips, all LEDs are connected in parallel. Therefore, you can set the color of the entire LED strip, but you cannot set specific color for a specific LED. These tapes are easy to connect and not expensive.
    Digital LED strips are a little more complicated. An additional microcircuit is installed for each LED, which makes it possible to control any LED. Such tapes are much more expensive than regular ones.

    In this article we will consider working only with analog LED strips.

    Analog RGB LED strips

    Data sheet:
    - 10.5mm width, 3mm thickness, 100mm length of one segment
    - waterproof
    - 3M tape on the bottom
    - max. current consumption (12V, white) - 60mA per segment
    - glow color (wavelength, nm): 630nm/530nm/475nm

    RGB LED strip circuit diagram

    The tape is supplied in rolls and consists of sections 10 cm long. Each section contains 3 RGB LEDs, size 5050. Each section turns out to contain 9 LEDs: 3 red, 3 green and 3 blue. Section boundaries are marked and contain copper pads. Therefore, if necessary, the tape can be cut and easily soldered. LED strip diagram:

    Energy consumption

    In each section of the tape, 3 LEDs are connected in series, so 5V power is not suitable. The power supply should be 12V, but you can also supply 9V, but then the LEDs will not burn so brightly.

    One segment LED line consumes approximately 20mA when supplied at 12V. That. if you turn on the white color (i.e. red 100%, green 100% and blue 100%), then the power consumption of the section will be about 60mA.

    Now, you can easily calculate the current consumption of the entire tape. So, the length of the tape is 1 meter. The tape has 10 sections (10 cm each). The tape consumption with white color will be 60mA*10=600mA or 0.6A. If you use a PWM fade effect between colors, the power consumption can be halved.

    Connecting the tape

    In order to connect the tape, you need to solder the wires to the 4 contact pads. We used a white wire for +12V, and the rest of the colors according to the colors of the LEDs.

    Cut off protective film at the end of the tape. From which side the connection will be made is not important, because... symmetrical tape.

    Strip the insulation layer to expose the contact pads.

    Tin them.

    Solder four wires. It is better to use multi-core wire (for example PV3 or PVS cable), it is more flexible.

    To protect against water and external influences, you can use heat-shrink tubing. If led strip will be used in a humid environment, then in addition, the contacts can be coated with silicone.

    Working with LED strip

    The tape can easily be used with any microcontroller. To control LEDs, it is recommended to use pulse width modulation (PWM). Do not connect the tape pins directly to the MK pins, because This is a large current load and the controller may burn out. It's better to use transistors.

    You can use NPN transistors or better yet N-channel mosfets. When selecting a transistor, do not forget that the maximum switching current of the transistor must be taken with a reserve.

    Connecting LED strip to Arduino controller

    Let's look at an example of connecting an LED strip to a popular one. To connect, you can use inexpensive and popular mosfets. You can also use conventional bipolar transistors, for example TIP120. But compared to the mosfet, it has more voltage loss, so it is still recommended to use the former.
    The diagram below shows RGB connection LED strip when using N-channel mosfets. The mosfet gate is connected to pin1 of the controller, the drain to pin2 and the source to pin3.

    Below is a connection diagram when using conventional bipolar transistors (for example TIP120). The base of the transistor is connected to pin1 of the controller, the collector to pin2 and the emitter to pin3. A resistor with a resistance of 100-220 Ohms must be placed between the base and the controller output.

    TO Arduino controller connect a power source with a voltage of 9-12 Volts, and +12V from the LED strip must be connected to the Vin pin of the controller. You can use 2 separate power supplies, just do not forget to connect the grounds of the source and the controller.

    Example program

    To control the tape, the PWM output of the controller will be used, for this you can use the analogWrite() function for pins 3, 5, 6, 9, 10 or 11. With analogWrite(pin, 0) the LED will not light up, with analogWrite(pin, 127 ) the LED will burn at full intensity, and with analogWrite(pin, 255) the LED will burn at maximum brightness. Below is an example sketch for Arduino:

    #define REDPIN 5 #define GREENPIN 6 #define BLUEPIN 3 #define FADESPEED 5 // the higher the number, the slower the fade effect will be void setup() ( pinMode(REDPIN, OUTPUT); pinMode(GREENPIN, OUTPUT); pinMode(BLUEPIN , OUTPUT); ) void loop() ( int r, g, b; // fade from blue to purple for (r = 0; r 0; b--) ( analogWrite(BLUEPIN, b); delay(FADESPEED); ) // fade from red to yellow for (g = 0; g 0; r--) ( analogWrite(REDPIN, r); delay(FADESPEED); ) // fade from green to teal for (b = 0; b 0; g--) ( analogWrite(GREENPIN, g); delay(FADESPEED); ) )

    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. Connected to it various sensors, controls or encoder 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 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– device based on ATmega 328 microcontroller


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

    Programming is carried out in the Arduino environment with open source code, installed on 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 quantities 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 to other inductive load, you need protection diode. 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 insufficient for direct connection strips with LEDs attached to it, then 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 common emitter. For higher gain, use a circuit with multiple elements or transistor assembly(amplifier microcircuit).

    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 different power and tension. 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 designs in advance given programs. You can download their libraries from the official website, find them on the Internet, or write 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 system " smart home» 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 feeding to analog inputs beep through filters, the output is a color-music installation.
    • Computer modding. With the help of appropriate sensors and programs, the color of the LEDs can depend on temperature or processor load, 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

    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 connecting Arduino and LED strip with DHT 11 sensor

    To avoid correct operation LED strip (flickering, color mismatch, incomplete glow, etc.), the power supply for 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 the LED strip to change 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


    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. After this, you can 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)