Pulse phase control on arduino. Arduino Controlled Light Dimmer

Arduino controlled energy!

Attention!

Are available

Buy wholesale

Do you love programming microcontrollers !? Now you can easily solve the tasks of power control in the 220V AC network. We made a power regulator that can be easily connected to a microcontroller, for example, an Arduino board. Just connect the PWM output of the microcontroller with our power regulator and programmatically control electrical appliances: smoothly turn on the electric motors, adjust the heating temperature and much more.

Attention!

In some instructions for the device, a typo was made in the connection diagram. Due to the impossibility of replacing the instructions in all available packages, please use the diagram from the site when connecting.

Specifications

Features:

  • Powered by microcontroller control board
  • The ability to adjust the speed of asynchronous motors.
  • Does not interfere with the 220V network.

Principle of operation

Classic triac power regulator with analog control input, compatible with PWM - microcontroller outputs. The analog part of the module is powered by your microcontroller board (+ Vcc). Permissible supply voltage +3.3 ... + 5.0 V. When the signal at the analog input of the regulator changes from 0 to + Vcc, the power in the load changes from 0 to 100%

Functions

  • load power control

Additional Information

Draw your attention to!

When operating the module with a load of more than 3000 W, it is necessary to reinforce the PCB tracks leading to the power element. For this, it is necessary to solder a single-core mounting wire with a diameter of 1.5-2 mm from the contacts of the LOAD terminal to the contacts of the POWER element terminals.

The module is designed to work with a 300 Hz PWM signal.

Check the module is working
Remove the jumper.



Close the + VCC and control contacts with a jumper. In this case, the brightness of the lamp should increase.
Close the GND and control contacts with a jumper. In this case, the brightness of the lamp should decrease.

Scheme

Contents of delivery

  • mP248 module - 1 pc. PC.
  • Manual - 1 pc. PC.

Preparation for operation

  • ATTENTION! Observe safety precautions when working with high voltage before switching on.
  • Remove the jumper.
  • Connect the incandescent lamp to the module.
  • Connect the 5V supply for the low voltage part of the circuit.
  • Connect the 220V power supply for the high-voltage part of the circuit.
  • Short circuit the + VCC contacts and control. In this case, the brightness of the lamp should increase.
  • Short circuit the GND pins and the control. In this case, the brightness of the lamp should decrease.
  • When the jumper is installed, inversion of control is activated.
  • Verification completed, pleasant operation.

Precautions

  • Observe the rules for working with high voltage.
  • Make connections only with a securely de-energized network.
  • Place the device in a housing such as a sufficiently sized plastic back box before use.
  • Provide sufficient natural ventilation and cooling of the module, especially when power management is over 100W

Maintenance

  • Dangerous high voltages are present on the board. Observe safety measures when working with electrical installations.

Questions and answers

  • Good afternoon! I understand correctly that power control is possible both using PWM and an analog signal, while the duty cycle of the PWM output signal to the triac will correspond to the level of the analog signal as a percentage of the supply voltage?
    • This module was designed to work with an Arduino PWM 300 Hz. We haven't tried it with others. But, in principle, it should work, because the analog signal is taken as a basis, and not the pulse width and frequency.
  • Good afternoon! Is it possible with this device with Arduino to control the speed of a 12V DC motor (car stove fan)
    • The device is designed to control the load only in the AC 220V circuit.
  • In my regulator, no load is 190 V, and under load, a light bulb, the spiral barely glows.
    • Check the presence of a resistor R10 - 10kOhm and an electrolytic capacitor C5 - 100mkFx16V, if possible, install them and check the operation of the device.
  • Good afternoon. I purchased this regulator from you. But for some reason the jumper to the left of the control input is not wired and there is no capacitor at the input. And until he bridged the jumper, the regulator did not react to control in any way. There is not a word about this in the instructions. Why?
    • You need to install C5 - 100mkFx16V, R10 - 10kOhm. If this is not possible, hand it over to the place of purchase on the basis of it, a repair or replacement with a new module will be performed .. ru they will prompt your actions.
  • Good afternoon. Can this regulator be used like others with a variable resistor? Or do you still need 5 V and jumpers (buttons) for the control contact to control?
    • That's right, to use in dimmer mode, you need an external 5V power supply and a button.

