0% found this document useful (0 votes)
41 views54 pages

AdvMC SECTION B (Open Source Embedded Development Board

Uploaded by

ajithtech21600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views54 pages

AdvMC SECTION B (Open Source Embedded Development Board

Uploaded by

ajithtech21600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Advanced Microcontrollers &

Applications (EC-506)

Class: ECE- 5th Semester


Academic session: Aug-Dec,
2020
(SECTION-B)
Open Source Embedded
Development Board
(Arduino)
Introduction to Arduino
• Arduino is an open source platform used for
building electronics projects.

• Arduino consists of both a physical


programmable circuit board or microcontroller
and a software, IDE (Integrated Development
Environment) that runs on the computer.

• It is used to write and upload computer code to


the physical board.
• Arduino boards are equipped with sets of digital
and analog input/output pins, USB connection
which is used for loading programs from
computers, power jack, reset button etc.
Basic Arduino UNO board
(Hardware)
Arduino UNO board with Atmega328
Microcontroller
Atmega328 Microcontroller and its pin
mapping with Arduino board pins
Arduino Bootloader
• It is the first program which executes when a
device is connected to a power supply.
Arduino IDE Features
• It is an open source software.
• Easy to write code and upload it to the
physical board.
• Easy to learn programming language with its
inbuilt functions.
• Runs on windows, Mac OS and Linux.
• This software can be used with any arduino
board.
Arduino IDE ( Software)
• A program written with the IDE for Arduino is
called a sketch.

• The IDE enables to write and edit code and convert


this code into instructions that Arduino hardware
understands. The IDE also transfers those
instructions to the Arduino board (a process called
as uploading).
Arduino IDE (Software)
• The Arduino IDE supports the languages C and C++
using special rules of code structuring.

• It consists of only two functions, setup and loop.

• The setup function is used to initialize variables, input


and output pin modes and other libraries needed in the
sketch.

• After setup has been called, function loop is executed


repeatedly in the main program. It controls the board until
the board is powered off or reset.
Arduino IDE (Software)
Arduino programming Language
Interface Digital and Analog I/O
Devices
(Arduino Interfacing)
Communication Protocols

• Communication protocols are defined to


achieve data exchange.
• Each protocol can be categorized into one of
the two categories:
– Serial
OR
– Parallel
• Parallel Communication:
➢ Useful in shorter distances ( upto several meters).
➢ Example Parallel connection between Arduino and peripherals
via I/O ports.
➢ Long distance communication is not possible.
Advantages and Drawbacks of Parallel
Communication
• Faster than serial, easy to implement .
• Parallel interfaces transfer multiple bits at the same
time.
• Disadvantages:
• Requires many input/output (I/O) ports and lines.
Serial Communication
• Sent and receive data through stream of bits.
• This stream of bits is governed by set of rules
called as protocol.
• It is a set of rules, which must be applied such
that the devices can correctly interpret data they
mutually exchange.
• Arduino automatically takes care of this, so that
the work of the programmer/user is reduced to
simple write (data to be sent) and read (received
data).
Types of Serial communication
• Synchronous :
Devices that are synchronized use the
same clock and their timing is in
synchronization with each other.
• Asynchronous:
Devices that are asynchronous have their
own clocks and are triggered by the output of
the previous state.
UART (Universal Asynchronous Receiver
Transmitter) module is asynchronous.
• The asynchronous serial protocol has a number of built-in
rules. These rules are nothing but mechanisms that help
ensure robust and error-free data transfers.
❑ Synchronization bits:
Two or three bits transferred with each packet of data.
They are start bit and stop bit.
The start bit is always indicated by an idle data line going from
1 to 0, while the stop bit(s) will transition back to the idle state
by holding the line at 1.
❑ Data bits:
The amount of data in each packet can be set to any size from
5 to 9 bits.
Certainly, the standard data size is your basic 8-bit (byte), but
other sizes have their uses.
A 7-bit data packet can be more efficient than 8, especially if
you are just transferring 7-bit ASCII characters.
❑Parity bits:
The user can select whether there
should be a parity bit or not, and if yes,
whether the parity should be odd or
even. The parity bit is 0 if the number of
1’s among the data bits is even. Odd
parity is just the opposite
❑ Baud rate:
The term baud rate denotes number of bits transferred per
second (bps).
It is usually required by the protocol that each byte
is transferred along with several control bits.
It means that one byte in serial data stream may
consist of 11 bits.
For example, if the baud rate is 300 bps then
maximum 37 and minimum 27 bytes may be
transferred per second.
Example
• Arduino UART:
void setup()
{
Serial.begin(9600); //set up serial library baud rate to
9600
Serial.println("hello world"); //print hello world
}
void loop()
{}
Code will make Arduino deliver output
depending on the input provided.
• Open the Serial monitor at the top right section of Arduino IDE.
• Type anything into the top box of the Serial Monitor and press
send or enter on your keyboard. This will send a series of bytes
to the Arduino.
• The following code returns whatever it receives as an input.
The following code will make Arduino deliver
output depending on the input provided
Serial communication Protocols
I2C (Inter- Integrated Circuit) :
• Inter-integrated circuit (I2C) is a system for serial
data exchange between the microcontrollers and
specialized integrated circuits of a new
generation.
• Used when the distance between them is short
(receiver and transmitter are usually on the same
printed board).
• Connection is established via two conductors.
One is used for data transfer and the other is
used for synchronization (clock signal).
Schematic of I2C BUS

The address of I2C is 7 bits long. Theoretically allows 128 I2C addresses,
however some addresses are reserved for special purposes, thus only 112
addresses are available with 7 bit scheme.
• Working:
• One device is always a master. It performs addressing
of one slave chip before the communication starts.

• Baud rate is usually 100 Kb/sec (standard mode) or 10


Kb/sec (slow baud rate mode). Systems with the baud
rate of 3.4 Mb/sec have recently appeared.

• The distance between devices, which communicate


over an I2C bus is limited to several meters.
Arduino board I2C pins
• The I2C bus consists of two signals − SCL and
SDA.
• SCL is the clock signal, and SDA is the data
signal.
• Master always generates the clock signal. Some
slave devices may force the clock low at times to
delay the master sending more data (or to require
more time to prepare data before the master
attempts to clock it out). This is known as “clock
stretching”.
• Following are the pins for Arduino boards −
• Uno, Pro Mini {A4 (SDA), A5 (SCL)}
Arduino I2C Modes
• Two modes - master code and slave code -
to connect two Arduino boards using I2C.

• Master Transmitter / Slave Receiver


• Master Receiver / Slave Transmitter
Master Transmitter / Slave Receiver
• Master Transmitter:
• The following functions are used to initialize the Wire library
and join the I2C bus as a master or slave. This is normally
called only once.
• Wire.begin(address) − Address is the 7-bit slave address in
our case as the master is not specified and it will join the bus
as a master.
• Wire.beginTransmission(address) − Begin a transmission
to the I2C slave device with the given address.
• Wire.write(value) − Queues bytes for transmission from a
master to slave device (in-between calls to
beginTransmission() and endTransmission()).
• Wire.endTransmission() − Ends a transmission to a slave
device that was begun by beginTransmission() and transmits
the bytes that were queued by wire.write().
Example
#include <Wire.h> //include wire library
void setup() //this will run only once
{
Wire.begin(); // join i2c bus as master }
short age = 0; // short storage size is 2 bytes (value range -32768 to
32767)
void loop()
{ Wire.beginTransmission(2); // transmit to device #2
Wire.write("age is = ");
Wire.write(age); // sends one byte
Wire.endTransmission(); // stop transmitting
delay(1000);
}
Slave receiver
The following functions are used −
• Wire.begin(address) − Address is the 7-bit
slave address.
• Wire.onReceive(received data handler) −
Function to be called when a slave device
receives data from the master.
• Wire.available() − Returns the number of
bytes available for retrieval with
Wire.read().This should be called inside the
Wire.onReceive() handler.
Example
• #include <Wire.h> //include wire library
• #DEFINE SLAVEADDR 2
• void setup()
• { //this will run only once
• Wire.begin(2); // join i2c bus with address #2
• Wire.onReceive(receiveEvent); // call receiveEvent when the master send
any thing
• Serial.begin(9600); // start serial for output to print what we receive }
• void loop()
• { delay(250); } //-----this function will execute whenever data is received from
master-----//
• void receiveEvent(int howMany)
• { while (Wire.available()>1) // loop through all but the last
• { char c = Wire.read(); // receive byte as a character
• Serial.print(c); // print the character }
• }
Master Receiver / Slave Transmitter
• Master Receiver
• The Master, is programmed to request, and
then read bytes of data that are sent from the
uniquely addressed Slave Arduino.
• The following function is used −
• Wire.requestFrom(address,number of
bytes) − Used by the master to request bytes
from a slave device. The bytes may then be
retrieved with the functions wire.available()
and wire.read() functions.
Serial Peripheral Interface (SPI)
• A Serial Peripheral Interface (SPI) bus is a system for
serial communication, which uses up to four
conductors, commonly three.
• One conductor is used for data receiving,
• One for data sending,
• One for synchronization and
• One alternatively for selecting a device to
communicate with.
• It is a full duplex connection: means that the data is
sent and received simultaneously.
• The maximum baud rate is higher than that in the I2C
communication system.
Transmission modes
SPI pins on Arduino Board
• SPI uses the following four wires −
• SCK − This is the serial clock driven by the
master.
• MOSI − This is the master output / slave input
driven by the master.
• MISO − This is the master input / slave output
driven by the master.
• SS − This is the slave-selection wire.
• Active low for data communication else HIGH.
Schematic and direction of data Transfer

SCK- Serial Clock

MOSI- Master out Slave In


MASTER
MICROCONTROLLER SLAVE
(ARDUINO UNO) DEVICE
MISO-Master In Slave Out

SS- Slave Select


SPI Modes of Operation
• Depending upon Clock Polarity (CPOL) and
Clock Phase (CPHA) value.
1. Mode0
2. Mode1
3. Mode2
4. Mode3
• Mode 0 (the default) − Clock is normally low (CPOL = 0),
and the data is sampled on the transition from low to high
(leading edge) (CPHA = 0).
• CPOL-0
• CPHA-0
• Mode 1 − Clock is normally low (CPOL = 0), and the
data is sampled on the transition from high to low
(trailing edge) (CPHA = 1)
• CPOL-0
• CPHA-1
• Mode 2 − Clock is normally high (CPOL = 1), and the
data is sampled on the transition from high to low
(leading edge) (CPHA = 0).
• CPOL-1
• CPHA-0
• Mode 3 − Clock is normally high (CPOL = 1), and the data is
sampled on the transition from low to high (trailing edge)
(CPHA = 1).
• CPOL-1
• CPHA-1
Two Arduino UNO Board connection as
Master and Slave
• (SS) : pin 10
• (MOSI) : pin 11
• (MISO) : pin 12
• (SCK) : pin 13
• The ground is common.
Schematic representation of connection
Functions used in SPI
• include the SPI.h library
• SPI.begin() − Initializes the SPI bus by setting
SCK, MOSI, and SS to outputs, pulling SCK and
MOSI low, and SS high.
• SPI.setClockDivider(divider) − To set the SPI
clock divider relative to the system clock. On AVR
based boards, the dividers available are 2, 4, 8,
16, 32, 64 or 128.
• The default setting is SPI_CLOCK_DIV4, which
sets the SPI clock to one-quarter of the frequency
of the system clock (5 MHz for the boards at 20
MHz).
• Divider − It could be (SPI_CLOCK_DIV2,
SPI_CLOCK_DIV4,SPI_CLOCK_DIV8,SPI_CLOCK_DIV16,SPI_C
LOCK_DIV32,SPI_CLOCK_DIV64,SPI_CLOCK_DIV128).

• SPI.transfer(val) − SPI transfer is based on a simultaneous send


and receive: the received data is returned in receivedVal.

• SPI.beginTransaction(SPISettings(speedMaximum, dataOrder,
dataMode)) − speedMaximum is the clock, dataOrder(MSBFIRST
or LSBFIRST), dataMode(SPI_MODE0, SPI_MODE1,
SPI_MODE2, or SPI_MODE3).
SPI as master (example)
• #include <SPI.h>
• void setup (void)
• { Serial.begin(115200); //set baud rate to 115200 for usart
• digitalWrite(SS, HIGH); // disable Slave Select
• SPI.begin (); SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8 }
• void loop (void)
• {
• char c; digitalWrite(SS, LOW); // enable Slave Select
• // send test string
• for (const char * p = "Hello, world!\r" ; c = *p; p++)
• {
• SPI.transfer (c);
• Serial.print(c);
• }
• digitalWrite(SS, HIGH); // disable Slave Select
• delay(2000);
• }
SPI as Slave
• #include <SPI.h>
• char buff [50];
• volatile byte indx;
• volatile boolean process;

• void setup (void) {


• Serial.begin (115200);
• pinMode(MISO, OUTPUT); // have to send on master in so it set as output
• SPCR |= _BV(SPE); // turn on SPI in slave mode
• indx = 0; // buffer empty
• process = false;
• SPI.attachInterrupt(); // turn on interrupt
• }
• ISR (SPI_STC_vect) // SPI interrupt routine {
• byte c = SPDR; // read byte from SPI Data Register
• if (indx < sizeof buff) {
• buff [indx++] = c; // save data in the next index in the array buff
• if (c == '\r') //check for the end of the word
• process = true;
• }
• }

• void loop (void) {


• if (process) {
• process = false; //reset the process
• Serial.println (buff); //print the array on serial monitor
• indx= 0; //reset button to zero
• }
• }
Digital I/O interfacing
Analog I/O interfacing

You might also like