Basic Arduino Workbook Guide
Basic Arduino Workbook Guide
Firrst published 20
020
A right reserved.
All
N
No part of this publication
p mayy be reproduced d, stored in a rettrieval system or
o
T
Transmitted in any
a form by anyy means, electro onic, mechanicaal, photocopying g,
recording or otherwise,
o with
hout the prior pe
ermission of Copyright owner
ii
PREFACE
In the name of Allah, the Most Gracius and the Most Merciful
Please and blessings of Allah be upon His Messenger, Muhammad, and all his family and companions
Alhamdullillah, all praises to Allah for the blessing and guidance upon completing this book.
The contents of this book were tailored to the needs of technology-driven world today. Learning to program gives
you a much better understanding of this world as all ‘things’ nowadays can be programmed, autonomous and
intelligent. This book will uses Arduino as a microcontroller. Arduino board is a popular board for embedded
development. This book helps you to get started with Arduino Uno development. Several basic samples projects are
provided to accelerate your learning process. Most importantly it is shown by step by step procedure which make
learning Arduino is fun, simple and interesting.
This book have been written by Mrs. Noor Azlyn Binti Ab Ghafar and Mr. Raizizan Bin Rahim which both are
Mechatronics Engineering lecturers at Mechanical Engineering Department, Politeknik Kota Kinabalu. We are both
graduated from International Islamic University of Malaysia in Mechatronics Engineering.
Lastly we would like to take this oppurtunity to express our gratitude especially to our leader Mr Muhammad Izani
bin Sahak, Mr Julkifli Bin Awang Besar and Mr Philips Dharmaraj for inspiring, motivating and continuously
supporting us with ideas, knowledge and oppurtunities. Not to be forgotten, our lovely family members, colleagues,
and students. May this book will benefit us all and any feedback or improvement is highly welcome.
Preface ii
Content iii
Chapter 1 - Introduction
- What is Arduino? 2
- Type of Arduino 3
- Arduino board pinout 4
iii
References 70
iv
“Open source” term means that the code, schematic, design etc. can be taken freely
by anyone to do what they like it.
- Arduino Integrated Development (IDE) software to run on computer. It is a software used for writing the programming code
and then will be uploaded to the Arduino board.
There are many types of Arduino board in the market you can choose. You can choose which Arduino board to be used depending on the
need of your own project. In addition, the price of each board is also different.
ARDUINO UNO
Most famous and complete board for user
Dimension: 2.7 inch x 2.1 inch
Digital input = 14 pin
Analog in = 6 pin
Pwm = 6 pin
- The installer will be downloaded automatically. When finished, run the installer.
- After that, open your Arduino IDE by clicking the icon on your desktop.
New – Create new blank Open – Show a list of Save – Save the current
sketch saved code to open code
Verify – Check the
code for errors Serial Monitor – Display
serial data being sent and
received to computer
Upload – Upload
current code to the
Arduino board
- In your Arduino IDE, click Tools -> Board -> Arduino/Genuino Uno
- You have to repeat these steps everytime you plug in your Arduino board to your computer. If you skip these steps, your code will
not successfully uploaded to your Arduino board.
10
COMPONENT REQUIRED
Arduino Uno USB cable Breadboard
- As a microcontroller for this project - To connect between computer and Arduino - Components will be connected
board. here in the breadboard.
- Bidirectional
Jumper wire (Male to male) LED (Light Emitting Diode) Resistor
- A conducting wire that establishes - A component which emits light when - A component that is used to
an electrical connection between current flows through it. reduce current flow
two points in a circuit - Longer leg is anode while shorter leg is - No polarity
cathode
12
13
- void setup() function is called when the code starts. It only run once. It is use to initialize the variables, pin modes, instruction to
start using library etc.
- void loop() will run code and loops consecutively, allowing program to change and response. It is use to actively control Arduino
board. This is where your important code like reading sensor, making decision and running output will be executed.
16
2. digitalWrite() / analogWrite()
- Use to write a HIGH or a LOW value to digital pin.
- If HIGH, voltage will be set to the corresponding pin to be outputed 5V and 0V for LOW.
- For example, if you want the LED to turn on, you need to set pin connected to LED to 5V (HIGH)
- Your code should be like below:
3. analogRead() / digitalRead()
- analogRead() is use to read sensor value for analog sensor. The value will read up to 1023 (5V)
- While for digitalRead() will read from digital sensor the digital value which is HIGH or LOW only
17
DATA TYPE
- In order for you to be able to use variable, first you need to know what is type of data you want to store.
- In Arduino, there are 3 basic daya type used namely:
o char – for character or alphabet. Written in single quote for single character ‘A’ and double quote for string
“ABC”
o int – for integer number. Example 12, 4, 56782
o float/double – for number that has a decimal point. Example 2.345, 3.142, 345.3
OPERATOR
- Tell the compiler to perform specific mathematical or logical functions.
18
The above expression means that, temperature read by sensor MUST be between 0 until 19 so that the output will be true (1).
The above expression means that, any of the push button (pushButton1 OR pushButton2) is pressed, the output will be true (1).
19
The above if…else statement will work as follows. If temperature is more than 50˚C, fan will on. However is the temperature is not more than
50˚C(less than 50˚C) , the fan will be off
o For loop - executes statements a predetermined number of times. The control expression for the loop is
initialized, tested and manipulated entirely within the for loop parentheses.
The above for loop will start counting from 0 until 3. Means there will be 3 loops. Count++ indicates the increment by 1. Thus, the buzzer will
turn on for 0.5 second and then off for 0.5 second for 3 times.
20
- This is done by clicking the magnifing glass icon at the top right corner of the Arduino IDE
- The instruction for enable serial monitor is as below:
OTHER SYNTAX
- Comments are lines in the program that are used to inform yourself or others about the way the program works.
- Comments' only purpose is to help you understand (or remember), or to inform others about how your program
works.
- There are 2 types of comment
o Block comment – start from /* symbol until */ symbol. All inside these symbols are comments
o Single line comment – start from // symbol unill the end of a line is comment.
21
COMPONENT REQUIRED
• As mention before, we will still using Arduino UNO, USB cable, Jumper wire, LED and resistor as project 1. But now we will add the
push button to our circuit.
Push Button
- Push button is a component that is designed to close or open an electric circuit when a button or knob is depressed, and to
return to a normal position when it is released.
- It has 4 legs. But actually only 2 legs is required when wiring. The other 2 legs are internally connected
23
24
25
26
27
Start
Yes
If push button is LED on
pressed
No
LED off
End
- If you press the push button, the LED will be turned on. While if you do not press the push button, LED will be off.
- Observe your LED. Does it light up when you press the push button?
28
COMPONENT REQUIRED
• Please take note that we will still using Arduino UNO, USB cable, Jumper wire, LED and resistor as project 1. Now we will change
the push button to Ultrasonic sensor.
- Ultra sonic sensor is a sensor that is used to measure distance by emitting ultrasonic sound waves, and converts the
reflected sound into an electrical signal
- This sensor 2 main components which are TRIGGER and ECHO. Trigger will emit the sound wave and when there are
object in front of it, the sound wave will be refected back and received by Echo.
30
- Example of animals that uses this type of sound wave are bat and whale.
31
33
Arduino IDE
- Make sure the baud rate is 9600 and observe the reading of your sensor. Place an object in front of the ultrasonic sensor
and move it toward and outward the sensor. The value will change accordingly.
34
Start
Read distance
Yes
If distance < 50cm LED on
No
LED off
End
- Based on the flowchart above, if the Ultrasonic sensor sense an object located less than 50cm from the sensor, the LED
will be turn on. This indicate that the object is near to the sensor
- If the object is more than 50cm from the sensor (far from sensor), the LED will be turn off.
- The code is as below. Please observe what has been added to the previous code.
35
- As for the decision part, we added if .. else statement. If the distance is less than 50cm, LED will on. Other than that
(>50) LED is off.
37
COMPONENT REQUIRED
• We will using same circuit as previous project but we will change Ultrasonic sensor with DHT 11 sensor
- The DHT11 a sensor that is use to measure temperature and [Link] is small, low cost and easy to use.
- It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data
pin.
- Its temperature range is from 0 to 50 degrees Celsius with +-2 degrees accuracy. Its humidity range is from 20 to 80 percent
with 5 percent accuracy.
39
40
- Click the [Link] website. [Link] is a website that compile all code, library, projects, and build software alongside 40
million developers. It is an open source website.
41
43
- Next, click File -> Examples -> DHT sensor library ->DHTtester
44
45
Start
Read temperature
Yes
If temperature>30 50cm
LED on
No
LED off
End
47
- In order for us to be able to use LED, first we must declare the pin number of LED connected to Arduino Digital Pin 13.
- Next, in void setup, we must define LedPin as an output.
- Lastly at the last part of the code, we add if.. else statement
48
Yes
If temperature >30 and
humidity <60 LED on
No
LED off
End
50
COMPONENT REQUIRED
OLED
5V DC Motor
- There are many types of motor in the market. 3 basic type of motor that are commonly used are DC
motor, Servo Motor and Stepper Motor. But in this mini project, we will be using DC Motor
- This DC motor need to be supplied with 5v in order for it to turn on.
- If you wish to control the direction (clockwise or anti clockwise) and the speed, you need to use Motor
Driver Module. However for this mini project, we just want it to rotate any direction with only one
speed.
52
53
54
- Same as step before, click the Clone or Download button and click Downlod ZIP. The zip folder will be
automatically downloaded.
- Wait until it finished downloading. Normally all the downloaded item will be store in the Download file
- Open Arduino IDE
- Click Sketch -> Include Library -> Add .ZIP Library
56
57
- Then, u can upload your code and observe what appear in your OLED
58
- Did you go through the code? Did you find it difficult and complicated? Don’t worry. For beginner, just retype the coding as
below
- Before that, open DHTTester programming code side by side of ssd1306_128x64_i2c programming code
- First, in DHTtester, uncomment #define DHTTYPE DHT11 and comment #define DHTTYPE DHT22. Moreover because we connect
DHT11 signal pin to Arduino Digital Pin 6, thus, we need to change to #define DHTPIN 6
59
60
61
- Upload the coding to your Arduino Board and observe the display at your OLED.
62
Start
Read temperature
Yes
If temperature >30 50cm Motor on
No
Motor off
End
- So we need to declare Arduino digital pin for motor, define pin for output and add if…else statement.
- Below is the full coding for this mini project
63
69
70