0% found this document useful (0 votes)
5 views

ESD MODULE-2 Compressed

The document provides an overview of the Arduino Uno microcontroller, detailing its features, architecture, and programming techniques for input/output operations, including analog-to-digital (ADC) and digital-to-analog (DAC) conversions. It explains the functionality of timers and counters within the Arduino system, highlighting their roles in timing events and generating signals. Additionally, it includes example codes for practical applications such as reading sensor values and controlling devices using PWM.

Uploaded by

sreenidhi.r2022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ESD MODULE-2 Compressed

The document provides an overview of the Arduino Uno microcontroller, detailing its features, architecture, and programming techniques for input/output operations, including analog-to-digital (ADC) and digital-to-analog (DAC) conversions. It explains the functionality of timers and counters within the Arduino system, highlighting their roles in timing events and generating signals. Additionally, it includes example codes for practical applications such as reading sensor values and controlling devices using PWM.

Uploaded by

sreenidhi.r2022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 138

MODULE - 2 1

• Memory interfacing.
• A/D, D/A.
I/O Interfacing • Timers.
• Watch-dog timer.
Techniques • Counters.
• Encoder & Decoder.
• UART.
• Sensors and actuators interfacing.

MODULE - 2 2
21-01-2025 3
INTRODUCTION TO ARDUINO UNO
Features
 An open-source microcontroller board
 Microcontroller : ATmega328
 Operating Voltage : 5V
 Input Voltage (external) : 6-20V (7-12 recommended)
 Digital I/O Pins : 14 (6 PWM output)
 Analog Input Pins :6
 DC Current per I/O Pin : 40 mA
 DC Current for 3.3V Pin : 50 mA
 Flash Memory : 32 KB (0.5 KB for bootloader)
 Clock Frequency : 16 MHz
 Programming Interface : USB (ICSP)
21-01-2025 4
INTRODUCTION TO ARDUINO UNO
Arduino Uno - Board Details

21-01-2025 5
INTRODUCTION TO ARDUINO UNO
Arduino Uno - Pin Details

21-01-2025 6
INTRODUCTION TO ATmega328
Features Architecture
 Instruction set Architecture : RISC
 Data bus size : 8-bit
 Address bus size : 16-bit
 Program memory (ROM) : 32 KB
 Data memory (RAM) : 2 KB
 Data memory (EEPROM) : 1 KB
 Input/output ports : 3 (B, C, D)
 General purpose registers : 32
 Timers : 3 (Timer0, 1 & 2)
 Interrupts : 26 (2 Ext. interrupts)
 ADC modules (10-bit) : 6 channels
 PWM modules :6
 Analog comparators :1 21-01-2025 7

 Serial communication : SPI, USART, TWI(I2C)


PROGRAMMING I/P and O/P

Information exchange is done by input and output devices

It may be digital or analog signal

Digital signals will be directly interfaced to CPU

Analog devices requires some converter for interfacing with CPU

All peripherals can not be always on-chip


21-01-2025 8
PROGRAMMING I/P and O/P

Internal I/O devices


 Physically silicon devices
 Small size
Ex: RTC, ADCs, memory
External I/O devices
 Have large size
 Has bus interface
Ex: LCD, keypad, MIC

21-01-2025 9
PROGRAMMING INPUT & OUTPUT

 CPU communicates to the device by reading and writing to the


registers.
 Data register – read and write, Status register – read only

Status
Register
CPU Device
Mechanism
Data
Register

21-01-2025 10
PROGRAMMING INPUT & OUTPUT

 Microcontroller programming input and output


 I/O mapped I/O
 Memory mapped I/O (widely used)

 Memory mapped I/O –


 Common address shared by both memory and I/O

 Read and write instructions to communicate with the devices

 Example: In 8051 Timer Register - MOV TMOD,#01H 21-01-2025 11


PROGRAMMING INPUT & OUTPUT

 Busy-Wait I/O
 Checking an I/O device if it has completed the task by reading its
status register often called polling.
 Example: AGAIN: JNB TF0, AGAIN

 Interrupts
 It enables I/O devices to signal the completion or status and force
execution of a particular piece of code.
 Example: MOV IE, #82H
21-01-2025 12
21-01-2025 13
ANALOG TO DIGITAL (ADC) CONVERTER

 An ADC is an electronic circuit whose digital output is


