Summer Training Report: Embedded Hardware Design
Summer Training Report: Embedded Hardware Design
Undertaken at
DKOP Labs
Noida
on
Embedded Hardware Design
Submitted by
Rakshit Tikoo
101406143
2015
Electronics and Communication Engineering Department
Thapar University, Patiala
(Declared as Deemed-to-be-University u/s 3 of the UGC Act., 1956)
Post Bag No. 32, Patiala – 147004
Punjab (India)
CERTIFICATION
i
DECLARATION
I hereby declare that the project entitled “Home Automation” is an authentic record of my own
work carried out at DKOP Labs, as requirements of 6 weeks training for the award of degree
of B.E. Electronics & Instrumentation Engineering, Thapar University, Patiala, under the
guidance of Mr. Kirti Karan during the period of 1/6/2016 to 15/7/2016.
(Signature of student)
Name of the student
ii
ACKNOWLEDGEMENT
I would like to thank my training teacher Mr. Kirti Karan for imparting knowledge on Embedded
Systems and for guiding me at every juncture.
I would also like to thank my fellow classmates in the course for providing regular support and inputs.
I am really grateful to the Electronics and Communication Department at Thapar University for
providing guidance and to give us an opportunity to learn beyond the books.
I am really blessed to have really supportive parents to whom I will always be grateful.
And last but not the least I would like to thank all the people publishing content on the internet for
review and research.
iii
ABSTRACT
It deals with the basic understanding of Embedded Systems, their structure and uses.
It provides information about microcontrollers in detail especially Arduino UNO and ATmega328 and
its difference from a microprocessor. The major goal of the training was the creation of a cost
effective project using the knowledge of microcontrollers.
The use of Arduino IDE (Integrated Development Environment) is extensive in the project and the
report explains the use of AVR-C programming language with a few examples.
The project developed is a simple Home Automation system which uses a user’s phone to wirelessly
switch the appliances of a home on and off.
Then the project section itself deals with a number of things, mainly:
What is Bootloader?
Bootloading a Microcontroller
Arduino as Programmer
Hardware utilized for basic home automation system
The project provides all the necessary download links along with the android application used. It also
provides the code which could be uploaded to the ATmega328 microcontroller for usage.
The code utilizes the USART (Universal Synchronous Asynchronous Receiver/Transmitter) of the
microchip to receive data from the phone.
The design is explained in a really easy to understand step by step format, from bootloading to the
final upload.
Then the future scope which tells about the possible enhancements to the project have also been
discussed.
iv
TABLE OF CONTENTS:
Chapter 1: INTRODUCTION
3.1 Microprocessors……………………...……………………………………………………..8
3.3 Microcontrollers…….……...………….…………………………………………….……11
5.1 Introduction………………………………………………………….................................21
5.2 Registers......………………………………………………………………………………25
5.3 AVR-C Programming Language...…………………………….………………………….28
5.4 Seven (7) Segment Interfacing……………………………………..……………………..28
v
5.5 Setting ATmega328 to get Digital Input…………………………………………………..29
Chapter 6: PROJECT
6.1 Introduction…………………………………………………………………..…………...31
6.2 Hardware Used.…………………………………………………………………………...31
6.4 Setup………………………………………………………………………………………35
7.2 Conclusion………………………………………………………………………………..45
REFERENCES.....................................................................................................................................46
vi
LIST OF IMPORTANT CONTENTS
Acronyms used-
1. µP – MicroProcessor
2. µC – MicroController
3. CPU – Central Processing Unit
4. ALU – Arithmetic Logic Unit
5. ROM – Read Only Memory
6. RAM – Random Access Memory
7. EEPROM – Electrically Erasable Programmable ROM
8. SRAM – Static RAM
9. DRAM – Dynamic RAM
10. I/O –Input/Output
11. MIPS – Million Instructions Per Second
12. IDE – Integrated Development Environment
13. USART – Universal Synchronous-Asynchronous Receiver/Transmitter
14. DIY – Do It Yourself
Download links-
vii
CHAPTER 1
INTRODUCTION
The course of Embedded Systems at DKOP Labs was a comprehensive course divided into various
parts for the ease of access and thorough understanding. The course mainly focused on the basics of
microprocessors and microcontrollers. The major topics that we dealt with included-
The course also included basics of computer science like data structures and algorithms which helped
in the programming part of the course.
All the topics were covered in a step by step procedure along with lab experiments which helped us
cement the topics being taught and also gave us practical knowledge. All the queries were handled
with care and were clarified.
1
The objective of the course was to give an overview of Embedded Systems and their use. Our goal was
the understanding of devices like microcontrollers and microprocessors, learning their use, and gaining
knowledge in detail about their programming so that we could apply that knowledge in a useful manner.
We were given examples about how these technologies could be applied for the ease of our day to day
life in the form of various DIY projects. We were also given examples of the use of such devices in
professional environments. These were done to explain us the use of what we learned in various places.
The final and the most important objective of the course was for the students to apply the knowledge
they gained in a useful project of their own.
Therefore the project created by me was ‘Home Automation’ using a Bluetooth module, relays and the
Atmel Atmega328 microcontroller.
2
CHAPTER 2
EMBEDDED SYSTEMS
Embedded systems is a combination of computer hardware and software. As with any electronic system,
this system requires a hardware platform and that is built with a
3
Embedded systems are managed by single or multiple processing cores in the form of
microcontrollers or digital signal processors (DSP), field-programmable gate arrays (FPGA),
application-specific integrated circuits (ASIC) and gate arrays. These processing components are
integrated with components dedicated to handling electric and/or mechanical interfacing.
An embedded system's key feature is dedication to specific functions that typically require strong
general-purpose processors. For example, router and switch systems are embedded systems, whereas a
general-purpose computer uses a proper OS for routing functionality. However, embedded routers
function more efficiently than OS-based computers for routing functionalities.
Commercial embedded systems range from digital watches and MP3 players to giant routers and
switches. Complexities vary from single processor chips to advanced units with multiple processing
chips.
Embedded systems can be classified into different types based on performance, functional requirements
and performance of the microcontroller.
Embedded systems are classified into four categories based on their performance and functional
requirements:
Embedded Systems are classified into three types based on the performance of the microcontroller such
as:
4
Types of Embedded Systems
Standalone embedded systems do not require a host system like a computer, it works by itself. It takes
the input from the input ports either analog or digital and processes, calculates and converts the data
and gives the resulting data through the connected device-Which either controls, drives or displays the
connected devices. Examples for the stand alone embedded systems are mp3 players, digital cameras,
video game consoles, microwave ovens and temperature measurement systems.
A real time embedded system is defined as, a system which gives a required o/p in a particular time.
These types of embedded systems follow the time deadlines for completion of a task. Real time
embedded systems are classified into two types such as soft and hard real time systems.
These types of embedded systems are related to a network to access the resources. The connected
network can be LAN, WAN or the internet. The connection can be any wired or wireless. This type of
embedded system is the fastest growing area in embedded system applications. The embedded web
server is a type of system wherein all embedded devices are connected to a web server and accessed
and controlled by a web browser. Example for the LAN networked embedded system is a home
security system wherein all sensors are connected and run on the TCP/IP protocol.
Mobile embedded systems are used in portable embedded devices like cell phones, mobiles, digital
cameras, mp3 players and personal digital assistants, etc. The basic limitation of these devices is the
other resources and limitation of memory.
5
SMALL SCALE EMBEDDED SYSTEMS-
These types of embedded systems are designed with a single 8 or 16-bit microcontroller that may even
be activated by a battery. For developing embedded software for small scale embedded systems, the
main programming tools are an editor, assembler, cross assembler and integrated development
environment (IDE).
These types of embedded systems design with a single or 16 or 32 bit microcontroller, RISCs or DSPs.
These types of embedded systems have both hardware and software complexities. For developing
embedded software for medium scale embedded systems, the main programming tools are C, C++, and
JAVA, Visual C++, and RTOS, debugger, source code engineering tool, simulator and IDE.
These types of embedded systems have enormous hardware and software complexities that may need
ASIPs, IPs, PLAs, scalable or configurable processors. They are used for cutting-edge applications that
need hardware and software Co-design and components which have to assemble in the final system.
Embedded systems are used in different applications like automobiles, telecommunications, smart
cards, missiles, satellites, computer networking and digital consumer electronics.
Security systems
Telephone and banking
Defence and aerospace
Communication
6
EMBEDDED SYSTEMS IN PERIPHERALS AND COMPUTER NETWORKING-
Digital Cameras
Set top Boxes
High Definition TVs
DVDs
7
CHAPTER 3
3.1 Microprocessors
8
(microprocessor) incorporates various functional units in an appropriate manner. Such an internal
structure or organizational structure of µP, which determines how it operates, is known as its
architecture.
A typical microprocessor architecture is shown in Figure above. The various functional units are as
follows:
BUSSES-
Microcomputer, like all computers, manipulates binary information. The binary information is
represented by binary digits, called bits. Microcontroller operates on a group of bits which are referred
to as a word. The number of bits making-µP a word varies with the µP. Common word sizes are 4, 8,
12 and 16 bits (µPs with 32 bit-word and higher have also of late entered the market). Another binary
terms that will be of interest in subsequent discussions are the byte and the nibble, which represent a
set of 8 bits and 4 bits, respectively. Figure 1.2 shows busses interconnecting various blocks. These
busses allow exchange of words between the blocks. A bus has a wire or line for each bit and thus
allows exchange of all bits of a word in parallel. The processing of bits in the µP is also in parallel.
The busses can thus be viewed as data highways. The width of a bus is the number of signal lines that
constitute the bus. The figure shows for simplicity three busses for distinct functions. Over the address
bus, the µP transmits the address of that I/O device or memory locations which it desires to access.
This address is received by all the devices connected to the processor, but only the device which has
been addressed responds. The data bus is used by the µP to send and receive data to and from
different devices (I/O and memory) including instructions stored in memory. Obviously the address
bus is unidirectional and the data bus is bi-directional. The control bus is used for transmitting and
receiving control signals between the µP and various devices in the system.
The arithmetic-logic unit is a combinational network that performs arithmetic and logical operations
on the data.
INTERNAL REGISTERS-
A number of registers are normally included in the microprocessor. These are used for temporary
storage of data, instructions and addresses during execution of a program. Those in the Intel 8085
microprocessor are typical and are described below:
9
Upon completion of the arithmetic/logical operation, the result is placed in the accumulator
(replacing the earlier operand). Because of the later function, this register is also called as
result register.
10
‘auxiliary carry’ Set if instruction caused a carry out of bit 3 and
into
into bit 4 of the resulting value.
7. Dedicated Registers-
Several other registers are incorporated in the µP for its internal operation. They cannot be
accessed by the programmer and hence do not concern much a µP user.
It decodes each instruction and under the supervision of a clock controls the external and internal
units ensuring correct logical operation of the system.
3.3 Microcontrollers
11
Central processing unit(CPU)
Random Access Memory)(RAM)
Read Only Memory(ROM)
Input/output ports
Timers and Counters
Interrupt Controls
Analog to digital converters
Digital analog converters
Serial interfacing ports
Oscillatory circuits
2. A microcontroller internally consists of all features required for a computing system and functions
as a computer without adding any external digital parts in it.
3. Most of the pins in the microcontroller chip can be made programmable by the user.
4. A microcontroller has many bit handling instructions that can be easily understood by the
programmer.
5. A microcontroller is capable of handling Boolean functions.
6. Higher speed and performance.
7. On-chip ROM structure in a microcontroller provides better firmware security.
8. Easy to design with low cost and small size.
12
CPU-
CPU is the brain of a microcontroller .CPU is responsible for fetching the instruction, decodes
it, and then finally executed. CPU connects every part of a microcontroller into a single system.
The primary function of CPU is fetching and decoding instructions. Instruction fetched from
program memory must be decoded by the CPU.
MEMORY-
The function of memory in a microcontroller is same as microprocessor. It is used to store data
and program. A microcontroller usually has a certain amount of RAM and ROM (EEPROM,
EPROM, etc.) or flash memories for storing program source codes.
SERIAL PORTS-
Serial ports provide various serial interfaces between microcontroller and other peripherals like
parallel ports.
TIMERS/COUNTERS-
This is the one of the useful function of a microcontroller. A microcontroller may have more
than one timer and counters. The timers and counters provide all timing and counting functions
inside the microcontroller. The major operations of this section are perform clock functions,
modulations, pulse generations, frequency measuring, making oscillations, etc. This also can
be used for counting external pulses.
13
DAC perform reversal operation of ADC conversion. DAC convert the digital signal into
analog format. It usually used for controlling analog devices like DC motors, various drives,
etc.
INTERRUPT CONTROL-
The interrupt control used for providing interrupt (delay) for a working program .The interrupt
may be external (activated by using interrupt pin) or internal (by using interrupt instruction
during programming).
14
Microprocessors Microcontrollers
Memory, I/O ports, timers, interrupts All are integrated inside the microcontroller
2 are not available inside the chip chip
This must have many additional digital Can function as a micro computer without any
3 components to perform its operation additional components.
4 Systems become bulkier and expensive. Make the system simple, economic and compact
7 Very few pins are programmable Most of the pins are programmable
15
CHAPTER 4
Arduino UNO
TECHNICAL SPECS-
Microcontroller ATmega328P
Operating Voltage 5V
16
Input Voltage (limit) 6-20V
32 KB (ATmega328P)
Flash Memory
of which 0.5 KB used by bootloader
SRAM 2 KB (ATmega328P)
EEPROM 1 KB (ATmega328P)
Length 68.6 mm
Width 53.4 mm
Weight 25
The Arduino UNO is based on the ATmega 328P microcontroller developed by Atmel. It is a high-
performance picoPower 8-bit AVR RISC-based microcontroller combines 32KB ISP flash memory
with read-while-write capabilities, 1024B EEPROM, 2KB SRAM, 23 general purpose I/O lines, 32
general purpose working registers, three flexible timer/counters with compare modes, internal and
external interrupts, serial programmable USART, a byte-oriented 2-wire serial interface, SPI serial
port, a 6-channel 10-bit A/D converter (8-channels in TQFP and QFN/MLF packages), programmable
watchdog timer with internal oscillator, and five software selectable power saving modes. The device
operates between 1.8-5.5 volts.
By executing powerful instructions in a single clock cycle, the device achieves throughputs
approaching 1 MIPS per MHz, balancing power consumption and processing speed.
17
KEY PARAMETERS-
PARAMETER VALUE
Flash (kBytes): 32 kBytes
Pin Count: 32
Max. Operating Freq. (MHz): 20 MHz
CPU: 8-bit AVR
# of Touch Channels: 16
Hardware QTouch Acquisition: No
Max I/O Pins: 23
Ext Interrupts: 24
USB Speed: No
USB Interface: No
PIN DESCRIPTION-
18
Pin Number Description Function
1 PC6 Reset
8 GND Ground
22 GND Ground
19
27 PC4 Analog Input
ARDUINO IDE-
The Arduino Integrated Development Environment is a text editor software specially designed for the
programming of Arduino boards. It is based on the C++ language and uses various predefined
functions and libraries for the easy programming of Arduino Boards.
20
CHAPTER 5
5.1 Introduction
The Atmel 8-bit AVR RISC-based microcontroller combines 32 kB ISP flash memory with read-
while-write capabilities, 1 kB EEPROM, 2 kB SRAM, 23 general purpose I/O lines, 32 general
purpose working registers, three flexible timer/counters with compare modes, internal and
external interrupts, serial programmable USART, a byte-oriented 2-wire serial interface, SPI serial
port, 6-channel 10-bit A/D converter (8-channels in TQFP and QFN/MLF packages), programmable
watchdog timer with internal oscillator, and five software selectable power saving modes. The device
operates between 1.8-5.5 volts. The device achieves throughput approaching 1 MIPS per MHz.
KEY PARAMETERS-
Parameter Value
Flash memory 32 kB
SRAM 2 kB
EEPROM 1 kB
21
Pin count 28-pin PDIP, MLF, 32-pin TQFP, MLF[2]
External interrupts 24
USB Interface No
USB Speed No
PIN DESCRIPTION-
22
It is same as ATmega328p-
1 PC6 Reset
8 GND Ground
22 GND Ground
23
24 PC1 Analog Input
PROGRAMMING-
Programming
Pin Name I/O Function
signal
BS1 PD4 I Byte Select 1 (“0” = Low byte, “1” = High byte)
24
BS2 PC2 I Byte Select 2 (“0” = Low byte, “1” = 2nd High byte)
Programming mode is entered when PAGEL (PD7), XA1 (PD6), XA0 (PD5), BS1 (PD4) is set to
zero.[2] RESET pin to 0V and VCC to 0V. VCC is set to 4.5 - 5.5V. Wait 60 μs, and RESET is set to
11.5 - 12.5 V. Wait more than 310 μs. Set XA1:XA0:BS1: DATA = 100 1000 0000, pulse XTAL1 for
at least 150 ns, pulse WR to zero. This starts the Chip Erase. Wait until RDY/BSY (PD1) goes high.
XA1:XA0:BS1: DATA = 100 0001 0000, XTAL1 pulse, pulse WR to zero. This is the Flash write
command. And so on...
Serial Programming[2]
Serial data to the MCU is clocked on the rising edge and data from the MCU is clocked on the falling
edge. Power is applied to VCC while RESET and SCK are set to zero. Wait for at least 20 ms and then
the Programming Enable serial instruction 0xAC, 0x53, 0x00, 0x00 is sent to the MOSI pin. The
second byte (0x53) will be echoed back by the MCU.
5.2 Registers
Registers are temporary memory units which can store data in bits. A single register can store a single
bit binary data (0 or 1). Microcontrollers and various other devices use registers to both store data and
use that data for special purposes. Some examples of registers in ATmega 328 include:
25
26
27
5.3 AVR-C Programming Language
It is programming language based on the C language developed to program the Atmel AVR. It helps
us to program AVR microcontrollers much easily as compared to the much tedious Assembly and
Machine Languages. It utilizes the inbuilt registers of the AVR for programming.
SETUP-
28
CODE-
#include <avr/io.h>
#define F_CPU 2000000L
#include<avr/delay.h>
int num[10]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF};
int i = 0;
int main(void)
{ DDRD = 0xFF;
/* Replace with your application code */
while (1)
{
PORTD = num[i];
i++;
_delay_ms(500);
if(i==10)
i = 0;
}
}
The setup includes the use of a push button to switch ON and OFF an led.
SETUP-
CODE-
29
#include <avr/io.h>
#define F_CPU 2000000L
int main(void)
{ DDRD = 0x01;
PORTD = 0x02;
/* Replace with your application code */
while (1)
{
if(!(PIND & 0x02))
{PORTD |= 0x01;}
else
{PORTD &= 0xFE;}
}
}
5.6 Interrupts
An event external to the currently executing process that causes a change in the normal flow of
instruction execution; usually generated by hardware devices external to the CPU. Interrupts are used
by microcontrollers to execute various tasks such as Pulse Width Manipulation (PWM), Analog
Inputs, changing duty cycles, Serial Communication, etc.
It's nearly impossible to find compilers that agree on how to handle interrupt code. Since the C
language tries to stay away from machine dependent details, each compiler writer is forced to design
their method of support.
In the AVR-GCC environment, the vector table is predefined to point to interrupt routines with
predetermined names. By using the appropriate name, your routine will be called when the
corresponding interrupt occurs. The device library provides a set of default interrupt routines, which
will get used if you don't define your own.
Patching into the vector table is only one part of the problem. The compiler uses, by convention, a set
of registers when it's normally executing compiler-generated code. It's important that these registers,
as well as the status register, get saved and restored. The extra code needed to do this is enabled by
tagging the interrupt function with __attribute__((signal)).
30
CHAPTER 6
PROJECT
6.1 Introduction
The title of the project is ‘Home Automation’. It is a project utilizing a major part of the knowledge
gained in the training course. It consists of a wireless switching mechanism which one can use to
switch ON and OFF the appliances at their homes. It utilizes the Bluetooth of a phone to control the
appliances. The mobile application used for this project was developed with specialized purpose of
naming each switch of the switching (relay) mechanism. The goal was to create a decent and cost
effective project.
The basic hardware components used for developing this project include:
It is the brain of the project. It receives data from the Bluetooth Module Serially and then uses it to
switch the relays ON and OFF. The microcontroller was chosen as it is cheaper than the Pico power
(ATmega328p) version. Furthermore the microcontroller was unbooted make it cheaper.
31
BLUETOOTH MODULE-
Features-
RELAY MODULE-
32
The project utilizes an 8 channel relay board for switching. It contains 8 x 5V relays rated at
10A/250V AC (DC 30V/10A). Each
relay is switched on/off by an opto-
isolated digital input that can be
connected directly to a microcontroller
output pin. It only requires a voltage of
approx. 1.0V to switch the inputs on but
can handle input voltages up to 5V. This
makes it ideal for both 5V and 3.3V
devices.
Features-
8x 5V relays
Each relay rated at 10A / 250V
Each relay consumes approx. 30mA when switched on (at 5V)
Power supply 5V
Fully Opto-isolated input signals
Input signals from 1.0V to 5V
Individual indicator LEDS for each relay
Power On LED indicator
Isolated Input GND
4x mounting holes
Board Size: 14cm x 5.5cm
A supply voltage of 5V should be connected to the VCC and GND pins. This is used to power the
relay coils when they are switched on. A power Indicator LED indicates power is supplied.DO NOT
apply power the wrong way round. This can seriously damage the components on the board
Opto-isolated input signals should be supplied between the COM pin (signal GND) and each input
pin. There is a shorting link which links the signal ground to the power ground. If you want full
isolation for your microcontroller from potentially noisy relays, this link can be removed.
CRYSTAL OSCILLATOR-
33
A crystal oscillator is an electronic oscillator circuit that uses the
mechanical resonance of a vibrating crystal of piezoelectric
material to create an electrical signal with a
precise frequency.[1][2][3] This frequency is commonly used to keep
track of time, as in quartz wristwatches, to provide a stable clock
signal for digital integrated circuits, and to stabilize frequencies
16 MHz crystal oscillator
for radio transmitters and receivers.
Even though the ATmega328 has an internal oscillator of 8MHz, the project utilizes an external
quartz crystal of 16MHz as the clock.
PROGRAMMER-
A microcontroller programmer or
microcontroller burner is a hardware device
accompanied with software which is used to
transfer the machine language code to the
microcontroller/EEPROM from the PC. The
compiler converts the code written in
languages like assembly, C, java etc. to machine language code (which is understandable by the
machines/microcontrollers) and stores it in a hex file. A microcontroller programmer acts as an
interface between the PC and the target controller. The API/software of the programmer reads data
from the hex file stored on the PC and feeds it into the controller’s memory. The target controller on
which the program needs to be burned is placed on the programmer using a ZIP socket. The software
transfers the data from the PC to the hardware using serial, parallel or USB port.
The programmer generally contains a microcontroller which is pre-programmed to take data from the
PC and program the target controller. The programmer burns the target controller using any of the
protocols like SPI, parallel interfacing, I2C/TWI or CAN. The speed of burning depends on the way
of programmer is interfaced with PC and the protocols used to burn the target controller.
ARDUINO IDE-
The Arduino IDE was used to program and bootload the µC (ATmega 328). The IDE used was
version 1.6.9 (Download Link: https://www.arduino.cc/download_handler.php) for bootloading and
34
version 1.01 (Download Link:
https://drive.google.com/open?id=0BxrP3wW7fUS9bGdNV3FyQXpyaHc) for programming.
The application for the project was developed with the help of MIT app inventor. App Inventor for
Android is an open-source web application originally provided by Google, and now maintained by
the Massachusetts Institute of Technology (MIT).
6.4 Setup
BOOTLOADING-
A bootloader is a program that runs in the microcontroller to be programmed. It receives new program
information externally via some communication means and writes that information to the program
memory of the processor. Without the bootloader the microcontroller will not be programmed.
35
1).To bootload first make the connections as follows-
An optiloader is a header file along with an Arduino file which helps us bootload a number of
microcontrollers.
3).Download the contents and put them in a folder named optiLoader and save that folder in the
directory where your Arduino programs are saved.
36
4). Open the optiLoader.ino file in Arduino IDE 1.6.9 and upload it to the Arduino UNO. If the
connections are correct as shown above then pressing the reset button of the Arduino UNO will
bootload the ATmega328.
For uploading the code we use a modified version of Arduino IDE 1.01 (Download link given above).
This version detects ATmega328p to be same as ATmega328.
1).To upload a code open the IDE (v 1.6.9) and select Arduino ISP code from examples and then
upload it.
37
This code turns the Arduino into an ISP (In Serial Programmer) which could be used to upload codes.
2).Then open the modified IDE (v1.01) and select “Arduino UNO” as board and “Arduino as ISP” as
Programmer from the tools menu.
38
4). Keeping the connections same as in the bootloader, press Shift and then click on the upload button
which uploads the code with Arduino as ISP.
THE CONNECTIONS-
39
The connections are-
1. Microcontroller
1.1. Pin 20 , Pin 7 to +5V
1.2. Pin 7 , Pin 22 to GND
1.3. Pins PB5 to PD6 to the relay
2. Relay
2.1. Pin Vcc to +5V
2.2. Pin Gnd to GND
2.3. Rest to the microcontroller
3. Bluetooth Module
3.1. +5V to +5V
3.2. Gnd to GND
3.3. Tx to Pin 2 of microcontroller
3.4. Rx to Pin 3 of microcontroller
THE APP-
The app sends two types of strings. One string is to set the relay name and the other is to switch them
on and off.
Com1:EXAMPLE STRING
The number is the relay switch number. You can set up to 9 switches.
There is a reset button in the app too which can be used to send all 9 command string together.
THE CODE-
The code utilizes the USART (Universal Synchronous Asynchronous Receiver/Transmitter) hardware
of the microcontroller. The bits are set according to the USART registers of ATmega328. The device
receives one character at a time which the code converts into a string. The code distinguishes between
the two types of strings (Command and Configuration) and works accordingly.
40
The Code is as follows –
#include <avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#include <stdio.h>
#define F_CPU 16000000L
#define BUAD 9600
#define BRC ((F_CPU/16/BUAD)-1) //Buad rate calculator
#define RX_BUFFER_SIZE 128
String voice;
char rxBuffer[RX_BUFFER_SIZE];//A character array made to receive characters
uint8_t rxReadPos = 0;
uint8_t rxWritePos = 0;
char getChar(void);// To get character from rxBuffer
char peekChar(void);// To read character from rxBuffer
int m = 0, n;
String com[9];// A String array to store names of relays
int main(void)
{
if (voice[0] == 'C' && voice[1] == 'o' && voice[2] == 'm')// Setting the
command array
{ m = 2;
int x = (int)voice[3];
n = 5;
41
com[x - 48 - 1] = "";
while (n < voice.length())
{
com[x - 48 - 1] += voice[n];
n++;
}
}
if (m == 1)// Referring the command array to do task
{
if (voice == com[0] + " ON")
PORTB &= ~(1 << PORTB5);
if (voice == com[0] + " OFF")
PORTB |= (1 << PORTB5);
if (voice == com[1] + " ON")
PORTB &= ~(1 << PORTB4);
if (voice == com[1] + " OFF")
PORTB |= (1 << PORTB4);
if (voice == com[2] + " ON")
PORTB &= ~(1 << PORTB3);
if (voice == com[2] + " OFF")
PORTB |= (1 << PORTB3);
if (voice == com[3] + " ON")
PORTB &= ~(1 << PORTB2);
if (voice == com[3] + " OFF")
PORTB |= (1 << PORTB2);
if (voice == com[4] + " ON")
PORTB &= ~(1 << PORTB1);
if (voice == com[4] + " OFF")
PORTB |= (1 << PORTB1);
if (voice == com[5] + " ON")
PORTB &= ~(1 << PORTB0);
if (voice == com[5] + " OFF")
PORTB |= (1 << PORTB0);
if (voice == com[6] + " ON")
PORTD &= ~(1 << PORTD7);
if (voice == com[6] + " OFF")
PORTD |= (1 << PORTD7);
if (voice == com[7] + " ON")
PORTD &= ~(1 << PORTD6);
if (voice == com[7] + " OFF")
PORTD |= (1 << PORTD6);
if (voice == com[8] + " ON")
PORTD &= ~(1 << PORTD5);
if (voice == com[8] + " OFF")
PORTD |= (1 << PORTD5);
}
voice = "";
}
}
ISR(USART_RX_vect)// Interrupt Vector .Fired on when new data is incoming
{
42
rxBuffer[rxWritePos] = UDR0;
rxWritePos++;
if (rxWritePos >= RX_BUFFER_SIZE)
rxWritePos = 0;
m = 1;
}
char getChar(void)
{
char ret = '\0';
if (rxReadPos != rxWritePos)
{
ret = rxBuffer[rxReadPos];
rxReadPos++;
if (rxReadPos >= RX_BUFFER_SIZE)
rxReadPos = 0;
}
return ret;
}
char peekChar(void)
{
char ret = '\0';
if (rxReadPos != rxWritePos)
{
ret = rxBuffer[rxReadPos];
}
return ret;
}
43
CHAPTER 7
Automation is the future. There will be automation everywhere, industries, markets, schools and
homes. The goal of automation is to make our lives easier. Therefore it is necessary to take a first step
in the process.
Even though the project does not exactly work automatically, it does automatically recognize and do
the tasks it has been ordered to do and thus makes it easier for us to control the appliances at our
homes.
1).The first step to improve the project would be to add a “true” automation feature in it. That could a
counter which would automatically switch on and off the appliances in a room when there are one or
more people in it. It could be achieved using PIR sensors.
For basic functionality would only require two of these sensors but a
better system would be to use 4-6 sensors. PIR Sensor
2).The second step would be to use analog output which could be used to control the output of
different appliances like fan speed or light intensity.
44
4).The final step would to connect it to the internet so that one could control it from anywhere in the
world.
Even though there are many advantages of the project, there are negatives too. First and foremost is
the security issues which would have to be dealt with various measures. The second would be the
misuse and too much dependency on the system which would have to be monitored personally.
7.2 Conclusion
It is said that the best way to learn anything is by doing it. This project was a great example. It helped
to learn about a number of things, especially about microcontrollers, which was the main goal to
begin with.
Along with it the training was able to guide me to a whole world of possibilities including both DIY
and industrial applications.
In the end one should remember that even though knowledge is important, one should never forget
about security and ethics which helps us in building reliable and acceptable technology for the benefit
of mankind.
45
REFERENCES
Internet Links-
https://en.wikipedia.org/wiki/Embedded_system
http://www.engineersgarage.com/articles/embedded-systems
https://en.wikipedia.org/wiki/Microcontroller
https://en.wikipedia.org/wiki/Microprocessor
http://www.circuitstoday.com/basics-of-microcontrollers
http://www.engineersgarage.com/tutorials/difference-between-microprocessor-and-
microcontroller
https://www.arduino.cc/en/Tutorial/ArduinoISP
https://learn.sparkfun.com/tutorials/installing-an-arduino-bootloader
http://www.instructables.com/id/Burning-the-Bootloader-on-ATMega328-using-Arduino-/
https://github.com/WestfW/OptiLoader
http://appinventor.mit.edu/explore/ai2/tutorials.html
Books-
46