An Arduino-based dimmer is one of hundreds of simple and interesting devices with which you can smoothly change the mains voltage from 0 to the nominal value. Each Arduino user will find use for such a useful homemade product, and the experience gained during the assembly with his own hands will replenish the knowledge base.

Scheme and principle of its work

Like most inexpensive dimmers, this circuit works by phase adjustment of the voltage, which is achieved by forcibly opening the power switch - the triac. The principle of the circuit is as follows. Arduino at the software level generates pulses, the frequency of which is adjusted by the resistance of the potentiometer. The control pulse from the P1 pin passes through the MOC3021 optocoupler and goes to the control electrode of the triac. It opens and passes current until the half-wave of the mains voltage crosses zero, after which it closes. Then the next impulse comes and the cycle repeats. Due to the shift of the control pulses, the edge-cut part of the sinusoid is formed in the load.

In order for the triac to open in accordance with the specified algorithm, the pulse repetition rate must be synchronized with the 220 V mains voltage. In other words, the Arduino must know at what moment the mains voltage sine wave passes through zero. For this, a feedback loop is implemented in the dimmer on the elements R3, R4 and PC814, the signal from which is fed to pin P2 and analyzed by the microcontroller. A 10 kOhm resistor R5 is added to the zero detector circuit, which is needed to feed the output transistor of the optocoupler.

One power output of the triac is connected to the phase wire, and the load is connected to the second. The neutral wire of the 220 V network directly follows from the terminal block J1 to J2, and then to the load. The use of optocouplers is necessary for galvanic isolation of the power and low-voltage parts of the dimmer circuit. The potentiometer (not shown in the diagram) is connected by the middle pin to any analog input of the Arduino, and by the two extreme ones - to +5 V and "common".

PCB and assembly parts

A minimum of radioelements allows you to design a one-sided printed circuit board, the size of which does not exceed 20x35 mm. As you can see from the figure, there is no variable resistor on it, so that the radio amateur can independently select a potentiometer of a suitable form factor and determine the place of its attachment to the case of the finished dimmer. The connection to the Arduino is made through wires that are soldered into the corresponding holes on the board.

To assemble an Arduino-controlled dimmer with your own hands, you will need the following radio elements and parts:

  1. Triac BT136-600D, capable of withstanding a reverse voltage of up to 600 V and passing a current of up to 4 A into the load (of course, with preliminary installation on a radiator). In the circuit, you can use a triac with a higher load capacity. The main thing is to ensure the removal of heat from its body and to select the correct current to the control electrode (reference parameter). When a high-power electrical appliance is connected to the load, the width of the printed conductors in the power section of the circuit will need to be recalculated. Alternatively, the power tracks can be duplicated on the other side of the board.
  2. Optocoupler MOC3021 with triac output.
  3. Optocoupler PC814 with transistor output.
  4. Resistors rated 1 kOhm, 220 Ohm, 10 kOhm power 0.25 W and 2 resistors 51 kOhm power 0.5 W.
  5. Variable resistor 10 kOhm.
  6. Terminal blocks - 2 pcs., With two connectors and a pitch of 5 mm.

All the necessary files for the project are in a ZIP archive: dimmer-arduino.zip

Arduino control algorithm

The triac control program is based on the Timer1 timer and the Cyber.Lib library, due to which there is no influence on the operation of other program codes. Its principle of operation is as follows. When the mains voltage goes through zero "from bottom to top", the timer is reset to the reverse transition "from top to bottom" and starts counting the time in accordance with the value of the variable "Dimmer". At the moment the timer expires, the Arduino generates a control pulse and the triac opens. At the next transition through zero, the triac stops passing current and waits for the next timer operation. And so 50 times a second. The variable "Dimmer" is responsible for adjusting the delay for opening the triac. It reads and processes the signal from the potentiometer and can take a value from 0 to 255.

Dimmer scope on Arduino

Of course, using an expensive Arduino to control the brightness of halogen lamps is redundant. For this purpose, it is better to replace the conventional switch with a commercial dimmer. The Arduino dimmer is capable of solving more serious tasks:

  • control any kind of active load (heating temperature of a soldering iron, instantaneous water heater, etc.) with precise retention of a given parameter;
  • simultaneously perform several functions. For example, to ensure that the light turns on smoothly in the morning (off in the evening), and also control the temperature and humidity of the terrarium.