proportional to its analog input.

 Effectively it “measures” the input voltage and gives a binary


output number proportional to its size.

 The list of possible analog input signals is endless, (e.g. audio


and video, medical or climatic variables)

 The ADC when operates within a larger environment, often


called a data acquisition system. 21-01-2025 14
ANALOG TO DIGITAL (ADC) CONVERTER

𝑽𝒊
 ADC conversion D= × 𝟐𝒏
𝑽𝒓

Where,
 Vi is the input voltage
 Vr the reference voltage
 n the number of bits in the converter output
 D the digital output value.

 The output binary number D is an integer


 For an n-bit number can take any value from 0 to (2n - 1).
21-01-2025 15
ANALOG TO DIGITAL (ADC) CONVERTER

 ADC has maximum and minimum permissible input values.

 Resolution is a measure of how precisely an ADC can convert


and represent a given input voltage.
𝑽𝒓
Resolution =
𝟐𝒏

 Arduino ADC is 10-bit. This leads to a resolution of 5/1024, or


4.88 mV.
21-01-2025 16
ANALOG TO DIGITAL (ADC) CONVERTER

 Not all the pins on a microcontroller have the ability to do


analog to digital conversions.

 In Arduino board, analog pins have an ‘A’ in front of their label


(A0 through A5).

 A 10-bit device a total of 1024 different values.

 An input of 0 volts would result in a decimal 0.

 An input of 5 volts would give the maximum of 1023. 21-01-2025 17


ANALOG TO DIGITAL (ADC) CONVERTER

 ADC value to voltage conversion

 ADC value – 434


𝑽𝒊
434 = × 𝟏𝟎𝟐𝟑
𝟓

𝑽𝒊 = 2.12 V

21-01-2025 18
ANALOG TO DIGITAL (ADC) CONVERTER

Analog I/O
 AREF pin and analogReference(type) ADC reference other than 5 V.
 Increase the resolution available to analog inputs
That operate at some other range of lower voltages below +5
Must be declared before analogRead()
analogReference(type)

Parameters type:
which type of reference to use (DEFAULT,
INTERNAL, INTERNAL1V1, INTERNAL2V56, or
EXTERNAL)
Returns 21-01-2025 19
None
ANALOG TO DIGITAL (ADC) CONVERTER

Analog I/O
analogRead(pin)
Reads the value from a specified analog pin with a 10-bit
resolution.
 Works for analog pins (0–5).
 It will return a value between 0 to 1023. analogRead(pin)
100 microseconds for one reading.
Parameters pin:
 Reading rate 10000 per sec. the number of the analog input pin to read
from (0-5)
 INPUT nor OUTPUT need not be declared.
Returns 21-01-2025 20
int(0 to 1023)
ANALOG TO DIGITAL (ADC) CONVERTER
analogWrite(pin,value)
Analog I/O
analogWrite(pin,value) Parameters pin: the number of the pin you
want to write
 Write an analog output on a digital pin. value: the duty cycle between 0 (always off,
0%) and 255 (always on, 100%)
 This is called Pulse Width Modulation.
Returns
None
 PWM uses digital signals to control analog devices.

 Digital Pins # 3, # 5, #6, # 9, # 10, and # 11 can be used as PWM


pins.

 No need to configure pin as an OUTPUT. 21-01-2025 21


ANALOG TO DIGITAL (ADC) CONVERTER

EXAMPLE 1

Program an Arduino board to read analog value from an analogy input


pin, convert the value into digital value and display it in a serial window.

21-01-2025 22
ANALOG TO DIGITAL (ADC) CONVERTER

CODE int sensorValue = 0;

void setup()
{
//monitor output in serial port
Serial.begin(9600); //intitating serial communication with 9600 baud rate
}

void loop()
{
sensorValue = analogRead(A0);
//analog input is read from A0 pin
Serial.println(sensorValue);
//printing the output in serial port
delay(100); // simple delay of 100ms
} 21-01-2025 23
ANALOG TO DIGITAL (ADC) CONVERTER

CODE

21-01-2025 24
ANALOG TO DIGITAL (ADC) CONVERTER

EXAMPLE 2

Code an Arduino board to read a temperature sensor connected


