(GiaoTrinh) ArduinoBasic

You might also like

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

Dr. Le Trong Nhan - MSc.

Bang Ngoc Bao Tam


Contents

Chapter 1. LED Animations on Tinker CAD 5


1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Create Tinker CAD account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Blinking LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.1 Two Toggling LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.2 Multiple Blinking LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3 Seven Segment LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.4 Two Digit Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.5 One Way Traffic Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.6 Two Way Traffic Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.7 Traffic Light with Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.8 Analog Clock Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.9 Analog Clock with Second . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.10 Finalize the Analog Clock . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Chapter 2. Motor Controller and Serial Monitor 17


1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2 Analog vs Digital . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3 Motor Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.1 Left Motor Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2 Left Motor Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 Right Motor Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4 Full Motor Control Functions . . . . . . . . . . . . . . . . . . . . . . . . 21
3.5 Full Motor Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4 Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1 LED Controller 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 LED Controller 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.3 Robot Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.4 Robot Headlights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Introduction to Computer Page 3


Page 4 HCMUT - Computer Engineering
CHAPTER 1

LED Animations on Tinker CAD


1 Introduction
Arduino is an open-source electronics platform based on easy-to-use hardware and soft-
ware. Arduino boards are able to read inputs, e.g. light on a sensor, a finger on a button,
and turn it into outputs, such as activating a motor, turning on a LED. You can also control
your board by sending a set of instructions to the microcontroller on the board. To do so,
you will use the Arduino programming language (based on C language), and the Arduino
Software (IDE).

Figure 1.1: Arduino boards

Thanks to its simple and accessible user experience, Arduino has been used in thousands
of different projects and applications. The Arduino software is easy-to-use for beginners,
yet flexible enough for advanced users. Makers, of course, use it to build many of the
projects exhibited at the Maker Faire, for example. Arduino is a key tool to learn new
things. Anyone - children, hobbyists, artists, programmers - can start tinkering just fol-
lowing the step by step instructions of a kit, or sharing ideas online with other members
of the Arduino community.

Finally, the most interest of Arduino is the IDE (or Programming Editor). It is available in
many Operating System, from Mac, Windows to Linux. Morover, there are a lot of online
platforms are availale for Arduino programming, and TinkerCad is just one of them.

Tinkercad is an excellent tool that allows you to simulate Arduino-based systems (and a
lot more). You can simulate all exercises and even your own designs before trying them
on real hardware. It also allows you to do programming using blocks. You can download /
copy-paste the generated code later into Arduino IDE to program the real Arduino board,
rather than having to write it from scratch. Main targets in the this lab are:

• Create an account in TinkerCad

• Create the Blinky project

• Exercises concerning LED controller

Page 6 HCMUT - Computer Engineering


2 Create Tinker CAD account
When you sign up for a Tinkercad account, the login you create becomes your Autodesk
ID, too, which means you can use the same login everywhere on all of the Autodesk web-
sites. This is especially useful if you are using other Autodesk applications, such as Auto-
CAD or Inventor, because it gives you an identity on the Autodesk user forums and on the
Autodesk Knowledge Network, or AKN for short.

To create a Tinkercad account:

1. Go to the homepage of Tinkercad via the link: https://www.tinkercad.com/

2. Click JOIN NOW then choose Create a personal account

3. Sign in with your student email address and accept the Tinkercad terms of service.

After creating account successfully, you are able to use not only Tinkercad but also any
Autodesk tools. So, let’s begin the journey.

Introduction to Computer Page 7


3 Blinking LED
This is a very first project in an embedded platform such as the Arduino board. In contrast
to a program running on a PC, print the string "Hello world!!" to the screen is impractical
in a low-cost micro-controller platform. Therefore, this first project can be considered as
the "Hello world" project in the Arduino board. Moreover, this project uses the built-in
LED that are available on most Arduino boards. This LED is connected to a digital pin
and its number may vary from board type to board type. To make your program easier,
a constant LED_BUILTIN is defined to present the name of a pin connected to the LED,
and allows you to control the built-in LED easily. Following steps provide the details to
implement this project.

Step 1: From the main page of TinkerCad, select the Circuit and then, Create New Circuit
as following.

Figure 1.2: Create a first project on TinkerCad

