Sunday, June 26, 2011

VB6 sending signal RS232

1. Use computers to control the real world

1.1 Introduction

The diagram on the right is a simplified model for computer control. It starts with the user interface. Users give instructions and read information from the control target through the user interface. The software in the computer will process the instructions and send it to the control/interface board through a communication port. The control/interface board will interpret and execute the instructions according to a pre-written protocol.

The interface board uses a micro controller to interpret and execute the instructions from computer. To make the micro controller understand the instructions, it needs to be loaded with a software. The software is called "firmware" to differentiate it from the software in the computer. The micro controller can read status of an input and give feedback to the computer. It can also switch an output to high (normally 5 volt) or low (0 volt).

However, an interface board cannot drive an electric device directly since the output from the micro controller can only take very small amount of current (normally less than 20 mA). So we need to use a relay or a driving board between the interface board and an electric device. A driving board normally consists of integrated circuits (IC) that can take larger current and can be switched by the micro controller outputs. The RSVB1500 adapter board is an example of such driving board. A relay is an electric switch. When a small current is applied, it can switch on a much larger current. You can find more information in the section 1.4 of this tutorial. Sometimes the interface board and the driving board are integrated into one board (the RSVB1600 board and RSVB1700 controller belong to this type).

1.2 User interface, software and computer communication

I here put the 3 topics under one section since they are very closely related to each other. A user interface is a part of software that is used to get information from user and to display the information it received. The software needs to be written using a programming language. There are many programming languages available for writing a software, such as assembly language, C language, Java, Basic, and Visual Basic. Among these languages, C language is the most powerful and is the major language for making complicated application software. As it is not easy to command, the language is normally for professional software engineers. Visual Basic (here after we will refer it as VB) is a powerful and very user friendly language from Microsoft. It is easy to remember the commands and easy to build user interface. VB is an excellent tool for those who want to develop automation solution but do not have training on programming. The RSVBcon software is made using VB. It services as a good example about the capability of VB. The software written using a programming language need to be translated to a format that can be executed by a computer. We call this process "compile". The tool used for the translation is called "compiler". Each language need its specific compiler.

The software sited in a computer talks to a interface board through a communication port. It can be a RS232 port, a parallel port, a LAN port, or a USB port. RS232 port was mainly used for modem connection. It is also convenient for interfacing with small instruments. Parallel port is mainly used for controlling a printer. LAN port is useful for networking such as internet connection. USB port is just getting more and more popular nowadays because of its hot "plug/play" feature. It is very easy for a device user. However for a USB interface developer, it demands very profound knowledge in computer operation system.

For a VB based software, the most convenient communication approach is using a RS232 port. This is because RS232 is very simple to setup (only need two wires for communication) and VB can send commands and receive feedback through a RS232 port directly. Even if a PC does not have a RS232 port, a USB port can be easily converted using a USB/RS232 converter. The price of a converter ranges from 20 to 40 dollars. You can find more details about how to use VB to send out commands through a RS232 port in section 2.

1.3 Control/interface board and microcontroller

1.3.1 Microcontroller

A microcontroller is an integrated circuit (IC) that includes data processor, program storage, data storage, and input/output. It needs to be programmed before it can be used. The developer first write a software (firmware) using a specific language. Then the firmware is compiled to a format usable by the microcontroller. The compiled software is then loaded to the microcontroller. This step is called programming and the tool for loading firmware is called programmer. The firmware in the microcontroller interprets the commands from computer and tells the microcontroller what to do. It will also report its status to computer. To come out a right firmware, the developer needs to know detailed structure of a microcontroller and the communication protocol between the computer and the microcontroller. This is normally the biggest barrier for a person to develop an automation solution without a background in computer control. The RSVB1000 board saves a developer the efforts in dealing with these issues.

1.3.2 Input and output of a microcontroller

Input is like the ears or eyes and the output is like mouth or hands. An output drives a device to do something and an input checks the status and gives feedback to controllers. In a control, it is not sufficient just to be able to tell a device what to do. It is necessary to monitor the status of the devices so that we know if a device has done what it is supposed to do and decide next action according to its status. The first type control is called "open loop" control and the second type is called "closed loop" control.

Input and output can be of analog type or digital type according to the signals they process or generate. An analog signal has continuous value (such as voltage and current) while a digital signal consists of "0" and "1" or their combinations.

To allow a computer to process an analog signal, the signal needs to be converted to digital type first. The RSVB1000 board has two 10-bit analog-to-digital converters for handling analog signals. Here the term "10-bit" is a description of resolution for the A/D converter. The maximum value for a 10-bit variable is 1023. The digital range 0-1023 is used to cover 0-5 volt analog input. One digital unit will be equivalent to 5 mV. If the analog signal changes less than 5 mV, the A/D converter will not be able to detect the difference. An A/D converter can be used with analog type sensors to achieve closed loop control, such as use a temperature sensor to control a heater.

