(GiaoTrinh) ArduinoBasic
(GiaoTrinh) ArduinoBasic
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:
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.
Step 1: From the main page of TinkerCad, select the Circuit and then, Create New Circuit
as following.
Step 2: Choose the Arduino board from the starter list, then drag and drop the Blink
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.
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.
In this exercise, the status of two LEDs are toggled every 2 seconds, as demonstrated in
the figure bellow.
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 }
https://www.tinkercad.com/things/4vp8chmyfAm
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 }
https://www.tinkercad.com/things/1Vxi3loXSKO
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.
5 digitalWrite (1 , LOW ) ;
6 digitalWrite (2 , LOW ) ;
7 digitalWrite (3 , LOW ) ;
8 digitalWrite (4 , LOW ) ;
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 }
https://www.tinkercad.com/things/0gIPR9MqG0c
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
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 }
https://www.tinkercad.com/things/jN5V40xZ1Za
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 }
5 void loop () {
6 // TODO : Add your processing code here
7 }
5 void loop () {
6 // TODO : Add your processing code here
7 }
https://www.tinkercad.com/things/jcOtVDzvddr
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.
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 }
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."
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.
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
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) ;
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
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 }
5 } else {
6 speed = 0 - speed ;
7 }
8 }
2 void loop () {
3 // TODO : Implement testing script
4 }
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.
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.