to an analog input A0 and display the temperature value in serial
window. Each time the value goes beyond 40 degree Celsius an
LED connected to a digital output glows as warning.

21-01-2025 25
ANALOG TO DIGITAL (ADC) CONVERTER
const int lm35_pin = A0; /* LM35 O/P pin */
CODE const int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}

void loop() {
int temp_adc_val;
float temp_val;
temp_adc_val = analogRead(lm35_pin); /* Read Temperature */
temp_val = (temp_adc_val * 4.88); /* Convert adc value to equivalent voltage */
temp_val = (temp_val/10); /* LM35 gives output of 10mv/°C */
if (temp_val > 40) {
digitalWrite(ledPin, HIGH); // turn LED on:
} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
Serial.print("Temperature = ");
Serial.print(temp_val);
Serial.print(" Degree Celsius\n");
delay(1000); 21-01-2025 26
}
ANALOG TO DIGITAL (ADC) CONVERTER

CODE

21-01-2025 27
DIGITAL TO ANALOG (DAC) CONVERTER

 A digital-to-analog converter is a circuit which converts a


binary input number into an analog output.

 DAC has a digital input, represented by D, and an analog


output, represented by Vo.
𝑽𝒓
+

+ 𝑫
𝑫 DAC 𝑽𝒐 𝑽𝒐 = × 𝑽𝒓
- 𝟐𝒏

21-01-2025 28

Control Lines
DIGITAL TO ANALOG (DAC) CONVERTER

 For each input digital value, there is a corresponding analog


output.

 The number of possible output values is given by 2n.

 The step size by Vr/2n this is called the resolution.

 The maximum possible output value occurs when D = (2n-1),


so the value of Vr as an output is never quite reached.
21-01-2025 29
DIGITAL TO ANALOG (DAC) CONVERTER

21-01-2025 30
ANALOG TO DIGITAL (ADC) CONVERTER

21-01-2025 31
PULSE WIDTH MODULATION (PWM)
EXAMPLE 1
Code an Arduino board to control the speed of a DC motor using
potentiometer.
CODE int potvalue;
void setup()
{
pinMode(A0, INPUT);
pinMode(6, OUTPUT);
}

void loop()
{
potvalue=analogRead(A0)/4;
analogWrite (6, potvalue); 21-01-2025 32
}
ANALOG TO DIGITAL (ADC) CONVERTER

21-01-2025 33
21-01-2025 34
TIMER / COUNTER

 Digital timer/counters are used throughout embedded designs


 To provide a series of time or count related events within the
system
 With the minimum of processor and software overhead

 Most embedded systems have a time component within them


such as
 Timing references for control sequences
 To provide system ticks for operating systems and
 Even the generation of waveforms
 Serial port baud rate generation and audible tones.
21-01-2025 35
TIMER / COUNTER

 Timers and counters are distinguished from one another


largely by their use, not their logic.

 Timer – Periodic Signal


 Counter – Aperiodic signal

21-01-2025 36
TIMER / COUNTER

 The central timing is derived from a clock input.


 Internal to the timer/counter
(or)
 External and thus connected via a separate pin.

 The clock may be divided using a simple divider


 A pre-scalar - effectively divides the clock by the value
that is written into the pre-scalar register

 The divided clock is then passed to a counter


 count-down operation or count-up operation
21-01-2025 37
TIMER / COUNTER

 When a zero count is reached, an event occurs.


 such as an interrupt of an external line changing state.

 The final block is an I/O control block


 It generates interrupts
 Can control the counter based on external signals

21-01-2025 38
TIMER / COUNTER

Timers on the Arduino


 The Arduino Uno has 3 timers: Timer0, Timer1 and Timer2
1. Timer 0 – 8 bit timer ; Functions – delay(), millisec().
2. Timer 1 – 16 bit timer; Servo library
3. Timer 2 – 8 bit timer; Function – tone()

 Arduino Uno has a 16Mhz internal clock.


 It takes 62 nano seconds for a single count.
 Update the pre-scalar to alter the frequency and various
counting modes.
 Generate interrupts when the timer reaches a specific count
21-01-2025 39
TIMER / COUNTER