A digital input monitors the two status: logic low (0) and logic high (1). The two status are associated with voltage change (normally 0 volt is for logic low and 5 volt is for logic high). The RSVB1000 has 7 digital input for monitoring the high and the low status. We also call them TTL compatible input (TTL=transistor-transistor logic). Some digital input can have extra data processing function. For example, it may count how many times the status has changed. The counter in the RSVB1000 and RSVB1600 boards belong to this type. A counter can be used with a motor encoder for position control and speed control.

1.3.3 Function of interface board

An interface board consists of a microcontroller and other functions to allow a microcontroller to work properly. Among the functions are power supply, external clock, input/output connectors, signal conditioning components, and circuit for communicating with computer. The RSVB1000 interface board is made ready for receiving commands from a RS232 ports. For example, when a string "H6" is send out from a VB software, the interface will pass the information to its microcontroller. The firmware in the microcontroller interprets it as "switch output 6 to 5 volt" and makes the microcontroller do it accordingly. If this command is executed, the LED for output 6 will light up. If you use a multimeter to measure, the voltage of of the pin for output 6 will be around 5 volt.

1.4 Drive electric devices

An electric device can be a light bulb, a valve, or a motor. Each device has a working range in voltage and current. If the voltage applied to a device is above the upper limit of a device, the device may get damaged. On the other hand, if a device draw more current than the output current of a driving board or a power supply, the system may shut down or even get permanent damage.

1.4.1 Use a microcontroller output to drive a device

Suppose we want to control a valve with a 12 volt working voltage and 700 mA current. An output on a microcontroller can only draw less than 20 mA current and can only provide 5 volt. So it cannot drive the valve directly.

One approach is using a relay. A relay is like a switch that allows you to use a smaller power to switch a bigger power. The diagram on the right shows how a relay works. When a small trigger voltage is applied to a relay, a larger power can be switched on (for the valve, the power supply should have a 12 volt output voltage and above 700 mA output current). When the trigger voltage is removed, the power supply for the device is also cut off. Some relays can be triggered directly by a microcontroller output (such as some solid state relays).

Another approach is using transistor based integrated circuits. This kind of ICs can be directly triggered by a microcontroller output. The RSVB1500 adapter board and the RSVB1600 control board use this type of ICs. Sixteen of their outputs can have up to 500 mA output current and up to 24 volt output voltage. Sometimes we can use a microcontroller output to switch a transistor based IC and let an output of the transistor IC to switch a relay for much higher current.

1.4.2 Match supplied voltage with the working voltage of a device

Sometimes the output voltage of a power supply is too large for a device. A voltage regulator may be used to lower a portion of the output to the desired voltage. There are many low cost voltage regulators available in the market (such as LM317, L7805, L7812). Their connection is quite simple. Another approach is connecting a resistor in series with the device so that the resistor will consume the extra power. Although this approach is very easy to do, the resistor simply convert the power to heat and gets it wasted. So this is normally used when the total current is very small.

A more advanced approach is using the pulse width modulation technique (PWM). In a PWM regulated output, a continuous supply is divided into many small pulses. During each pulse period, the output is set to high for some time and set to low for the rest of the period. The number of pulses per second is called PWM frequency. The ratio of time that an output is maintained high to the total time of a pulse is called duty cycle. A PWM output can be used to switch a power supply on and off quickly. It will lower the current flowing through a device without interrupting its operation. For example, we may use a 24 volt power supply to drive a 12 volt heating element by setting the PWM duty cycle at 50%. PWM output can also be used to control the speed of DC motors. The RSVB1000 board has two PWM outputs. They are used in RSVB1500 board for manipulating wattage for output 1-8. Detailed information about how to use PWM to control motor speed and to adjust output voltage is available in the user manuals of the boards.

1.4.3 Match the working current of a device with the output current of a supply

For voltage, we need to make sure the supplied voltage not be higher than the working voltage of a device. In term of current, it is the other way round. We must make sure supplied current capacity (output current) is higher than the working current (or rated current) of a device.

The working current of a device depends on two factors: supplied voltage and the resistance of the device. For most cases, the current can be estimated using Ohm's law. Let us use the relay in section 1.4.1 as an example. If the coil has a resistance of 100 Ohm and its working voltage is 5 volt, we can estimate the current flowing through the coil will be 50 mA. If we use a microcontroller output to drive the relay coil directly, the current flowing through the output will be too high (one output can only allow 20 mA). The microcontroller will either shut down or get damaged. We may try to add a resistor in between to regulate the current. If the resistor is too large, it may reduce the power below the working range and the coil may not be powerful enough to do a switch. Other options include selecting a relay with smaller working current (such as a solid state relay) and using a transistor type IC in between.

