Arduino English
Arduino English
PREFACE
Microcontrollers have a great role in the Industry 4.0 industrial revolution. Many process operations are
carried out under microcontroller control. It is important for our students to understand and learn about
microcontrollers. One of the biggest needs of our students in this regard is the need for applied source books
suitable for technical education.
As the IQinDIGIT KA202 project team, this booklet has been created to meet this need of our students. This
booklet will guide our students and they will realize their shortcomings in order to improve themselves. This
booklet is based on Arduino. Arduino is an open-source electronics platform based on easy-to-use hardware
and software. Today, anyone interested in robotics and mechatronics can easily use it. It attracts the
attention of everyone from 7 to 70. It can be called the most popular microcontroller. Arduino IDE software
language has been chosen for programming the microcontroller. Circuit applications can be built on a bread-
board or simulated in the Tinkercad program of Autodesk. Thus, all experiments can be done in the computer
environment and can be seen as a contribution to the Digital Transformation in the Education system.
We are happy to share the knowledge and experience of the teachers who came together for the Erasmus+
project KA202 - Increasing Digital Qualification in the Digital Transformation- Industry4.0. We hope that the
book will be useful to all technical staff.
www.arduino.cc
www.tinkercad.com
“Funded by the Erasmus+ Programme of the European Union. However, European Commission and
Turkish National Agency cannot be held responsible for any use which may be made of the information
contained therein”
2
INDEX
Index ………………………………………………………………………………………………………………………………….2
Arduino ………………………………………………………………………………………………………………………………….3
1. Arduino ………………………………………………………………………………………………………………………………….3
Worksheet1 ……………………………….……………………………………………………………………………………..………..15
Worksheet2 ……………………………….………………………………………………………………………………..……………..19
Worksheet3 ……………………………….…………………………………………………………………………………..…………..23
Worksheet4 ……………………………….…………………………………………………………………………………..…………..31
Worksheet5 ……………………………….……………………………………………………………………………………..………..36
References ……………………………….……………………………………………………………………………………..………..40
Arduino
1. Arduino
A microcontroller is a complete computer built on a single integrated circuit. It can store the written program
inside. For this reason, it is widely used in industrial applications. They can be 8 bits, 16 bits, and 32 bits
depending on the data size they are processing. The features that a microcontroller should have are:
Arduino was designed by "Ivrea Interaction Design Instituteé in 2005 to develop applications for students
quickly and easily without the need for electronics and programming background. Arduino is a historical
character belonging to the town where this institute is located.
Arduino microcontroller platform is open hardware. It is based on Wiring in hardware and Processing language
in software (IDE). Arduino is a microcontroller platform where computer capabilities are compressed into a
single chip.
On the Arduino microcontroller platform, there are units such as an Atmel series microcontroller, analog and
digital input-output pins, USB input, power supply and reset circuit.
Arduino is the most preferred microcontroller platform today. It is designed with AVR architecture developed
with high-level C language. The reason for this is that it is open source, its hardware and software are easily
accessible, and can be programmed with a piece of code. It is an 8-bit RISC microcontroller on the Arduino
UNO.
Microcontroller Atmega328P
Operating Voltage 5V
Input Voltage (recommend) 7-12V
Input Voltage (limited) 6-20V
DC current per I/O pin 20mA
DC curren per 3.3V 50mA
Flash bellek 32 KB
EEPROM 1 KB
Clock 16 MHz
Arduino is compatible with all operating systems such as Windows, MAC, Linux.Low cost of Arduino and open
source is the reason for preference. Applications can be easily developed with a wide variety of sensors
compatible with Arduino. Arduino products are as follows;
Basic Level Arduino UNO, Arduino 101, Arduino Pro, Arduino Micro, Arduino Nano
Keywords reserved by Ardunio IDE cannot be used by the user. Such as;
asm, code, switch, static, operator.
When naming variables, constants, and functions, all letters of the English alphabet (upper or lower
case), numbers, “_”, can be used. However, the first character of the specified name must be either a
letter or "_". Arduino programming language is case sensitive.
Total3, Fuse, temp_t1, _user
Character expressions are written in single quotes. String expressions are enclosed in double quotes.
'b', 'M', "Hello World"
Characters that are not directly typed on the screen, but have display-related tasks, are called space
characters. It starts with "\".
\n goes to the bottom line
Separator signs are used;
; is used for termination. It is also used in the description line in assembly codes.
In our programs, we want to keep some data in memory for later use. we use variables for this keeping.
Before using variables, we need to define them.
Integer types are generally used in integer operations. The variables we will use, must meet the size we
need.
Byte 1 0…255
Char 1 -128…127
(signed) int 2 -32768…32767
unsigned int 2 0…65535
(signed) long 4 -2147483648…2147483647
unsigned long 4 0…4294967295
float 4 -3.4*1038…+3.4*1038
double 4 -3.4*1038…+3.4*1038
boolean 1 TRUE (1) or FALSE (0)
Byte Data Types
The smallest unit that can take a value of 1 or 0 in digital electronics is called a bit. A data type consisting of 8
bits is called a byte. Integers between 0-255 can be stored with this data type.
Also, the Const qualifier is used to define a constant. The value of the defined constant remains the same.
Some constants are defined in Arduino. We can use them in our programs. Like HIGH, LOW, INPUT, OUTPUT.
1.2.2. Operators
Operators in the same category have the same priority. For operators with the same precedence on the same
line, the order of operations is from left to right. = *= /= %= += -= &= ^= <<= >>=, ! ++ -- + - * & (type)
except sizeof. Unless, of course, there are any parentheses in the line.
Operators that perform operations such as addition, subtraction, multiplication that we use are called
arithmetic operators.
Operator Process
+ Addition
- Subtraction
* Multiplication
/ Division
% Mode Retrieval
++ Increase one
-- Decrease one
Operators that perform logical comparisons between expressions and return TRUE or FALSE as a result are
called comparison operators.
7
Operator Expression
== Equals
!= Not equal
The operators used in the process of passing a value to a variable are called assignment operators. The most
used is the "=" operator. The comparison operator should not be confused with ==.
Operator Meaning
= Basic assignment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Mode assignment
Operators that return TRUE or FALSE by making one or more comparisons are called logical operators.
Operator Meaning
|| Logical OR process
In addition, the << left shift and >> right shift operators are also used a lot.
Functions are subprogram particles that perform some tasks according to input parameters. If we are going to
perform an operation more than once in the program, then this operation can be turned into a function, rather
than rewriting the same codes. So, we can call when we need it. After the operations are done in the function,
a value is returned to the main program with the return command. In C, the main program is written inside
the main() function block. Other functions are used by calling them from within this main program.
The type the function will return Name of the function (parameter list);
void main() {
…
8
return …
While defining a function, Void is used in cases where the result of this function cannot be reversed (with
return).
Lcd_Out_Cp (pressure);
Variables defined at the start of the program are called global variables. These variables are defined
throughout the entire program.
Variables defined at the beginning of functions are only defined within that function. These variables are called
local variables.
Arrays are one of the commonly used methods in C language. With arrays, we can store more than one data
of the same type in a variable array.
int array_name [index] = { The elements of the array are written with a comma between them. }
unsigned short fuses [9] = { 6, 10, 16, 20, 25, 32, 40, 50, 63 }
The index value of the first element of arrays is always 0. With fuses[0] we can reach 6, and with fuses[8] we
can reach 63.
The preprocessor is part of the compilation process. The preprocessor commands are run before starting the
compilation. Lines starting with the # sign are preprocessor commands. We can add an external file to our
program using preprocessor commands. The #include command is used for this.
We can also define macros such as functions and use them in programs using a preprocessor. We use #define
preprocessor commands to create macros. The #define statement is not actually part of the programming
language. The #define statement places the 2nd expression where the 1st expression occurs together.
#include <file_name>
#include “built_in.h”
Control statements are structures that compare the values given during the program flow and enable the
program to be processed according to these values.
If - Else
The if-else block makes comparisons. This allows the program to branch based on the result of the comparison.
9
if (condition)
if (condition) {
else {
Switch - Case
The switch statement checks if a statement matches a constant integer and lets the program flow from the
match. When a match is found, the break command is used to end the control statement. If none of the
conditions match, the commands in the default statement will be executed.
Switch (statements) {
default : expressions ;
While writing a program, we need some code to be run more than once. Loop structures are structures used
for multiple executions of these codes. There are 3 types of loops.
While
The while loop executes the loop if the given condition is true. Before entering the loop, the condition is
checked, if it is TRUE, the loop is executed, if it is FALSE, the loop continues from the next line.
}
10
Do-While
The do-while loop is a variation of the while loop. The only difference is that the condition's correctness is done
at the end of the loop. So even if the condition is false, the loop block is executed once.
do {
} while ( condition );
For
It is the most used loop type. In previous loop types we use a counter variable for the number of loops. We
constantly check the loop condition by increasing or decreasing this counter. However, this process is quite
simple in the For loop.
for ( initial value of the counter; condition expression; increment amount of the counter) {
When a break statement is encountered in a loop, the loop is exited. The continue statement is used to return
to the beginning of the loop.
while ( .. ) { do { for ( .. ; .. ; .. ) {
…. … …
…. … …
} while ( .. ); }
String names are used for character strings. String outputs can be viewed with the Serial Monitor.
11
One of these components is the Arduino Board on which the program runs. Arduino board contains the
microcontroller, other peripheral components and input-output ports. This card is used in practice.
The other component is the integrated development environment (IDE), on which we write our programs,
compile and upload the program.
In order to install the Arduino software, firstly, the https://www.arduino.cc/en/Main/Software link is entered
in our internet browser. Whichever operating system we have, we perform the download by choosing one of
Windows, Mac OS X, Linux.
Arduino IDE
First Programme
After uploading our Arduino program and making our usb connections, let's do our first application, blink, led
blinking without writing any code. First of all, from the Arduino program;
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
By pressing the Verify button, we compile our program. Then, by clicking the upload button we
upload our program to our card. When we see the “Done uploading” message, it indicates that the upload was
successful.
The programs which written in Arduino are called sketches. Arduino program files are saved as .ino extension.
There are basically two functions in Arduino programs: setup() and loop(). The setup function is called once
and is used to make the initial settings in the program. The loop function is a super loop function that is
continuously operated as long as the Arduino continues to run.
15
WORKSHEET 1
DIGITAL INPUT OUTPUT APPLICATIONS
It is aimed to teach the use of digitalRead() and digitalWrite() functions through Worksheet 1 applications.
In this application, when the button is pressed, the LED will turn on, and if it is not pressed, it will turn off.
While building the circuit, connect the 5V and GND terminals on the Arduino to the board. Connect the anode
of the LED to pin 12 of the Arduino and the cathode end to the GND with a 220 ohm resistor.
https://www.tinkercad.com/things/lz919875hFO
Materials Used :
• 1 x 10 Kohm resistor
Circuit diagram:
16
Circuit Programme:
int buttonStatus=0;
void setup() {
pinMode(buttonPin,INPUT);
pinMode(ledPin,OUTPUT);
void loop() {
buttonStatus=digitalRead(buttonPin);
if (buttonStatus == HIGH) {
digitalWrite(ledPin,HIGH);
else {
digitalWrite(ledPin,LOW);
}
17
In this application, 10 LEDs in the circuit will light up from right to left and from left to right, respectively.
Only one LED will light at a time.
https://www.tinkercad.com/things/k7OkyMbdP2U
Materials Used :
1 x Arduino UNO
10 x LEDs
10 x 220 ohm resistors
Circuit diagram:
18
Circuit Programme:
byte ledPin[]={4,5,6,7,8,9,10,11,12,13};
int ledDelay(65);
int direct=1;
int currentLed=0;
void setup() {
pinMode(ledPin[x],OUTPUT);
changing=millis();
void loop() {
if ((millis() - changing)>ledDelay) {
changeLed();
changing=millis();
void changeLed() {
digitalWrite(ledPin[x],LOW);
digitalWrite(ledPin[currentLed], HIGH);
currentLed +=direct;
if (currentLed==0) {direct=1;}
}
19
WORKSHEET 2
ANALOG INPUT/OUTPUT APPLICATIONS
It is aimed to teach the use of analogRead() and analogWrite() functions through Worksheet2 applications.
In this application, the LED in the circuit glows slightly from the off state and turns off again after it is brightest.
This situation is repeated as long as there is energy in the circuit. The pin to which the LED will be connected
is PWM pin 3. With the for loop in the Loop function, the counter value i is started from 0 and counted up to
255. The LED continues this process by going from the lowest light level to the highest level. Observation can
be made by changing the delay time of 10 ms.
https://www.tinkercad.com/things/bhFj7n1PLv6
Materials Used :
1 x Arduino UNO
1 x 220 ohm resistor
1 x LED
Circuit diagram:
20
Circuit Programme:
void setup() {
pinMode(led,OUTPUT);
void loop() {
analogWrite(led,i);
delay(10);
}
21
The potentiometer works as a voltage divider in this application with varying resistance value. The
analogRead() command is used to convert the analog values to digital which taken from the setting pin. Every
value that the potentiometer gives will be transferred to the PC environment, it can be observed on the serial
monitor.
Since the read value is 10 bits, it takes 210=1024 different values. When the input voltage is 5V, the value of
the generated digital data is 1023. The value read every second is sent to the PC. To view these values, it is
sufficient to click the "Serial Monitor" button in the Arduino IDE program.
https://www.tinkercad.com/things/k62KtnVY9hI
Materials Used :
1 x Arduino UNO
1 x 100 Kohm pot
Circuit diagram:
22
Circuit Programme:
int value=0;
void setup() {
Serial.begin(9600);
void loop() {
value=analogRead(POT);
Serial.println(value);
delay(1000);
}
23
WORKSHEET 3
SENSOR APPLICATIONS
It is aimed to teach the use of various sensors (heat, light, distance) with Arduino through Worksheet3
applications. The use of analogRead() and Serial.println() functions will be reinforced.
In this application, the color of the RGB LED changes according to the light intensity falling on the LDR. If the
environment is too bright, the RGB LED will turn red, if the ambient light intensity is normal, the RGB LED will
turn green, and if the environment is too dark, the RGB LED will turn blue. This visual warning, which changes
according to the information from the LDR, can also be observed as a text message from the serial monitor.
The resistance values of the LDRs change depending on the light intensity. It is inversely proportional to the
intensity of the light falling on it. Its resistance is minimum at maximum light intensity, while its resistance is
maximum in the dark. In this application, potentiometer and LDR are used as voltage divider. In this way, a
light-effective circuit whose sensitivity can be adjusted according to the light intensity has been created. The
voltage falling on the LDR in the circuit depends on the intensity of the light and the setting of the
potentiometer.
https://www.tinkercad.com/things/9wne0CaTee6
Materials Used :
Circuit diagram:
24
Circuit Programme:
int ldrValue=0;
int redLed=12;
int greenLed=9;
int blueLed=10;
void setup() {
Serial.begin(9600);
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(blueLed,OUTPUT);
void loop() {
ldrValue=analogRead(0);
digitalWrite(greenLed,HIGH);
digitalWrite(redLed,LOW);
digitalWrite(blueLed,LOW);
digitalWrite(redLed,HIGH);
digitalWrite(greenLed,LOW);
digitalWrite(blueLed,LOW);
else if (ldrValue>=500)
digitalWrite(blueLed,HIGH);
digitalWrite(greenLed,LOW);
25
digitalWrite(redLed,LOW);
delay(200);
}
26
In this application, the analog information to be read from the LM35 temperature sensor is converted to
degrees Celsius with the analogRead() command.
If the measured temperature is above 50 degrees Celsius, the red LED will light and the alarm will sound. If the
measured temperature is 50 degrees Celsius and below, the green LED will turn on and the alarm will turn off.
https://www.tinkercad.com/things/1WAQUsl3U1n
Materials Used :
Circuit diagram:
27
Circuit Programme:
int lm35_pin=A1;
int greenLed=11;
int redLed=5;
int buzzerAlarm=2;
void setup() {
pinMode(lm35_pin,INPUT);
pinMode(greenLed,OUTPUT);
pinMode(redLed,OUTPUT);
pinMode(buzzerAlarm,OUTPUT);
digitalWrite(greenLed,HIGH);
void loop() {
float lm35_reading_value=analogRead(lm35_pin);
float analog_temp=(lm35_reading_value/1023)*5000;
float digital_temp=analog_temp/10.0;
if (digital_temp>50)
tone(buzzerAlarm,300);
digitalWrite(redLed,HIGH);
digitalWrite(greenLed,LOW);
else
noTone(buzzerAlarm);
digitalWrite(redLed,LOW);
digitalWrite(greenLed,HIGH);
}
28
In this application, the analog value from the ultrasonic sensor is converted to cm. If the measured distance is
closer than 40 cm, the green LED is on, and if it is 40 cm or more, the red LED is on. At the same time, distance
information in cm can be observed on the serial monitor. If the measured distance value is outside the range
of 4-250 cm, "Out of range" is printed on the serial monitor and the person making the measurement is
warned.
https://www.tinkercad.com/things/eRaD10MzVUv
Materials Used:
1 x Arduino UNO
1 x HC-SR04 ultrasonic sensor
2 x LED (Red, Green)
2 x 220 ohm resistor
Circuit Diagram:
29
Circuit Programme:
#define trigPin 13
#define echoPin 12
#define greenLED 11
#define redLED 10
void setup()
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(greenLED,OUTPUT);
pinMode(redLED,OUTPUT);
void loop()
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
distance = (duration/2)/29.1;
if (distance<40)
digitalWrite(greenLED,HIGH);
digitalWrite(redLED,LOW);
else
digitalWrite(greenLED,LOW);
30
digitalWrite(redLED,HIGH);
Serial.println("Out of range");
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
else
delay(500);
}
31
WORKSHEET 4
LCD AND DISPLAY DRIVING APPLICATIONS
In Worksheet4 applications, it is aimed to teach the use of 7-segment display and LCD with Arduino. In the
LCD driving application, the library file will be added for the first time.
In this application, the numbers from 0 to 9 are displayed at one second intervals on the 7-segment display.
Since a common anode display is used in the circuit, Logic 0 information is sent to light the LEDs that will
create the desired number to be displayed.
https://www.tinkercad.com/things/b5Xmj3tT7Ky
Materials Used:
1 x Arduino UNO
1 x 7 segment display
7 x 220 ohm resistor
Circuit Diagram:
32
Circuit Programme:
void setup() {
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
void loop() {
for(int i=0;i<10;i++)
displayDigit(i);
delay(1000);
turnOff();
digitalWrite(a,LOW);
digitalWrite(b,LOW);
if(digit!=2)
digitalWrite(c,LOW);
33
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
void turnOff()
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
34
In this application, temperature measurement is made with the LM35 sensor and the measured value is
displayed on a 2-line 16-character LCD. The LM35 temperature sensor outputs 10 mV per degree Celsius. An
example of 20x10mV=200mV can be given for 20 degrees Celsius. In practice, the output of LM35 is connected
to the analog 0 input of Arduino. The library file included in the program determines the connections between
the Arduino and the LCD screen.
https://www.tinkercad.com/things/j3V6kux6oG4
Materials Used :
1 x Arduino UNO
1 x 10 Kohm pot
1 x LM35
1 x LCD (2x16)
Circuit diagram:
35
Circuit Programme:
#include <LiquidCrystal.h>
float temp;
float voltage;
int tempPin=0;
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
void loop() {
voltage=analogRead(tempPin);
voltage=(voltage/1023)*5000;
temp=voltage/10.0;
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.setCursor(0,1);
lcd.print(temp);
delay(1000);
}
36
WORKSHEET 5
MOTOR DRIVING APPLICATIONS
Motors used in microcontroller systems are machines that convert electrical energy into mechanical energy.
Frequently, DC, step and servo types are used. In this tutorial, it is aimed to teach the use of DC and servo
motor with Arduino UNO.
DC motors determine the direction and speed of rotation depending on the polarity and value of the DC
voltage. In this application, the middle end of the pot connected to the A0 pin produces a voltage between 0-
5V as the value of the pot changes. The range of numerical values that result from the ADC is between 0-1023.
By using the map() function in the code of the application, the values of the value variable between 0-1023 are
limited between 0-255. The purpose of this operation is that the pwm value can be between 0-255.
Accordingly, the value read from the pot is transferred to the base of the transistor as a pwm output between
0-255. This changes the speed of the engine
https://www.tinkercad.com/things/1Xri4sAbJbv
Materials Used :
Circuit diagram:
37
Circuit Programme:
int value=0;
void setup() {
pinMode(motorPin,OUTPUT);
void loop() {
value=analogRead(pot);
value=map(value,0,1023,0,255);
analogWrite(motorPin,value);
delay(10);
}
38
The type of motor used in this application moves angularly according to the given commands and can stay in
the position we want. They have 3 connecting pins. These pins are GND (usually brown or black), 5V (red), and
the signal lead (usually white or orange). Since the motor is supplied from an external power source in the
circuit, the 7805 regulator integrated is used. The values corresponding to the analog data between 0-1023
read from the potentiometer in the circuit are limited between 0-179 degrees with the map() function. In this
way, as the value of the potentiometer changes, the servo motor moves between 0-180 degrees.
https://www.tinkercad.com/things/aGxTfixk283
Materials Used :
1 x Arduino UNO
1 x 7805
1 x 9 V battery
1 x servo motor
1 x 10 Kohm pot
Circuit diagram:
39
Circuit Programme:
#include <Servo.h>
Servo servoControl;
int value=0;
void setup() {
servoControl.attach(ServoPals);
void loop() {
value=analogRead(Pot);
value=map(value,0,1023,0,179);
servoControl.write(value);
delay(15);
}
40
References:
Arduino Language Reference
Arduino Temel Atolye Uygulamaları, 2015
Arduino, Çoşkun TASDEMIR, Dikeyeksen, 2012
Derinlemesine Arduino, Bulent COBANOGLU, Abakus, 2017
Cocuklar Icin Uygulamalarla Arduino, Bülent COBANOGLU, Abakus, 2017
Arduino web site, https://www.arduino.cc/
Tinkercad web site, https://www.tinkercad.com
THE CONTRIBUTORS