Control of electrical circuits via usb. Device management through the computer's USB port - Hardware - Computer and electronics for it!!! Full control over USB-connected equipment

A computer control device for various devices, the diagram of which is shown in fig. 1 plugs into the computer's USB port, which is in every one of them today. The only chip of the device is a common microcontroller ATmega8. It is necessary for the organization of communication on the bus USB. Although it does not have a specialized hardware module, this function is performed by software.

Picture 1

Resistor R1, connected between the positive output of the power supply and the USB D-bus line, puts it into low-speed LS mode with an exchange rate of 1.5 Mbps, which allows you to decrypt computer messages programmatically. Resistors R4 and R5 eliminate transients that occur during the exchange of information, which increases the stability of the work. Capacitor C1 blocks impulse noise in the power circuit, which also improves the stability of the device. Diodes VD1 and VD2 serve 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 - the voltage is about 3.4 V. The low level voltage is close to zero. The outputs can be connected to devices that consume a current of not more than 10 mA (from each output). If large values ​​of current or voltage are required, then matching nodes should be used.

The device is assembled on a prototyping board, a printed circuit board was not developed. MLT resistors, capacitors C2 and C3 - high-frequency ceramic capacitors, C1 - K50-35 or similar imported ones are used. 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. The library used to work with the USB bus swusb.LBX, which performs real-time software decoding of USB signals. 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 microcontroller configuration bits must correspond to that shown in fig. 2.

Figure 2

The first time you connect your device to your computer, the operating system will detect a new USB HID compatible device named " uniUSB" and install the necessary drivers. In a few seconds, the device is configured and ready for use. The UniUSB program was created to work with it. It is presented 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, Windows 7, and the 64-bit version has been tested only on Windows XP x64.

Program UniUSB written in language PureBasic(version 4.31) using the user-defined functions 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 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 is started, the data from the file is loaded into the table located in the main window, and when the work is completed, it is saved in the file. Clicking the left mouse button on the table cells allows you to change their state: 1 - high logical level, 0 or empty - low logical level.

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

Please note that the Windows operating system is 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 great accuracy in maintaining time intervals of less than 100 ms.

To briefly stop iterating through columns, use the "Pause" button. Pressing it again will continue the iteration from where it stopped. The "Stop" button completely stops iterating over the columns of the table. If the communication between the computer and the device fails or the device is disconnected from the computer's USB connector, the program will report an error by displaying an appropriate 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 the microcontroller (MC) can communicate with external devices. If you need to connect the MK to a personal computer or laptop, then we can say with confidence 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-established programs designed to work with the COM port. Since MK signals have TTL levels, a level converter is required to match the RS-232 interface. It is often performed on the basis of 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. With the help of this complex, you can control the light, TV and other devices. Executing devices do not have to be in close proximity to the PC.

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

Resistor R4 connected to USB pin 2 puts the device in low-speed LS mode, which allows decoding messages from a PC when exchanging data at a speed of 1.5 Mbps using the program.

With the help of resistors R2 and R3, transients are eliminated. Capacitor C5 blocks impulse noise in the power circuit. Zener diodes D1 and D2 are needed to match the logic 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 equal to 12 and 4 MHz.
Pull-up resistors should be connected to the /RESET pins in order to avoid arbitrary reset of the MK in the future due to the influence of interference and static voltages. In this scheme, all commands are displayed on the LEDs connected to port B. To control any devices, it is necessary to connect the controller outputs to the relay (Figure 2).

You can assemble the device on a breadboard, although it is better, nevertheless, on a full-fledged printed circuit board. Elements can be placed, for example, as shown in Figure 3.

The program for the U1 microcontroller was developed by GetChiper in the Bascom-AVR environment. The library 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 your hard drive. The first time you connect, 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 MC operation algorithm is shown in Figure 4. The UART hardware module should be configured to interrupt when data is received. The MK itself will not perform any function until an interrupt occurs. To reduce power consumption, you can use the sleep mode, but in this design it was not necessary to do this. As soon as commands follow from the PC terminal, the MK will instantly move on to scanning them. The controller currently supports the following command system:

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

After finishing entering each command, press Enter. Thus, the MK will be able to determine the end of the command and start scanning it. For each correct command, the controller will respond with “ok”. If you enter incorrect data, then "error" will be returned to the terminal line. An example of command execution is shown in Figure 5.

Firmware version 1.0. It is necessary to set the fuses in accordance with Figure 6. The next firmware version is being developed, where the MC self-learning and changing the command systems in the terminal will take place.

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

  • .... actually, I would like to see the connection between the USB port of one computer with the COM port of the second ... or the LPT port of the third ...
  • Thanks! Typo fixed :)
  • Why use 2 MK? Does Attiny2313 have little flash? Or just not enough I/O ports? Then okay, you can see that USB hangs on INT0 / INT1.
  • The resistance of the coils of low-power relays is in the region of 100-200 Ohms, not taking into account the saturated transistor (this is not a starter, and not a contactor). So 50-200 mA will not be afraid of a suitable key. The material is very interesting in terms of binding the MK to USB without any interface chips and without the presence of hardware USB in the MK structure. But given the goals and objectives of the original source http://www.recursion.jp/avrcdc/cdc-232.html, one of the two MKs still performs the functions of a USB-COM converter. And a very cheap converter, which certainly pleases.
  • Here is an interesting citizen caught with "noble manners", judging by the 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 by 5V USB, then, of course, I would like to minimize the consumption from the host on the PC. This can be achieved with optocouplers and additional relay power from the load side, which complicates the circuit. Or a couple more options. Is the emphasis in the article on optimization? The author has achieved his goal and is doing the right thing by not laying out 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 to opsitatsya? 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 did not notice that somewhere in the circuit signals DTR, DTS, RTS, CTS are used. V-USB not enough? About two "bricks" have already been written above - one would be enough. And about the diode has already been fixed, thank the Buddha! The diode is needed to protect the transistor from a voltage pulse of self-induction of the relay winding, at the moment of opening the current. Here, by the way, I remembered one implementation. The article was in the Radio magazine, but I also found it on the Internet, you can look at it if you are interested.

Computer, some parts, 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 can. a number of different modes of operation, etc. In order to make such complex control as, for example, switching channels or operating modes, in addition to the above, you will need additional knowledge, parts and tools, but everyone who has the above can make simple switching on and off. Arduino communicates with the computer via 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 Arduino to interact with the outside world, for example, a special module with a relay block for switching loads, using such modules greatly simplifies the work, 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. The 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 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 pin "GND" (it is marked on the board). If there is a high voltage on one of the general-purpose outputs, then by connecting something conducting current between this output and the "GND" output, 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 according to Ohm's law, i.e. 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 the Arduino's general purpose output can produce may vary 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) so you need to use an additional one to amplify the current element such as a bipolar transistor. A bipolar transistor has three terminals: emitter, collector, base. The maximum current of the transistor is also limited like that of Arduino and is usually higher, for example, the popular KT315 has a maximum current of 100mA = 0.1A. Bipolar transistors are of two types n-p-n and p-n-p, you can use both types but in different ways, and then we will consider the use of a KT315 transistor whose type is n-p-n. In order for the transistor to amplify the current from Arduino, it is necessary to connect its base to the output of Arduino THROUGH A RESISTOR with a resistance of 1kOhm (it can be written on the resistor 1k), the emitter of this transistor is connected to the "GND" of the Arduino and the minus of the power supply or "GND" of the power source, the voltage of which equal to the voltage of the existing relay winding (let's say 12V) connect one of the relay winding terminals to the collector of the transistor, the other with the plus of the power source (+12V is acceptable) and another one that does not affect the gain, but a VERY important detail is a diode that needs to be connected with an anode to the collector and a cathode to plus power supply (+12V). If the diode is imported, then most likely there will be a light strip on its case - it indicates the cathode, the other terminal of the diode is the anode. The remaining relay outputs are the outputs of its contacts, if there are two of them and they are not closed, then when sufficient current is applied to the relay winding, these contacts will close, they must 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 shown in the picture:

Figure 1 - Instrument control from a computer


This is a non-standard scheme for a better understanding, usually the following schemes are used:

Figure 2 - Controlling the device from a computer


Although in this scheme there is also a non-standard designation of the Arduino board. The figure indicates Arduino UNO (can be ordered from this link http://ali.pub/1v22bh) but you can use any other. Connections can be made, for example, on a breadboard and wires or soldering. After everything is properly connected and checked, you can connect the Arduino via USB to the 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 off the symbol "b". In order to send a symbol to the arduino, you can, in the Arduino IDE, go to the Tools-Serial 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 shift registers 74hc595, which are controlled by the PIC18F252 microcontroller. The circuit outputs are logical, i.e. log "1" or log. "0". They are of low power nye, therefore, amplifiers (keys) are needed to control some devices. Do not forget about the galvanic isolation of controlled devices with the circuit! The controller is controlled by the USART protocol directly from the computer. Methods for implementing the protocol are discussed below.

Let's consider two ways to get USART.

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

I think there is nothing to tell here. 7.8 pins are connected to the COM port of the computer, and 9.10 to the main circuit of the device.

2nd way: USB to COM converter

Almost all new computers do not have a serial COM port, and most of the old equipment works through this interface, the so-called RS232. The USB serial bus has appeared on new computers, but the communication protocol via USB is more complicated than via RS232.
Thanks to the development of the English company FTDI (Future Technology Devices International), it became possible to convert USB into a "virtual" serial port, data exchange with which is carried out by the usual well-known methods.

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

The DD2 AT93C46 EEPROM chip can be omitted. It stores the manufacturer's identifiers (VID) and personal (PID), the serial number of the product and other data. This is necessary if several devices based on FT232BM chips are simultaneously connected to the computer via USB. The serial number is especially important, 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 forming a virtual COM port can be connected to the computer. Instead of AT93C46, you can use AT93C56 or AT93C66. The ROM is programmed directly in the device using a special utility supplied by FTDI.

I shortened the schematic a little and did it like this. Power made from the general circuit of the device.


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

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

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



What else to read