For a DC motor, we cannot simply use Ohm's law to estimate the working current. This is because a running DC motor can generate an opposite current to suppress the current flow. The faster it runs, the lower the net current flowing through the motor. The current flowing through a free running motor is normally 10-20% of the value estimated using Ohm's law. When a DC motor is slowed down due to increased load, its current will increase to make it move faster. If the motor is stopped due to blockage or overloading, the current will surge by 5 to 10 times. If the power is not cutoff quickly, the system can get damaged. In some DC motor systems that require high reliability, current detection circuit is built in.

1.4.4 Good practice in handling electric devices

  • Always check the specification of a device before connecting it to the system. Most datasheet can be easily obtained from internet.
  • Discharge your body before touching an electronic board as many electronic components are sensitive to static charge. This can be easily done by touching a water tap using a hand.
  • Make sure the power is switched off before adding or removing a component.
  • When switching on an electronic board, look at the power light indicator. If it does not light up or the light is not steady, switch it off immediately. Either the power supply is not right or a device is connected incorrectly.

Back to top

2. Visual Basic programming for control

2.1 Introduction

VB is a very easy yet very powerful application development tool under the Microsoft Windows family. As you may already know, it is possible to get your first program running in less than an hour. There are three editions of VB, they are the learning edition, the professional edition, and the enterprise edition. To develop software for control, a professional edition is necessary. The tutorial here is based on professional edition of VB6.

When we start up VB, a dialog panel will appear, asking us to select a project type (see figure 2.1). For control software development, we will mainly use the "Standard.exe" project under the "New" menu bar. When you click on the icon representing "Standard.exe", the VB development environment will show up (see figure 2.2). Double click on an icon in the tool box, that icon will appear in the "Form1" frame and the relevant properties will be displayed in the property panel. Each icon represents an object, such as a button, a text box, or a timer.

Figure 2.1 VB project selection panel

Figure 2.2 VB development environment

In a VB program, all the actions must be triggered by letting something happen to an object. For example if we want a text box to change content, something must happen to an object. This event can be a mouse click at a button, a timer is enabled, or a check box is checked. This is one of the major difference between VB and other traditional programming language.

2.2 Setup programming environment

VB has a special function for RS232 communication called MScomm. It is not included in the standard tool box. It needs to be added before making a control software. To do this, first go to menu bar "Project" and select "Components" in the drop down list. Then check "Microsoft Comm Control 6.0" (see figure 2.3) and click on the "Apply" button. A telephone icon will appear in the tool box. Double click at the icon, it will appear in the form and its properties will appear in the properties panel (see figure 2.4). One important property is CommPort value. It is the number of the RS232 port (COM port) used for communicating with the interface board. The default value is 1. If your computer has more than one COM port or you use a USB converted port, the number could be different. If this setting is not correct, the PC will not be able to communicate with the interface board. A convenient way to find out the correct port number is to use the RSVBtest software shipped with the RSVB1000 and RSVB1600 boards. It can automatically detect the port number. Another important property is the communication speed in RS232 setting. It must be the same as the setting in the interface board. The default value in the VB is 9600 (bit per second). The RSVB1000 and RSVB1600 boards use a faster communication speed (38400). You need to make the change in VB accordingly. For other MScomm properties, we can leave them as the default setting.

Figure 2.3 Add MScomm to the tool box

Figure 2.4 Set right properties for RS232 communication

2.3 A simple programming example

After setting up the development environment, we can start building a program to communicate with a RSVB1000 or a RSVB1600 board. The first thing is to build a user interface. Double click at the button icon, the text box icon, and the MScomm icon. The form will be like the one in figure 2.4. Then set the RS232 setting and the CommPort to the correct value. Double click at the form will bring out the programming panel for writing code.

Figure 2.5 is the programming panel with a simple program. It is divided into 3 sections (subs). Each sub tells what to do under an event. The first sub tells what to do at the beginning when the form is displayed (remember what we said before: something must happen to an object to trigger actions). It will do two things. One is to open the selected RS232 port. It needs to be opened before we can send out commands. The 2nd thing is to make the text box blank. The 2nd sub tells what to do when you quit a program. When the form is closed, the RS232 port is closed also. The 3rd sub tells what to do when the "command1" button is clicked. It will do 3 things. First it will send command "H6" to the interface board. The phrase "chr(13)" is equivalent to pressing the "enter" key. When the interface board receives "H6", it will set output 6 to 5 volt (high status). The 2nd thing is to send out command "A1" to the interface board. When the interface board receives this command, it will send out the value of analog input An1 to the computer. The 3rd thing is to wait until the data from RSVB1000 reaches the computer and put the data to the text box. To understand this part, we need to go a bid deeper about RS232 communication in VB.