Control Registers

 Timer/Counter Control Registers A: (TCCRnA)


 Timer/Counter Control Registers B: (TCCRnB)
 Timer CounNT: TCNTn
 Output Compare Register A: OCRnA
 Output Compare Register B: OCRnB
n= C/T number 0,1,2
21-01-2025 40
http://www.avrbeginners.net/architecture/timers/timers.html
TIMER / COUNTER

Timer/Counter Modes

 Normal : Counter count up to maximum value and reset to 0


 Phase correct PWM: symmetric with respect to system clock
using PWM
 Clear Time on Compare (CTC): Used in interrupt generation
 Fast PWM: PWM goes as fast as clock but not synchronized
with timing clock
21-01-2025 41
http://www.avrbeginners.net/architecture/timers/timers.html
TIMER / COUNTER

Timers on the Arduino


 Timer/Counter Control Registers (TCCRnA/B)
 Controls the mode of timer
 Contains the pre-scalar values of timers
 Prescalars values 1, 8, 64, 256, 1024
To calculate preloaded value for timer1
 Timer/Counter Register (TCNTn) for time of 2 Sec:
 Control the counter value TCNT1= 65535-(16x𝟏𝟎𝟔 x time(s)/
 Set the pre-loader value Prescalar value)
TCNT1 = 65535 – (16x 𝟏𝟎𝟔 x2 / 1024)
= 34285 21-01-2025 42
TIMER / COUNTER

Timers on the Arduino


 delay(ms)
 Pauses the program for the amount of time.

 Time is specified in milliseconds


delay(ms)
 A delay of 1000 ms equals 1 s
Parameters
ms: the number of milliseconds to pause
(unsigned long)

Returns 21-01-2025 43
None
TIMER / COUNTER

Timers on the Arduino


 delayMicroseconds()
 Used to delay for a much shorter time.

 A time period of 1000 µs would equal 1 ms.


delayMicroseconds()

Parameters
us: the number of microseconds to pause
(unsigned int)

Returns 21-01-2025 44
None
TIMER / COUNTER

Timers on the Arduino


 millis()
 Use of one of internal timer1 to maintain a running counter.
 How many milliseconds the microcontroller has been running
since the last time it was turned on or reset.
 It returns a value in milliseconds millis()
Parameters
None

Returns
Number of milliseconds since the program
21-01-2025 45
started (unsigned long)
TIMER / COUNTER

Timers on the Arduino


 micros()
 Returns the value in microseconds.

micros()

Parameters
None

Returns
Number of microseconds since the program
21-01-2025 46
started (unsigned long)
TIMER / COUNTER

EXAMPLE

Code an Arduino board to read a pushbutton connected to a


digital input to display millis() when ON and display micros() when
OFF. Blink an LED connected to a digital output with 500 ms delay.

21-01-2025 47
TIMER / COUNTER

CODE #define LED 13


int buttonState = 0;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(7, INPUT);}
void loop() {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
buttonState = digitalRead(7);
if(buttonState==LOW) {
Serial.write("micros()=");
Serial.println(micros());
}else {
Serial.write("millis()=");
Serial.println(millis()); 21-01-2025 48
}}
TIMER / COUNTER

21-01-2025 49
21-01-2025 50
PULSE WIDTH MODULATION (PWM)

 Pulse width modulation (PWM) create simple square wave


varying the duty cycle.

 It used to control an analog variable, usually voltage or


current.

 Used in a variety of applications, ranging from


telecommunications to robotic control.

 Arduino has six PWM outputs (Pin11, Pin10, Pin9, Pin6, Pin5,
Pin3).
21-01-2025 51
PULSE WIDTH MODULATION (PWM)

 PWM depends on pulse width and duty cycle.

 The pulse width (also called a period) is a short duration of


time in which the duty cycle will operate.

 The duty cycle is the proportion of time that the pulse is “on” or
“high” and is expressed as a percentage.
𝒑𝒖𝒍𝒔𝒆 𝒐𝒏 𝒕𝒊𝒎𝒆 𝒕𝒐𝒏
𝑫𝒖𝒕𝒚 𝒄𝒚𝒄𝒍𝒆 = × 𝟏𝟎𝟎% = × 𝟏𝟎𝟎%
𝒑𝒖𝒍𝒔𝒆 𝒑𝒆𝒓𝒊𝒐𝒅 𝑻

 100% duty cycle - continuously on


 0% duty cycle - continuously off 21-01-2025 52
