Control of electrical circuits via USB. Managing devices via a computer's USB port - Hardware - Computer and electronics!!! Full control over USB-connected equipment

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 is present in each of them today. The only chip of the device is a common microcontroller ATmega8. It is necessary for organizing communication via the bus. USB. Although it does not have 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, switches it to low-speed LS mode with an exchange rate of 1.5 Mbit/s, which allows you to decrypt computer messages in software. Resistors R4 and R5 eliminate transients that occur during information exchange, which increases operating 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.

Device control signals are generated at the outputs PB0-PB5 and PC0, PC1 of the microcontroller. High logic level - voltage about 3.4 V. Voltage low level close to zero. You can connect devices to the outputs that consume a current of no more than 10 mA (from each output). If required large values current or voltage, then matching nodes should be used.

The device is assembled on a breadboard, a printed circuit board has not been developed. MLT resistors are used, capacitors C2 and SZ are high-frequency ceramic capacitors, C1 is 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 environment Bascom-AVR versions 1.12.0.0. A library is used to work with the USB bus swusb.LBX, which performs software decoding of USB signals in real time. The resulting program code from a file with the HEX extension should be loaded into the FLASH memory of the microcontroller. The state of the microcontroller configuration bits must correspond to that shown in Fig. 2.

Figure 2

When you connect your device to your computer for the first time operating system will discover something new USB HID compatible device named " uniUSB" and will install the necessary drivers. After a few seconds, the device is configured and ready for use. The UniUSB program was created to work with it. It is available in two versions: for 32-bit (x86) and 64-bit (x64) operating systems of the Windows family. The 32-bit version has been tested on Windows 98, Windows XP, and Windows 7 operating systems, while the 64-bit version has been tested only on Windows XP x64.

Program UniUSB written in language PureBasic(version 4.31) using the library of user-defined functions HID_Lib, supporting work with USB HID devices. Appearance The program window is shown in Fig. 3.

Figure 3

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

To add or delete a table column, you need to right-click on it and select the required action in the menu that appears. When you connect a device to a USB port, the program will detect it and activate the Start button located at the top of the window on the toolbar. Clicking this button starts the process of sorting through the table columns and setting the output states indicated in them. For greater clarity, to the left of the table the numbers of outputs on which this moment The logic level is set high. The search speed (time in milliseconds between transitions from column to column) is set in the "Speed, ms" field.

Please note, operating room Windows system- multitasking! This means that 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, you should not expect great accuracy in maintaining time intervals of less than 100 ms.

To briefly stop sorting through columns, use the "Pause" button. Pressing it again will continue the search from where it stopped. The "Stop" button completely stops searching through the table columns. If the exchange of information between the computer and the device fails or the device is disconnected from the connector Computer USB, the program will report an error by displaying a corresponding message in the status bar.

Source: Radio No. 2, 2011

13-01-2014

ATiny2313

Zakharov Denis, Ukraine

As you know, there are a sufficient number of interfaces through which a microcontroller (MCU) can communicate with external devices. If you need to connect the MK to a personal computer or laptop, then we can confidently say that it is best to use the RS-232 COM port interface.

The reason for this choice is obvious - almost all controllers have UART hardware modules, with which you can transfer information with minimal consumption of MK resources. In addition, there are many well-proven programs designed to work with the COM port. Since MK signals have TTL levels, a level converter is required to match with the RS-232 interface. It is often performed based on the affordable and popular MAX232 chip.

Picture 1.

The presented device (Figure 1) is designed to control devices using any PC with a USB port. Modern computers and laptops have several of these ports. Using this complex you can control light, TV and other devices. Executing devices do not necessarily have to be in close proximity to the PC.

The device consists of quite accessible and common elements. Both microcircuits are ATtiny2313 family microcontrollers. The first controller is connected to the computer's USB port and serves as a USB-COM format converter. The second connects to the first and constantly scans commands that are sent from the PC through the terminal program Terminal v1.9b.

Resistor R4 connected to USB pin 2 switches the device to low-speed LS mode, which allows you to decrypt messages from a PC when exchanging data at a speed of 1.5 Mbit/s using the program.

With the help of resistors R2 and R3, transient processes are eliminated. Capacitor C5 blocks impulse noise in the power circuit. Zener diodes D1 and D2 are necessary to match the logical levels of the MK and the USB input of the PC. For error-free data transfer between controllers, the frequencies of the quartz resonators must be 12 and 4 MHz.
Pull-up resistors should be connected to the /RESET pins in order to subsequently avoid arbitrary reset of the MC due to the influence of interference and static voltages. In this diagram, all commands are displayed on LEDs connected to port B. To control any devices, you must connect the controller outputs to a relay (Figure 2).

You can assemble the device on a breadboard, although it’s still better to use a full-fledged one printed circuit board. The elements can be placed, for example, as shown in Figure 3.

The program for the U1 microcontroller was developed by a friend of GetChiper in the Bascom-AVR environment. A library is used to work with the USB bus swusb.LBX. With its help, software decoding of the USB protocol is performed in real time. For the device to work with a PC, you need to install the appropriate drivers by copying them to HDD. When you connect for the first time, the device will be recognized and will ask for a driver. Next you need to specify the path to the folder with the files, and everything will work.

The U2 microcontroller program was written by me in the AVRStudio environment in assembly language. The block diagram of the MK operation algorithm is presented in Figure 4. The UART hardware module should be configured to interrupt upon completion of data reception. The MK itself will not perform any functions until an interruption occurs. To reduce power consumption, you can use the sleep mode, but in this design this was not necessary. As soon as commands are sent from the PC terminal, the MK will instantly begin scanning them. Currently the controller supports the following system commands:

-on1, on2, on3, on4, on5, on6, on7, on8- commands for setting ports to “log. 1";
-off1, off2, off3, off4, off5, off6, off7, off8- commands for setting ports to “log. 0";
-ser - set all ports to the active “log” state. 1";
-clr- reset all ports to “log.0” state.

After finishing entering each command, you must press Enter. This way the MK will be able to determine the end of the command and begin scanning it. The controller will respond “ok” to every correct command. If you enter incorrect data, “error” will be returned to the terminal line. An example of the command execution is shown in Figure 5.

Firmware version 1.0. It is necessary to set fuses in accordance with Figure 6. The next version of the firmware is being developed, where the MK will self-learn and change the command systems in the terminal.

MK software, Proteus virtual model and driver for PC -
Data transfer protocol between MK and PC - download

  • ....actually I wanted to see the connection between the USB port of one computer and the COM port of a second...or LPT port of a third...
  • Thank you! Typo fixed :)
  • Why use 2 MK? Does Attiny2313 really have little flash? Or is there simply not enough I/O ports? Okay then, you can see that USB is hanging on INT0/INT1.
  • The resistance of low-power relay coils is around 100-200 Ohms, not taking into account the saturated transistor (this is not a starter or a contactor). So the 50-200 mA suitable switch will not be afraid. The material is very interesting in terms of connecting the MK to USB without any interface chips and without the presence of hardware USB in the structure of the MK. But taking into account the goals and objectives of the original source http://www.recursion.jp/avrcdc/cdc-232.html, of the two MKs, one still performs the functions of a USB-COM converter. And a very cheap converter, which is certainly pleasing.
  • Here interesting citizen got caught with “noble manners,” judging by his nickname. What extremes are we talking about? It seems that the material does not even mention the type of relay or transistors. And if the relay is powered from 5V USB, then, of course, I would like to minimize consumption from the host on the PC. This can be achieved using optocouplers and additional power supply to the relay on the load side, which complicates the circuit. Or a couple more options. Is the emphasis in the article on optimization? The author achieved his goal and is doing the right thing by not posting a specific fee. For the one who will repeat, this node is enough.
  • Yes, the article is still the same... but is it worth it? I also wanted to comment on something as soon as I read it, including the diode. But you can't be anonymous here. That's why the author of AVR-CDC? I didn’t notice that DTR, DTS, RTS, CTS signals are used somewhere in the circuit. V-USB not enough? We have already written about two “bricks” above; one would be enough. And about the diode has already been corrected, thank Buddha! The diode is needed to protect the transistor from the self-induction voltage pulse of the relay winding at the moment the current opens. By the way, I remembered one implementation. The article was in Radio magazine, but it was also found on the Internet, if you are interested you can take a look.

A computer, some parts, tools, anyone can organize simple control of household appliances from this computer. Many devices used in everyday life have many functions, for example, most modern televisions can show a certain number of different channels, a fan may have a number of different operating modes, etc. In order to make such complex controls as, for example, switching channels or operating modes, in addition to the above, you will need additional knowledge, parts and tools, but anyone who has the above can do a simple switch on and off. Arduino communicates with the computer via a USB port; information can be transferred from the computer to Arduino through the development environment for Arduino (called Arduino IDE) which can be downloaded from the page on the official Arduino website. There are many different modules for interfacing Arduino with outside world, For example special module with a relay block for switching loads, when using such modules the work is greatly simplified, here we will look at self-production module with one relay for on/off household appliances, if necessary, you can make more than one such module and use them with one Arduino, thereby making it possible to easily control many household appliances. Arduino (any) has a certain number of pins general purpose which are indicated on the board simply by numbers or numbers with a wavy “~” sign. By connecting the Arduino to the 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(approximately +5V (HIGH)) or low (approximately 0V (LOW)). The Arduino also has a “GND” pin (as indicated on the board). If there is a high voltage on one of the general purpose pins, then by connecting something conducting current between this pin and the “GND” pin, a current will flow through what is connected electricity and the magnitude of this current will depend on the resistance of this object and can be calculated according to Ohm’s law, i.e. the lower the resistance, the greater the current, but if the resistance is too low, too much current will flow through the Arduino and it will burn out. The maximum current that an Arduino general purpose pin can produce can vary depending on the microcontroller it uses, but is usually 40mA = 0.04A - this may not be enough to turn on the relay that will turn on the device(s), so you need to use additional element for example a bipolar transistor. A bipolar transistor has three terminals: emitter, collector, base. The maximum current of the transistor is also limited as with Arduino and is usually higher, for example, for the popular KT315 the maximum current is 100mA = 0.1A. Bipolar transistors there are two types n-p-n and p-n-p you can use both types, but in different ways, and then we will consider using the KT315 transistor whose type is n-p-n. In order for the transistor to increase the current from the Arduino, it is necessary to connect its base to the Arduino output THROUGH A RESISTOR with a resistance of 1 kOhm (the resistor may say 1 k), connect the emitter of this transistor to the “GND” of the Arduino and the minus of the power supply or the “GND” of the power supply whose voltage equal to the voltage of the winding of the existing relay (let's say 12V), connect one of the terminals of the relay winding to the collector of the transistor, the other with the plus of the power source (+12V, let's say) and another part that doesn't affect the gain but is VERY important is the diode, which needs to be connected by the anode to the collector and the cathode to plus of the power supply (+12V). If the diode is imported, then most likely there will be a light stripe- it points to the cathode, the other terminal of the diode is the anode. The remaining terminals of the relay are the terminals 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 will close, they need to be connected in series with the device and this serial connection can be plugged into a 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, usually the following schemes are used:

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 (can be ordered via this link http://ali.pub/1v22bh) but you can use any other one. Connections can be made, for example, on a breadboard and with wires or soldering. After everything is correctly connected and checked, you can connect the Arduino via USB to your computer and upload the sketch to 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 is already described on the page “Simple control of a stepper motor from a computer via Arduino”. Next, to turn on the device, you need to send the symbol “a” to the Arduino; to turn it off, the symbol “b”. In order to send a symbol to the Arduino, you can: Arduino environment IDE, go to the Tools-Serial Port Monitor tab and in the window that appears in the upper text field, 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 in the lower right corner of the serial port monitor window as specified in the sketch, i.e. 9600 baud In order to include 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);

The circuit is based on 74hc595 shift registers, which are controlled by a PIC18F252 microcontroller. The output circuits are logical, i.e. log "1" or log. "0". They are underpowered These are different, so to control some devices you need amplifiers (keys). Do not forget about the galvanic isolation of the controlled devices from the circuit! The controller is controlled via USART protocol directly from the computer. Methods for implementing the protocol are discussed below.

Let's look at two ways to obtain USART.

1st, easiest way:
This is an RS232 to USART level converter. The diagram of which is given below.

I think there is nothing to tell here. Pins 7 and 8 are connected to the COM port of the computer, and pins 9 and 10 are connected to the main circuit of the device.

2nd method: USB to COM Converter

Almost all new computers do not have a serial COM port, and most of old technology, works precisely through this interface, the so-called RS232. New computers have a USB serial bus, but the data exchange protocol via USB is more complex than via RS232.
Thanks to the developments of the English company FTDI (Future Technology Devices International), it has become possible to convert USB into “virtual” serial port, data exchange with which is carried out using the usual well-known methods.

This USB to COM interface converter is designed for connecting a modem, scanner, various measuring equipment, i.e., to the USB bus. in fact, any device that previously used the RS-232 interface. Moreover, the user does not need any knowledge about the device and operation of USB. The software drivers supplied by FTDI create the impression that the exchange is taking place through a regular COM port. Of course, this is only true if software uses the COM port in standard mode.
The schematic diagram of the interface converter is shown in the figure. Its basis is the DD1 FT232BM chip, included in standard scheme recommended by the manufacturer.

The DD2 AT93C46 EEPROM chip does not need to be installed. It stores manufacturer identifiers (VID) and personal identifiers (PID), product serial number and other data. This is necessary if several devices on FT232BM chips are simultaneously connected to the computer via USB. Particularly important serial number, since the software driver relies on its uniqueness, associating one or another virtual COM port with a specific device. If there is no ROM, only one device can be connected to the computer, forming a virtual COM port. Instead of AT93C46, you can use AT93C56 or AT93C66. The ROM is programmed directly into the device using a special utility supplied by FTDI.

I shortened the circuit a little and did it like this. Power made from general scheme devices.


RX and TX pins to the main circuit of the device. The driver for this MS can be downloaded from. website for a specific OS.

The device operates through two-way communication with a PC at a speed of 115200 Kbps. with checking the command checksum. Those. erroneous data transmission is excluded. When the program starts, it polls for the presence of a connected device and if it is detected or lost during the process, the program will let you know.

This device is controlled by a program specially written by me and is in the archive. In principle, the program is like a demo; it can only control outputs. So, please contact us for an individual program for a specific case.



What else to read