When we add a MScomm object to form1, the computer will automatically allocate two special memory zones for receiving and sending out data. It is called inputbuffer and outputbuffer. When VB sends out "H6", the computer cannot send all of it at one go. This is because RS232 communication is of serial type. It can only send one bit at a time. Command "H6" has two characters and it is equivalent to 16 bits (In ASCII code, each character is represented by a 8-bits value). The RS232 protocol will add 2 more bits for each character. One is the starting bit and the other stop bit so that the interface board will know when a complete character has been transferred . So some bits has to wait in the output buffer before being sent out. When the interface board send data back to PC, it first divides the analog value into bits and then add the starting bits and the stop bits. Then the bits are send out one by one. When the bits arrive at the computer, they will gather in the inputbuffer to form a complete character or number. If the communication speed is set at 38400 bits per second, the time for transferring one character is around 0.25 mili second. This may appear very fast to you but it is much slower than the execution speed of the program. If we read the inputbuffer immediately after sending out "A1", the data we get will be blank as the command is still in its way to the interface board.

The paragraph from "Dim dummy" to "loop until ..." is a delay program. It checks the number of bits in inputbuffer continuously. When its data reach one byte, the data is removed and put to the text box. A command line like "A=B" in VB is not exactly like "A equals B". Its meaning in a program is "put value of B to A". A and B are two places for holding data or two variables. After the program executing "Text1.text = Val(MSComm1.input)", the content in inputbuffer will be displayed in text1 box and the inputbuffer will be empty and ready for next coming data.

Figure 2.5 Code writing panel and a simple program

Figure 2.6 Run the program

To try out this program, first connect a RSVB1000 board or a RSVB1600 board to a RS232 port and apply power to the board. Then go to Menu bar "Run" and select "Start with full compile (see figure 2.6). The program is compiled and ready to run. When you click at the "Command1" button, the LED representing output 6 in the board should light up and text1 box should show some number. If you see these indeed, then you have successfully build an application software that can tell a device what to do and can monitor its status.

Back to Top

2.4 A more advanced example

In the above example, we need to click at the button to see something happen. You may wonder how we can let things happen automatically. You may also notice in the above example we need to remove the data from inputbuffer before next data can go in, otherwise different type of data can mix up. In next example, we will show how to achieve automation and how to read more inputs at one go.

First we will build an interface as shown in figure 2.7. The ten text boxes belong to one array (Text1). Each of them has its own name (text1(0) to text1(9)). We can make an object array by copying the object and paste it several times. Later you will see that using array can simplify programming considerably. The 10 text boxes will be used to display all the input data from RSVB1000 board. It includes 2 analog input, one counter input, and 7 TTL compatible input. Above each text box is a label indicating the meaning of the data in the text box. We can change the word on a label by changing the "Caption" property through the property panel. The two timers will be used for automation. The interval is set as 100 (milli second) for timer1and 20 for timer2. In the previous example, we set the properties through the property panel In this example, we will show how to set the properties through the code.

Figure 2.7 Interface in design status

Figure 2.8 Interface in running status

Below is the complete code for this example. When the "Start" button is clicked, timer1 is enabled. When timer1 is enabled, it will start counting. When the counting is equivalent to 100 ms, it will generate an event (make something happen) and start 2nd round of counting. If it is not disabled, timer1 will repeatedly generate events every 100 ms. These events can be used to achieve automation.
Code Explanation
Dim RSVBinput As String This section is for defining variables that can be used by all the subs in the program. RSVBinput will be used to read the input from the RSVB1000 board.
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 100
Timer2.Enabled = False
Timer2.Interval = 20

MSComm1.Settings = "38400,n,8,1"
MSComm1.CommPort = 1
MSComm1.PortOpen = True

Dim i As Integer
For i = 0 To 9
Text1(i).Text = ""
Next i

End Sub

This sub tells what to do when the software is just launched (form loaded). Here the properties are set for the timers and the RS232 communication port. In the previous example, we set the properties through the property panel. You may use either approach.

Here the CommPort is set as 1. It varies with the real port number in the PC. You may use the RSVBtest software to find the accurate port number.

Command "Timer1.Enabled = False" will disable the timer. Without doing this, the timer will start running when the program is launched.

These four line commands clear the 10 text boxes. If the box is not named under one array, we will have to use 10 command lines to clear the box one by one.

Private Sub Form_Unload(Cancel As Integer)
MSComm1.Output = "L20" + Chr(13)
MSComm1.PortOpen = False
End Sub
This section tells what to do when you quite the program. First it will set all output to low. Then it will close the communication port

Private Sub Command1_Click()

Timer1.Enabled = True
MSComm1.Output = "P250" + Chr(13)
MSComm1.Output = "M250" + Chr(13)
End Sub

Command1 is the name of the "Start" button. When the button was added to the form, the word on it is the same as its name. Later we changed it to "Start" by modifying its caption property. When this button is clicked, timer1 starts generating events repeatedly. The other two command lines are for setting the duty cycle of PWM1 and PWM2 output. The RSVB1500 driving board use the PWM output to regulate current for output 1-8.
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
When the "Stop" button is clicked, it will stop the process by disabling timer1.
Private Sub Command3_Click()
Timer1.Enabled = False
MSComm1.Output = "L20" + Chr(13)
MSComm1.PortOpen = False
End
End Sub
When the "Exit" button is clicked, the running process will stop, all the output in the RSVB1000 board will be switched off, the RS232 port will be closed, and finally the program will be closed.
Private Sub Timer1_Timer()