PULSE WIDTH MODULATION (PWM)

21-01-2025 53
PULSE WIDTH MODULATION (PWM)

 DC motor control is a very common task in robotics.

 Speed of DC motor is proportional to the applied DC voltage.

 Conventional DAC output is used,


 drive it through an expensive and bulky power amplifier
 use the amplifier output to drive the motor

 Alternatively, a PWM signal can be used


 to drive a power transistor directly
21-01-2025 54
PULSE WIDTH MODULATION (PWM)
analogWrite(pin,value)
Analog I/O
Parameters pin: the number of the pin you
analogWrite(pin,value) want to write
value: the duty cycle between 0 (always off,
 Write an analog output on a digital pin. 0%) and 255 (always on, 100%)

 This is called Pulse Width Modulation. Returns


None
 PWM uses digital signals to control analog devices.

 Digital Pins # 3, # 5, #6, # 9, # 10, and # 11 can be used as PWM pins.

 No need to configure pin as an OUTPUT.


21-01-2025 55
PULSE WIDTH MODULATION (PWM)

EXAMPLE 1

Code an Arduino board to control the brightness of an LED. Monitor


the PWM waveform in an oscilloscope and interface a buzzer to
listen to different tones.

21-01-2025 56
PULSE WIDTH MODULATION (PWM)

CODE int brilho = 0;

void setup()
{
pinMode(3, OUTPUT);
}

void loop()
{
for (brilho = 0; brilho <= 255; brilho += 5) {
analogWrite(3, brilho);
delay(30); // Wait for 30 millisecond(s)
}
for (brilho = 255; brilho >= 0; brilho -= 5) {
analogWrite(3, brilho);
delay(30); // Wait for 30 millisecond(s)
} 21-01-2025 57
}
ANALOG TO DIGITAL (ADC) CONVERTER

21-01-2025 58
PULSE WIDTH MODULATION (PWM)
EXAMPLE 1
Code an Arduino board to control the speed of a DC motor using
potentiometer.
CODE int potvalue;
void setup()
{
pinMode(A0, INPUT);
pinMode(6, OUTPUT);
}

void loop()
{
potvalue=analogRead(A0)/4;
analogWrite (6, potvalue); 21-01-2025 59
}
ANALOG TO DIGITAL (ADC) CONVERTER

21-01-2025 60
21-01-2025 61
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

INTRODUCTION
 UART stands for Universal Asynchronous Receiver/Transmitter

 It is used to transmit and receive serially

 Two wires are required to transmit and receive data Tx and Rx


Pin

21-01-2025 62

https://www.circuitbasics.com/basics-uart-communication/
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

INTRODUCTION
 UARTs has no clock signal and follows asynchronous data
communication

 Start and stop bits are added to data packet for identifying
beginning and end of data packet

 Start bit and stop bit are used instead of clock signal

21-01-2025 63
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

INTRODUCTION
 After detecting start bit UART starts to read the incoming data
packet

 A specific frequency is used for data communication known


as baud rate

 Baud rate is expressed as bits per second (bps). It is a


measure of speed of data transfer.

 Both receiving and transmitting UART should work in same


baud rate
21-01-2025 64
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

INTRODUCTION

Parameters Specification
Wires used 2
Maximum speed 115200 baud rate
Synchronous ? No
Serial? Yes
Max number of master 1
Max number of slave 1

21-01-2025 65

https://www.circuitbasics.com/basics-uart-communication/
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

HOW UART WORKS


 In UART, data is transmitted in serial from data bus to
transmitting UART

 The transmitting UART adds a start bit, stop bit and a parity bit
to parallel data from data bus

 the data packet is transmitted serially from Tx to RX pin

 The Rx pin reads the data and converts parallelly by removing


other bits 21-01-2025 66
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

HOW UART WORKS


Frame Format

1 start bit 5 to 9 data 0 or 1 parity 1 to 2 stop


bit bit bits

21-01-2025 67

https://www.circuitbasics.com/basics-uart-communication/
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

UART Communication 21-01-2025

https://www.circuitbasics.com/basics-uart-communication/
68
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

HOW UART WORKS

A common asynchronous serial data format