You can see how the voltage in the load changes using an oscilloscope. To do this, a resistive divider is soldered to the output terminals of the dimmer, due to which the signal at the control point should decrease by about 20 times. After that, the oscilloscope probes are connected to the divider and power is supplied to the circuit. By changing the position of the potentiometer knob, you can see on the oscilloscope screen how smoothly the Arduino controls the triac and whether there is high-frequency interference.

Read the same

With this lesson, I begin a series of articles dedicated to switching regulators, digital regulators, output power control devices.

The goal I set is to develop a controller for the refrigerator on the Peltier element.

We will make an analogue of my design, only implemented on the basis of the Arduino board.

  • This development interested many, and I received letters with requests to implement it on Arduino.
  • The development is ideal for studying the hardware and software of digital controllers. In addition, it combines many of the tasks learned in previous lessons:
    • measurement of analog signals;
    • work with buttons;
    • connection of display systems;
    • temperature measurement;
    • work with EEPROM;
    • communication with a computer;
    • parallel processes;
    • and much more.

I will conduct the development sequentially, step by step, explaining my actions. I don't know what the result will be. Hope for a full working draft of the refrigerator controller.

I don't have a finished project. I will write lessons based on the current state, so during the tests it may turn out that at some stage I was mistaken. I will fix it. This is better than me debugging development and delivering ready-made solutions.

Differences between development and prototype.

The only functional difference from the prototype development on the PIC controller is the absence of a fast voltage regulator that compensates for the ripple of the supply voltage.

Those. this version of the device must be powered from a stabilized power supply with a low ripple level (no more than 5%). All modern switching power supplies meet these requirements.

And the option of power supply from an unstabilized power supply unit (transformer, rectifier, capacitive filter) is excluded. The speed of the Arduino system does not allow for a fast voltage regulator. I recommend reading about the Peltier cell power requirements.

Development of the general structure of the device.

At this stage, you need to understand in general terms:

  • what elements the system consists of;
  • on which controller to execute it;
  • are the outputs and functionality of the controller sufficient?

I imagine the controller as a “black box” or “trash pit” and connect everything I need to it. Then I see if, for example, the Arduino UNO R3 board is suitable for these purposes.

In my interpretation, it looks like this.

I drew a rectangle - the controller and all the signals needed to connect the elements of the system.

I decided that I need to connect to the board:

  • LCD indicator (for displaying results and modes);
  • 3 buttons (for control);
  • error indication LED;
  • fan control key (to turn on the hot side radiator fan);
  • pulse stabilizer key (to adjust the power of the Peltier element);
  • analog input for measuring load current;
  • analog input for measuring load voltage;
  • temperature sensor in the chamber (accurate 1-wire sensor DS18B20);
  • radiator temperature sensor (I have not yet decided which sensor, more likely also DS18B20);
  • communication signals with a computer.

There were 18 signals in total. The Arduino UNO R3 or Arduino NANO has 20 pins. There are still 2 withdrawals left in reserve. Maybe you want to connect one more button, or an LED, or a humidity sensor, or a cold-side fan ... We need 2 or 3 analog inputs, the board has 6. everything suits us.

You can assign pin numbers immediately, you can during development. I appointed immediately. The connection takes place through the connectors, you can always change. Keep in mind that the pin assignment is not final.

Impulse stabilizers.

For accurate temperature stabilization and optimal operation of the Peltier element, it is necessary to regulate the power on it. Regulators are analog (linear) and pulse (key).

Analog regulators are a regulating element and a load connected in series to the power supply. By changing the resistance of the regulating element, the voltage or current on the load is adjusted. As a rule, a bipolar transistor is used as a regulating element.

The regulating element operates in linear mode. "Extra" power is allocated on it. At high currents, stabilizers of this type get very hot and have a low efficiency. A typical linear voltage regulator is the 7805 IC.

This option does not suit us. We will make an impulse (key) stabilizer.

Impulse stabilizers are different. We need a step-down switching regulator. The voltage across the load in such devices is always lower than the supply voltage. The circuit of the step-down switching regulator looks like this.

And this is a diagram of the regulator's operation.

