[0. Intro]
Hey Everyone, Sin here.
Today we’re going to do some engineering
and stuff.
In this video I’m going to show you how
to take an existing Arduino project and bring
it into LabVIEW, as well as getting it to
work with LINX.
In the first half I will show you how to get
a DC motor working with the L298N DC Motor
driver, based on an existing Arduino Tutorial.
And in the second half, I will show you how
to turn on and off the DC motor using a relay.
[1. L298N]
On screen, I have a tutorial for the L298N
DC motor driver by Last Minute Engineers,
so all credit goes to them for this well explained
tutorial.
What we’re going to do is, work out how
the L298N works, wire up the circuit, and
lastly test it with the example Arduino code
provided.
Then we will take what we have learned and
apply it in LabVIEW to drive the L298N using
LINX.
I am not going to go into detail on the background
theory, as I’m assuming you already have
a basic understanding DC motor theory, PWM
signals and how a H-Bridge works.
And If you don’t, by all means check out
this tutorial as they do a good job of summarising
it for you.
So, in general with any piece of electronics,
the first thing you want to find is the data
sheet so you can get its specifications and
a wiring or pinout diagram.
In this tutorial, they have summarised all
the key points nicely for us.
So we don’t have to do all the digging ourselves.
However, obviously, in a professional setting
you’d actually want to find the OEM data
sheet and check it yourself as online tutorials
aren’t always the best source, you need
to do your own research.
So if we scroll down, we see the power supply
section where we can see the L298N is powered
through a 3 pin screw terminal. The first
input starting from the left is the motor
supply voltage (Vcc), ground (GND) and the
third is 5V.
Now this 5V can be an input or an output depending
on the jumper setting as they have shown in
this diagram.
If you leave the jumper on it is a 5V output
supplied by the on-board 5V regulator chip,
which you can use to power your Arduino for
example.
If you remove the jumper, you will need to
provide 5V into the third pin.
In this example, we will leave the jumper
on.
The next section discusses the voltage drop
of the switching transistor of the H-Bridge.
In summary, there is an approximate 2V drop
from the supply side to the output side.
So, you will need to provide a voltage 2 volts
greater than your motors specification.
For example, if you have a 12V motor, you
will need to supply 14V to get 12V to the
motor.
Next, we have the output pins, this is where
you will connect your DC motor.
Following on from this next section is about
the control pins, the most important part.
As these are what will control the speed and
direction of your motor.
For rotation direction control, each motor
has two input pins, input 1 and 2 for motor
A, and input 3 and 4 for motor B.
Which are driven by 5V logic levels, based
on the truth table below.
This truth table of an Exclusive OR (XOR)
gate.
If both inputs are high or low, the motor
is off (F). However, if one input is High
and the other is Low, the motor will turn
on.
Here we see if input one is high and input
two is low, the motor will rotate in one direction.
And if the opposite is true, the motor will
rotate in the opposite direction.
So, we know now, we will need to supply two
digital output pins from the Arduino to control
the motor direction.
Next, we have the speed control pins, one
for motor A and one for motor B.
By default, if you leave the jumper on, the
motor will always turn on at 100% speed. Additionally,
if you pull the pins LOW, it will turn the
motor off.
However, if we supply a PWM signal from the
Arduino we can control the speed of the motor
by specifying the duty cycle.
So, we need to assign an PWM-enabled pin from
the Arduino to control the motor speed.
If we go down a bit further, we have the pinout
diagram from the L298N which summarises what
each connection does, if you require further
detail.
Now let’s go over the circuit schematic
provided.
Just note my circuit deviate as follows, I’ll
be using one motor, a 9V battery as a power
source and the Arduino will be powered over
USB.
As later when we run the LINX firmware, it
must be connected to the PC to work with LabVIEW.
Ok back to the schematic.
We connect outputs 1 and 2 to the DC motor,
then we remove the jumper from the enable
pin A and connect it to the PWM enabled pin
9 of the Arduino.
Next, we connect input pins 1 & 2 to two digital
output pins 8 & 7 of the Arduino.
Next, we supply the motor power source to
VCC and GND.
Make sure you connect the ground pin of the
Arduino to the ground screw terminal of the
L298N, for a common ground.
So, let’s wire everything up.
And that’s it you’re ready to program.
Last minute engineers have kindly provided
a piece of example code to get your motor
running using the Arduino IDE.
If you want a detailed code explanation, they
have also provided that further down below.
But I will just summarise.
We initialise all the variables here.
Then we define the pin modes and default states
of the output pins.
Next, we loop two functions with a 1 second
delay between them.
These functions are defined below.
The directionControl function just turns on
each motor at full speed, in one direction,
then switches direction after two seconds.
Finally, it turns the motors off.
The speedControl function, turns on the motors
in one direction, then increments a PWM signal
to accelerate the motor from zero to maximum
speed.
Then it does the opposite and decelerates
from maximum to zero.
And finally, it turns off the motors.
Let’s upload this sketch and see how it
works. This will also confirm our circuit
is correct.
Ok, we have confirmed the circuit is correct
and that everything is working.
Let’s bring this all into LabVIEW.
I’m using LabVIEW 2020 community edition,
so if you have a previous version make sure
the LINX toolkit is installed through the
VIPM.
Or see my first video for a tutorial on how
to do that.
Once you have LabVIEW launched, let’s proceed
to upload the LINX firmware.
Go to, tools > makerhub > LINX > LINX firmware
wizard…
Change the device type to an Arduino Uno,
then select the COM port it is connected to.
Hit Next, then Next again and wait for it
to upload.
Once it is done, make sure you check the Log,
by pressing View Log.
As you want to make sure the LINX firmware
successfully deployed to the Arduino.
You can check this by seeing if there where
any sync or time out errors in the avrdude
programmer. As you can see here, we had no
problems.
Next lets select, File > New VI.
As this time, we will be making the VI from
scratch.
Click on the block diagram, the window with
the white background.
This is where we will be putting all our LINX
functions.
Right click and locate the MakerHub pallet,
then hit the pin icon.
This will always keep it on screen. Click
on LINX.
Then drag the open and close Vis to the block
diagram.
These functions will open and close the serial
reference to the Arduino.
Next select Peripherals, then PWM.
Drag the Set Duty Cycle function to the block
diagram.
This is the function that will control the
PWM duty cycle on pin 9 to control the motor
speed.
Next hit the up button, then select Digital,
and drag the write functions to the block
diagram.
This is the function that will write digital
highs from the Arduino.
Click on the drop-down box of the digital
write function and select N channels.
This will allow us to write to more than one
channel by using an array of Boolean values.
As in this case we need to write to channel
7 and 8.
Next, right click the block diagram and select
structures, then select while loop and drag
it over the Set Duty Cycle and Digital Write
functions.
And now we essentially have the foundation
for our VI. All there is left to do is wire
everything up and create the constants and
controls.
Just remember, to do this, right click on
the terminal and then choose constant, control
or indicator.
In the case of the digital write function.
Right click on the channel terminal and choose
constant.
It automatically generates and wires an array
of integers for the channels.
Now let’s get started.
Alright, I have finished the block diagram
on the left, and front panel on the right.
If we examine the block diagram starting from
the left, we open a serial connection to the
Arduino on the COM port specified by our control
here.
We also wire in error cluster constant to
initialise the error cluster.
We then pass the LINX resource reference and
error reference to the while loop and to all
corresponding functions, to maintain flow
control.
Note, I put the duty cycle function before
the digital write function, because I always
want the speed of the motor to be changed
first before changing its direction.
For the Set Duty Cycle, function I have wired
in an integer constant to specify PIN 9 of
the Arduino.
I have also wired a horizontal slide control
to adjust the duty cycle from 0 to 1.
Next for the Digital Write function, we feed
in an array of integers, this allows us to
select multiple pins on the Arduino at once.
And I have also wired a Boolean array control
as you can see on the front panel, to allow
us to drive PINS 7 and 8 HIGH to control the
motor direction.
The default state is both PINS are pulled
HIGH, as if you remember in the truth table
this will result in the motor being turned
off.
You can of course do some clever logic to
drive the motor direction automatically instead
of doing it click by click, however for the
sake of this video, I will leave it as a manual
control.
Next, we wired a STOP control button to the
loop condition, make sure you do this otherwise
LabVIEW won’t be happy with you.
And finally, we close out the LINX reference
to the Arduino once we exit the while loop
and let the Simple Error Handler VI handle
any errors that occur.
It’s as easy as that, we can now control
the speed and direction of DC motor using
the L298N in LabVIEW.
Now let’s watch it in action, before we
move on to 
the relay.
[2. Relay]
Now this video is long enough, so the relay
section will be short as the circuit is simple
and the LabVIEW code we already made can be
re-used.
Relays are electromechanical switches, that
are useful for driving higher power circuits
from lower powered ones.
For example, if I have 24V motor circuit,
and I want to turn it on and off with an Arduino,
I would use a relay, as the Arduino is not
capable of driving a 24V circuit directly,
as its digital output pins are limited to
5 volts.
So here is the circuit schematic, and as you
can see it is straight forward.
We are powering the Arduino over USB and taking
its 5V output and GND and wiring it directly
to the VCC and GND inputs of the Relay.
We then connect digital pin 12 of the Arduino
to the digital input pin of the Relay.
Now with this relay, it has three output terminals.
Normally Closed (NC), Common and Normally
Open (NO).
Normally Closed means that the default state
of your circuit is closed, so when you provide
power to it via the relay, it will turn on.
Then when you trigger the relay, it will disconnect
your circuit.
Normally Open, is the opposite of this. The
default state of your circuit will be open,
so when you provide power via the relay the
circuit remains open circuited until you trigger
the relay, which will then complete the circuit.
And of course, common is the common terminal
between each output. That is, it is connected
to both the NO and NC terminals.
In our circuit, we will be using the Normal
Open connection and Common. As I want the
motor to be OFF until we drive the relay with
the Arduino.
So, getting back to the circuit.
We connect the positive rail of the 9V source
to the common terminal of the relay, and the
negative rail of the 9V source to one side
of the motor.
We then connect the remaining side of the
motor to the Normally Open terminal.
And that’s it for our circuit. Let’s quickly
wire it up and test it with our code in LabVIEW.
Ok now, that you have everything wired up.
Launch the LabVIEW code we made earlier and
changed one of the digital output pins to
pin 12.
And as you can see we are able to successfully
turn on and off the DC motor using the relay.
Notice, that when the output is LOW the motor
is turned off, as we expected in the Normal
Open state, if you’re motor turns on you
have wired it the wrong way.
You should also hear your relay click on and
off and see an LED illuminate when you drive
it from the Arduino.
[3. Outro]
I hope you learned something today, and if
you enjoyed this video.
Please like, comment, and subscribe.
Bye for now.
