Arduino Part 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CHAPTER 2

Motor Controller and Serial Monitor


1 Introduction
A direct current, or DC, motor is the most common type of motor. DC motors normally
have just two leads, one positive and one negative. If you connect these two leads directly
to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the op-
posite direction.

To control the direction of the spin of DC motor, without changing the way that the leads
are connected, you can use a circuit called an H-Bridge. An H bridge is an electronic cir-
cuit that can drive the motor in both directions. H-bridges are used in many different
applications, one of the most common being to control motors in robots. It is called an
H-bridge because it uses four transistors connected in such a way that the schematic dia-
gram looks like an "H."

Figure 2.1: L298N driver on the market

The L298N Motor Driver is a controller that uses an H-Bridge to easily control the direc-
tion and speed of up to 2 DC motors. This tutorial will show you how to control a motor
using L298N. A simple program on Arduino platform to control 3 pins is able to control
the speed and the spin (forward or backward) of the motor.

Secondly, the Serial Monitor is an essential tool when creating projects with Arduino. It
can be used as a debugging tool, testing out concepts or to communicate directly with the
Arduino board.

The TinkerCad environment has the Serial Monitor tool integrated with the editor, which
means that an Arduino board can send and receive data from the Serial Monitor. This
interface is useful to extend the connection to other devices such as a Bluetooth device
(e.g. HC06) for controlling remotely the system.

Page 18 HCMUT - Computer Engineering


2 Analog vs Digital
An analog signal is one that can take on any number of values, unlike a digital signal which
has only two values: HIGH and LOW. The Arduino does not have a built-in digital-to-
analog converter (DAC), but it can pulse-width modulate (PWM) a digital signal to achieve
some of the functions of an analog output. The function used to output a PWM signal is
analogWrite(pin, value). pin is the pin number used for the PWM output. value is a num-
ber proportional to the duty cycle of the signal. When value = 0, the signal is the minimum
voltage in the circuit, which is GND = 0V. Meanwhile, when value = 255, the signal is the
maximum voltage, or VCC = 5V. If the value is 127, the output signal has the voltage level
at 1.65V, which is 50% of the maximum value.

On most Arduino boards, it is important to notice that the PWM function is available on
pins 3, 5, 6, 9, 10, and 11. These pins have both digitalWrite and analogWrite function.
Other output pins only have digitalWrite function. The first demonstration to compare
analog and digital function is proposed in the circuit bellow, with 2 different LEDs, con-
nected to pin number 2 and 3.

https://www.tinkercad.com/things/dNofqdRzDh1

Figure 2.2: Analog vs Digital Testing

The brightness of the LED connected to pin number 3 is increased from the minimum
to the maximum, which can be compared to the brightness of the LED connected to pin
number 2. This LED is set to the maximum brightness by using the digitalWrite function.
The source code of this demo is shown bellow:
1 void setup () {
2 pinMode (2 , OUTPUT ) ;
3 pinMode (3 , OUTPUT ) ;
4 digitalWrite (2 , HIGH ) ;
5 }
6

7 void loop () {
8 for ( int i = 0; i < 255; i += 5) {
9 analogWrite (3 , i ) ;
10 delay (100) ;

Introduction to Computer Page 19


11 }
12 }
Although the animation in TinkerCad is not easy to observe, students can find the princi-
ple of the analogWrite. This can be used to control some devices such as a motor (speed)
or a buzzer.

3 Motor Controller
From this section, students are proposed to construct their programs to control 2 motors,
which are normally used for a robot movement. The schematic of the simulation is pro-
posed as following.

https://www.tinkercad.com/things/5fQLcEA1P4Q

Figure 2.3: Analog vs Digital Testing

3.1 Left Motor Controller


In order to control the left motor, following pins are required:
• Pin 8: Direction configuration pin 1 by dititalWrite

• Pin 9: Direction configuration pin 2 by digitalWrite

• Pin 11: Speed controller pin by analogWrite


Students are proposed to implement following program to test the first motor.

