You are on page 1of 28

Embedded C Programming

UNIT III
IOT AND ARDUINO PROGRAMMING
Introduction to the Concept of IoT Devices – IoT Devices Versus Computers – IoT
Configurations – Basic Components – Introduction to Arduino – Types of Arduino –
Arduino Toolchain – Arduino Programming Structure – Sketches – Pins – Input/Output
From Pins Using Sketches – Introduction to Arduino Shields – Integration of Sensors and
Actuators with Arduino.
INTRODUCTION TO THE CONCEPT OF IOT DEVICES
IoT devices are pieces of hardware, such as sensors, actuators, gadgets, appliances, or
machines, that are programmed for certain applications and can transmit data over the internet or
other networks. They can be embedded into other mobile devices, industrial equipment,
environmental sensors, medical devices, and more.
IoT devices are using AI and machine learning to bring intelligence and autonomy to
systems and processes, such as autonomous driving, industrial smart manufacturing, medical
equipment, and home automation. Many of these devices are small, power- and cost-constrained
microcontroller-based systems. Network bandwidth and consumer expectations around data
privacy and user experience continue to demand more on-device processing, where data is
processed on the IoT endpoint, rather than using cloud-based approaches.
Internet of Things Devices is non-standard devices that connect wirelessly to a network
with each other and able to transfer the data. IoT devices are enlarging the internet connectivity
beyond standard devices such as smartphones, laptops, tablets, and desktops. Embedding these
devices with technology enable us to communicate and interact over the networks and they can
be remotely monitored and controlled.
IoT devices have both industrial and consumer uses and are typically integrated into other
tools such as mobile devices, industrial equipment and medical devices. Over a broad range, they
can also be used in smart cities. They're then used to send data or interact with other IoT devices
over a network. IoT and IoT devices aid in making daily activities faster, easier or more
convenient for consumers while also providing real-time data for industrial or enterprise use
cases. There are large varieties of IoT devices available based on IEEE 802.15.4 standard. These
devices range from wireless motes, attachable sensor-boards to interface-board which are useful
for researchers and developers. IoT devices include computer devices, software, wireless
sensors, and actuators. These IoT devices are connected over the internet and enabling the data
transfer among objects or people automatically without human intervention.

R.Rama Rajesh.,M.E.MBA.,MHRM., 1
Embedded C Programming

Some of the common and popular IoT devices are given below:

IOT DEVICES VERSUS COMPUTERS


IOT Devices Computers

IoT devices are special-purpose devices. Computers are general-purpose devices.


IoT devices can do only a particular task for
Computers can do so many tasks.
which it is designed.
The hardware and software built-in in the IoT The hardware and software built-in in the
devices are streamlined for that particular computers are streamlined to do many tasks(such
task. as calculation, gaming, music player, etc. )
IoT devices can be cheaper and faster at a
A computer can be expensive and slower at a
particular task than computers, as IoT devices
particular task than an IoT device.
are made to do that particular task.
Examples: Music Player- iPod, Alexa, smart
Examples: Desktop computers, Laptops, etc
cars, etc.

R.Rama Rajesh.,M.E.MBA.,MHRM., 2
Embedded C Programming

IOT COMPONENTS

1. Things or Device
These are fitted with sensors and actuators. Sensors collect data from the environment
and give to gateway where as actuators performs the action (as directed after processing of data).
Devices and sensors are the components of the device connectivity layer. These smart sensors
are continuously collecting data from the environment and transmit the information to the next
layer. The latest techniques in semiconductor technology are capable of producing micro smart
sensors for various applications.
Common sensors are:
Temperature sensors and thermostats
Pressure sensors
Humidity / Moisture level
Light intensity detectors
Moisture sensors
Proximity detection
RFID tags

R.Rama Rajesh.,M.E.MBA.,MHRM., 3
Embedded C Programming

Gateway
The sensors give data to Gateway and here some kind of pre-processing of data is even
done. It also acts as a level of security for the network and for the transmitted data. IoT Gateway
manages the bidirectional data traffic between different networks and protocols. Another
function of the gateway is to translate different network protocols and make sure interoperability
of the connected devices and sensors.
Gateways can be configured to perform pre-processing of the collected data from
thousands of sensors locally before transmitting it to the next stage. In some scenarios, it would
be necessary due to the compatibility of the TCP/IP protocol. IoT gateway offers a certain level
of security for the network and transmitted data with higher-order encryption techniques. It acts
as a middle layer between devices and the cloud to protect the system from malicious attacks and
unauthorized access.
Cloud
The data after being collected is uploaded to cloud. Cloud in simple terms is basically a
set of servers connected to internet 24*7. The Internet of Things creates massive data from
devices, applications, and users, which has to be managed in an efficient way. IoT cloud offers
tools to collect, process, manage and store huge amounts of data in real time. Industries and
services can easily access these data remotely and make critical decisions when necessary.
Basically, the IoT cloud is a sophisticated, high-performance network of servers
optimized to perform high-speed data processing of billions of devices, traffic management, and
deliver accurate analytics. Distributed database management systems are one of the most
important components of the IoT cloud. Cloud system integrates billions of devices, sensors,
gateways, protocols, and data storage and provides predictive analytics. Companies use these
analytics data to improve products and services, preventive measures for certain steps, and build
their new business model accurately.
Analytics
The data after being received in the cloud processing is done . Various algorithms are
applied here for proper analysis of data (techniques like Machine Learning etc are even applied).
Analytics is the process of converting analog data from billions of smart devices and sensors into
useful insights which can be interpreted and used for detailed analysis. Smart analytics solutions
are inevitable for IoT systems for the management and improvement of the entire system.