21-01-2025 69
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

UART IN ARDUINO
 UART allows the Atmega chip to do serial communication
while working on other tasks, through 64 byte serial buffer.

 All Arduino boards have at least one serial port (also known as
a UART or USART): Serial.

 Serial is used for communication between the Arduino board


and a computer or other devices.

 It communicates on digital pins 0 (RX) and 1 (TX) as well as


with the computer via USB. Thus, if you use these functions, you
21-01-2025 70

cannot also use pins 0 and 1 for digital input or output.


UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

Arduino functions for USB serial communication


• if (Serial) – indicates whether or not the USB serial connection is open
• Serial.available()-Get the number of bytes available for reading from the serial port.
• Serial.begin()-Sets the data rate in bits per second (baud) for serial data transmission.
• Serial.end()-Disables serial communication, allowing the RX,TX to be used for input & output.
• Serial.find()-reads data from the serial buffer until the target is found.
• Serial.println()-Prints data as ASCII text followed by a carriage return and newline character.
• Serial.read()-Reads incoming serial data.
• Serial.readString() - reads characters from the serial buffer into a String.
• Serial.write() - Writes binary data to serial port. This data is sent as a byte or series of bytes
For other functions refer the URL: 21-01-2025 71

https://www.arduino.cc/en/pmwiki.php?n=Reference/serial#:~:text=Serial%20is%20used%20for%20communication,with%20the%20computer%20via%20USB.
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

 EXAMPLE-1: Print data received through serial communication on to the serial monitor of
Arduino

void setup() {
Serial.begin(9600); //set up serial library baud rate to 9600
}

void loop() {
if(Serial.available()) //if number of bytes (characters) available for reading from serial port
{
Serial.print("I received:"); //print I received
Serial.write(Serial.read()); //send what you read
}
}

21-01-2025 72
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

 EXAMPLE-1: Arduino code for serial interface to blink switch ON LED when “a” is
received on serial port int inByte; // Stores incoming command
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); // Led pin
Serial.println("Ready"); // Ready to receive commands
}
void loop() {
if(Serial.available() > 0) { // A byte is ready to receive
inByte = Serial.read();
if(inByte == 'a') { // byte is 'a'
digitalWrite(13, HIGH); This function can be used
Serial.println("LED - On"); if(Serial.find(“a"))
}
else { // byte isn't 'a'
digitalWrite(13, LOW);
Serial.println("LED - off"); 21-01-2025 73
}}}
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

Arduino SoftwareSerial Library


 The SoftwareSerial library has been developed to allow serial
communication on other digital pins of the Arduino
 Uses software to replicate the functionality of the hardwired RX and TX
lines hence the name "SoftwareSerial".
 It is possible to have multiple software serial ports with speeds up to
115200 bps.
 This can be extremely helpful when the need arises to communicate with
two or more serial enabled devices
 Limitations:
 maximum RX speed is 57600bps and RX doesn't work on21-01-2025
Pin 13 74
 If using multiple software serial ports, only one receive data at a time.
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

Arduino functions for UART communication (SoftwareSerial Library)


SoftwareSerial() – Need to enable serial communication
available() – gets the no of bits available for reading from serial port
begin() – sets the speed of serial communication
overflow() – checks the serial buffer overflow has occurred
peek() – returns the character received in the serial port
read() - returns the character that was received on the Rx pin of serial port
Print() – works same as serial.print
Listen() – enables the selected serial port to listen
Write() – print data to transmit pin of software serial
21-01-2025 75

Refer. URL: https://www.arduino.cc/en/Reference/SoftwareSerial


UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

 EXAMPLE-3: Arduino code to Receives from the hardware serial, sends to software serial
and Receives from software serial, sends to hardware serial.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(57600); // Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mySerial.begin(4800); // set the data rate for the SoftwareSerial port
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read()); 21-01-2025 76

}}
Sensor & Actuators

21-01-2025 77
Sensor & Actuators

21-01-2025 78
Sensor & Actuators

21-01-2025 79
Sensor & Actuators

21-01-2025 80
Sensor & Actuators

21-01-2025 81
Sensor & Actuators

21-01-2025 82
Sensor & Actuators

21-01-2025 83
Sensor & Actuators

21-01-2025 84
Sensor & Actuators