1 void setup () {
2 pinMode (8 , OUTPUT ) ;
3 pinMode (11 , OUTPUT ) ;
4 pinMode (9 , OUTPUT ) ;
5 }
6 void loop () {
7 digitalWrite (11 , HIGH ) ;
8 digitalWrite (8 , LOW ) ;
9 analogWrite (9 , 100) ;
10 }

Page 20 HCMUT - Computer Engineering


Students can change the parameter of the analogWrite function. The parameter is in the
range from 0 to 255.

Finally, students implement following function to control the left motor.

1 void left_speed ( int speed ) {


2 // TODO : Implement your code here
3 if ( speed > 0) {
4

5 } else {
6 speed = 0 - speed ;
7 }
8 }

3.2 Left Motor Testing


Implement a short program to make the left motor moves forward with speed 100 for 2
seconds, then stop for 2 seconds, then moves backward with speed for 2 seconds before
stopping for 2 seconds again. This behavior is repeated forever. The function left_speed
is used in this demo.
1

2 void loop () {
3 // TODO : Implement testing script
4 }

3.3 Right Motor Testing


Similar to the left motor, a function to control the right motor is also required. The con-
nection pins for this motor are described as follows:

• Pin 12: Direction configuration pin 1

• Pin 13: Direction configuration pin 2

• Pin 10: Speed controller pin

2 void loop () {
3 // TODO : Implement testing script
4 }
What does happen if the numer 300 is used in analogWrite function? Briefly provide
your answer in the report.

3.4 Full Motor Control Functions


Implement four functions to control the movements of a Robot having 2 motors.

Introduction to Computer Page 21


1

2 void forward ( int speed ) {


3 // TODO
4 }
5

6 void backward ( int speed ) {


7 // TODO
8 }
9

10 void turnleft ( int speed ) {


11 // TODO
12 }
13

14 void turnright ( int speed ) {


15 // TODO
16 }

3.5 Full Motor Testing


Implement a script in a loop, to test four functions implemented above.
1 void loop () {
2 // TODO
3 }

4 Serial Monitor
In data transmission, serial communication is the process of sending data one bit at a
time, sequentially, over a communication channel or computer bus. This is in contrast
to parallel communication, where several bits are sent as a whole, on a link with several
parallel channels. The serial communication is available in most of micro-processor and
micro-controller systems, such as the PC, Smart Phone or the Arduino board.

In this lab, a serial communication is used for data transmission between the Arduino
board and a computer or other devices. All Arduino boards have at least one serial port
(also known as a UART or USART) named Serial. It communicates on digital pins 0 (RX)
and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you can-
not also use pins 0 and 1 for digital input or output. To activate the serial communication,
following source code is required in the setup function.
1 void setup () {
2 Serial . begin (115200) ;
3 Serial . println ( " Hello TinkerCad " ) ;
4 }
From the TinkerCad terminal, you can send some data to the Arduino board by pressing
any charater on your PC keyboard and the click buttion Send. However, some code need
to be implemented in the Arduino board as following: the board keep checking if there is
a character sent to it. If there is a character, it will read this character and send it back to
the PC terminal.

Page 22 HCMUT - Computer Engineering


1 void loop () {
2 if ( Serial . available () )
3 {
4 char temp = Serial . read () ;
5 Serial . print ( " I received : " ) ;
6 Serial . println ( temp ) ;
7 }
8 }
Students are propose to run the program again and check the communications between
the Arduino board and the TinkerCad terminal.

4.1 LED Controller 1


Implement a short program to make the LED connected to pin 13 turn on if a character
’O’ is received. Other characters, the LED is turned off.

Hint: Use the if statement: if(temp == ‘O’) . . . . else . . .

4.2 LED Controller 2


Improve your program to make the LED connected to pin 13 turn on if a character ’O’ is
received. However, the LED is turned off only when receiving character ’F’.

4.3 Robot Controller


When a character W, S, A and D is sent, the robot moves forward, backward, left and right,
respectively. With all other characters, the robot stops moving.

4.4 Robot Headlights


Add two more LEDs to the system and turn an appropriate LED when Robot is turned left
or right.

Introduction to Computer Page 23

You might also like