In this tutorial you will learn to write a basic and very simple program or VI and I will provide you with the basic information regarding the blocks and palettes. The first program uses just string constants and indicators and the second program is to do simple addition and subtraction. At the end of each program I have given some exercises and are left for the reader to do it by themselves to make their concepts clear.
Introduction to labview programming
We have already discussed some of the basic concepts about LabView in previous tutorial. Let’s now move towards programming. Writing a program in LabView is way more simpler than writing the program doing the same functionality in any other programming language i.e. C, C#, Java etc. You need not to learn any of the coding skills or syntax. All you have to do is to make a block diagram and run it. A LabView program is known as a Virtual Instrument (VI). In the entire upcoming tutorials I will use VI instead of LabView programs. When you create a new VI in LabView it will show two windows, one for block diagram which serves for the functional graphical code and other for front panel which serves as the user interface. You build the block diagram of the required task using controls (input devices) and indicators (output devices) in the block diagram window. The front panel is a user interface and the output of your block diagram is displayed on the front panel which includes all the controllable inputs and all output devices. Let’s move toward the programming now.
writing Your first labview program
Figure 1: Block diagram and front panel
- Right clicking on anywhere on the front panel gives a dropdown menu, known as control palette, and right clicking on block diagram also gives a dropdown known as function palette from which we can select the controls and indicators and place them on the front panel. A control placed on front panel will automatically appear on the block diagram too.
Figure 2: Control placements
- In the similar way we can also place indicators. Placing indicators on front panel will automatically display that on block diagram too.
- The control palette and function palette contains almost everything one can use to interact with the VI and to display graphs or runtime values of a variable. Both these palettes collectively give an entire variety of controls and indicators.
- Every time you drop an item on the panel there is a corresponding terminal that appears on the block diagram. Clicking on the item in front panel will blink the corresponding item o the block diagram, and updating the value in either of the window will update the value in the other. In short both the windows are linked together.
- One can also resize and re color any of these items to their liking, using the panel shown in the figure below.
Figure 3: Resizing and Re coloring
- The function palette includes different functionality dropdowns, including structure, arrays, clusters and waveforms etc as shown in the figure below
Figure 4: Function palette
Now lets see sample examples of labview programs
1.Hello! World program in labview
- Let’s now create a very simple LabView program which will print “Hello!! World” on the front panel.
- From the Control palette on the front panel select string and path and then select string indicator as shown in the figure below.
Figure 5: Placement of string indicator
- Placing this control on the front panel will also place a string control on the block diagram. Now in the function palette on the block diagram select String and then select String constant, and place it on the block diagram as shown below.
Figure 6: Placement of string constant
- Hover over the string constant and double click to start a wire and connect it to the string control as shown in the figure below.
Figure 7: Wire connections
Figure 8: String input
- Now run the VI from the “run” button on the block diagram or press < Ctrl+R> . The string indicator at the control panel will display “Hello!! World”
Figure 9: Output display
This is the basic program used to understand the working of string constants and indicator and is used as a beginners code all over the world. Now let’s do a simple exercise to make this concept clear.
Exercise:
- Try to display your name and address on 2 separate string indicators
(Hint: Use different string constants and indicators)
Addition and subtraction program in labview
Trying to do difficult tasks including complex mathematics will somehow always find its way to simple addition and subtractions. If you are not familiar with doing simpler tasks you can never be able to do complex task. Starting up your pace using addition will make you feel that you are learning something. If I start from complex problem where the beginner is not even able to write a simple code after the end of the tutorial then writing these tutorials is of no use. Using numerical operators such as addition, subtraction, multiplication, and division is the key to be good programmer in the future.
Let’s now move towards the programming part and try to do some calculation. At the end I will assign you an exercise to do it by yourself, because learning without practicing is of no use and programming cannot be learned just by reading. You have to write programs and solve a particular task if you are willing to be a good programmer in future.
- Now from the function palette in the block diagram select Numeric and then select Add as shown in the figure below
Figure 10: Placement of Add operator
- The operator used is a 2 input addition operator, if you want to do multiple input additions you have to use multiple operators using associative property.
- Place to numeric constants from function palette >> Numeric >> Numeric constant and connect them at the input side of the add operator.
Figure 11: Placement of numeric constants
- Now place numeric indicators from numeric palette >> Numeric >> Numeric indicator and connect its associated indicator in the block diagram at the output of the add operator.
Figure 12: Placement of numeric indicator.
- Now in the constant enter the values you want to add and then press the “run” button at the top or press <Ctrl+R> to run the VI. The input constant after addition will be displayed on the numeric indicator.
Figure 13: Output of addition displayed
- For subtraction the procedure is same we just have to replace the add operator with the subtract operator as shown below.
Figure 14: Placement of subtraction
Figure 15: Subtraction output
- You can see that the second digit is subtracted from the first digit. If you are stuck at any part of the code and don’t know how to use an operator just simply right click on the operator and select Help. The built in LabView help feature will give you the details how the operator operates. This figure below shows the help window of subtract operator.
Figure 16: Subtraction help
Now do a small exercise to make your concepts of simple numeric operators and constants clear.
Exercise to learn Labview programming
- Try to do multiple input addition i.e. give 3 inputs to the add operator and display the output and verify your answer. Similarly do addition for four and five input values.
(Hint: Try using multiple add operators)
- Try to do multiple input subtractions i.e. three four and five input subtraction.
(Hint: Always remember the order of subtraction to avoid ambiguity)
- Try using multiplication as well as division operators.
No one is so rich that they can not help others, and no one is too poor to help others in some way
Thanks for this