21-01-2025 85
Sensor & Actuators

21-01-2025 86
Sensor & Actuators

21-01-2025 87
Sensor & Actuators

21-01-2025 88
Sensor & Actuators

21-01-2025 89
Sensor & Actuators

21-01-2025 90
Sensor & Actuators

21-01-2025 91
Sensor & Actuators

21-01-2025 92
Sensor & Actuators

21-01-2025 93
Sensor & Actuators

21-01-2025 94
Sensor & Actuators

21-01-2025 95
Sensor & Actuators

21-01-2025 96
Sensor & Actuators
KNOB CIRCUIT

21-01-2025 97
Sensor & Actuators

21-01-2025 98
Sensor & Actuators

21-01-2025 99
Sensor & Actuators

21-01-2025 100
Sensor & Actuators

21-01-2025 101
Sensor & Actuators

21-01-2025 102
Sensor & Actuators

21-01-2025 103
21-01-2025 104
EEPROM interface using SPI

 Arduino interface with an AT25HP512 Atmel serial EEPROM using the Serial
Peripheral Interface (SPI) protocol.

 EEPROM chips are very useful for data storage.

 Note that the chip on the Arduino board contains an internal EEPROM.

21-01-2025 105
EEPROM interface using SPI
 Serial Peripheral Interface (SPI) is a synchronous serial data protocol
used by Microcontrollers for communicating with one or more peripheral
devices quickly over short distances.

 With an SPI connection there is always one master device (usually a


microcontroller) which controls the peripheral devices.

 Typically, there are three lines common to all the devices,


 Master In Slave Out (MISO) - The Slave line for sending data to the master,
 Master Out Slave In (MOSI) - The Master line for sending data to the peripherals,
 Serial Clock (SCK) - The clock pulses which synchronize data transmission generated by the
master, and
 Slave Select pin - allocated on each device which the master can use to enable and disable
specific devices and avoid false transmissions due to line noise.
21-01-2025 106
EEPROM interface using SPI
 All SPI settings are determined by the Arduino SPI Control Register (SPCR).

 Control registers code control settings for various microcontroller


functionalities. Usually, each bit in a control register effects a particular
setting, such as speed or polarity.

 Data registers simply hold bytes. For example, the SPI data register
(SPDR) holds the byte which is about to be shifted out the MOSI line, and
the data which has just been shifted in the MISO line.

 Status registers change their state based on various microcontroller


conditions. For example, the seventh bit of the SPI status register (SPSR)
gets set to 1 when a value is shifted in or out of the SPI. 21-01-2025 107
EEPROM interface using SPI

21-01-2025 108
EEPROM interface using SPI
 AT25HP512 is a 65,536 byte serial EEPROM.

 It supports SPI modes 0 and 3, runs at up to 10MHz at 5v and can run at


slower speeds down to 1.8v.

 It's memory is organized as 512 pages of 128 bytes each.

 It can only be written 128 bytes at a time, but it can be read 1-128 bytes
at a time.

 The device also offers various degrees of write protection and a hold pin

21-01-2025 109
EEPROM interface using SPI

21-01-2025 110
EEPROM interface using SPI
 The device is enabled by pulling the Chip Select (CS) pin low.

 Instructions are sent as 8 bit operational codes (opcodes) and are shifted
in on the rising edge of the data clock.

 It takes the EEPROM about 10 milliseconds to write a page (128 bytes) of


data, so a 10ms pause should follow each EEPROM write routine.

21-01-2025 111
EEPROM interface using SPI
 Connect EEPROM pins 3, 7 and 8 to 5v and pin 4 to ground.

 Connect EEPROM pin 1 to Arduino pin 10 (Slave Select - SS), EEPROM pin
2 to Arduino pin 12 (Master In Slave Out - MISO), EEPROM pin 5 to
Arduino pin 11 (Master Out Slave In - MOSI), and EEPROM pin 6 to
Arduino pin 13 (Serial Clock - SCK).

21-01-2025 112
EEPROM interface using SPI
 The first step is setting up our pre-processor directives.
 We define the pins we will be using for our SPI connection, DATAOUT,
DATAIN, SPICLOCK and SLAVESELECT. Then we define our opcodes for the
EEPROM.

