ESD MODULE-2 Compressed
ESD MODULE-2 Compressed
• 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
21-01-2025 9
PROGRAMMING INPUT & OUTPUT
Status
Register
CPU Device
Mechanism
Data
Register
21-01-2025 10
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
𝑽𝒊
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.
𝑽𝒊 = 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.
EXAMPLE 1
21-01-2025 22
ANALOG TO DIGITAL (ADC) CONVERTER
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
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
+ 𝑫
𝑫 DAC 𝑽𝒐 𝑽𝒐 = × 𝑽𝒓
- 𝟐𝒏
21-01-2025 28
Control Lines
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
21-01-2025 36
TIMER / COUNTER
21-01-2025 38
TIMER / COUNTER
Control Registers
Timer/Counter Modes
Returns 21-01-2025 43
None
TIMER / COUNTER
Parameters
us: the number of microseconds to pause
(unsigned int)
Returns 21-01-2025 44
None
TIMER / COUNTER
Returns
Number of milliseconds since the program
21-01-2025 45
started (unsigned long)
TIMER / COUNTER
micros()
Parameters
None
Returns
Number of microseconds since the program
21-01-2025 46
started (unsigned long)
TIMER / COUNTER
EXAMPLE
21-01-2025 47
TIMER / COUNTER
21-01-2025 49
21-01-2025 50
PULSE WIDTH MODULATION (PWM)
Arduino has six PWM outputs (Pin11, Pin10, Pin9, Pin6, Pin5,
Pin3).
21-01-2025 51
PULSE WIDTH MODULATION (PWM)
The duty cycle is the proportion of time that the pulse is “on” or
“high” and is expressed as a percentage.
𝒑𝒖𝒍𝒔𝒆 𝒐𝒏 𝒕𝒊𝒎𝒆 𝒕𝒐𝒏
𝑫𝒖𝒕𝒚 𝒄𝒚𝒄𝒍𝒆 = × 𝟏𝟎𝟎% = × 𝟏𝟎𝟎%
𝒑𝒖𝒍𝒔𝒆 𝒑𝒆𝒓𝒊𝒐𝒅 𝑻
21-01-2025 53
PULSE WIDTH MODULATION (PWM)
EXAMPLE 1
21-01-2025 56
PULSE WIDTH MODULATION (PWM)
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
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
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
The transmitting UART adds a start bit, stop bit and a parity bit
to parallel data from data bus
21-01-2025 67
https://www.circuitbasics.com/basics-uart-communication/
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER
https://www.circuitbasics.com/basics-uart-communication/
68
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.
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
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.
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.
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.
21-01-2025 108
EEPROM interface using SPI
AT25HP512 is a 65,536 byte serial EEPROM.
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.
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.
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