MSComm1.Output = "I8" + Chr(13)
Timer2.Enabled = True

If Val(Text1(0)) >= 35 Then
MSComm1.Output = "H18" + Chr(13)
ElseIf Val(Text1(0)) < 30 Then
MSComm1.Output = "L18" + Chr(13)
End If

If Val(Text1(5)) = 0 Then
MSComm1.Output = "X1" + Chr(13) + "I2" + Chr(13) + "S400" + Chr(13)
End If

End Sub
When timer1 counting reaches its set value (100 ms) an event called "Timer1_timer" will occur. This event will trigger following actions: 1) send out command "I8" to ask RSVB1000 or RSVB1600 to send out all its input data; 2) enable timer2 for reading the input data; 3) control output 18 using the analog signal An1; 4) let a stepper motor move 400 steps at step/2 ms in direction 1 when input In1 is 0. As the event "timer1_timer" occurs repeatedly, the program will trigger the actions again and again. Action 3 may be used to control a heater or a cooling fan according to feedback of a temperature sensor. Action 4 may be used to do position control. When In1 in RSVB1000 is connected to ground, In1 will be 0. Then the stepper motor connected to outputs 1- 4 will move 400 steps.

Private Sub Timer2_Timer()

RSVBinput = MSComm1.Input
Dim head As Integer
Dim tail As Integer
head = InStr(RSVBinput, "A")
tail = InStr(RSVBinput, "B")
If head > 0 Then
Text1(0).Text = Mid$(RSVBinput, head + 1, tail - head - 1)
End If

head = InStr(RSVBinput, "B")
tail = InStr(RSVBinput, "C")
If head > 0 Then
Text1(1).Text = Mid$(RSVBinput, head + 1, tail - head - 1)
End If

head = InStr(RSVBinput, "C")
tail = InStr(RSVBinput, "D")
If head > 0 Then
Text1(2).Text = Mid$(RSVBinput, head + 1, tail - head - 1)
End If

head = InStr(RSVBinput, "D")
If head > 0 Then
Text1(3).Text = Mid$(RSVBinput, head + 1, 1)
Text1(4).Text = Mid$(RSVBinput, head + 2, 1)
Text1(5).Text = Mid$(RSVBinput, head + 3, 1)
Text1(6).Text = Mid$(RSVBinput, head + 4, 1)
Text1(7).Text = Mid$(RSVBinput, head + 5, 1)

Text1(8).Text = Mid$(RSVBinput, head + 6, 1)
Text1(9).Text = Mid$(RSVBinput, head + 7, 1)
End If

Timer2.Enabled = False


End Sub

When timer1 enables timer2, timer2 will count for 20 ms and then generate event "timer2_timer". The 20 ms interval will allow the command "I8" to reach RSVB1000 board and all the input data to reach the computer. The timer2_timer event will trigger 3 actions: 1) remove the input data in the inputbuffer and give them to variable RSVBinput; 2) separate the mixed data and put then to individual text box; 3) disable itself. Since timer1 will enable timer2 every 100 ms, timer2 will run repeatedly as well.

The major part of this section is for sorting out the input data. When the RSVB1000 send out the data, they are put together in the inputbuffer. To help identify which is which, RSVB1000 insert some letters in between. The whole data string is like "A25B119C0D1111111". Its sequence is as A-An1-B-An2-C-Cn1-D-In1 to In7. The process for sorting out the information is called "parsing". In VB this can be done using two functions. One is "InStr" and the other "Mid$". (Perhaps your head start aching now. Do not give up. After here the rest will be easy).

InStr can be used to check if a word or a letter exists in a text string. Command line head = InStr(RSVBinput, "A") checks if RSVBinput has letter "A" and its position in the string. The value of variable head is the position of the letter in the string. If head=0, the letter does not exist in the string.

MId$ function can be used to get a segment of content from a text string. Command line Text1(0).Text = Mid$(RSVBinput, head + 1, tail - head - 1) will take the content starting from next to letter A and end before letter B in RSVBinput and put the content to text1(0) box as An1 value.

After getting An1, the program goes to next until all the input data have been sorted out. You may notice for In1 to In7 only head is used for the parsing. This is because these TTL inputs have fixed length (one letter each). We can calculate their position once we locate the position of letter D. For other inputs, the length can change, we have to use a head and a tail to get the complete piece of information. As timer1 triggers timer2 repeatedly, the data in the 10 text box will be updated every 100 ms (0.1 second).

The parsing part may look a bid complicated. It is OK if you do not understand it fully. You can copy this part to your future program for getting the input data. The parsing approach is more reliable than the approach used in the previous example for getting the input data. When you run this program, the interface screen will be like the one in figure 2.8. You may notice the icons for timer and MScomm are not there.

