How to set up smartphones and PCs. Informational portal
  • home
  • news
  • Load management via usb. Ryabenky V.M

Load management via usb. Ryabenky V.M

P. VYSOCHANSKY, Rybnitsa, Transnistria, Moldova

A device for computer control of various devices, the diagram of which is shown in Fig. 1, functionally similar to that described in, but connects to the USB port of a computer, which (unlike the COM port) today is in each of them. The only microcircuit of the device is the common ATmega8 microcontroller. It is necessary for organizing communication via the USB bus. Although it lacks a dedicated hardware module, this function is performed in software.

Resistor R1, connected between the positive terminal of the power supply and the USB D-bus line, puts it in the low-speed LS mode with an exchange rate of 1.5 Mbit / s, which allows the computer to be decrypted by software. Resistors R4 and R5 eliminate communication transients for increased stability. Capacitor C1 blocks impulse noise in the power supply circuit, which also improves the stability of the device. Diodes VD1 and VD2 are used to lower the microcontroller supply voltage to approximately 3.6 V - this is required to match the levels with the USB bus. Instrument control signals are generated at the outputs of the PBO-PB5 and RSO, PC1 of the microcontroller. High logic level - voltage about 3.4 V. Low level voltage is close to zero. Devices consuming no more than 10 mA current (from each output) can be connected to the outputs. If large values ​​of current or voltage are required, then the matching nodes shown in Fig. 5 and 6.

The device is assembled on a breadboard, the printed circuit board has not been developed. Used resistors MLT, capacitors C2 and SZ - ceramic high-frequency, C1 - K50-35 or similar imported. Silicon diodes with a voltage drop across the junction of about 0.7 V.

The program for the microcontroller was developed in the Bascom-AVR environment version 1.12.0.0. To work with the USB bus, the swusb.LBX library was used, which performs software decoding of USB signals in real time. The program code obtained as a result of compilation from a file with the HEX extension should be loaded into the FLASH memory of the microcontroller. For this, the programmer was used together with the utility built into the Bascom-AVR. The state of the bits of the microcontroller configuration must correspond to that shown in Fig. 2.

When you connect the device to your computer for the first time, the operating system detects a new USB HID compatible device named "uniUSB" and installs the necessary drivers. After a few seconds, the device is set up and ready to use. To work with it, the UniUSB program was created. It is presented in two versions: for 32-bit (x86) and 64-bit (x64) Windows operating systems. The 32-bit version has been tested in Windows 98, Windows XP, Windows 7, and the 64-bit version is tested only in Windows XPx64.

The UniUSB program is written in PureBasic (version 4.31) using the HIDJJb custom function library that supports USB HID devices. The appearance of the program window is shown in Fig. 3.

In the same folder with its executable file, there must be a file called UniUSB_KOfl.txt or UniCOM_KOfl.txt. The latter option is required for compatibility with the UniCOM program proposed in. This file contains a script for controlling external devices. When the program starts, the data from the file is loaded into the table located in the main window, and when the program is finished, it is saved to the file. Left-clicking on the table cells allows you to change their state: 1 - high logic level, 0 or empty - low logic level.

To add or remove a column of a table, you need to right-click on it and select the required action in the menu that appears.

When a device is connected to the USB port, the program detects it and activates the> button located at the top of the window on the toolbar. By pressing this button, the process of enumerating the columns of the table and setting the states of the outputs indicated in them is started. For greater clarity, to the left of the table, the numbers of outputs on which a high logic level is currently set are highlighted. The search speed (time in milliseconds between transitions from column to column) is set in the "Speed, ms" field.

Consider the Windows operating system - multitasking! This means that the processor time is divided among many processes, sometimes hidden from the user, which are executed in turn, taking into account the priorities set in the system. Therefore, one should not expect a high accuracy of keeping time intervals less than 100 ms.

To momentarily stop iterating over the columns, use the JB button. Pressing it again will continue the search from the point of stopping. The ■ button completely stops iterating over the table columns. If during the exchange of information between the computer and the device a failure occurs or the device is disconnected from the USB connector of the computer, the program will report an error by displaying a corresponding message in the status bar.