R.Rama Rajesh.,M.E.MBA.,MHRM., 4
Embedded C Programming

One of the major advantages of an efficient IoT system is real-time smart analytics which
helps engineers to find out irregularities in the collected data and act fast to prevent an undesired
scenario. Service providers can prepare for further steps if the information is collected
accurately at the right time.
Big enterprises use the massive data collected from IoT devices and utilize the insights
for their future business opportunities. Careful analysis will help organizations to predict trends
in the market and plan ahead for a successful implementation. Information is very significant in
any business model, and predictive analysis ensures success in the concerned area of the business
line.
User Interface
`User end application where user can monitor or control the data. User interfaces are the
visible, tangible part of the IoT system which users can access. Designers will have to make sure
of a well-designed user interface for minimum effort for users and encourage more interactions.
Modern technology offers much interactive design to ease complex tasks into simple touch panel
controls. Multicolor touch panels have replaced hard switches in our household appliances, and
the trend is increasing for almost every smart home device.
The user interface design has higher significance in today’s competitive market; it often
determines the user whether to choose a particular device or appliance. Users will be interested
in buying new devices or smart gadgets if it is very user-friendly and compatible with common
wireless standards.
Challenges in IOT
1. Security: As the number of devices connected to internet are increasing so as poorly designed
devices are also getting more prone to security threats.
2. Privacy: The data of the user has to be protected from theft as almost all activities done by an
individual can be monitored.
3. Standards: There are no proper standards and documentations discussing the best practices.
4. Regulation: Legal issues with Int involves cross border data flow, data retention and
destruction policies etc.
5. Development: It should be expanded and implemented to even developing and under
developed countries instead of focusing only on developed countries.

R.Rama Rajesh.,M.E.MBA.,MHRM., 5
Embedded C Programming

INTRODUCTION TO ARDUINO
Arduino is a prototype platform (open-source) based on an easy-to-use hardware and
software. It consists of a circuit board, which can be programed (referred to as a microcontroller)
and a ready-made software called Arduino IDE (Integrated Development Environment), which is
used to write and upload the computer code to the physical board.
Key Features
1. Arduino boards are able to read analog or digital input signals from different sensors and turn
it into an output such as activating a motor, turning LED on/off, connect to the cloud and many
other actions.
2. You can control your board functions by sending a set of instructions to the microcontroller on
the board via Arduino IDE (referred to as uploading software).
3. Unlike most previous programmable circuit boards, Arduino does not need an extra piece of
hardware (called a programmer) in order to load a new code onto the board. You can simply use
a USB cable.
4. Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn to
program.
5. Finally, Arduino provides a standard form factor that breaks the functions of the micro-
controller into a more accessible package

R.Rama Rajesh.,M.E.MBA.,MHRM., 6
Embedded C Programming

1. Power USB
Arduno board can be powered by using the USB cable from your computer. All you need to do is
connect the USB cable to the USB connection (1).
2. Power (Barrel Jack)
Arduino boards can be powered directly from the AC mains power supply by connecting it to the
Barrel Jack (2).
3 Voltage Regulator
The function of the voltage regulator is to control the voltage given to the Arduino board and
stabilize the DC voltages used by the processor and other elements.
4. Crystal Oscillator
The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate
time? The answer is, by using the crystal oscillator. The number printed on top of the Arduino
crystal is 16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz.
5. Arduino Reset: You can reset your Arduino board, i.e., start your program from the
beginning. You can reset the UNO board in two ways. First, by using the reset button (17) on the
board. Second, you can connect an external reset button to the Arduino pin labelled RESET (5).

R.Rama Rajesh.,M.E.MBA.,MHRM., 7
Embedded C Programming

6. Pins (3.3, 5, GND, Vin)


3.3V (6) − Supply 3.3 output volt, 5V (7) − Supply 5 output volt. Most of the components
used with Arduino board works fine with 3.3 volt and 5 volt. GND (8)(Ground) − There are
several GND pins on the Arduino, any of which can be used to ground your circuit. Vin (9) −
This pin also can be used to power the Arduino board from an external power source, like AC
mains power supply.
10. Analog pins
The Arduino UNO board has six analog input pins A0 through A5. These pins can read the
signal from an analog sensor like the humidity sensor or temperature sensor and convert it into a
digital value that can be read by the microprocessor.
11. Main microcontroller
Each Arduino board has its own microcontroller (11). You can assume it as the brain of your
board. The main IC (integrated circuit) on the Arduino is slightly different from board to board.
The microcontrollers are usually of the ATMEL Company. You must know what IC your board
has before loading up a new program from the Arduino IDE. This information is available on the
top of the IC.
12. ICSP pin( In Circuit Serial Port Programming)
Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI,
MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral
Interface), which could be considered as an "expansion" of the output. Actually, you are slaving
the output device to the master of the SPI bus.
13. Power LED indicator
This LED should light up when you plug your Arduino into a power source to indicate that your
board is powered up correctly. If this light does not turn on, then there is something wrong with
the connection.
14. TX and RX LEDs
On your board, you will find two labels: TX (transmit) and RX (receive). They appear in two
places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate the pins
responsible for serial communication. Second, the TX and RX led (13). The TX led flashes with
different speed while sending the serial data. The speed of flashing depends on the baud rate
used by the board. RX flashes during the receiving process.

R.Rama Rajesh.,M.E.MBA.,MHRM., 8
Embedded C Programming

15. Digital I/O


The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse Width
Modulation) output. These pins can be configured to work as input digital pins to read logic
values (0 or 1) or as digital output pins to drive different modules like LEDs, relays, etc. The
pins labeled “~” can be used to generate PWM.
16. AREF
AREF stands for Analog Reference. It is sometimes, used to set an external reference voltage
(between 0 and 5 Volts) as the upper limit for the analog input pins.

TYPES OF ARDUINO
1. Arduino UNO
Arduino UNO is based on an ATmega328P microcontroller. It is easy to use compared to
other boards, such as the Arduino Mega board, etc. The Arduino UNO includes 6 analog pin
inputs, 14 digital pins, a USB connector, a power jack, and an ICSP (In-Circuit Serial
Programming) header. It is the most used and of standard form from the list of all available
Arduino Boards. It is also recommended for beginners as it is easy to use.
2. Arduino Nano
The Arduino Nano is a small Arduino board based on ATmega328P or ATmega628
Microcontroller. The connectivity is the same as the Arduino UNO board. The Nano board is
defined as a sustainable, small, consistent, and flexible microcontroller board. It is small in size
compared to the UNO board. The devices required to start our projects using the Arduino Nano
board are Arduino IDE and mini USB. The Arduino Nano includes an I/O pin set of 14 digital
pins and 8 analog pins. It also includes 6 Power pins and 2 Reset pins.
3. Arduino Mega
The Arduino Mega is based on ATmega2560 Microcontroller. The ATmega2560 is an 8-
bit microcontroller. We need a simple USB cable to connect to the computer and the AC to DC
adapter or battery to get started with it. It has the advantage of working with more memory
space. The Arduino Mega includes 54 I/O digital pins and 16 Analog Input/Output (I/O), ICSP
header, a reset button, 4 UART (Universal Asynchronous Reciever/Transmitter) ports, USB
connection, and a power jack.

R.Rama Rajesh.,M.E.MBA.,MHRM., 9
Embedded C Programming

4. Arduino Micro
The Arduino Micro is based on the ATmega32U4 Microcontroller. It consists of 20 sets
of pins. The 7 pins from the set are PWM (Pulse Width Modulation) pins, while 12 pins are
analog input pins. The other components on board are reset button, 16MHz crystal oscillator,
ICSP header, and a micro USB connection. The USB is inbuilt in the Arduino Micro board. The
Arduino Micro is also called as the shrunk version of Arduino Leonardo.
5. Arduino Leonardo
The basic specification of the Arduino Leonardo is the same as the Arduino Micro. It is
also based on ATmega32U4 Microcontroller. The components present on the board are 20
analog and digital pins, reset button, 16MHz crystal oscillator, ICSP header, and a micro USB
connection.
6. Arduino Due
The Arduino Due is based on the 32- bit ARM core. It is the first Arduino board that has
developed based on the ARM Microcontroller. It consists of 54 Digital Input/Output pins and 12
Analog pins. The Microcontroller present on the board is the Atmel SAM3X8E ARM Cortex-M3
CPU. It has two ports, namely, native USB port and Programming port. The micro side of the
USB cable should be attached to the programming port
7. Arduino Shields
The Arduino shields are the boards, which can be plugged on the top of the PCB. The
shields further extend the potential of the PCB's. The production of shields is cheap. It is also
easy to use. There are various types of Arduino shields that can be used for different purposes.
For example, the Xbee shield. The Xbee shield can be used for wireless communication between
multiple Arduino boards over distances upto 300 feet using the Zigbee Module.
8. Arduino Lilypad
The Arduino LilyPad was initially created for wearable projects and e-textiles. It is based
on the ATmega168 Microcontroller. The functionality of Lilypad is the same as other Arduino
Boards. It is a round, light-weight board with a minimal number of components to keep the size
of board small. The Arduino Lilypad board was designed by Sparkfun and Leah. It was
developed by Leah Buechley. It has 9 digital I/O pins.

R.Rama Rajesh.,M.E.MBA.,MHRM., 10
Embedded C Programming

9. Arduino Bluetooth
The Arduino Bluetooth board is based on ATmega168 Microcontroller. It is also named
as Arduino BT board. The components present on the board are 16 digital pins, 6 analog pins,
reset button, 16MHz crystal oscillator, ICSP header, and screw terminals. The screw terminals
are used for power. The Arduino Bluetooth Microcontroller board can be programmed over the
Bluetooth as a wireless connection.
10. Arduino Diecimila
The Arduino Diecimila is also based on ATmeg628 Microcontroller. The board consists
of 6 analog pin inputs, 14 digital Input/Output pins, a USB connector, a power jack, an ICSP (In-
Circuit Serial Programming) header, and a reset button. We can connect the board to the
computer using the USB, and can power-on the board with the help of AC to DC adapter. The
Diecimila was initially developed to mark the 10000 delivered boards of Arduino. Here,
Diecimila means 10,000 in Italian.
11. Arduino Robot
The Arduino Robot is called as the tiny computer. It is widely used in robotics. The board
comprises of the speaker, five-button, color screen, two motors, an SD card reader, a digital
compass, two potentiometers, and five floor sensors. The Robot Library can be used to control
the actuators and the sensors.
12. Arduino Ethernet
The Arduino Ethernet is based on the ATmega328 Microcontroller. The board consists of
6 analog pins, 14 digital I/O pins, crystal oscillator, reset button, ICSP header, a power jack, and
an RJ45 connection. With the help of the Ethernet shield, we can connect our Arduino board to
the internet.
13. Arduino Zero
The Arduino Zero is generally called as the 32-bit extension of the Arduino UNO. It is
based on ATmel's SAM21 MCU. The board consists of 6 analog pin inputs, 14 digital
Input/Output pins, a USB connector, a power jack, and an ICSP (In-Circuit Serial Programming)
header, UART port pins, a power header, and AREF button. The Embedded debugger of Atmel
is also supported by the Arduino Zero. The function of Debugger is to provide a full debug
interface, which does not require additional hardware.

R.Rama Rajesh.,M.E.MBA.,MHRM., 11
Embedded C Programming

14. Arduino Esplora


The Arduino Esplora boards allow easy interfacing of sensors and actuators. The outputs
and inputs connected on the Esplora board make it unique from other types of Arduino boards.
The board includes outputs, inputs, a small microcontroller, a microphone, a sensor, a joystick,
an accelerometer, a temperature sensor, four buttons, and a slider.
15. Arduino Pro Micro
The structure of Arduino Pro Micro is similar to the Arduino Mini board, except the
Microcontroller ATmega32U4. The board consists of 12 digital Input/output pins, 5 PWM (Pulse
Width Modulation) pins, Tx and Rx serial connections, and 10-bit ADC (Analog to Digital
Converter).
ARDUINO TOOLCHAIN
A toolchain is a set of programming tools that is used to perform a complex set of
operations. In the Arduino Software (IDE) the toolchain is hidden from the user, but it is used to
compile and upload the user Sketch. It includes compiler, assembler, linker and Standard C &
math libraries.
A toolchain is just a series of programming tools that work together in order to complete
a task. If we were cooking something, like carrot soup, the toolchain might include a carrot
peeler, a produce knife, a pot, some water, and a stove. Together, these tools help you create the
desired output, carrot soup. When we're developing programs for Arduino, we'll also be using a
toolchain. This all happens behind the scenes. On our end, we literally only have to press a
button for the code to get to the board, but it wouldn’t happen without the toolchain.
We do this in the Arduino IDE, which is akin to a text editor. We also write the code in a
programming language called C++, with a file type extension of .ino. The code that we write is
called human readable code since it's meant for us to read and, hopefully, understand. However,
the Arduino hardware doesn't understand C++ code. It needs what's called machine code.

R.Rama Rajesh.,M.E.MBA.,MHRM., 12
Embedded C Programming

In order to generate that machine code, we use a software tool called a compiler.
Remember that Verify button in the Arduino IDE that looks like the checkmark.
When you press this button, it compiles the code using compiler software called AVR-
GCC. This compiler software does a bunch of stuff. The main thing it does it rearrange some
code and check for errors. This is like the professional editor at the publishing company. The
compiler also translates the human readable code into machine code. There are some in-between
file types and other fuzzy things that go on, but the final output of the compiler is a machine
language saved as a .hex file. In order to get this .hex file loaded onto our Arduino's integrated
circuit, we need to press the Upload button.

This begins another piece of software called AVRDUDE, which sends the file to the
integrated circuit.Normally, we would have to use some external hardware to load that circuit.
However, in Arduino’s infinite wisdom, they loaded a small piece of software onto the integrated
circuit that automatically comes with an Arduino board. That piece of software is called a
bootloader. It works with ARVDUDE to take the outputted .hex file and put it onto the flash
memory of that Arduino's integrated circuit using only the USB cable. Again, all we had to do
was press the Upload button. This whole process happens behind the scenes. Now the process is
complete. So, what was our toolchain? This is our Arduino toolchain.

R.Rama Rajesh.,M.E.MBA.,MHRM., 13
Embedded C Programming

We have the Arduino IDE Editor. Then, we have the compiler, which is AVR-GCC.
The result is a .hex file. Next, we have AVRDUDE and the bootloader on the integrated circuit
of the Arduino board. These work together to upload the .hex file to the board.
ARDUINO PROGRAMMING STRUCTURE
The Arduino program is divided into three main parts that are structure, values, and
functions.

When writing a code, the important thing is following the syntax of the language being used
because in order to run the code successfully the correct syntax is necessary. So, when writing a
program for Arduino following syntax should be followed:
 To complete the statement a semicolon “;” is used at the end of the statement.
 To enclose the block parenthesis “{}” are used. Block in a program contains some
statements, declaration of the variables, functions, or loops.

 Comments can be written for each statement in the code to better understand the
statement functionality. It can be done by using double forward slash “//” at the start of
the comment if there is only a single line comment. However, if there are multi line
comments in a row, a forward slash asterisk “/*” at the start and asterisk forward slash
“*/” at the end of the comment. Comments can also be used to exclude any statement.

R.Rama Rajesh.,M.E.MBA.,MHRM., 14
Embedded C Programming

After understanding the syntax, let’s move towards how to use variables in the Arduino
program and what type of variables are used in program structure. To store any values which will
be used in the program can be a number or an alphabet. Using the variables gives the option of
saving, changing, updating and accessing the information when the program is running. There
are different types of variables that can be used including char, int, double, string, float, unsigned
int, long and unsigned long.
The following are operators used in the programing of Arduino:
1. For assigning any value to a variable or a character equal to “=” sign is used
2. There are different mathematical operators like percentage, multiply, subtraction,
addition can be used (%, +, *, -, /)
3. For comparison of the different values the operators like less than equal to, greater
than equal to, equal to, less than, greater than are used (==, ,=)
4. Logical operators are used to define the conditional statements such as AND (&&),
NOT(!) and OR (||) operators.
Arduino Program structure
The Arduino program structure is divided into two functions: the set-up function and the
loop function.

The setup() function contains initialization of the libraries, variables used for the code.
Similarly, pin modes of the Arduino are also declared in this function. It also initializes the
communication between the Arduino board and the computer. It only runs once.
The loop() function keeps on repeating the instructions and actively controls and
monitors the Arduino.

R.Rama Rajesh.,M.E.MBA.,MHRM., 15
Embedded C Programming

ARDUINO SKETCH
A sketch is the name that Arduino uses for a program. It's the unit of code that is
uploaded to and run on an Arduino board. It’s the unit of code that is uploaded to and run on an
Arduino board. A basic Arduino sketch consists of two functions:
1. setup()
2. loop()
The setup() is called once, when the sketch starts. It's a good place to do setup tasks like
setting pin modes or initializing libraries. The loop() function is called over and over and is heart
of most sketches. You need to include both functions in your sketch, even if you don't need them
for anything.
Statements in the setup() function are executed only once when the sketch is run.
Statements in the loop() function will run continuously from top to bottom and then back to the
top. If the loop() function contains two statements, the first statement will be executed first, then
the second statement, then the first statement again and so on. Hence, the statements in the main
loop will be executed continuously until the Arduino is switched off or reset.
It is important to have the loop() function in the sketch, even if it is empty because
without it the microcontroller on the Arduino board will try to execute whatever it finds next in
memory after the statements in the setup() function have been executed. But the loop() function
prevents it from doing so by keeping the program execution in the loop.
The setup() function is called when a sketch starts. Use it to initialize variables, pin
modes, start using libraries, etc. The setup() function will only run once, after each powerup or
reset of the Arduino board.
Eg)
int buttonPin = 3;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
// ...

After creating a setup() function, which initializes and sets the initial values, the loop()
function does precisely what its name suggests, and loops consecutively, allowing your program
to change and respond. Use it to actively control the Arduino board.

R.Rama Rajesh.,M.E.MBA.,MHRM., 16
Embedded C Programming

Eg)
int buttonPin = 3;

// setup initializes serial and the button pin


void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}

// loop checks the button pin each time,


// and will send serial if it is pressed
void loop() {
if (digitalRead(buttonPin) == HIGH) {
Serial.write('H');
}
else {
Serial.write('L');
}

delay(1000);
}

ARDUINO PINS

R.Rama Rajesh.,M.E.MBA.,MHRM., 17
Embedded C Programming

Pin Category Pin Name Details


Vin: Input voltage to Arduino when using an
external power source.
5V: Regulated power supply used to power
Power Vin, 3.3V, 5V, GND microcontroller and other components on the board.
3.3V: 3.3V supply generated by on-board voltage
regulator. Maximum current draw is 50mA.
GND: ground pins.
Reset Reset Resets the microcontroller.
Analog Pins A0 – A5 Used to provide analog input in the range of 0-5V
Input/Output
Digital Pins 0 - 13 Can be used as input or output pins.
Pins
Serial 0(Rx), 1(Tx) Used to receive and transmit TTL serial data.
External
2, 3 To trigger an interrupt.
Interrupts
PWM 3, 5, 6, 9, 11 Provides 8-bit PWM output.
10 (SS), 11 (MOSI), 12
SPI Used for SPI communication.
(MISO) and 13 (SCK)
Inbuilt LED 13 To turn on the inbuilt LED.
TWI A4 (SDA), A5 (SCA) Used for TWI communication.
AREF AREF To provide reference voltage for input voltage.

INPUT/OUTPUT FROM PINS USING SKETCHES


Arduino Uno board has various digital IO pins which can be used for input/output
devices. Following image shows the digital IO pins of Arduino Uno. Arduino analog pins can
also be used as digital input/output pins.
Digital Output
Arduino (ATmega) digital pins can be configured as output to drive output devices. We have to
configure these pins to use as output.
To configure these pins, pinMode() function is used which set direction of pin as input or output.
 pinMode(pin no, Mode)
This function is used to configure GPIO pin as input or output.
pin no number of pin whose mode we want to set.
Mode INPUT, OUTPUT or INPUT_PULLUP
E.g. pinMode (3, OUTPUT) //set pin 3 as output

R.Rama Rajesh.,M.E.MBA.,MHRM., 18
Embedded C Programming

These Arduino (ATmega) pins can source or sink current up to 40 mA which is sufficient
to drive led, LCD display but not sufficient for motors, relays, etc.

Digital Input
To read data from senor or from any device/circuit, we need to configure digital pin as input.
Arduino pin are set as digital input (default). So, there is no need to configure pin as input.
To configure pin as digital input, pinMode() function is used. We can read data from GPIO pin
using digitalRead() function.
 digitalRead(pin)
It is used to read data from specified GPIO pin.
Digital Input with Pull-up Resistor
Sometimes switching between one state to another or pins configured as input with
nothing connected to them may arise situation of High-impedance state i.e. floating state. This
state may report random changes in pin state. To avoid this state, there is option of adding pull-
up (to +5V) or pull-down (to Gnd) resistor which helps to set the input to a known state.
Following image show the high impedance (undefine) state and pull-up resistor.

R.Rama Rajesh.,M.E.MBA.,MHRM., 19
Embedded C Programming

 Arduino (ATmega) has in-built configurable pull-up resistor. These resistors enable using
pinMode() with mode set to INPUT_PULLUP. When connecting a device or sensor to a
pin configured as input with pull-up, the other end should be connected to ground.
e.g. pinMode (3, INPUT_PULLUP).
 We can configure input pull-up in another way too. If we set direction of pin as input and
then write HIGH value on that pin will turn on the pull-up resistor. In other manner, if we
write HIGH on pin configured as OUTPUT and then configure that pin as input will also
enable the pull-up resistor.
e.g. pinMode (3, INPUT) //set pin as input
digitalWrite (3, HIGH) //setting high on input pin enables pullup
ARDUINO SHIELDS
Arduino shields are the boards, which are plugged over the Arduino board to expand its
functionalities. There are different varieties of shields used for various tasks, such as Arduino
motor shields, Arduino communication shields, etc. Shield is defined as the hardware device that
can be mounted over the board to increase the capabilities of the projects. It also makes our work
easy. For example, Ethernet shields are used to connect the Arduino board to the Internet. The
pin position of the shields is similar to the Arduino boards. We can also connect the modules and
sensors to the shields with the help of the connection cable. Arduino motor shields help us to
control the motors with the Arduino board.
Advantages of Using Arduino Shields
 It adds new functionalities to the Arduino projects.
 The shields can be attached and detached easily from the Arduino board. It does not
require any complex wiring.
 It is easy to connect the shields by mounting them over the Arduino board.
 The hardware components on the shields can be easily implemented.

R.Rama Rajesh.,M.E.MBA.,MHRM., 20
Embedded C Programming

Types of Shields
Various types of shields are as follows
1. Ethernet shield
2. Xbee Shield
3. Proto shield
4. Relay shield
5. Motor shield
6. LCD shield
7. Bluetooth shield
8. Capacitive Touchpad Shield
Ethernet Shield
The Ethernet shields are used to connect the Arduino board to the Internet. We need to
mount the shield on the top of the specified Arduino board. The USB port will play the usual role
to upload sketches on the board. The latest version of Ethernet shields consists of a micro SD
card slot. The micro SD card slot can be interfaced with the help of the SD card library. We can
also connect another shield on the top of the Ethernet shield. It means that we can also mount
two shields on the top of the Arduino board.
Xbee Shield
We can communicate wirelessly with the Arduino board by using the Xbee Shield with
Zigbee. It reduces the hassle of the cable, which makes Xbee a wireless communication model.
The Xbee wireless module allows us to communicate outdoor upto 300 feet and indoor upto 100
feet. It can also be used with different models of Xbee.
Proto shield
Proto shields are designed for custom circuits. We can solder electronic circuits directly
on the shield. The shield consists of two LED pads, two power lines, and SPI signal pads. The
IOREF (Input Output voltage REFerence) and GND (Ground) are the two power lines on the
board. We can also solder the SMD (Surface Mount Device) ICs on the prototyping area. A
maximum of 24 pins can be integrated onto the SMD area.
Relay shield
The Arduino digital I/O pins cannot bear the high current due to its voltage and current
limits. The relay shield is used to overcome such situation. It provides a solution for controlling

R.Rama Rajesh.,M.E.MBA.,MHRM., 21
Embedded C Programming

the devices carrying high current and voltage. The shield consists of four relays and four LED
indicators. It also provides NO/NC interfaces and a shield form factor for the simple connection
to the Arduino board. The LED indicators depicts the ON/OFF condition of each relay. The relay
used in the structure is of high quality. The NO (Normally Open), NC (Normally Closed), and
COM pins are present on each relay. The applications of the Relay shield include remote control,
etc.
Motor shield
The motor shield helps us to control the motor using the Arduino board. It controls the
direction and working speed of the motor. We can power the motor shield either by the external
power supply through the input terminal or directly by the Arduino. We can also measure the
absorption current of each motor with the help of the motor shield.
The motor shield is based on the L298 chip that can drive a step motor or two DC motors.
L298 chip is a full bridge IC. It also consists of the heat sinker, which increases the performance
of the motor shield. It can drive inductive loads, such as solenoids, etc. The operating voltage is
from 5V to 12V. The applications of the motor shield are intelligent vehicles, micro-robots, etc.
LCD shield
The keypad of LCD (Liquid Crystal Display) shield includes five buttons called as up,
down, left, right, and select. There are 6 push buttons present on the shield that can be used as a
custom menu control panel.It consists of the 1602 white characters, which are displayed on the
blue backlight LCD. The LED present on the board indicates the power ON.The five keys
present on the board helps us to make the selection on menus and from board to our project. The
LCD shield is popularly designed for the classic boards such as Duemilanove, UNO, etc.
Bluetooth shield
The Bluetooth shield can be used as a wireless module for transparent serial
communication. It includes a serial Bluetooth module. D0 and D1 are the serial hardware ports in
the Bluetooth shield, which can be used to communicate with the two serial ports (from D0 to
D7) of the Arduino board. We can install Groves through the two serial ports of the Bluetooth
shield called a Grove connector. One Grove connector is digital, while the other is analog. The
communication distance of the Bluetooth shield is upto 10m at home without any obstacle in
between.

R.Rama Rajesh.,M.E.MBA.,MHRM., 22
Embedded C Programming

Capacitive Touchpad shield


It has a touchpad interface that allows to integrate the Arduino board with the touch
shield. The Capacitive touchpad shield consists of 12 sensitive touch buttons, which includes 3
electrode connections and 9 capacitive touch pads. The board can work with the logic level of
3.3V or 5V. We can establish a connection to the Arduino project by touching the shield.
INTEGRATION OF SENSORS AND ACTUATORS WITH ARDUINO.
SENSORS
The sensors are defined as a machine, module, or a device that detect changes in the
environment. The sensors transfer those changes to the electronic devices in the form of a signal.
A sensor and electronic devices always work together. The output signal is easily readable by
humans. Nowadays, Sensors are used in daily lives. For example, controlling the brightness of
the lamp by touching its base, etc. The use of sensors is expanding with new technologies.
The sensors are used to measure the physical quantities, such as pressure, temperature,
sound, humidity, and light, etc. An example of sensors is Fire Alarm, a detector present on the
fire alarm detects the smoke or heat. The signal generated from the detector is sent to the
alarming system, which produces an alert in the form of alarm. The types of detectors are smoke
detectors, heat detectors, carbon monoxide detectors, multi-sensors detectors, etc. The data signal
runs from the sensor to the output pins of the Arduino.
Types of sensors in Arduino
1. Light sensor : The light sensor is used to control the light. It is used with LDR (Light
Dependent Resistor) in Arduino.
2. Ultrasonic sensor :The ultrasonic sensor is used to determine the distance of the object using
SONAR.
3. Temperature sensor: The temperature sensor is used to detect the temperature around it.
4. Knock Sensor: The knock sensor is used to pick the vibrations of the knocking. It is a
common category of a vibration sensor.
5. Object Detection Sensor: It is used to detect the object by emitting infrared radiations, which
are reflected or bounced back by that object.
6. Tracking Sensor: It allows the robots to follow a particular path specified by sensing the
marking or lines on the surface.
7. Metal Touch Sensor: It is suitable for detecting the human touch.

R.Rama Rajesh.,M.E.MBA.,MHRM., 23
Embedded C Programming

8. Water Level Sensor: It is used to measure the water or the depth of the water level. It is also
used to detect leaks in containers.
9. Vibration Sensor: The vibration sensor is used to measure the vibrations.
10. Air Pressure sensor: It is commonly related to meteorology, biomedical fields.
11. Pulse Sensor: The pulse sensor is used to measure the pulse rate.
12. Capacitive soil moisture sensor: It is used to measure the moisture level of the soil.
13. Microphone sensor: The microphone sensor in Arduino is used to detect the sound. The
analog and digital are the two outputs of this module. The digital output sends the high signal
when the intensity of sound reaches a certain threshold. We can adjust the sensitivity of a module
with the help of a potentiometer.
14. Humidity sensor: The humidity sensor is used to monitor weather conditions.
15. Motion sensor: The motion sensor detects the movement and occupancy from the human
body with the help of Infrared radiation.
16. Sound sensor: The sound sensor is suitable to detect the sound of the environment.
17. Magnetic field sensor: The magnetic field sensor measures the magnetic field strength and
produces a varying voltage as the output in Arduino.
Actuators
An actuator is a device that receives an energy input and converts it into motion or force
and is an essential component in many modern technologies and engineering fields. From
robotics to renewable energy, actuators play a critical role in controlling and automating various
processes and systems. They come in many different forms and types, each with unique
capabilities and use, for example, a handwheel, a simple pneumatic cylinder, or a complex
electrical motor.

R.Rama Rajesh.,M.E.MBA.,MHRM., 24
Embedded C Programming

Actuators can be categorized based on their movement type, either linear or rotary.
Linear actuators produce linear motion in a straight line, while rotary actuators produce rotary
motion in a circular path.
Linear actuators
Linear actuators move objects along a straight line and use a belt and pulley, rack and
pinion, or ball screw to convert electric motor rotation into linear motion. Linear actuators stop at
a fixed linear distance and are known for their high repeatability and positioning accuracy, easy
installation and operation, low maintenance, and ability to withstand harsh environments. These
actuators are commonly used in food processing, automotive, material handling, and more for
tasks like pushing, pulling, lifting, and positioning.
Rotary actuators
Rotary actuators convert energy into rotary motion through a shaft to control equipment
speed, position, and rotation. These actuators have a continuous rotational motor and are
versatile in usage. An electric motor is a rotary actuator powered by an electric signal. They have
high torque, constant torque during full angle rotation, compatibility with different diameters,
hollow shafts with zero backlash, twice the output, low maintenance, and can achieve any degree
of rotation. Rotary actuators are used in medical equipment, radar and monitoring systems,
robotics, flight simulators, the semiconductor industry, special machine manufacturing, and
defense.
Pneumatic actuators
Pneumatic actuators use compressed air to generate motion. They can be used for various
applications, such as moving machine parts or controlling valve positions. They are often
preferred for applications that require high force, fast response times, or explosion-proof
environments.
Hydraulic actuators:
Hydraulic actuators use fluid pressure to generate motion. They are commonly used for
heavy-duty applications such as construction equipment, manufacturing machinery, and
industrial robots. Hydraulic actuators offer high levels of force, durability, and reliability.
Electric actuators:
Electric actuators use electrical energy to generate motion. They can be driven by AC or
DC motors and are often used in applications that require precise control, low noise, and low

R.Rama Rajesh.,M.E.MBA.,MHRM., 25
Embedded C Programming

maintenance. Electric actuators are commonly used in automation systems, medical devices, and
laboratory equipment.
Magnetic and thermal actuators:
Magnetic and thermal actuators are two types of actuators that use magnetic and
temperature changes to generate motion, respectively. Magnetic actuators use magnetic fields to
generate force. Thermal actuators use the expansion or contraction of materials in response to
temperature changes. Both actuators are commonly used in micro-electromechanical systems
(MEMS) and other miniaturized applications.
Mechanical actuators:
Mechanical actuators use physical mechanisms such as levers, gears, or cams to generate
motion. Mechanical actuators are commonly used in applications where low cost, simple
operation, and durability are important. Examples include hand-crank machines, manual valve
systems, and mechanical locks.
Integrating Ultra Sonic Sensor with Arduino
The SEN136B5B is an ultrasonic range finder from Seeedstudio. It detects the distance of
the closest object in front of the sensor (from 3 cm up to 400 cm). It works by sending out a burst
of ultrasound and listening for the echo when it bounces off of an object. It pings the obstacles
with ultrasound. The Arduino board sends a short pulse to trigger the detection, then listens for a
pulse on the same pin using the pulseIn() function. The duration of this second pulse is equal to
the time taken by the ultrasound to travel to the object and back to the sensor. Using the speed of
sound, this time can be converted to distance.

R.Rama Rajesh.,M.E.MBA.,MHRM., 26
Embedded C Programming

Detect a Knock
Piezo sensor is used to detect a knock. The sketch reads the piezo output using the
analogRead() command, encoding the voltage range from 0 to 5 volts to a numerical range from
0 to 1023 in a process referred to as analog-to-digital conversion, or ADC. If the sensors output
is stronger than a certain threshold, your board will send the string "Knock!" to the computer
over the serial port.

Integrating Acutator

A motor driver is an integrated circuit design specifically to control DC motors, which


drive DC linear actuators. Motor drivers commonly make use of an H-bridge to allow for both
direction and speed control. How to exactly connect your Arduino to your motor driver will

R.Rama Rajesh.,M.E.MBA.,MHRM., 27
Embedded C Programming

depend on the exact motor driver but will require at least two I/O pins to do so and one of them
will be a PWM signal. PWM or pulse-width modulation is a method of varying a signal between
on and off values to effectively supply a lower than operating voltage. The motor driver can then
use this signal to adjust the speed at which the motor runs.
Above is an example of our High Current DC Motor Drive interfaced with an Arduino.
For this motor driver, you are required to send two PWM signals, one to extend the actuator and
the other to retract. The PWM is given as an unsigned byte meaning it ranges from 0, no voltage,
to 255, max voltage (5V), which will be proportional to the speed of the motor. As PWM is not a
binary value, we need to use the PWM pins of the Arduino and use the analog write function, as
seen in the example below. PWM pins will be indicated on the Arduino with a ~ or simply
labelled as PWM pins.

R.Rama Rajesh.,M.E.MBA.,MHRM., 28

You might also like