21-01-2025 113
EEPROM interface using SPI
 char buffer [128] this is a 128 byte array we will be using to store the data
for the EEPROM write:

21-01-2025 114

MODULE - 4
EEPROM interface using SPI
 First we initialize our serial connection, set our input and output pin
modes and set the SLAVESELECT line high to start.
 This deselects the device and avoids any false transmission messages
due to line noise:

21-01-2025 115
EEPROM interface using SPI
 Now we set the SPI Control register (SPCR) to the binary value 01010000.
In the control register each bit sets a different functionality.

 The eighth bit disables the SPI interrupt, the seventh bit enables the SPI,
the sixth bit chooses transmission with the most significant bit going first,
the fifth bit puts the Arduino in Master mode, the fourth bit sets the data
clock idle when it is low, the third bit sets the SPI to sample data on the
rising edge of the data clock, and the second and first bits set the speed
of the SPI to system speed / 4 (the fastest).

 After setting our control register up we read the SPI status register (SPSR)
and data register (SPDR) in to the junk clr variable to clear out any
spurious data from past runs: 21-01-2025 116
EEPROM interface using SPI

21-01-2025 117
EEPROM interface using SPI
 The EEPROM MUST be write enabled before every write instruction.
 To send the instruction we pull the SLAVESELECT line low, enabling the
device, and then send the instruction using the spi_transfer function.
 Note that we use the WREN opcode we defined at the beginning of the
program. Finally we pull the SLAVESELECT line high again to release it:

21-01-2025 118
EEPROM interface using SPI
 Now we pull the SLAVESELECT line low to select the device again after a
brief delay.
 We send a WRITE instruction to tell the EEPROM we will be sending data to
record into memory.
 We send the 16 bit address to begin writing at in two bytes, Most
Significant Bit first.
 Next we send our 128 bytes of data from our buffer array, one byte after
another without pause.
 Finally we set the SLAVESELECT pin high to release the device and pause
to allow the EEPROM to write the data:

21-01-2025 119
EEPROM interface using SPI

21-01-2025 120
EEPROM interface using SPI
 In the main loop we just read one byte at a time from the EEPROM and print it out the serial port.
We add a line feed and a pause for readability.

 Each time through the loop we increment the EEPROM address to read.

 When the address increments to 128 we turn it back to 0 because we have only filled 128
addresses in the EEPROM with data.

21-01-2025 121
EEPROM interface using SPI
 The spi_transfer function loads the output data into the data transmission register, thus starting the
SPI transmission.

 It polls a bit to the SPI Status register (SPSR) to detect when the transmission is complete using a bit
mask, SPIF.
 It then returns any data that has been shifted in to the data register by the EEPROM.

21-01-2025 122
EEPROM interface using SPI
 The read_eeprom function allows us to read data back out of the EEPROM.

 First we set the SLAVESELECT line low to enable the device.

 Then we transmit a READ instruction, followed by the 16-bit address we wish to read from, Most
Significant Bit first.

 Next we send a dummy byte to the EEPROM for the purpose of shifting the data out.

 Finally we pull the SLAVESELECT line high again to release the device after reading one byte, and
return the data.

21-01-2025 123
EEPROM interface using SPI

21-01-2025 124
Encoders & Decoders
ENCODER

21-01-2025 125
Encoders & Decoders
DECODER

21-01-2025 126
Encoders & Decoders
WORKING OF ENCODER

21-01-2025 127
Encoders & Decoders
LOGICS AND TRUTH TABLE

21-01-2025 128
Encoders & Decoders
TYPES OF ENCODERS

21-01-2025 129
Encoders & Decoders
DECODERS

21-01-2025 130
Encoders & Decoders
LOGICS AND TRUTH TABLE

21-01-2025 131
Encoders & Decoders
3 TO 8 DECODER

21-01-2025 132
Encoders & Decoders
APPLICATIONS

21-01-2025 133
Watch Dog Timers
WATCH DOG TIMERS

21-01-2025 134
Watch Dog Timers
WATCH DOG TIMERS

21-01-2025 135
Watch Dog Timers
WATCH DOG TIMERS

21-01-2025 136
Watch Dog Timers
WATCH DOG TIMERS

21-01-2025 137
•THANKS
21-01-2025 138

You might also like