Grade 1 Grade 2 Grade 3 Grade 4 Grade 5

A computer control device for various devices, the diagram of which is shown in Fig. 1, connects to the USB port of the computer, which today is in each of them. The only microcircuit of the device is a common microcontroller ATmega8... It is required for organizing bus communication. USB... Although it lacks a dedicated hardware module, this function is performed in software.

Picture 1

Resistor R1, connected between the positive terminal of the power supply and the USB D-bus line, puts it in the low-speed LS mode with an exchange rate of 1.5 Mbit / s, which allows the computer to be decrypted by software. Resistors R4 and R5 eliminate communication transients for increased stability. Capacitor C1 blocks impulse noise in the power circuit, which also improves the stability of the device. Diodes VD1 and VD2 are used to lower the microcontroller supply voltage to approximately 3.6 V - this is required to match the levels with the USB bus.

Instrument control signals are generated at the outputs РВ0-РВ5 and РС0, РС1 of the microcontroller. High logic level - voltage about 3.4 V. Low level voltage is close to zero. Devices consuming no more than 10 mA current (from each output) can be connected to the outputs. If large values ​​of current or voltage are required, then matching nodes should be used.

The device is assembled on a breadboard, the printed circuit was not developed. MLT resistors were used, capacitors C2 and SZ were high-frequency ceramic, C1 - K50-35 or similar imported. Silicon diodes with a voltage drop across the junction of about 0.7 V.

The program for the microcontroller is developed in the environment Bascom-AVR versions 1.12.0.0. The library was used to work with the USB bus swusb.LBX which performs software decoding of USB signals in real time. The program code obtained as a result of compilation from a file with the HEX extension should be loaded into the FLASH memory of the microcontroller. The state of the bits of the microcontroller configuration must correspond to that shown in Fig. 2.

Picture 2

When you connect the device to the computer for the first time, the operating system detects a new USB HID compatible device named " uniUSB"and installs the necessary drivers. In a few seconds the device is configured and ready to use. To work with it, the UniUSB program was created. It is presented in two versions: for 32-bit (x86) and 64-bit (x64) Windows operating systems. The 32-bit version was tested in Windows 98, Windows XP, Windows 7, and the 64-bit version was tested only in Windows XP x64.

Program UniUSB written in language PureBasic(version 4.31) using custom function library HID_Lib supporting work with USB HID devices. The appearance of the program window is shown in Fig. 3.

Figure 3

In the same folder with its executable file, there should be a file called UniUSB_KOfl.txt... This file contains a script for controlling external devices. When the program starts, the data from the file is loaded into the table located in the main window, and when the program is finished, it is saved to the file. Left-clicking on the table cells allows you to change their state: 1 - high logic level, 0 or empty - low logic level.

To add or remove a column of a table, you need to right-click on it and select the required action in the menu that appears. When a device is connected to a USB port, the program detects it and activates the "Start" button located at the top of the window on the toolbar. By pressing this button, the process of enumerating the columns of the table and setting the states of the outputs indicated in them is started. For greater clarity, to the left of the table, the numbers of outputs on which a high logic level is currently set are highlighted. The search speed (time in milliseconds between transitions from column to column) is set in the "Speed, ms" field.

class = "eliadunit">

Consider the Windows operating system - multitasking! This means that the processor time is divided among many processes, sometimes hidden from the user, which are executed in turn, taking into account the priorities set in the system. Therefore, one should not expect a high accuracy of keeping time intervals less than 100 ms.

Use the "Pause" button to briefly stop iterating over the columns. Pressing it again will continue the search from the point of stopping. The "Stop" button completely stops iterating over the table columns. If during the exchange of information between the computer and the device a failure occurs or the device is disconnected from the USB connector of the computer, the program will report an error by displaying a corresponding message in the status bar.

Source: Radio No. 2, 2011