Back to To Top

3. Control stepper motors

3.1 Introduction


Stepper motors are excellent for position control. Unlike DC motors, their speed is not affected by the loading as long as it is not beyond its loading capacity. They are so reliable that an open loop control is sufficient for most situations. Stepper motors can be classified as unipolar and bipolar types. The figures below show the major difference of the two.

Both types have two groups of coils. A unipolar motor has two extra wires connected to the middle of each coil. You can tell the type of a stepper motor according to its number of wires. A bipolar motor normally has only 4 wires while a unipolar motor normally has 6 wires. Some unipolar motors have 5 wires when the two middle wires are jointed as one. Occasionally you may see a unipolar motor with 8 wires in which each coil is divided into two coils.

Figure 3.1 Unipolar stepper motor

Figure 3.2 Bipolar stepper motor

3.2 Drivers of stepper motors

Stepper motors are driven using electric pulses. A motor moves only one step with one pulse. To make a stepper motor run continuously, we need to provide electric pulses repeatedly. For each pulse, the current flow for each coil needs to change following a certain sequence pattern. Below are two sequence examples for moving stepper motors:

a1 a2 b1 b2 The sequence on the left can be used to drive a unipolar stepper motor. In the first pulse, the current flows from V+ to a1. In pulse 2 the current flows from V+ to a2, etc.. In pulse 5, the sequence will start from line 1 again. a1 a2 b1 b2 The sequence on the left is for a bipolar stepper motor. First current flow from a2 to a1 and b2 to b1, then the current in coil b will change direction. Next current in coil a will change direction. In step 4, current in coil a will change direction again.
on off off off - + - +
off on off off - + + -
off off on off + - + -
off off off on + - - +

If the sequence is executed in an opposite direction (from bottom to top), the motor will move in the other direction. When a bipolar motor is running, both coils are powered for each pulses, while in case of unipolar motor, only one coil is powered for each pulse. It is why a bipolar stepper motor is more powerful than a unipolar steper motor.

In the sequence for a unipolar stepper motor, current always flow from V+ to one of the 4 coil ends. We only need to control 4 switches for diverting the current to the right ends of a coil. In the sequence for a bipolar motor, current will change direction in each step. It makes the control for a bipolar motor a bid more complicated. Normally a H-bridge circuit is needed for current direction change.

Since control of a stepper motor is not straight forward, a driving circuit is needed between a micro controller and a stepper motor. The driving circuit looks after the sequence of current flow and provides suitable current output for the motor. Normally the pulse generation is done by the microcontroller.

3.3 Control stepper motors using RSVB1000 + RSVB1500 boards or a RSVB1600 board

3.3.1 Working principle

The RSVB1000 interface board has two build-in drivers for driving two bipolar stepper motors simultaneously. The drivers look after both pulse generation and sequence pattern. The relevant commands for VB are very simple. Below are two examples:

MSComm1.Output = "X1" + Chr(13) + "I2" + Chr(13) + "S400" + Chr(13) (move motor X 400 steps at interval 2 ms/step)

MSComm1.Output = "Y2" + Chr(13) + "I5" + Chr(13) + "S0" + Chr(13) (move motor Y till In2 =1 at interval 5 ms/step)

The command has 3 parts separated by chr(13). The first part tells which motor to run and the moving direction. "X" is for motor connected to outputs 1-4 and "Y" is for motor connected to outputs 5-8. The digit after the letter (1 or 2) tells the moving direction. As the real direction depends on the wire connection, you need to test with a specific motor to decide which is clockwise and which is counter clockwise. The second part tells the interval between steps (in ms). The third part tells the number of steps. If it is 0, the motor will move until it reaches its home position. For motor X the home position is indicated by status of input In1 and for motor Y In2 is used as indicator. If we want a motor to move to a certain position, first we need to drive it to its home position and then let it move necessary steps from there. The RSVB3000 experiment station has a sensor for detecting the home position and ready wiring allowing you to practice the position control.

A RSVB1600 board can be used to drive stepper motors directly. As the output of RSVB1000 cannot provide sufficient current for a motor, a RSVB1500 board need to be used to provide larger current. A RSVB1600 board is equivalent to a combination of a RSVB1000 and a RSVB1500 board. In the rest of this tutorial, a RSVB1000 and a RSVB1500 are used. The information can be easily applied to a RSVB1600 board.

3.3.2 Hardware connections

When a RSVB1500 board is attached to a RVB1000 board, it is ready for driving two stepper motors. Figure 3.3 shows how to make the connection. Outputs 1-4 is for motor X and output 5-8 is for motor Y. Although the build-in driver is for bipolar stepper motors, it can also use unipolar stepper motors. You can follow the same way as connecting bipolar stepper motors and simply leave the V+ wires unconnected.