The VT transistor operates in a key mode, i.e. it can only have two states: open or closed. The control device, in our case a microcontroller, switches the transistor with a certain frequency and duty cycle.

  • When the transistor is open, current flows through the circuit: power supply, transistor switch VT, choke L, load.
  • When the switch is open, the energy stored in the choke is supplied to the load. Current flows through the circuit: choke, VD diode, load.

Thus, the constant voltage at the output of the regulator depends on the ratio of the time of the open (topen) and closed key (tcap), i.e. on the duty cycle of control pulses. By changing the duty cycle, the microcontroller can change the voltage across the load. Capacitor C smooths out the ripple in the output voltage.

The main advantage of this control method is its high efficiency. The transistor is always open or closed. Therefore, a small power is dissipated on it - always either the voltage across the transistor is close to zero, or the current is 0.

This is a classic switching buck regulator circuit. In it, the key transistor is torn off from the common wire. The transistor is difficult to drive and requires special bias circuits to the supply voltage rail.

So I changed the schema. In it, the load is torn off from the common wire, but a key is tied to the common wire. This solution allows you to control the transistor switch from the microcontroller signal using a simple current amplifier driver.

  • When the key is closed, the current enters the load through the circuit: power supply, choke L, key VT (current path is shown in red).
  • When the switch is open, the energy stored in the inductor is returned to the load through the regenerative diode VD (current path is shown in blue).

Practical implementation of the key regulator.

We need to implement a pulse regulator node with the following functions:

  • the actual key regulator (key, choke, regenerative diode, smoothing capacitor);
  • load voltage measurement circuit;
  • regulator current measurement circuit;
  • hardware overcurrent protection.

I, practically unchanged, took the regulator circuit from.

Pulse regulator circuit for working with the Arduino board.

As a power switch, I used IRF7313 MOSFET transistors. In an article on increasing the power of the Peltier element controller, I wrote in detail about these transistors, about a possible replacement, and about the requirements for key transistors for this circuit. Here is a link to the technical documentation.

A key MOSFET transistor driver is assembled on transistors VT1 and VT2. This is just a current amplifier, in terms of voltage it even attenuates the signal to about 4.3 V. Therefore, the key transistor must necessarily be low-threshold. There are different options for implementing MOSFET transistor drivers. Including with the use of integral drivers. This option is the simplest and cheapest.

To measure the voltage across the load, the divider R1, R2 is used. With such values \u200b\u200bof the resistances of the resistors and the reference voltage source of 1.1 V, the measurement range is 0 ... 17.2 V. The circuit allows you to measure the voltage at the second terminal of the load relative to the common wire. We will calculate the voltage across the load, knowing the voltage of the power source:

Uload \u003d Upower - Umeasured.

It is clear that the measurement accuracy will depend on the stability of maintaining the voltage of the power supply. But we do not need high accuracy in measuring voltage, current, load power. We only need to accurately measure and maintain temperature. We will measure it with high accuracy. And if the system shows that a power of 10 W is set on the Peltier element, but in fact it will be 10.5 W, this will not in any way affect the operation of the device. This applies to all other energy parameters.

The current is measured using the current sensor resistor R8. Components R6 and C2 form a simple low pass filter.

The simplest hardware protection is assembled on the R7 and VT3 elements. If the current in the circuit exceeds 12 A, then the voltage across the resistor R8 will reach the transistor opening threshold of 0.6 V. The transistor will open and close the RES (reset) pin of the microcontroller to ground. Everything should shut down. Unfortunately, the threshold for this protection is determined by the base-emitter voltage of the bipolar transistor (0.6 V). Because of this, protection is triggered only at significant currents. An analog comparator can be used, but this will complicate the circuit.

The current will be measured more accurately as the resistance of the current sensor R8 increases. But this will lead to the allocation of significant power on it. Even with a resistance of 0.05 Ohm and a current of 5 A, 5 * 5 * 0.05 \u003d 1.25 W is dissipated across the resistor R8. Note that resistor R8 is 2W.

Now, what current are we measuring. We measure the current consumption of the switching regulator from the power supply. The circuit for measuring this parameter is much simpler than the circuit for measuring the load current. Our load is “untied” from the common wire. For the system to work, it is necessary to measure the electrical power at the Peltier element. We will calculate the power consumption of the regulator by multiplying the power supply voltage by the current consumption. Let us assume that our regulator has an efficiency of 100% and decide that this is the power at the Peltier element. In fact, the efficiency of the regulator will be 90-95%, but this error will not affect the operation of the system in any way.

Components L2, L3, C5 - a simple RFI filter. It may not be necessary.

Calculation of the throttle of the key stabilizer.

The choke has two parameters that are important to us:

  • inductance;
  • saturation current.

The required inductance of the inductor is determined by the PWM frequency and the allowable current ripple of the inductor. There is a lot of information on this topic. I will give the most simplified calculation.

We applied voltage to the inductor and the current through it began to increase the current. Increase, but did not appear, because some current was already flowing through the choke at the moment I was turned on).


The transistor has opened. The voltage was connected to the choke:

Uthrottle \u003d Usupply - Uload.

The current through the inductor began to grow according to the law:

Ithrottle \u003d Uthrottle * top / L

  • topt is the duration of the public key pulse;
  • L is the inductance.

Those. the value of the choke current ripple or by how much the current has increased during the time of the open key is determined by the expression:

Ioff - Ion \u003d Uthrottle * topen / L

The voltage across the load may vary. And it determines the voltage across the choke. There are formulas that take this into account. But in our case, I would accept the following values:

  • supply voltage 12 V;
  • the minimum voltage on the Peltier element is 5 V;
  • means the maximum voltage across the choke is 12 - 5 \u003d 7 V.

The pulse duration of the public key top is determined by the frequency of the PWM period. The higher it is, the less inductance the choke is needed. The maximum PWM frequency of the Arduino board is 62.5 kHz. I will tell you how to get such a frequency in the next lesson. We will use it.

Let's take the worst case - PWM switches exactly in the middle of the period.

  • The duration of the period is 1/62500 Hz \u003d 0.000016 sec \u003d 16 μs;
  • Public key duration \u003d 8 μs.

The ripple current in such circuits is usually set to 20% of the average current. Not to be confused with output voltage ripple. They are smoothed by capacitors at the output of the circuit.

If we allow a current of 5 A, then we take a ripple current of 10% or 0.5 A.

L \u003d Uthrottle * topen / Ipulsation \u003d 7 * 8 / 0.5 \u003d 112 μH.

Choke saturation current.

Everything in the world has a limit. And the choke too. At some current, it ceases to be inductance. This is the inductor saturation current.

In our case, the maximum choke current is defined as the average current plus ripple, i.e. 5.5 A. But it is better to choose the saturation current with a margin. If we want hardware protection to work in this version of the circuit, then it must be at least 12 A.

The saturation current is determined by the air gap in the inductor magnetic circuit. In the articles on Peltier element controllers, I talked about choke design. If I begin to expand on this topic in detail, then we will move away from Arduino, from programming and I do not know when we will return.

My choke looks like this.


Naturally, the choke winding wire must be of sufficient cross-section. The calculation is simple - the definition of heat losses due to the active resistance of the winding.

Active resistance of the winding:

Ra \u003d ρ * l / S,

  • Rа - active resistance of the winding;
  • Ρ - specific resistance of the material, for copper 0.0175 Ohm mm2 / m;
  • l is the length of the winding;
  • S is the section of the winding wire.

Heat losses at the active resistance of the choke:

The key regulator draws a decent current from the power supply and should not be allowed to flow through the Arduino board. The diagram shows that the wires from the power supply are connected directly to the blocking capacitors C6 and C7.

The main impulse currents of the circuit pass through the circuit C6, load, L1, D2, R8. This circuit must be closed by links with a minimum length.

The common wire and the power bus of the Arduino board are connected to the blocking capacitor C6.

The signal wires between the Arduino board and the key stabilizer module should be as short as possible. Capacitors C1 and C2 are best located on the board connectors.

I assembled the circuit on the board. I soldered only the necessary components. The assembled circuit looks like this.

I set the PWM to 50% and checked the operation of the circuit.

  • When powered by a computer, the board formed a given PWM.
  • With autonomous power supply from an external power supply, everything worked great. Pulses with good edges were formed on the choke, there was a constant voltage at the output.
  • When I turned on the power from both the computer and the external power supply at the same time, my Arduino board burned out.

My stupid mistake. I'll tell you so that no one will repeat it. In general, when connecting an external power supply, you must be careful, ringing all connections.

The following happened to me. There was no VD2 diode on the circuit. I added it after this nuisance. I figured the board could be powered externally via the Vin pin. He himself wrote in lesson 2 that the board can be powered from an external source through the connector (RWRIN signal). But I thought it was the same signal, only on different connectors.

I connected the power supply (not plugged in) and the Arduino board cable to the USB connector of the computer. The output of the stabilizer of the U1 NCP1117 board from the USB connector received a voltage of +5 V. And the input turned out to be closed through a sufficiently low resistance of the switched off power supply. The scheme is in

For the Smart Home system, the main task is to control household appliances from a control device, be it an Arduino microcontroller, or a Raspberry PI microcomputer, or any other. But you can't do this directly, let's figure out how to control a 220 V load with Arduino.

To control AC circuits, microcontroller means are not enough for two reasons:

1. At the exit microcontroller a constant voltage signal is generated.

2. The current through the pin of the microcontroller is usually limited to 20-40 mA.

We have two options for switching using a relay or using a triac. The triac can be replaced by two connected anti-parallel thyristors (this is the internal structure of the triac). Let's take a closer look at this.

Load control 220 V using a triac and microcontroller

The internal structure of the triac is shown in the picture below.

The thyristor works as follows: when a forward bias voltage is applied to the thyristor (plus to the anode and minus to the cathode), the current will not pass through it until you apply a control pulse to the control electrode.

I wrote the impulse for a reason. Unlike a transistor, a thyristor is a SEMI-CONTROLLED semiconductor switch. This means that when the control signal is removed, the current through the thyristor will continue to flow, i.e. it will remain open. In order for it to close, you need to interrupt the current in the circuit or change the polarity of the applied voltage.

This means that when holding a positive pulse on the control electrode, the thyristor in the AC circuit will only pass the positive half-wave. The triac can pass current in both directions, but since it consists of two thyristors connected towards each other.

The control pulses in polarity for each of the internal thyristors must correspond to the polarity of the corresponding half-wave, only when this condition is met, an alternating current will flow through the triac. In practice, such a scheme is implemented in a common one.

As I said, the microcontroller gives out a signal of only one polarity, in order to match the signal, you need to use a driver built on an optosimistor.

Thus, the signal turns on the internal LED of the optocoupler, it opens the triac, which sends a control signal to the power triac T1. MOC3063 and the like can be used as an opto driver, for example, the photo below shows the MOC3041.

Zero crossing circuit - phase zero crossing detector circuit. It is needed to implement all sorts of triac regulators on a microcontroller.

If the circuit is without an opto-driver, where the coordination is organized through a diode bridge, but in it, unlike the previous version, there is no galvanic isolation. This means that at the first voltage surge, the bridge can break through and the high voltage will be at the microcontroller pin, which is bad.

When turning on / off a powerful load, especially of an inductive nature, such as motors and electromagnets, voltage surges occur, therefore, a snubber RC circuit must be installed in parallel to all semiconductor devices.

Relay and A rduino

To control a relay with an A rduino, an additional transistor must be used to amplify the current.

Please note, a reverse conduction bipolar transistor (NPN structure) is used, it may be a domestic KT315 (beloved and known to everyone). The diode is needed to damp the EMF surges of self-inductance in the inductance, this is necessary so that the transistor does not fail from a high applied voltage. Why this occurs, the law of commutation will explain: "The current in an inductor cannot change instantly."

And when the transistor is closed (the control pulse is removed), the energy of the magnetic field accumulated in the relay coil needs to disappear somewhere, so a reverse diode is installed. Once again, I note that the diode is connected in the REVERSE direction, i.e. cathode to positive, anode to negative.

Such a circuit can be assembled by hand, which is much cheaper, plus you can use it designed for any constant voltage.

Or buy a ready-made module or a whole shield with a relay for Arduino:

The photo shows a homemade shield, by the way, it uses KT315G to amplify the current, and below you see the same factory-made shield:

Conclusion

Safe control of the AC load means, first of all, all the information described above is valid for any microcontroller, not just the Arduino board.