Archive for the article "Load management via USB interface"
Description: Source codes of programs, microcontroller firmware file, uniUSB program
File size: 89.3 KB Number of downloads: 2 773

On Habré about controlling a lamp via the Internet, the idea came up to control the lighting at home from a computer, and since I already have a computer controlled from a cell phone, this means that the light can also be controlled from the same phone. After showing the article to one of my work colleagues, he said that he just needed it. Since he often falls asleep behind the films that he watches on the computer. Some time after the end of the film, the computer also falls asleep and turns off the monitor, but the light in the room remains on. Those. it was decided that the thing was useful, and I began to collect information and details for this miracle.
The rest of the information is under habracut (be careful with a lot of pictures - traffic).

Device diagram

For the original scheme, one of the schemes found on the Internet was taken and it looked like this:

But with only a slight change: a 390 Ohm resistor was added between the 1st pin of the 4N25 optocoupler and the 2nd pin of the LPT, and an LED was also added to indicate the power-on. The circuit was assembled in test mode, i.e. just wired as needed and tested. In this version, she simply turned on and off the old Soviet flashlight.
It was decided that if the control was already done, then not for one device, but for at least 4 devices (based on: one lamp on the table, a chandelier for two switches, a spare socket). At this stage, it became necessary to build a complete circuit of the device, and began to select various programs.
Were installed:

  1. KiCAD
  2. Eagle
After looking at all of them, I settled on the Eagle, as there were "similar" parts in its library. Here's what happened in it:

The diagram uses a DB9 port i.e. a normal COM port, this was done for reasons of saving both space on the board and the connectors themselves (I had COM), and since we will use only 5 conductors, this will be enough for us with a margin. Thus, we also make an adapter from DB25 (LPT) to DB9 (COM), in my case it is done as follows:
LPT 2-9 pin = COM 1-8 pin are control data pins;
LPT 18-25 pin (they are often interconnected) = COM 9 pin - this is our ground.
Also, the circuit uses an additional 12V power supply to power the relay, according to the plan it will be a simple Chinese charger or maybe a 9V crown (one relay works fine, you need to check for 4 at the same time). Separate power supply and galvanic isolation using an optocoupler are used to secure the computer port. If you wish, you can, of course, be powered from a 12V computer power supply, but everyone does this himself and at his own peril and risk.

Required parts to create a device
  1. COM port - 1 piece
  2. power connector - 1 piece
  3. green LED - 4 pcs
  4. optocoupler 4n25 - 4 pieces
  5. a seat for an optocoupler (I only had 8 legs) - 4 pcs
  6. resistor 390 Ohm - 4 pieces
  7. resistor 4.7 kOhm - 4 pcs
  8. transistor KT815G - 4 pieces
  9. relay HJR-3FF-S-Z - 4 pieces
  10. clamps for 3 contacts - 4 pcs.
  11. foil textolite

Preparing the PCB layout

After trying to use Eagle to prepare the PCB, I realized that it would be difficult and decided to find an easier option. This option is the sprint layout 5 program, even if it is in execution for windows, but it runs without problems in wine under linux. The interface of the program is intuitive, in Russian and the program has a fairly clear help (help). Therefore, all further actions for the development of the printed circuit board were carried out in sprint layout 5 (hereinafter referred to as SL5).
Although many people use this program to develop boards for their devices, it did not contain the parts I needed (even in the heap of downloaded collections of macros). Therefore, I had to first create the missing parts:
  1. COM port (the one that did not coincide with mine, along the mounting holes)
  2. power socket
  3. clamp for three contacts
  4. relay HJR-3FF-S-Z
View of these parts:

After adding the necessary details, the PCB design itself began. It took several attempts, there were about five of them. Each version of the board was printed on cardboard, holes were pierced and parts were inserted into them. Actually, it was found out that my COM port does not match the one that was in SL5. A small error in the relay circuit also surfaced - in fact, the relay case was shifted by 2-3 mm. Naturally, all errors have been corrected.
On the first printed version, it turned out that the transistor was not connected correctly, two contacts were mixed up.
After all the corrections and adjustments, we got the following board:

The SL5 has a PhotoView function for viewing the board, this is how it looks in it:

On the final version of the board, the tracks will be slightly tweaked, but otherwise it looks the same.

The SL5 also has a convenient option for printing the board, you can hide unnecessary layers and choose the color of printing for each layer, which is very useful.

Preparing the PCB

It was decided to make the board using the LUT method (laser-iron technology). Further, the whole process is in the photo.

Cut out a piece of PCB of the required size.

We take the finest sandpaper and carefully clean the copper surface.

After cleaning the surface, it must be rinsed and degreased. You can rinse with water, and degrease with acetone (in my case it was solvent 646).
Next, we print our board on a laser printer on coated paper, not forgetting to install the most bold print in the printer (without saving toner). This option turned out to be a little unsuccessful, as the toner was smeared, but another attempt was just fine.

Now you need to transfer the drawing from paper to textolite. To do this, cut out the drawing and apply it to the PCB, try to align it as needed and then warm it up with an iron. The entire surface must be thoroughly warmed up so that the toner melts and adheres to the copper surface. Then let the board cool down a little and go to wet it under running water. When the paper gets wet enough, it must be separated from the board. Only toner adhered to the board. It looks like this:

Next, you need to prepare a solution for etching. I used ferric chloride for this. On the jar with ferric chloride it is written that the solution must be made 1 to 3. I stepped back from this a little and made 60 g of ferric chloride per 240 g of water, i.e. it turned out 1 to 4, despite this, the board was etched normally, only a little slower. Please note that the process of dissolving dry ferric chloride in water is accompanied by the release of heat, so it must be added to the water in small portions and stirred. Naturally, for etching it is necessary to use non-metal dishes, in my case it was a plastic container (like from a herring). I got the following solution:

Before lowering the board into the solution, I glued the fishing line to its back side with adhesive tape, so that it would be more convenient to get and turn the board over. If the solution gets on your hands, you need to quickly wash it off with soap (soap neutralizes it), but stains may still remain, it all depends on the specific conditions. Stains are not removed from clothes at all, but I was lucky not to check it myself. It is necessary to immerse the board in the solution with copper down and not all flat, but at an angle. From time to time, it is advisable to clean the board from working off, since it interferes with further etching. This can be done with cotton swabs.

The whole etching process took me 45 minutes, 40 minutes would have been enough, but I was just busy with one more thing.
After etching, we wash the board with soap, tear off the tape with the fishing line and get:

Attention! Do not pour the ferric chloride solution into the sink (sewer) - this can damage the metal parts of the sink, and in general, the solution may still come in handy.
Next, we need to wash off the toner, this is successfully done with the same solvent 646 that was used for degreasing (prolonged contact of the solvent with the skin can damage it).

The next step is to drill the holes. I had 1mm and 1.5mm holes on the board initially, since I could not find thinner drills. It was also not possible to find a collet chuck in our city for attaching it to an electric motor, so everything was done with a large drill.

The first device came up

For the first time, I took only two drills, but when using such a drill, this was not enough. One drill broke and the other was bent. All that I managed to drill on the first day:

The next day I bought five drills. And they were just enough, because if they do not break (by the way, I broke only one of the five), then they become dull, and when drilling with blunt ones, the tracks deteriorate, the copper begins to flake off. After fully drilling the board, we get:

After drilling, the board must be tinned. For this I used the old method - a soldering iron, TAGS flux and tin. I wanted to try using Rose's alloy, but I can't find it in our city.

After tinning, we get the following result:

Next, you need to rinse the board to remove the remnants of the flux, since the TAGS is water-washable, you can do this either with water or alcohol. I did something in between - I washed it with old vodka and wiped it with cotton swabs. After all these steps, our board is ready.

Assembly of parts

To check the correctness of the board, I initially assemble only one (out of four) line of parts, you never know where the error crept in.