Step 2: Choose the Arduino board from the starter list, then drag and drop the Blink
project.

Figure 1.3: Create the Blinky project

Page 8 HCMUT - Computer Engineering


Step 3: Click on the Start Simulation button on the toolbar of TinkerCad (see figure bel-
low), either the LED connected on the pin number 13 or the build-in LED (notation with
letter L on the board) will be blinking. Actually, they are connected in parallel.

Figure 1.4: Start the simulation on Blinky project

Click on this button again (now it is Stop Simulation to stop the simulation, before click-
ing on the Code button, to check the source code of the project.

Step 4: Explore the source code of the project by clicking on the Code button, and then
select the Text mode. Following screen is opened.

Figure 1.5: Explore the source code of the project

The first thing you do is to initialize LED_BUILTIN pin as an output pin with the line:
1 pinMode ( LED_BUILTIN , OUTPUT ) ;
In the main loop, you turn the LED on with the line:
1 digitalWrite ( LED_BUILTIN , HIGH ) ;
This supplies 5 volts to the LED anode. That creates a voltage difference across the pins of
the LED, and lights it up. Then you turn it off with the line:
1 digitalWrite ( LED_BUILTIN , LOW ) ;
That takes the LED_BUILTIN pin back to 0 volts, and turns the LED off. In between the
on and the off, you want enough time for a person to see the change, so the delay() com-
mands tell the board to do nothing for 1000 milliseconds, or one second. When you use
the delay() command, nothing else happens for that amount of time.
Students are proposed to change the delay time and check the simulation again.

Introduction to Computer Page 9


4 Exercise

4.1 Two Toggling LEDs


Since schematic design is not in the scope of the course, students are supported by a
project in the shareable link bellow:
https://www.tinkercad.com/things/7aSmzepeKEB

Figure 1.6: Two LEDs Connections


By clicking on the Copy and Tinker button, the project is clone to your account. The
program skeleton is also provided in the code section. In this exercise, two LEDs are con-
nected to the Pin number 0 and 1, respectively.

In this exercise, the status of two LEDs are toggled every 2 seconds, as demonstrated in
the figure bellow.

Figure 1.7: State transitions for 2 LEDs

Students are propose to implement in the loop function. Your source code is required
to place in the following.

1 void setup () {
2 pinMode (0 , OUTPUT ) ;
3 pinMode (1 , OUTPUT ) ;
4 }
5

6 void loop () {
7 // TODO
8 }

Page 10 HCMUT - Computer Engineering


4.2 Multiple Blinking LED
The previous exercise is extended to eight different LEDs to play some animations. An
array and FOR statement are used to work with multiple LEDs, as supported in the link
bellow.

https://www.tinkercad.com/things/4vp8chmyfAm

Figure 1.8: Multiple Blinking LED

By clicking on the Copy and Tinker button, the project is clone to your account. The pro-
gram skeleton is also provided in the code section. In this program, a simple animations
with 2 stages are provided.

Students are propose to implement at least 10 animations for LEDs series. Your source
code is required to place in the following.

1 void setup () {
2 }
3

4 void loop () {
5

6 }

4.3 Seven Segment LED


An arrangement of seven different tiny LED in a package form a new component named
7 Segment LED. This component is widely used to display a digit from 0 to 9, as depicted
in the figure bellow.

Introduction to Computer Page 11


Figure 1.9: Display number using seven segment LED

The proposed connection in this exercise is provided in the link bellow.

https://www.tinkercad.com/things/1Vxi3loXSKO

Figure 1.10: Seven segment LED

In this schematic, seven pins from 0 to 6 are used to connect to the device. To turn on a
segment of a device, a LOW signal is required.

In the example code, number 0 is displayed on the device. Students are proposed to fi-
nalize the function displayNumber(int i) to finalize the project. The source code of this
function is required to place in the following.

1 void displayNumber ( int i ) {


2 if ( i == 0) {
3 digitalWrite (0 , HIGH ) ;
4

5 digitalWrite (1 , LOW ) ;
6 digitalWrite (2 , LOW ) ;
7 digitalWrite (3 , LOW ) ;
8 digitalWrite (4 , LOW ) ;

Page 12 HCMUT - Computer Engineering


9 digitalWrite (5 , LOW ) ;
10 digitalWrite (6 , LOW ) ;
11

12 } else if ( i == 1) {
13 // YOUR CODE HERE
14 } else if ( i == 2) {
15 // YOUR CODE HERE
16 } else if ( i == 3) {
17 // YOUR CODE HERE
18 } else if ( i == 4) {
19 // YOUR CODE HERE
20 } else if ( i == 5) {
21 // YOUR CODE HERE
22 } else if ( i == 6) {
23 // YOUR CODE HERE
24 } else if ( i == 7) {
25 // YOUR CODE HERE
26 } else if ( i == 8) {
27 // YOUR CODE HERE
28 } else if ( i == 9) {
29 // YOUR CODE HERE
30 }
31 }

4.4 Two Digit Number


The previous exercise is upgraded to 2 different seven segment LEDs. The project is
shared in the link bellow.

https://www.tinkercad.com/things/0gIPR9MqG0c

Figure 1.11: Two seven segment LEDs

In the source code of this project, a simple unit test is provided. Students can run the
simulation to figure out the connections in the circuit.
Students are proposed to display numbers of these LEDs, from 00 to 20 (and then loop
back). The updated display period is one second. To provide your source code, do not

Introduction to Computer Page 13


need to present the functions, which are reused from the exercise before (such as dis-
playNumber1 and displayNumber2).
1 void setup () {
2 // TODO : Add your setup here
3 }
4 void displayNumber1 ( int i ) {
5 // REUSED : Do not need the source code
6

7 }
8 void displayNumber2 ( int i ) {
9 // REUSED : Do not need the source code
10

11 }
12 void loop () {
13 // TODO : Add your processing to display 2 digit numbers
14 }

4.5 One Way Traffic Light


In the next three exercises, a traffic light project is proposed and is presented in the link
bellow.

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

Figure 1.12: Full traffic light with timer

The traffic light has 5 seconds for the RED, 2 seconds for the the YELLOW and 3 seconds
for the GREEN. Please arrange the LEDs in a right order.

At this exercise, only first way traffic light is required (the LEDs connected to pin number
8, 9 and 10).
1 void setup () {
2 // TODO : Add your setup here
3 }
4

5 void loop () {
6 // TODO : Add your processing code here
7 }

Page 14 HCMUT - Computer Engineering


4.6 Two Way Traffic Light
Your source code is upgrade to control the second traffic lights, connected to pin number
11, 12 and 13.
1 void setup () {
2 // TODO : Add your setup here
3 }
4

5 void loop () {
6 // TODO : Add your processing code here
7 }

4.7 Traffic Light with Timer


Finalize the system by a count-down timer using seven segment LED. Five seconds for the
RED means that the count-down process is started with 4 and ended by 0.
1 void setup () {
2 // TODO : Add your setup here
3 }
4

5 void loop () {
6 // TODO : Add your processing code here
7 }

4.8 Analog Clock Project


From this exercise, an analog clock project is proposed. Twelve different LEDs are used to
simulate a screen of a clock, as show in this link:

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

Figure 1.13: Analog clock with 12 LEDs

Based on the code skeleton, students are proposed to implement two functions bellow.
These functions are invoked in the loop function to perform unit test before implemen-
tation. Your source code is required to present in this report.

Introduction to Computer Page 15


1 void displayOnClock ( int num ) {
2 if ( num <= 12) {
3 // TODO : Implement your code here
4 }
5 }
6 void clearClock () {
7 // TODO : Clear 12 LEDs on Clock
8 }

4.9 Analog Clock with Second


The second information is updated to the LEDs follow exactly principle of an analog clock:
if second is between 0 and 4, the number 12 should be indicated. Similar to that, when
second is from 5 to 9, number 1 is indicated.

Please use the delay(1000) at the end of the loop function. Present your source code
just in the loop.
1 void loop () {
2 // TODO
3 }

4.10 Finalize the Analog Clock


Finalize the project with two more information are displayed on the clock.
Present your source code just in the loop.
1 void loop () {
2 // TODO
3 }

Page 16 HCMUT - Computer Engineering


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 11: Direction configuration pin 2 by digitalWrite

• Pin 9: 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