It can be a challenging task to figure out which wire is a1 and which is b2 as different manufacturers may follow different style. We do not have to differentiate between a1 and a2 or between b1 and b2. It is sufficient to find out which two wires belong to the same coil. In case of unipolar stepper motor, we also need to identify the V+ wire. This can be easily done by measuring resistance between the wires. If the resistance between two wires is in mega Ohm range, they belong to different coils. The resistance between the two ends in a coil is normally less than 200 Ohms. The resistance between V+ and one of the two ends in the same coil should be half of the resistance. For example if the resistance between a1 and a2 is 100 Ohms, the resistance between V+ and a1 should be 50 Ohms.

When connecting a stepper motor to outputs 1-4, make sure outputs 1 and 2 are used by the wires from the same coil. In case of outputs 5-8, make sure outputs 5 and 6 are used by the wires from the same coil. It is OK if you swap the two wires in the same coil.

Figure 3.3 Connection diagram for using RSVB1500 to drive stepper motors

After connecting the stepper motor to a RSVB1500 board, we need to consider the power supply. The supplied voltage for a RSVB1000 can be 8-24 volts. As shown in figure 3.3, a short wire can be used to connect the power from RSVB1000 to RSVB1500 board. Since each of outputs 1-8 can only have up to 500 mA output current, the working current of the stepper motor should be smaller than this value. Ideally the working voltage of the stepper motor should be the same or slightly lower than the supplied voltage. If the working voltage of the stepper motor is lower than the supplied voltage, we can use PWM1 and PWM2 to make an adjustment. Outputs 1-4 is regulated by PWM2 and output 5-8 is regulated by PWM1. If the supplied voltage is 24 volts and the stepper motor can only take 12 volts, we can set the duty cycle of relevant PWM to 215 (refer to section 1.4 and user manuals for more information about PWM technique).

3.3.3 Position control

To drive a stepper motor to a certain position, we need a reference point. This reference point is called its home position. We can drive the motor to its home position and then move necessary steps from there. A sensor is needed to tell if the home position has been reached. Figure 3.4 shows an optic sensor and its working principle. A 5 volt power is applied to the LED and its light switches on the transistor. Input In1 in RSVB1000 is connected to ground when the transistor is on. The status of In1 is 0 (same as ground). When light from the LED is blocked, In1 is cut from the ground. Its status becomes 1 (5 volt).

Figure 3.4 optic sensor

In RSVB3000 experiment station, an optic sensor is used for detecting the home position of the stepper motor. Such sensor is widely used for position control.

Back to To Top


4. Control DC motors

4.1 Introduction

It is very easy to make a DC motor turn. It can be done by simply connecting one of the wire to the positive end of a power supply and the other wire to the negative end of the power supply. It is also easy to make a DC motor change direction. Simply swapping the connection of the two wires will achieve this. The task becomes more challenging if we want to make the direction change occur automatically, to make a DC motor move at a steady speed when its load is changing, or to make a DC motor move to a certain position. A DC motor tends to move faster when its load is low. When the load is increased, the DC motor will slow down and the current flowing through the coil will increase. When we want a DC motor to stop, it will keep moving for some time, making it difficult for position control. The control becomes more complicated than controlling stepper motors. However, DC motors have their advantages over stepper motors in that the hardware connection is simpler, move is smoother, and their loading capacity is larger then stepper motors of similar wattage or size. When the load becomes too heavy, a stepper motor will simply give up (step slipping). But a DC motor will slow down and increase the current to keep the load moving. A geared DC motor is very good for moving heavy loads. The RSVB3000 experiment station is installed with a geared DC motor.

4.2 Control direction of DC motors

To change moving direction of a DC motor without manually swapping wire connection, a H-bridge circuit is needed. The RSVB1500 board has such circuits. Two outputs are needed to enable direction control. Either of these four output pairs can be used for direction control: 1 and 2, 3 and 4, 5 and 6, and 7 and 8. Let us use output 5 and 6 as an example. When output 5 is set to high and output 6 is set to low, current will flow from output 5 to DC motor and then to the ground through output 6. When output 5 is set low and output 6 is set high, the current flow direction and the motor moving direction will change. When both outputs 5 and 6 are set high, motor will be stopped with a braking effect. We can also stop a motor by setting the two outputs to low but it will take longer time since there is no braking effect.

In VB, we only need following two commands to change the direction of a DC motor:

MSComm1.Output = "H5" + Chr(13)+ "L6" + Chr(13)

MSComm1.Output = "L5" + Chr(13)+ "H6" + Chr(13)

4.3 Control speed of DC motors

PWM technique can be used to control the speed of a DC motor. Here we use an example to show how it can be done. The example needs RSVB1000 interface board, RSVB1500 driving board, and the RSVB3000 experiment station. The geared DC motor on RSVB3000 experiment station is connected to output 5 and 6 on RSVB1500 board. The encoder for the DC motor on the experiment station is connected to counter1 on the RSVB1000 board for speed feedback. This encoder consists of an optic sensor (see figure 3.4) and a disk with 60 slits. The disk is mounted on the shaft of the motor. When a slit passes the optic sensor, counter 1 status changes from high to low and the counter will increase by one. We can find out the speed according to the counter value within a period. Below is the Software interface and the code.