After installing the parts, we go and connect the device to the computer via LPT, for this an adapter from DB25 (LPT) to DB9 (COM) is soldered in the following form:

  • 2 pin DB25 to 1 pin DB9
  • 3 pin DB25 to 2 pin DB9
  • Pin 4 of DB25 to pin 3 of DB9
  • 5 pin DB25 to 4 pin DB9
  • 6 pin DB25 to 5 pin DB9
  • 7 pin DB25 to 6 pin DB9
  • 8 pin DB25 to 7 pin DB9
  • 21 pins DB25 (any number 18 to 25 is possible) to pin 9 of DB9
Since an ordinary twisted pair was used as a wire, one wiring was not enough, but for this device only five wires are enough, so this option is suitable. We use a simple Soviet flashlight as the load to be switched on. Well, as a power supply - a universal Chinese power supply (4 connectors and power supply from 3 to 12 V). Here is everything assembled:

But the device is already working:

This ended another evening and the assembly of the rest of the parts was left the next day.

And here is the fully assembled device:

Well, and a small video about how it works (the quality is not very good, there was nothing to shoot normally)

That's all, it remains only to find a normal case for the device and start it up.

Software part

Naturally, to manage the LPT port, you need some software, but since I have linux at home, it was decided to just write the simplest program myself, and then add it and adapt it as needed. It looked something like this:
#include
#include
#include
#include
#define BASE 0x378
#define TIME 100000
int main ()
{
int x = 0x0F;
int y = 0x00;
if (ioperm (BASE, 1, 1))
{
perror ("ioperm ()");
exit (77);
}
outb (x, BASE);
return 0;
}

This program sends port 0x0F = 00001111 to LPT, i.e. feeds 1 to 2-5 pins (Data0-Data3), and this is our control voltage between 2-5 pins and ground (18-25 pins), thus all four relays will turn on. The program for sending 0x00 to the shutdown port works in the same way, it just sends y - outb (y, BASE) instead of x. You can also read the state of the port:
#define BASEPORT 0x378 / * lp1 * /
...
printf ("status:% d \ n", inb (BASEPORT));
...

The only nuance of this program is that it must be run as root, since the ioperm function is not available to an ordinary user. I think you can not tell how to solve such a problem, everyone will choose a more suitable option.

Subsequently, the program was modified so that by passing command line parameters to it, it would be possible to indicate with which device and what to do.
Output of "sw --help":
The program for controlling the relay through the LPT port.
The program can have one or two parameters.
Parameter format: sw [device number] [action]
device number - from 1 to 8
action - "on", "off", "st" - enable, disable, status
Example: "sw 2 on" to enable a second device or "sw --help" to display help

PS If anyone needs it, then I can put the board schematic file in sl5 and the source of the control program somewhere.