The main task is to provide the required voltage and current to control the triac or relay and galvanic isolation of the control circuits and the AC power circuit.

In addition to safety for the microcontroller, in this way, you insure yourself so that you do not get an electrical injury during maintenance. When working with high voltage, you must comply with all safety rules, observe the PUE and PTEEP.

These schemes can be used and. Triacs and relays in this case act as an intermediate amplifier and signal matching. On powerful switching devices, large coil drive currents depend directly on the power of the contactor or starter.

Alexey Bartosh

Let's consider an interesting and useful question. Dimmer for adjusting the AC load using arduino. That is, it is a smooth control of such network devices as lamps, heaters in the form of heating elements or underfloor heating.
A couple of weeks ago, on the second channel, which is completely devoted to programming arduino, a video was released about controlling a DC load using a shim signal. What you are watching now was also supposed to be released on that channel. But I decided to publish it.

Radio parts, components and appliances in this Chinese store.

The alternating current in the outlet is a sinusoid, that is, the voltage changes constantly over time, and every 10 milliseconds is zero. If you watched the video about the PWM signal, you will understand that it will not work to take it and start adjusting the sine wave.

The device that we will make is called a dimmer. It includes ordinary sinusoids from the outlet and cut off. The dimmer does not pass part of the sinusoid. And the larger this part, the lower the average stress. By changing the intervals when the voltage is zero, we will regulate the total output voltage. Such a piece of iron as a triac opens and closes the voltage. They are available in different cases and for different currents. For example, a big guy can pump 40 amperes at 800 volts. Which is like about 30 kW.

To control the triac at the right times, you need a bag of loose powder. Several resistors and two optocouplers. All this can be bought for a penny at any radio parts store or on the radio market. For ease of connection, you can take the terminals. And you can assemble the entire circuit on a breadboard. The connection diagram is as follows.

The triac breaks the 220v network, arduino will open and close it through an optocoupler. That is, the arduino itself will be optically isolated by the mains voltage for our safety. And an important point. To open the triac on time, the arduino must know when the mains voltage passes through 0. For this, there is a second optocoupler connected in the opposite direction. And at the output from it, we get a signal every time the voltage in the network passes through 0. And we control the triac through the upper optocoupler. The work algorithm is a bit later.

Let's assemble the circuit in hardware

Ideally, such things should be done on a printed dress. A separate cycle of video lessons will soon be on the channel. We will show you how to wire the boards and how to poison. In the meantime, we do not know how to make printed circuit boards, there are two more ways. The first is to assemble the circuit on a breadboard. What are we going to do in a minute. And the second is to order the manufacture of boards from the Chinese. Made several variants of boards on the easyeda platform. The first is on a small seven-story, the second is on a large seven-story. And third, this is a three-channel dimmer. Which has one common input and one common zero detector output. Three outputs for load and 3 pin for control of three arduino triacs. The circuit is easy to scale and dimmer for any number of channels.

To order boards, you need to pull out gerber files from the project. Press the button and go to the page for ordering boards from the easyeda service. And press the button to download gerber files. They will be downloaded in one archive. We go to the site of the lg psb service. This is one of the cheapest and largest services for manufacturing printed circuit boards on an industrial scale with delivery. First, let's log in just in case. Go to the cart and add a new order. And add the gerber file, that is, the same archive. Single-layer board. Select one layer. The dimensions, as you can see, were set automatically. Quantity - at least 5 pieces can be ordered. PCB thickness, color. Let it be red. This is the color of the mask that the board is covered with. The solder is selected with which the tracks will be covered. This is tin-lead, lead-free and more, which we do not know. Further, the thickness of the copper foil is not PCB. Well, the price for some reason changes twice. Golden fingers are coming. This is a comb to insert the board into the connector. Then you can get a payment in this form. And you can also cut off the extreme contacts. But none of this is needed. We save the basket. As you can see, the price for 5 boards is $ 2. That is, it is about 25 rubles per fee. Industrial grade boards will get almost nothing.

The only thing is delivery. You need to provide your address. For convenience, we usually use the transliteration service, which translates Russian letters into transliteration. Well, the delivery itself. $ 30 for courier, and standard - 250 rubles for mailing. Dhl, do not work with individuals. If there are no acquaintances of the company, then it is better not to contact and wait a couple of weeks by mail. You can pay for this case by paypal or bank card. In general, I ordered printed circuit boards, and while they are traveling, we will assemble the circuit on a breadboard.

