UNIT 2 - Basics of the Arduino Board
UNIT 2 - Basics of the Arduino Board
Arduino provides a standard form factor that breaks the functions of the micro-controller into a
more accessible package.
This chapter is intended for enthusiastic students or hobbyists. With Arduino, one can get to know
the basics of micro-controllers and sensors very quickly and can start building prototype with very
little investment.
This chapter is intended to make you comfortable in getting started with Arduino and its various
functions.
Before you start proceeding with this chapter, I assume that you are already familiar with the basics
of C and C++. If you are not well aware of these concepts, then we will suggest you go through our
short tutorials on C and C++. A basic understanding of microcontrollers and electronics is also
expected.
IDE Architecture
IDE stands for Integrated Development Environment – An official software introduced by Arduino.cc,
that is mainly used for writing, compiling and uploading the code in the Arduino Device. Almost all
Arduino modules are compatible with this software that is an open source and is readily available to
install and start compiling the code on the go.
Arduino IDE is an open source software that is mainly used for writing and compiling the code into
the Arduino Module.
It is an official Arduino software, making code compilation too easy that even a common person with
no prior technical knowledge can get their feet wet with the learning process.
1
It is easily available for operating systems like MAC, Windows, Linux and runs on the Java Platform
that comes with inbuilt functions and commands that play a vital role for debugging, editing and
compiling the code in the environment.
A range of Arduino modules available including Arduino Uno, Arduino Mega, Arduino
Leonardo, Arduino Micro and many more.
Each of them contains a microcontroller on the board that is actually programmed and accepts the
information in the form of code.
The main code, also known as a sketch, created on the IDE platform will ultimately generate a Hex
File which is then transferred and uploaded in the controller on the board.
The IDE environment mainly contains two basic parts: Editor and Compiler where former is used for
writing the required code and later is used for compiling and uploading the code into the given
Arduino Module.
1. Menu Bar
2. Text Editor
3. Output Pane
As you download and open the IDE software, it will appear like an image below.
2
The bar appearing on the top is called Menu Bar that comes with five different options as follow
File – You can open a new window for writing the code or open an existing one. Following table
shows the number of further subdivisions the file option is categorized into.
3
As you go to the preference section and check the compilation section, the Output Pane will show
the code compilation as you click the upload button.
4
And at the end of compilation, it will show you the hex file it has generated for the recent sketch
that will send to the Arduino Board for the specific task you aim to achieve.
Edit – Used for copying and pasting the code with further modification for font
Tools – Mainly used for testing projects. The Programmer section in this panel is used for burning a
bootloader to the new microcontroller.
Help – In case you are feeling skeptical about software, complete help is available from getting
started to troubleshooting.
The Six Buttons appearing under the Menu tab are connected with the running program as follow.
The check mark appearing in the circular button is used to verify the code. Click this once you have
written your code.
The arrow key will upload and transfer the required code to the Arduino board.
5
The button appearing on the top right corner is a Serial Monitor – A separate pop-up window that
acts as an independent terminal and plays a vital role for sending and receiving the Serial Data. You
can also go to the Tools panel and select Serial Monitor, or pressing Ctrl+Shift+M all at once will
open it instantly. The Serial Monitor will actually help to debug the written Sketches where you can
get a hold of how your program is operating. Your Arduino Module should be connected to your
computer by USB cable in order to activate the Serial Monitor.
You need to select the baud rate of the Arduino Board you are using right now. For my Arduino Uno
Baud Rate is 9600, as you write the following code and click the Serial Monitor, the output will show
as the image below.
The main screen below the Menu bard is known as a simple text editor used for writing the required
code.
6
The bottom of the main screen is described as an Output Pane that mainly highlights the compilation
status of the running code: the memory used by the code, and errors occurred in the program. You
need to fix those errors before you intend to upload the hex file into your Arduino Module.
More or less, Arduino C language works similar to the regular C language used for any embedded
system microcontroller, however, there are some dedicated libraries used for calling and executing
specific functions on the board.
Libraries
Libraries are very useful for adding the extra functionality into the Arduino Module. There is a list of
libraries you can add by clicking the Sketch button in the menu bar and going to Include Library.
7
As you click the Include Library and Add the respective library it will on the top of the sketch with a
#include sign. Suppose, I Include the EEPROM library, it will appear on the text editor as
#include<EEPROM.h>.
Most of the libraries are preinstalled and come with the Arduino software. However, you can also
download them from the external sources.
digitalRead()
[Digital I/O]
Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)
Parameters
Returns
8
HIGH or LOW
Example Code
void setup() {
void loop() {
If the pin isn’t connected to anything, digitalRead() can return either HIGH or LOW (and this can
change randomly).
The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the
Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins, which can only be used as analog inputs.
Digital Pins
The pins on the Arduino can be configured as either inputs or outputs. This document explains the
functioning of the pins in those modes. While the title of this document refers to digital pins, it is
important to note that vast majority of Arduino (Atmega) analog pins, may be configured, and used,
in exactly the same manner as digital pins.
Arduino (Atmega) pins default to inputs, so they don't need to be explicitly declared as inputs with
pinMode() when you're using them as inputs. Pins configured this way are said to be in a high-
9
impedance state. Input pins make extremely small demands on the circuit that they are sampling,
equivalent to a series resistor of 100 megohm in front of the pin. This means that it takes very little
current to move the input pin from one state to another, and can make the pins useful for such tasks
as implementing a capacitive touch sensor, reading an LED as a photodiode, or reading an analog
sensor with a scheme such as RCTime.
This also means however, that pins configured as pinMode(pin, INPUT) with nothing connected to
them, or with wires connected to them that are not connected to other circuits, will report
seemingly random changes in pin state, picking up electrical noise from the environment, or
capacitively coupling the state of a nearby pin.
Digital output
digitalWrite()
[Digital I/O]
Description
Write a HIGH or a LOW value to a digital pin.
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the
corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal
pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the
internal pull-up resistor. See the Digital Pins tutorial for more information.
If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when
calling digitalWrite(HIGH), the LED may appear dim. Without explicitly
setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a
large current-limiting resistor.
Syntax
digitalWrite(pin, value)
Parameters
Returns
Nothing
Example Code
10
The code makes the digital pin 13 an OUTPUT and toggles it by alternating
between HIGH and LOW at one second pace.
void setup() {
void loop() {
The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the
Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins, which can only be used as analog inputs.
The Fading example demonstrates the use of analog output (PWM) to fade an LED. It is available in
the File->Sketchbook->Examples->Analog menu of the Arduino software.
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital
control is used to create a square wave, a signal switched between on and off. This on-off pattern
can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the
time the signal spends on versus the time that the signal spends off. The duration of "on time" is
called the pulse width. To get varying analog values, you change, or modulate, that pulse width.
If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is
a steady voltage between 0 and 5v controlling the brightness of the LED.
In the graphic below, the green lines represent a regular time period. This duration or period is the
inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the
green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such
11
that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty
cycle (on half the time) for example.
Once you get this example running, grab your arduino and shake it back and forth. What you
are doing here is essentially mapping time across the space. To our eyes, the movement blurs
each LED blink into a line. As the LED fades in and out, those little lines will grow and
shrink in length. Now you are seeing the pulse width.
12
These sensors are usually cheap.
An analog sensor is a sensor that gives you a voltage value that represents the measured value.
For example 2V could mean 25 degrees celsius from an analog temperature sensor.
The Arduino has analog inputs that can be used to read these values.
Some sensors come as modules with a pin you can connect directly to the analog input on the
Arduino.
Others come as resistive sensors that you need to combine with a resistor to read out it’s value like
this:
13
STEP 2: UPLOAD TEST CODE
void setup() {
Serial.begin(9600); // setup serial communication
}
void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // Write value to serial console
}
Use the “Serial Monitor” under the “Tools” menu to see the sensor readings.
To figure out the reading in a format you know, such as Celsius for temperature, check the datasheet
of your sensor and make the necessary calculations in your code.
Programming Interfaces
CONTRIBUTORS: JIMBLOM
FAVORITE22
SHARE
Arduino is awesome! It's our go-to electronics education platform, and it's our top choice for rapid
prototyping, but it isn't for everyone. Maybe it's the cryptic language, or the Java-based IDE, or
14
maybe it's just the teal window border -- regardless of your reasoning, if you're trying to escape the
Arduino IDE, here are a few alternatives we'd like to share.
The Arduino alternatives covered in this tutorial range from simple, introductory graphical
programming to web-based Arduino interfaces for your web browser. Here's a quick overview of
each interface covered, we'll go into further detail later on:
ArduBlock is a graphical programming add-on to the default Arduino IDE. Instead of memorizing
cryptic functions, forgetting semicolons, and debugging code, ArduBlock allows you to build
yourArduino program by dragging and dropping interlocking blocks.
ArduBlock builds on the simplicity of Arduino, and creates a perfect beginner gateway to physical
computing. Instead of tearing your hair out debugging, you can spend your time creating!
Installing ArduBlock
ArduBlock is something of an "add-on" to Arduino, so it requires that you have the Arduino IDE
installed. The benefit of that, though, is -- because Arduino is multi-platform -- ArduBlock works on
Windows, Mac, or Linux. Plus, having Arduino already present makes the transition from visual
programming to text programming easier, when the inevitability approaches.
Installing ArduBlock can be a little tricky -- there's no installer, just a Java file that needs to be stored
in a very specific location. Follow the steps below to install it:
1. Download and Install Arduino (if you haven't already) -- Ardublock is an extension of the
default Arduino IDE, so you'll need to have Arduino installed on your computer to run it.
Check out our Installing Arduino IDE tutorial for help with that.
15
2. Download ArduBlock -- Click the link to the left, or head over to the ArduBlockSourceforge
page to find the latest and greatest version.
3. Identify your Arduino Sketchbook location -- This is a folder on your computer where your
sketches and libraries are saved by default. To find your sketchbook location, run Arduino,
and open Preferences by going to File > Preferences. The contents of the top text box
defines your sketchbook location. Memorize that location and close Arduino.
16
6. Start Arduino -- Or restart it if it was open.
7. Select the Board and Serial Port -- Just as you would if you were using Arduino, make your
board and serial port selections from the "Tools" menu.
Open ArduBlock -- Run ArduBlock by clicking Tools > ArduBlock. If you don't see an entry for
ArduBlock here, double-check to make sure your directories are all correctly typed and cased.
ArduBlock is a perfect interface if you're just getting into programming, electronics, or Arduino.
Check out the ArduBlock section of this tutorial for an introduction and quick getting started guide.
One of Minibloq's most powerful features is its real-time code generation -- as you drag blocks into
your program, the equivalent code is generated concurrently. This makes Minibloq an excellent tool
for beginners to intermediate programmers.
Check out the Minibloq section of this tutorial for an introduction to the interface.Those are the
alternatives we'll be discussing in this tutorial, but there are many others worth checking out,
including:
Scratch for Arduino -- More visual programming! Scratch for Arduino (S4A) is a riff on the
popular Scratch programming environment. If you're an experienced Scratch user, this is
most definitely worth checking out!
17
Modkit -- After a successful Kickstarter campaign, Modkit is well on it's way to producing
another great visual alternative to Arduino. Check out their website and get a feel for
their browser-based visual environment.
Arduino IDE for Atmel Studio -- Atmel Studio is an incredibly powerful tool for programming
and debugging AVR chips like those on the Arduino. If you're looking for a
more advanced approach to Arduino, or Atmel chips in general, check out this extension to
Atmel Studio.
Industrial Applications
1. Home Automation
2. Water level indicators
3. Product Count systems
4. Photovoltaic Door Open systems
5. RFID sensors for Quality assessment
6. RFID based systems for product counts and pass
7. Toll Management systems
8. Vehicle intelligence systems
9. Robotic arms
10. Sense based object recognition devices
11. IOT appliations
18