A computer, some details, tools anyone can organize a simple control of household appliances from this computer. Many devices used in everyday life have many functions, for example, most modern TVs can show a number of different channels, a fan might have. a number of different operating modes, etc. In order to make such complex control as, for example, switching channels or operating modes, in addition to the above, additional knowledge, details and tools will be needed, but everyone who has the listed above can do a simple on and off. The Arduino communicates with the computer via the USB port, the transfer of information from the computer to the Arduino can be done through the development environment for Arduino (called the Arduino IDE), which can be downloaded from the page on the official Arduino website. There are many different modules for the interaction of Arduino with the outside world, for example, a special module with a relay block for switching loads, when using such modules, the work is greatly simplified, here we will consider the independent production of a module with one relay for turning on / off household appliances, if necessary, you can make more than one such module and use them with one Arduino, thus making it possible to easily control many household appliances. Arduino (any) has a number of general-purpose pins that are designated, on the board, simply by numbers or numbers with a wavy "~" sign. By connecting the Arduino to a computer and writing a special sketch (program for Arduino) into it (in Arduino), you can control these pins from this computer through the "Arduino IDE" program by making them high voltage (about + 5V (HIGH)) or low (about 0V (LOW)). Also on the Arduino there is a "GND" pin (marked on the board). If there is a high voltage at one of the general-purpose terminals, then by connecting something conducting current between this terminal and the "GND" terminal, an electric current will flow through what is connected and the value of this current will depend on the resistance of this object and can be calculated by, i.e. the lower the resistance, the greater the current, but if the resistance is too low, then too much current will flow through the Arduino and it will burn out. The maximum current that the general-purpose pin of Arduino can produce can be different depending on the microcontroller used in it, but usually it is 40mA = 0.04A - this may not be enough to turn on the relay that will turn on the device (devices), therefore, to amplify the current, you need to use an additional an element such as a bipolar transistor. A bipolar transistor has three leads: emitter, collector, base. The maximum current of the transistor is also limited, like that of Arduino, and usually it is higher, for example, in the popular KT315, the maximum current is 100mA = 0.1A. Bipolar transistors are of two types, n-p-n and p-n-p, both types can be used, but in different ways, and further we will consider the use of the KT315 transistor, the type of which is n-p-n. In order for the transistor to amplify the current from the Arduino, it is necessary to connect its base to the Arduino terminal THROUGH A RESISTOR with a resistance of 1kOhm (1k is written on the resistor), connect the emitter of this transistor to "GND" of the Arduino and the minus of the power supply or "GND" of the power supply, the voltage of which is equal to the voltage of the winding of the existing relay (for example 12V) one of the terminals of the relay winding is connected to the collector of the transistor another with a plus of the power supply (+ 12V is permissible) and one more that does not affect the amplification but a VERY important detail is the diode that must be connected with the anode to the collector and the cathode with plus the power supply (+ 12V). If the diode is imported, then most likely there will be a light strip on its body - it indicates the cathode, the other output of the diode is the anode. The remaining outputs of the relay are the outputs of its contacts, if there are two of them and they are not closed, then when sufficient current is supplied to the relay winding, these contacts close, they must be connected in series with the device and this serial connection can be plugged into the socket, then when the contacts are closed, the device will receive 220V and it will turn on. The above can be depicted in the picture:

Figure 1 - Controlling the device from a computer


This is a non-standard scheme for better understanding, they usually use schemes like this:

Figure 2 - Controlling the device from a computer


Although this diagram also contains a non-standard designation for the Arduino board. The figure shows Arduino UNO (you can order it at this link http://ali.pub/1v22bh) but you can use any other. Connections can be made, for example, on a breadboard and wire or solder. After everything is properly connected and checked, you can connect Arduino via USB to your computer and load a sketch into it:

Char pc_code = 0;

Void setup ()
{
pinMode (2, OUTPUT);
Serial.begin (9600);
}

Void loop ()
{
if (Serial.available ()> 0)
{
pc_code = Serial.read ();
if (pc_code == "a")
{
digitalWrite (2, HIGH);
}
else if (pc_code == "b")
{
digitalWrite (2, LOW);
}
}
}

How to properly configure Arduino and upload a sketch to it has already been described on the page. Further, to turn on the device, it is necessary to send the symbol "a" to the Arduino, to turn off the symbol "b". In order to send a symbol to arduino, you can, in the Arduino IDE, enter the Tools-Serial Port Monitor tab and in the window that appears in the upper text box, enter the symbols and send by pressing the "send" button, the symbol will come to the arduino, and for this case, if you send the symbol "a" then the device will turn on, if "b" then turn off accordingly. If Arduino does not accept characters, then you need to set the same speed as specified in the sketch in the lower right corner of the serial port monitor window, i.e. 9600 baud In order to turn on 2 devices, you can slightly change the sketch:

Char pc_code = 0;

Void setup ()
{
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
Serial.begin (9600);
}

Void loop ()
{
if (Serial.available ()> 0)
{
pc_code = Serial.read ();
if (pc_code == "a")
{
digitalWrite (2, HIGH);
}
else if (pc_code == "b")
{
digitalWrite (2, LOW);
}
else if (pc_code == "c")
{
digitalWrite (3, HIGH);
}
else if (pc_code == "d")
{
digitalWrite (3, LOW);

Top related articles