The form has 2 text boxes and 4 command buttons. Command1 and command2 are for start and stop. Command3 and command4 are for changing the set speed value. Two timers are used for automation. The interval is 1000 for timer1 and 10 for timer2. The MSComm1 setting is the same as in section 2.3. The properties are set with the property panel.

Figure 4.1 Interface in design stage

Figure 4.2 Interface in running stage

Code Explanation
Option Explicit
Dim setspeed As Integer
Dim realspeed As Integer
Dim pwmvalue As Integer
Define variables.
Private Sub Form_Load()
Timer1.Enabled = False
Timer2.Enabled = False
Text1.Text = "30"
Text2.Text = "0"
MSComm1.PortOpen = True
End Sub
This section tells what to do when the software is launched.

It stops timer, clear text box, and set text1 box value as 30.

Open the communication port

Private Sub Form_Unload(Cancel As Integer)
MSComm1.Output = "L20" + Chr(13)
MSComm1.PortOpen = False
End Sub
This section tells what to do when closing the software.

Switch off all the outputs and close the communication port.

Private Sub Command1_Click()
pwmvalue = 200
MSComm1.Output = "P" + Str(pwmvalue) + Chr(13)
Timer1.Enabled = True
MSComm1.Output = "H5" + Chr(13)
End Sub
When the start button is clicked, it will set the value for pwm1, switch on pwm1, start timer1 and switch on output 5. The DC motor will start running when both pwm1 and output 5 are switched on. Here variable pwmvalue is used to tell the pwm setting. Str(pwmvalue) tells VB to send the pwmvalue as string. The effect is equivalent to "P" +"200" + Chr(13)
Private Sub Command2_Click()
MSComm1.Output = "P0" + Chr(13)
Timer1.Enabled = False
Text2.Text = "0"
End Sub
When stop button is clicked, pwm1 is set to 0 (motor will sop), timer1 is stopped, and the real speed display is set to 0.
Private Sub Command3_Click()
If Val(Text1) < 60 Then
setspeed = setspeed + 1
Text1.Text = setspeed
End If
End Sub
When the "+" button is clicked, the value in text1 (set speed) increase by 1. The maximum set speed is limited to 50 count/sec

The DC motor can go up to 60 rpm or one round per second. The encoder disk has 60 slits. The max achievable speed for this motor is 60 count/sec.

Private Sub Command4_Click()
If Val(Text1) > 15 Then
setspeed = setspeed - 1
Text1.Text = setspeed
End If
End Sub
When the "-" button is clicked, the value in text1 (set speed) decrease by 1. The minimum set speed is limited to 15 count/sec

By lowering the pwm duty cycle, the DC motor can go as slow as 5 count/sec but the control is not very stable. So here it is limited to 15 count/sec

Private Sub Timer1_Timer()
MSComm1.Output = "C1" + Chr(13)
Timer2.Enabled = True
End Sub
Start the automatic control process. It will send out command "C1" to get the counter1 value and start timer2 for more controls

This event repeats every second as timer1's interval is 1000 ms.

Private Sub Timer2_Timer()
MSComm1.Output = "C0" + Chr(13)
realspeed = Val(MSComm1.Input)
Text2.Text = realspeed
setspeed = Val(Text1)
pwmvalue = pwmvalue + (setspeed - realspeed) * 2.2

If pwmvalue < 150 Then
pwmvalue = 150
ElseIf pwmvalue > 255 Then
pwmvalue = 255
End If

MSComm1.Output = "P" + Str(pwmvalue) + Chr(13)

Timer2.Enabled = False
End Sub
This event occurs 10 ms after the timer1 event. It gives sufficient time for counter2 data to reach the input buffer. First command "C0" is send to RSVB1000 to set counter1 to 0. Then the counter1 data is fetched from the inputbuffer and put to text2 box.

A new pwm value is calculated according to the difference between the set speed and the measured speed. Here "2.2" is the gain for the control. Its meaning is "to change the speed by 1 unit, the pwm value need to change by 2.2. This is obtained by experiment and will vary with motor and power supply. The real speed is measured when pwm is set at 255 and 155. The gain is 100 divided by the speed difference. This code can be used to get the real speed if the pwm calculation part is removed.

The section from "If" to "End if" is to limit fluctuation of control output (you may remove this section and try again). Finally a new pwm value is sent to RSVB1000 for speed adjustment.

When running this program, the DC motor will run first at 30 count/sec. By clicking at the "+" or the "-" button, the set speed will change. The real speed should make corresponding change to match the new setting. If you use a finger to slow down the motor, you can feel the increased push from the motor for maintaining the original speed.

No comments: