Control of electrical circuits via USB. Controlling devices via a computer USB port on ATmega8 Controlling external devices via USB

Control devices via a computer's USB port.

P. VYSOCHANSKY, Rybnitsa, Transnistria, Moldova
A computer control device for various devices, the diagram of which is shown in Fig. 1, functionally similar to that described in, but connects to the USB port of the computer, which (unlike the COM port) is present in each of them today. The only chip in the device is the common ATmega8 microcontroller. It is necessary for organizing communication via the USB bus. Although it does not have 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, switches it to low-speed LS mode with an exchange rate of 1.5 Mbit/s, which allows you to decrypt the computer’s data programmatically. Resistors R4 and B5 eliminate transient processes 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 serve to lower the microcontroller supply voltage to approximately 3.6 V - this is required to match the level with the USB bus.
Device control signals are generated at the outputs PB0-PB5 and PSO, PC1 of the microcontroller. High logic level - voltage about 3.4 V.
Low level voltage is close to zero. You can connect devices to the outputs that consume a current of no more than 10 mA (from each output). 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; 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 Bascom-AVR environment version 1.12.0.0. To work with the USB bus, the swusb.LBX library is used, 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. For this purpose, the programmer was used in conjunction with the utility built into Bascom-AVR. The state of the microcontroller configuration bits must correspond to that shown in Fig. 2


When you connect the device to your computer for the first time, the operating system will detect a new USB HID compatible device named "uniUSB" and install the necessary drivers. After 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) operating systems of the Windows family. The 32-bit version has been tested in operating systems: Windows 98, Windows XP, Windows 7, and the 64-bit version has been tested only in Windows XP x64. The UniUSB program is written in PureBasic language (version 4.31) using the HID Lib library of user-defined functions, which supports working with USB HID devices. The appearance of the program window is shown in Fig. 3


In the same folder with its executable file there should be a file called UniUSB_code.txt or UniCOM_code.txt. The last option is necessary for compatibility with the UniCOM program proposed in . 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 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 the logic level is currently set high are highlighted. 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 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 the search of columns, use the button. Pressing it again will continue the search from where it stopped. The 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 computer's USB connector, the program will report an error by displaying a corresponding message in the status bar.

LITERATURE

1. Nosov T. Control of devices via the COM port of a computer - Radio, 2007, No. 11, pp. 61,62.
2. Ryzhkov A. US programmer for AVR and AT89S microcontrollers, compatible with AVR910. - Radio, 2008, No. 7, p. 28, 29.

Based on materials from the magazine "Radio 2`2011"
You can download the microcontroller firmware and PC program

Nowadays, in every electrical repair shop, and, for that matter, every radio amateur, the working day begins with turning on the PC, and only then the auxiliary electrical equipment. In other matters, the working day begins with turning on the PC not only for radio amateurs, but also for many users of this “device”, which has become an integral part of our lives. But turning on every day in the morning and turning off in the evening, well, let’s say, a printer, active speakers, a table lamp, or who knows what else you might find with an ordinary user, gets a little boring. Based on the agreed objectives, the goal was to create a load switch designed to turn on and off all related devices. I agree that you can take the path of least resistance and put an ordinary switch on everything, but it seems that this is not the way for a radio amateur.We will do it much more conveniently - we will build the machine into the network filter:

Since the device monitors the fact that the device is turned on and off according to the “slave-master” principle, the easiest way is to use a USB port. Let me point out right away that when using a USB port, the device works in laptops and computers with a power supply that turns off all power sources. For example, in the latest models of power supplies, a +5 volt USB port is present as a standby port even when the unit is turned off. The device will not work with them, or rather, it will, but is always on. Here it is necessary to use some other port as a signal source, and, accordingly, coordinate the signal of the source and the actuator.

In this device, the actuator is a relay with contacts rated at 16 amperes and a 5 volt coil. The use of a relay provides galvanic isolation of the signal source and the 220 volt network.

After conducting a series of experiments, it was found that when powered, according to the passport data, +5 volts, the relay consumes 150 mA of current. If you use a desktop computer, there are no problems, because the +5 volt USB port comes directly from the power supply and does not heavily load it. If you are satisfied with all of the above, you can stop and make your own version according to the diagram below.

But if you plan to use a laptop, then it is not advisable to pass an additional 150 mA through the computer; in this case, you need to use an additional 5 volt power supply. In this embodiment, a switching power supply from a mobile phone charger was used. Any unregulated power source can be used, provided that it can provide a voltage of 5 volts and a current of about 250 mA, taking into account the margin. You can also use another voltage, but in this case it will be necessary to take a relay with the appropriate supply voltage.To match the signal source with the relay and power supply, it is necessary to install a transistor switch of the following type:

The transistor selected is type KT815 with any letter and a resistor of 1 kOhm, diode - KD522. With this connection scheme, the current consumption showed 4 mA, which is not so significant for a laptop. LED1 and LED2 indicate the status of the circuit. If desired, along with resistors, they can be excluded from the circuit.

The 220 V control device is assembled in the free space of the socket housing - network filter.Schematic file in plan located

Not only any programmer, but also every sufficiently experienced user knows that you can transfer a lot of routine work to a computer or, for example, make it do something for you when you are on vacation. So, the computer can cope quite well with monosyllabic replies to emails (say, I received the letter, I will answer in detail later). Can a computer, for example, feed your cat? Water your flowers? After all, just turn the lights in your apartment on and off every night to make it seem like you're at home?

In Hollywood films, we often see home automation of this kind, usually attached to the film's hero - a scientist or inventor, usually a little crazy, like Doc Brown from Back to the Future. There are legends about fidoshniks who tied electrical plugs to the print heads of dot matrix printers so that they could turn off their computer remotely. They even used the retractable tray of the CD drive to press the damned button. But is it possible to do this somehow more, let's say, regularly? Of course, there are many industrial-grade solutions for this type of task. Computers control machine tools, cranes, entire power plants, and this, in principle, does not surprise anyone. Moreover, computers also cope with turning on and off the lights in a room, this even got its name - "smart House", the only problem is that industrial computers are not sold in stores, and where they are sold, they are very expensive, but “smart home” components are usually specialized computers that do not require reprogramming by the end user. Simply put, they do what the manufacturer intended, but that's not always what you want.

Our devices are designed to fill a niche that for some reason is not noticed by manufacturers of computer peripherals: to enable the user of an ordinary home computer to use it to manage something more tangible than emails. To be more precise, using our devices you can make your computer control any devices in which electric current flows. All devices we currently offer are connected to a computer via a USB port (in some cases you can also use a classic COM port, but where can you get one these days). Most of our device models have built-in electromagnetic relays; the maximum switching power of these relays is small, only 300 W, but this is quite enough to control a table lamp or fan, to turn on and off active network equipment (routers, switches, access points and the like), and also for most not very fancy computers. If greater load power is required, it is necessary to use external relays, for example, any power relays with a supply voltage of 12 or 24 V, which you can also order from us or select yourself; The power of most relays available on the market is quite enough to control heating appliances, kettles, pumps and other powerful consumers of electricity. Some of our devices are designed initially to connect such relays, while others can be connected to external relays via circuits controlled by built-in relays.

In addition to controlling the electrical load, our devices are able to respond to external “stimuli” - input lines (signal circuits), which are ordinary electrical wires that can be closed or open. The device notifies the control computer about a change in the status of the input line, and, in addition, can react to such a change independently by turning on or off certain controlled circuits, which in some cases allows you to do without a control computer (for example, turning on and off the lights in a room, when the control computer is not working).

Naturally, control software is required on the computer side. For users with programming skills, we offer (free of charge) a number of programs in source code, including libraries of functions for accessing our devices; if independent writing of software on the host side is not part of your plans, we can offer control programs in the form of statically assembled (that is, not requiring installation) executable files for Linux and Windows. Since our devices support the HID standard, no drivers are required for them - or rather, the necessary driver is already in your system. Please note that the firmware of our devices is also available in source text form, and with the help of a programmer (again, we can offer to purchase a suitable programmer from us or select it yourself) you can change the behavior of our device, adapting it to your needs. We distribute both firmware and host software freely under the terms of the GNU GPL v.3 license.

Let us immediately note that we are focusing mainly on technology lovers - people who are at least comfortable with a screwdriver (although you can easily do without a soldering iron; we have already soldered everything you need). However, in a limited edition we are releasing a device that does not even require a screwdriver to use - it is an extension cord with five sockets and a built-in UNC device that allows you to control four of the five sockets (the last socket is always on; it can be used, for example, to power a control computer ). The most obvious use of this version of the device is to control everything that is around your computer desk: for example, you can use a combination of keys on the keyboard to make your desk lamp turn on or off, turn on and off the fan, speakers, modem and everything else that happens to be within a radius of length power cord from your computer and has a power rating of no more than 300 W. Such a device, however, is more of a toy than anything else; it is ideal as an original gift for a tech geek, but the full capabilities of our devices are revealed when you independently install controlled circuits (remember, for this you only need a screwdriver and a wire stripper). Here your possibilities are practically limited only by your imagination. Our device can be turned on instead of a light switch in the room, it can control the lighting and ventilation of the aquarium, an electromagnetic door lock (more precisely, only the electromagnet itself, without an expensive control module) will allow you to make your computer work as a doorman, a stepper motor will open and close the window, and the electromagnetic water valve from the washing machine will allow you to automate the watering of indoor flowers (here we rely on your common sense: computers sometimes break down, you should provide flood protection in case your system does not work as you expected - for example, it would be reasonable take water for irrigation not from the water supply, but from a container with a limited volume).

Keywords: budget smart home, DIY smart home, computer power control, computer power control, home automation, USB power on, computer room light control, lighting control, light controller, USB light control

– Igor (Administrator)

Today, many users have more than one USB device. In fact, connecting via USB has become an almost universal method. Most people have probably even forgotten about the existence of such ports/connectors as LPT and COM. For example, a fairly large part of peripheral devices (printer, etc.) are connected only via USB.

USBDeview manager program for managing USB devices

If you have many different USB devices and you actively use them, then most likely you have encountered problems and limitations of the standard Windows USB device manager. To manage connections more efficiently, there is a special utility called USBDeview from NirSoft (download link).

Note: The limitations of the standard Windows device manager are quite justified. Since this functionality is used by both experienced and novice users.

Note: There is quite a lot of information on the developer page. The download links themselves are at the very bottom.

The utility is portable and can be run anywhere. The executable file weighs about 170 KB and comes for both 32-bit and 64-bit versions of Windows. For such a small program, it has a surprisingly large number of useful features.

Note: You can find out more about all its capabilities on the developer’s website. This article will discuss only some of the most used of them.

As you probably noticed, the picture shows some of the available functions. Connect, disconnect, delete, open, check speed, change letters and much more.

Useful features of the USBDeview program

For various reasons, the Windows registry keeps records of all USB devices ever connected to the operating system. USBDeview will show you all the information about all devices for which registry entries have been saved. If necessary, you can correct the necessary information in the registry (the utility allows you to immediately open the registry key with information about the usb device).

Note: Storing device information is used for at least several reasons. Firstly, reconnecting the device will be faster. Secondly, for devices that require the installation of non-standard drivers, this is a vital function. Otherwise, you would have to install the drivers again every time. Third, devices can be assigned drive letters. And to limit the risk of problems with the same letters, it makes sense to assign a unique letter to each device.

Although the standards applied to determine the read and write speed of each version of the USB communication protocol, the speed may vary on each device. USBDeview allows you to perform a simple test using one large file for writing and reading. Of course, a more correct test would be copying and reading different groups of files with different sizes, but, nevertheless, even this test allows you to estimate the approximate speed.

The utility also allows you to change the drive letter. Of course, there are other ways, but in this case you can do it in just a couple of clicks.

Overall, USBDeview is an excellent USB device manager.

Computer control device
various devices, the diagram of which is shown in Fig. 1, functionally similar
described in, but connects to the USB port of a computer, which (unlike
from the COM port) is present in each of them today. The only chip of the device
- a common microcontroller ATmega8. It is necessary for organization
USB bus communication. Although it lacks specialized hardware
module, this function is performed in software. Resistor R1 connected between
positive terminal of the power supply and the USB D-bus line, transfers it to low-speed
LS mode with an exchange rate of 1.5 Mbit/s, which allows you to decrypt parcels
computer programmatically. Resistors R4 and R5 eliminate transients
processes that arise during the exchange of information, which increases the stability of work.
Capacitor C1 blocks impulse noise in the power circuit, which also improves
stability of the device. Diodes VD1 and VD2 are used to reduce the voltage
supply the microcontroller to approximately 3.6 V - this is required for
level matching with the USB bus. Device control signals are generated on
outputs РВ0-РВ5 and PC0, PC1 of the microcontroller. High logic level -
voltage is about 3.4 V. Low level voltage is close to zero. To the exits
You can connect devices that consume a current of no more than 10 mA (from each output).
If large current or voltage values ​​are required, then nodes should be used
agreements shown in Fig. 5 and 6.

The device is assembled on a breadboard,
no printed version was developed. MLT resistors and capacitors C2 and C3 are used -
ceramic high-frequency, C1 - K50-35 or similar imported. Diodes
silicon with a voltage drop across the junction of about 0.7 V. Program for a microcontroller
developed in the Bascom-AVR environment version 1.12.0.0. For USB bus operation
The swusb.LBX library is used, which performs software signal decoding
USB in real time. The resulting program code
from a file with the HEX extension should be loaded into the FLASH memory of the microcontroller.
For this purpose, the programmer was used in conjunction with the built-in Bascom-AVR
utility. The state of the microcontroller configuration bits must correspond
shown in Fig. 2. When connecting the device to the computer for the first time, the operating system
the system will detect a new USB HID compliant device with the name
“uniUSB” and install the necessary drivers. In a few seconds
The device is configured and ready to use.

A program was created to work with it
UniUSB. 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 verified to work
in operating systems Windows 98, Windows XP, Windows 7, and 64-bit -
only on Windows XP x64. The UniUSB program is written in PureBasic language (version
4.31) using the HID_lib library of user-defined functions,
supporting work with USB HID devices. Appearance of the program window
shown in Fig. 3. In the same folder with its executable file there must be
a file called UniUSB_Code.txt or UniCOM_Code.txt. The last option is required
for compatibility with the UniCOM program proposed in . In this file
The script for controlling external devices is stored. When the program starts, the data
from the file are loaded into the table located in the main window, and upon completion
works are saved in a 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 use it
right-click and select the required action from the menu that appears.

When connecting a device to a USB port
the program will detect it and activate the button located at the top
windows on the toolbar. Clicking this button starts the process
searching through the table columns and setting the output states indicated in them. For
For greater clarity, the output numbers are highlighted to the left of the table, on
which are currently set to a high logic level. Speed
search (time in milliseconds between transitions from column to column) is set
in the “Speed, ms” field.

Please note that the operating system is Windows
- multitasking! This means that CPU time is divided among many
sometimes hidden from the user processes that are executed in turn with
taking into account the priorities established in the system. So don't expect much
accuracy of maintaining time intervals less than 100 ms. For short term
To stop iterating through columns, use the button Click on it again
will continue searching from where it stopped. The button completely stops sorting through columns
tables. If during the exchange of information between the computer and the device
a failure occurs or the device is disconnected from the computer’s USB connector,
the program will report an error by displaying the corresponding
message.

LITERATURE

1. Nosov T. Control of devices
via the computer's COM port. - Radio, 2007, No. 11,0.61,62.

2. Ryzhkov A. US programmer
AVR and AT89S microcontrollers, compatible with AVR910. - Radio, 2008, No. 7, p.
28, 29.

From the editor. Programs for the microcontroller and computer are located
on our FTP server at ftp:// ftp.radio.ru/pub/2011/02/uniUSB.zip