This AC regulator assembly project is one of those that can be assembled on a breadboard by looking at the PCB layout. That is, we insert the components into the layout in the same way as on the seal. And we connect everything with the feet of the components themselves. For example, the triac reaches both terminal blocks and the optocoupler. We take and solder. And you can also use the life hack from the video about the battery welder. That is, print the layout of the board, glue it to the breadboard and solder it, focusing on the tracks. And after 10 minutes of work with tweezers and a soldering iron, a board is obtained. Compact.
Many components are used for the connection. The only thing was to connect the common output with a piece of copper wire. An important point. We solder with glycerin flux and you can see its traces. It shines. The dimmer can work with a voltage of 220V and pierce through the flux, and it is not stable. Or burn out altogether. Therefore, we take a toothbrush and go clean. Well, we cut off the excess with scissors for metal, and level the edge. And that's it, the dimmer is ready. Cool and compact.
On trimming the layout, I assembled an option with a big guy. There is a direct connection to the triac using pads. The left is the exit, the middle is the entrance, and the right is the common entry and exit. There is only one resistor to it according to the scheme. The triac itself is glued to double-sided tape. Ideally, the pads should have been screwed on. Well, that's fine. Everything. Soldered simply by kicking resistors. This board is needed in one of the following projects. Try to guess in the comments what it might be.
Now, finally, let's look at the algorithm by which the triac control works. So, we will control the triac with arduino. The firmware is written in a special program. There are two important points. The first is receiving a signal from the output of the zero detector, which reports that the sinusoid of the mains voltage crosses the 0 volt voltage. The zero detector output is connected to a hardware interrupt handler. This is the second pin of arduino. And the pin is pulled to ground by a 10 kilo-ohm resistor. An internal lift is failing. We don't know why. Unlike all firmwares on the Internet, the algorithm does not use delays. That is, triac control does not interfere with the execution of the rest of the program code. This is implemented using the timer-1 timer. As using regular meters will result in some flickering every few minutes.
For convenient work with the timer, we use the smart cyberlip library. In general, the point is, as soon as a zero crossing is detected from the bottom, this is a point, the timer starts for the dimming time, and the interrupt is reconfigured for a zero crossing from top to bottom. And the time has passed. After the timer has expired, the triac opens the current to the consumer. As soon as the interrupt replaces the top-down zero crossing, it stops the timer and reconfigures again. And also turns off the current through the triac. And this is repeated 50 times a second.

A potentiometer is used to adjust the time after which the triac will open after zero crossing. The dimmer variables should range from 0 to 255. These are full and minimum brightness. And that's all. Let me remind you that all the schemes and the sketch can be downloaded on the project page. Link in the description below the video.

The dimmer can be used for more than just brightness control. Of much greater interest is the feedback control system for the heating element. For precise maintenance of the set temperature.
Also, the dimmer can be used in systems such as a smart home, and control of this very dimmer via the Internet. To do this, you need to be able to write programs for windows, android or web.

Chinese printed circuit boards arrived. We repeat, if you are an individual, then it is better to order by mail. Through dhl, I had to hide behind a familiar company and re-register the documents at customs. All in all, such nice seals. Considering that they cost 25 rubles each, we hope the Chinese are at least a little profitable. Otherwise it's a shame.

We unsoldered one dimmer and connect it to arduino, just like before. We move the potentiometer, the incandescence of the light bulb changes from maximum to barely glowing. An interesting sight, actually.

Probably, I wanted to watch the whole video at the real waveform at the output of the dimmer. Does it match the pictures that it showed. Let's use a cheap Chinese oscilloscope that can measure voltages up to 12 volts. Stop. You can't do that. To measure the mains voltage, you need to use a thing like a voltage divider. A ratio of 1 to 20. To prevent the resistors from heating up, I took the denominations of two hundred and ten kilo-ohms. We carefully connect everything and only then plug it into the network. It is life threatening. And we see the same beauty as in the pictures. It can be seen how the voltage in the periods of the sinusoid appears, reaches zero and disappears. To restart by timer the next half cycle. Magnificent sight!