Arduino Tutorial



At this page (1) Page 2 Page 3 Page 4 (Examples) Page 5 (Examples)
⚛ Introduction to Arduino ⚛ Ultrasonic Sensors ⚛ Input/Output Port Management Functions (Pins) ⚛ The Flashing LED ⚛ Led Chase Effect
⚛ Supply of necessary components ⚛ Arduino Shields ⚛ Input - Output Functions ⚛ Fade in-Fade out ⚛ Led Chase Effect with potentiometer
⚛ LEDs ⚛ Input/Output Ports (Pins) ⚛ Digital Outputs ⚛ Fade in-Fade out, Blink at peaks ⚛ Effects by detecting light (Led effect + light sensor)
⚛ Buttons ⚛ Power Supply ⚛ Digital Inputs ⚛ Gradual increase and decrease of brightness with a potentiometer ⚛ Using sound (Buzzer)
⚛ Potentiometers ⚛ Arduino (IDE) Installation ⚛ Analog Outputs (PWM pins) ⚛ Using the serial screen ⚛ Sound & Light Sensor
⚛ Piezo Buzzers ⚛ Programming Arduino - Basic Functions ⚛ Analog Inputs ⚛ Playing with colors (RGB Led) ⚛ Moving shafts (Servos)
⚛ Photoresistors (LDR) ⚛ Basic structures and programming functions ⚛ Time Function - delay () ⚛ Making an RGB Led ⚛ Moving Servos with a potentiometer
⚛ Servo Motors ⚛ Type of variables ⚛ Time Function - delayMicroseconds () ⚛ Traffic lights ⚛ Motor Control (DC Motor)
⚛ DC Motors ⚛ Programming Comments ⚛ Time Logging Function - millis () ⚛ Pedestrian traffic lights ⚛ Controlling 2 or more motors
    ⚛ Value Mapping Function - map () ⚛ Pedestrian traffic lights and stop button ⚛ Using ultrasound to measure a distance
    ⚛ The Serial Communication Port (Serial)    
    ⚛ Selection Structure    
    ⚛ Repeat Structure (for)    
    ⚛ Support Libraries    



Introduction to Arduino

Arduino logo

Arduino is an open source electronic platform based on flexible and easy-to-use hardware and software. It is intended for systems designers, hobbyists and generally for anyone who is interested in creating interactive objects or environments for his own benefit.

The Arduino is a basic tool that we can build a computing system in the sense that it will control devices in the physical world, unlike the common computer. It is based on flexible, easy-to-use hardware and software. On a development board, incorporates a microcontroller and connects to the PC to be programmed through a simple development environment. With Arduino, devices are created that serve various purposes with the ability to receive changes from their environment through the sensors and/or react according to how they are programmed.

Arduino Uno

Sure there are other platforms and implementations that can do the same thing. But the noticeable difference, is that Arduino is based on an open source technologies. It can be built by anyone, it can be integrated into many devices even for commercial purposes and the most important thing is that there is an entire community that uses Arduino in constructions, so there is a large amount of free information. Projects on this Microcontroller can be autonomous (hardware level) or communicate with software on the developer's PC (programs such as Flash, Processing, MaxMSP). Arduino now uses a specially programmable Atmega382 instead of the FTDI chip, to allow both faster transfer speeds and fast serial communication.

The Arduino microprocessor, is usually programmed in advance to provide a BootLoader. The boot loader, is available to simplify the storage of programs in Arduino's Flash Memory via a serial USB port.

Arduino ADK-R3

In addition, the programming language, libraries, and the integrated development environment that exists for Arduino platform programming are open source software that delivers invaluable knowledge to everyone.

The advantages of the Arduino Platform are:


  • a) Financial: The Arduino platform is an economical solution because it is cheaper. In addition, it is architecturally open and anyone can develop it on its own.
  • b) Transportable: Compared to existing trading platforms, the Arduino platform provides full portability and can be programmed into most operating systems.
  • c) Expandable: The hardware and software of the Arduino platform is open and free for everyone. Every day, thousands of free software developers are developing several libraries to support the platform. At the same time, both the architecture and the hardware of the platform are constantly evolving.

Here is a table where it lists the most typical Arduino platforms available.




To be more technically speaking, there is a circuit that uses a microcontroller, which gives us a number of gates that can function either as inputs or outputs in our circuits. These inputs or outputs can be managed by writing code in the Arduino IDE programming environment based on C/C++. 

The Arduino IDE uses GNU toolchain and AVR Libc tools to provide C ++ compiler programs to appropriate AVR machine language commands as well as the avrudude tool to send the executable program to Arduino's Flash memory.

The digital design of Arduino's hardware is open and accessible to all and is published under Creative Commons Attribution Share-Alike 2.5. Also, Arduino's development environment (IDE) is free software and is published under the GNU General Public License Version2.

Arduino IDE

The Arduino development platform contains a text editor area for writing code, a message area, a menu, a toolbar with buttons for common functions, and a series of menus. It connects to Arduino hardware to load programs and to communicate with each other.

An integrated program is usually called a sketch. This sketch is written with the word processor. It has options for copying/pasting and for searching/replacing text. The console displays the output of text from the Arduino environment including full error messages and other information. Toolbar buttons let you control and upload programs, create new sketch, open and save sketches, and open the serial screen.

toolbar buttons

On the official Arduino page ( you can find a lot of information about it, and download the programming environment from the corresponding page (

In addition to the basic version of the Arduino IDE environment, there is also a variant version of Scratch, which can be used to write programs for Arduino, S4A. The following links, will give you more informations,, All are open source and free of charge. The advantage of this release is graphical programming (blocks like Scratch) in relation to writing commands in the classical environment. Similar logic is Ardublock, which also uses graphical programming via ready blocks for programming. Still, there are graphical releases on the web, such as BlocklyDuino * or ArduinoMio *. You can visit these environments from their respective websites.



Supply of necessary components

The circuitry of the Arduino units is open, that is, the design and its parts are known and given by its manufacturers, with the result that anyone who wants to be able to implement it. So, there is material called Arduino that comes from its creators and official manufacturers in Italy, and you can find many more units that are perfectly compatible with the programs and circuits that already exist and work with the official Arduino units. The only commitment requested by Arduino's creators is to name third-party constructions, holding the name Arduino for them. The community respected it so you will find many other versions that usually have names that end up in -ino, like one of the Chinese versions of Funduino.

A unit such as the Arduino Uno R3 which we use here, which with 14 digital inputs / outputs and 6 analog inputs (Pins) is more than enough for your first applications (and the corresponding usb cable to connect to Your computer), Official creators sell material through their website.

Arduino Uno

Get some resistors and capacitors for your first implementations, also cables to connect the pins to components, to sensors, etc, and a breadboard.

cables and breadboard

resistors and capacitors

Other components that you will need:




The LEDs come in various colors and operating voltages (Fig. 8). Depending on the predicted operating voltage of the led we have to use together and the appropriate resistance to avoid some destruction by hypertension. To find the resistance we need is enough to remember the formula: 


R = (VV - VL) / I


Where R is the resistance we need, VV the supplied voltage (5V from Arduino),  VL is the operating voltage of the led and I the operating current of the led. Typically, a resistor around 220 Ohms covers most of the LEDs.

Figure 8 - Leds in different colors

Also, the leds have polarity, that is, they work only if they are connected to the proper current. Usually the leg to be connected in the positive direction (+) is longer than the negative one (-). Also, the bulb from the negative side (-) is usually flat and not round as it is from the other foot (+). But do not worry, an uplink will not ruin it, it just will not light up in the opposite direction (except in the special case you have given a very high voltage).

In addition to simple leds, there are also RGB leds (figure 9), which can display any color based on the RGB system. We can think of them as three LEDs (red - green, green - blue, blue) in one, with which we can individually combine colors and produce any coloration, just like in digital screens.


The RGB leds have four connection points - one for each color (three sets) and one for the rising or falling. There are two types, anode (+) and cathode (-), depending on the following link. The cathodes, which we use in our examples, are connected to the cathode (-), ie the ground (GND). Thus, we connect from a Pin that will control the voltage that will be given to each color and ground of the Arduino, managing to have complete control over all possible colors.

Caution: We need to connect resistances to each color as they work with less voltage than Arduino's output, as shown in the figure. For the calculation, refer to the previous section. You need to know what kind of led you are using - typically a 150-180Ω resistor for red and 75-100Ω for green and blue will be enough. If you do not have exactly these values ​​for the resistors, use larger ones (this is true in all cases where resistance is used to protect against overvoltage - we prefer to have less voltage than a destruction due to hypertension).

Note for the wiring that the ground is connected to the longest leg of the led (second to the left in figure 9), the pin for the red adjacent to it (1st to the left), the pin for the green on the adjacent interior (3rd from Left) and the pin for the blue on the other outside (4th on the left).




You will often need to use buttons (figure 10) so that we can intervene in the circuit when we want it, eg. To power up a part of it or to stop the power supply of another part.


In the following diagrams, you can see two button circuits. One allows the current to pass when the button is pressed (that is, it is always in the LOW the input pin and when pressed it becomes HIGH) - this is called a pull-down resistor.

The other stops the power flow when it is pressed (that is, it is always in the HIGH input pin and when pressed it becomes LOW - this is called the pull-up resistor.

Pull-up/down Resistor

There is a rule that says the current will follow the path with the lowest resistance. The button has a small internal resistance inside it. Thus, in the circuit of the image (pull-up) when the button is pressed and the circuit is closed, we have a flow of 5V to the ground (lower resistance), whereupon the initial circuit (5V - Input) is interrupted. In the (pull-down) circuit, when the button is pressed, there is a flow from the source to the input (lower resistance), ie the circuit input (5V - Input). Therefore, depending on what we want to do in our circuits, we follow the corresponding way.




A potentiometer can be described as a variable resistor circuit. Turning the control that has, increases or decreases its resistance, thereby passing more or less current from it.


It has three connection points - one foot is connected to the source (5V), one to the ground (GND) and the middle to the circuit we want to get the variable current value (analog pin to Arduino). The potentiometer thus works as a voltage divider, but it changes every time we turn the control knob. Potentiometers are used in many applications, such as changing the volume of sound in stereo, changing the intensity of a lamp, etc.



Piezo Buzzers

A simple accessory that we can connect to our Arduino is a Buzzer. It works exactly like a led, with two wires, one for the source (5V) and one for the GND. If we give it a constant voltage (eg 5V) it will give us a steady sound , While a variable voltage (eg, instead of the source (5V), connecting it to a PWM pin on the Arduino and changing the voltage supplied to it) will give us a variable sound, that is, a sound effect.


In addition to the simple buzzers, there are also buzzer arrangements that change their properties according to the pressure exerted. Their wiring is the same and we can use them to detect power exerted on them.



Photoresistors (LDR)

Photoresistors are resistances that change their properties depending on how much light falls on them. Their resistance diminishes with light (when they are in a brighter environment) and grows as they approach a darker environment. Thus, if connected to a constant voltage circuit (eg, 5V - GND of Arduino), the current flowing through it will vary.


A circuit based on such a resistor can read the brightness and work with its change (for example, it turns on a light gradually as the brightness drops). Photosensitive resistors are used in circuits that detect motion in a space and automatically light up.



Servo Motors

An interesting component that can be handled through Arduino is the servo. It is a device that can turn an axis from 0 to 180 degrees. This arrangement applies to constructions that want to move a part in a controlled manner. If we stabilize somewhere on its base, we can use the servo to move 180 degrees in our construction. The servo is connected by a wire at the source, one on the ground and the third on an Arduino Pin so that we can give instructions on how and how it will turn.


We need first to insert the corresponding library into our program to work (#include ), then we define a variable  (servo myservo;) And the pin through which we will handle it.

Int servoPin = 13;
Servo myservo;

Instead of defining servoPin as an output in the setup function, we associate it with our servo in the setup () function with the command


The servo has three wires - one orange which is connected to the source (5V), one brown that goes to the ground (GND) and one (yellow) that goes to the pin through which we send commands.


Where num is a number from 0 to 179, representing its 180 degrees of torsion.



DC Motors

A useful component for many constructions is the DC motors. You will find them in various forms and features.


Their wiring is simple, they need two wires, one for the source and one for the ground, and the polarity of connections, will give the clockwise or anti-clockwise rotation. So, with a test, we will find the desired direction, while we are not in danger of burning anything from wrong wiring.

In many applications we want to use two motors at the same time. In order to manage them, we use H-bridges and a fairly widespread and useful layout is the L298N, which uses two H-bridges. A lot of good analysis can be found at

arduino motor controller L298N

To connect the motors we need three pins for each, a total of six. We have the pins ON, ENB (activation of the A and B motors respectively), IN1, IN2 for the operation of the A motor and IN3, IN4 for the operation of the B motor. Also, there are pins for the source (5V) and the ground (GND), while the device supports an external power source. Finally, next to the external power supply, we have two sockets for each motor, MOTOR A and MOTOR B respectively. There we connect our motors - if we want more power we can short-circuit two left and two right motors instead of one and one.

The direction of movement is made by the pin of each engine and the control (and speed) with the EN.


  • • IN1 (or IN3) <-- HIGH, IN2 (or IN4) <-- LOW forward movement
  • • IN1 (or IN3) <-- LOW, IN2 (or IN4) <-- HIGH backward movement
  • • IN1 (or IN3) <-- LOW, IN2 (or IN4) <-- LOW motion stop
  • • ENA <- HIGH to be able to check pins IN1, IN2
  • • ENB <- HIGH to be able to check pins IN3, IN4


Continue to page 2