0% found this document useful (0 votes)
28 views121 pages

Embedded Systems

The document covers I/O interfacing techniques in embedded systems, focusing on components such as memory interfacing, ADC/DAC converters, timers, and microcontroller programming. It introduces the Arduino Uno and its features, detailing the ATmega328 microcontroller architecture and programming methods for input and output. Additionally, it explains the operation of ADC and DAC, along with practical examples of coding for analog and digital signal processing.

Uploaded by

rongthomas07
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)
28 views121 pages

Embedded Systems

The document covers I/O interfacing techniques in embedded systems, focusing on components such as memory interfacing, ADC/DAC converters, timers, and microcontroller programming. It introduces the Arduino Uno and its features, detailing the ATmega328 microcontroller architecture and programming methods for input and output. Additionally, it explains the operation of ADC and DAC, along with practical examples of coding for analog and digital signal processing.

Uploaded by

rongthomas07
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/ 121

I/O Interfacing Techniques

BCSE305L – EMBEDDED SYSTEMS SYSTEMS


I/O Interfacing Techniques

MODULE 2
I/O INTERFACING TECHNIQUES

Dr. Sridhar Chandrasekaran


Assistant Professor (Senior)
VIT University, Chennai

BCSE305L, Dr. C. Sridhar Page 1


TOPICS IN MODULE 2
MEMORY INTERFACING, A/D, D/A, TIMERS, WATCH-DOG TIMER,
COUNTERS, ENCODER & DECODER, UART, SENSORS AND
ACTUATORS INTERFACING.

Weightage

INTERNALS (PROJECTS WITH 3 REVIEWS) – 3 MEMBERS IN A TEAM


EXTERNAL (2- CAT, AND FAT)
CAT 1 – MODULE 1 & 2, MODULE 3 TILL DATA COMPRESSOR

BCSE305L, Dr. C. Sridhar Page 2


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)
INTRODUCTION TO ARDUINO UNO
Arduino Uno - Board Details
INTRODUCTION TO ARDUINO UNO
Arduino Uno - Pin Details
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
Ø Serial communication : SPI, USART, TWI(I2C)
PROGRAMMING I/P and O/P

qInformation exchange is done by input and output devices

qIt may be digital or analog signal

qDigital signals will be directly interfaced to CPU

qAnalog devices requires some converter for interfacing with CPU

qAll peripherals can not be always on-chip


PROGRAMMING I/P and O/P

qInternal I/O devices


q Physically silicon devices
q Small size
Ex: RTC, ADCs, memory
qExternal I/O devices
q Have large size
q Has bus interface
Ex: LCD, keypad, MIC
PROGRAMMING INPUT & OUTPUT

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


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

Status
Register
CPU Device
Mechanism
Data
Register
PROGRAMMING INPUT & OUTPUT

q Microcontroller programming input and output


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

q Memory mapped I/O –


q Common address shared by both memory and I/O

q Read and write instructions to communicate with the devices

q Example: In 8051 Timer Register - MOV TMOD,#01H


PROGRAMMING INPUT & OUTPUT

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

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

q An ADC is an electronic circuit whose digital output is


proportional to its analog input.

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


output number proportional to its size.

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


and video, medical or climatic variables)

q The ADC when operates within a larger environment, often


called a data acquisition system.
ANALOG TO DIGITAL (ADC) CONVERTER

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

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

q The output binary number D is an integer


q For an n-bit number can take any value from 0 to (2n - 1).
ANALOG TO DIGITAL (ADC) CONVERTER

q ADC has maximum and minimum permissible input values.

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


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

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


4.88 mV.
Resolution and Voltage Levels
VREF

• Maximum resolution will only be


1-bit Resolution
achieved with full-scale input
0
• For inputs with fixed amplitude VREF
• Increase gain before ADC
• Set the correct 𝑉!"# 3-bit Resolution

0 17
ANALOG TO DIGITAL (ADC) CONVERTER

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


analog to digital conversions.

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


(A0 through A5).

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

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

q An input of 5 volts would give the maximum of 1023.


ANALOG TO DIGITAL (ADC) CONVERTER

q ADC value to voltage conversion

q ADC value – 434


𝑽𝒊
434 = ×𝟏𝟎𝟐𝟑
𝟓

𝑽𝒊 = 2.12 V
ANALOG TO DIGITAL (ADC) CONVERTER

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

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

Analog I/O
analogRead(pin)
qReads the value from a specified analog pin with a 10-bit
resolution.
q Works for analog pins (0–5).
q It will return a value between 0 to 1023. analogRead(pin)
q100 microseconds for one reading.
Parameters pin:
q Reading rate 10000 per sec. the number of the analog input pin to read
from (0-5)
q INPUT nor OUTPUT need not be declared.
Returns
int(0 to 1023)
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.
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
}
ANALOG TO DIGITAL (ADC) CONVERTER

CODE
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.
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);
}
ANALOG TO DIGITAL (ADC) CONVERTER

CODE
DIGITAL TO ANALOG (DAC) CONVERTER

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


binary input number into an analog output.

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


output, represented by Vo.
𝑽𝒓
+

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

Control Lines
DIGITAL TO ANALOG (DAC) CONVERTER

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


output.

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

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

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


so the value of Vr as an output is never quite reached.
DIGITAL TO ANALOG (DAC) CONVERTER
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,
q Write an analog output on a digital pin. 0%) and 255 (always on, 100%)

q This is called Pulse Width Modulation. Returns


None
q PWM uses digital signals to control analog devices.

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

q No need to configure pin as an OUTPUT.


ANALOG TO DIGITAL (ADC) CONVERTER
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);
}
ANALOG TO DIGITAL (ADC) CONVERTER
TIMER / COUNTER

q Digital timer/counters are used throughout embedded designs


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

q Most embedded systems have a time component within them


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

q Timers and counters are distinguished from one another


largely by their use, not their logic.

q Timer – Periodic Signal


q Counter – Aperiodic signal
TIMER / COUNTER

q The central timing is derived from a clock input.


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

q The clock may be divided using a simple divider


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

q The divided clock is then passed to a counter


q count-down operation or count-up operation
TIMER / COUNTER

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


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

q The final block is an I/O control block


q It generates interrupts
q Can control the counter based on external signals
TIMER / COUNTER

Timers on the Arduino


q 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()

q Arduino Uno has a 16Mhz internal clock.


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

Control Registers

q Timer/Counter Control Registers A: (TCCRnA)


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

Timer/Counter Modes

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


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

Timers on the Arduino


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

Timers on the Arduino


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

q Time is specified in milliseconds


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

Returns
None
TIMER / COUNTER

Timers on the Arduino


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

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


delayMicroseconds()

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

Returns
None
TIMER / COUNTER

Timers on the Arduino


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

Returns
Number of milliseconds since the program
started (unsigned long)
TIMER / COUNTER

Timers on the Arduino


q micros()
q Returns the value in microseconds.

micros()

Parameters
None

Returns
Number of microseconds since the program
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.
TIMER / COUNTER
#define LED 13
CODE 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());
}}
TIMER / COUNTER
Watchdog Timer

Reset
!

Inter
rupt!
RESET Interrupt
by watch dog
timer (WDT)
Watch Dog Timers
WATCH DOG TIMERS

07/08/24 53
Watch Dog Timers
WATCH DOG TIMERS

07/08/24 54
Watch Dog Timers
WATCH DOG TIMERS

07/08/24 55
ENCODER (4:2)
• The encoder is a device or a transducer or a circuit.
• The encoder will convert the information from one
format to another format i.e like electrical signals to
counters or a PLC.
• The feedback signal of the encoder will determine the
position, count, speed, and direction.
• The control devices are used to send the command to
a particular function.

3-56

BECE102L, Dr. C. Sridhar Page 56


ENCODER (8:3)

BECE102L, Dr. C. Sridhar Page 57


DECODER (2:4)

BECE102L, Dr. C. Sridhar Page 58


DECODER (3:8)

BECE102L, Dr. C. Sridhar Page 59


APPLICATIONS
• Speed synchronization of multiple motors in industries

• War field flying robot with a night vision flying camera

• A robotic vehicle with the metal detector

• RF-based home automation system

• Automatic health monitoring systems

BECE102L, Dr. C. Sridhar Page 60


APPLICATIONS

War- Field -Flying


Robot with a Night
Vision Flying
Camera

BECE102L, Dr. C. Sridhar Page 61


APPLICATIONS

• A robotic vehicle
with the metal
detector

BECE102L, Dr. C. Sridhar Page 62


APPLICATIONS

Speed
Synchronization of
Multiple Motors in
Industries

BECE102L, Dr. C. Sridhar Page 63


BECE102L, Dr. C. Sridhar Page 64
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

INTRODUCTION
q UART stands for Universal Asynchronous Receiver/Transmitter

q It is used to transmit and receive serially

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


Pin

https://www.circuitbasics.com/basics-uart-communication/

BECE102L, Dr. C. Sridhar Page 65


UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

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

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

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

BECE102L, Dr. C. Sridhar Page 66


UART UNIVERSAL ASYNCHRONOUS RECEIVER-TRANSMITTER

• UART operates by transmitting data as


a series of bits, including a start bit,
data bits, an optional parity bit, and
stop bit(s).

• UART sends data serially, one bit at a


time.

• As the name reveals the protocol


operates asynchronous which means
that it doesn't rely on a shared clock
signal. Instead, it uses predefined baud
rates to determine the timing of data
bits.

BECE102L, Dr. C. Sridhar Page 67


UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

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

q A specific frequency is used for data communication known


as baud rate

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


measure of speed of data transfer.

q Both receiving and transmitting UART should work in same


baud rate
BECE102L, Dr. C. Sridhar Page 68
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

https://www.circuitbasics.com/basics-uart-communication/
BECE102L, Dr. C. Sridhar Page 69
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

HOW UART WORKS


q In UART, data is transmitted in parallel from data bus to
transmitting UART

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

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

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


other bits
BECE102L, Dr. C. Sridhar Page 70
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

https://www.circuitbasics.com/basics-uart-communication/
BECE102L, Dr. C. Sridhar Page 71
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

UART Communication https://www.circuitbasics.com/basics-uart-communication/

BECE102L, Dr. C. Sridhar Page 72


UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

HOW UART WORKS

A common asynchronous serial data format


BECE102L, Dr. C. Sridhar Page 73
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

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

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

q Serial is used for communication between the Arduino board


and a computer or other devices.

q 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
cannot also use pins 0 and 1 for digital input or output.
BECE102L, Dr. C. Sridhar Page 74
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:
https://www.arduino.cc/en/pmwiki.php?n=Reference/serial#:~:text=Serial%20is%20used%20for%20communication,with%20the%20computer%20via%20USB.
BECE102L, Dr. C. Sridhar Page 75
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

q 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
}
}

BECE102L, Dr. C. Sridhar Page 76


UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

q 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");
}}}
BECE102L, Dr. C. Sridhar Page 77
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

Arduino SotwareSerial Library


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

Arduino functions for UART communication (SoftwareSerial Library)


SoftwareSerial() – Need to enable serial communication
avaialble() – 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

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


BECE102L, Dr. C. Sridhar Page 79
UNIVERSAL ASYNCHRONOUS RECEIVER/TRANSMITTER

q 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());
}}
BECE102L, Dr. C. Sridhar Page 80
EEPROM interface using SPI
q Arduino interface with an AT25HP512 Atmel serial EEPROM using the Serial
Peripheral Interface (SPI) protocol.
q EEPROM chips such as this are very useful for data storage.
q Note that the chip on the Arduino board contains an internal EEPROM.
EEPROM interface using SPI
q 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.

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


microcontroller) which controls the peripheral devices.

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


q Master In Slave Out (MISO) - The Slave line for sending data to the master,
q Master Out Slave In (MOSI) - The Master line for sending data to the peripherals,
q Serial Clock (SCK) - The clock pulses which synchronize data transmission generated by the
master, and
q 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.
EEPROM interface using SPI
q All SPI settings are determined by the Arduino SPI Control Register (SPCR).

q 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.

q 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.

q 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.
EEPROM interface using SPI
EEPROM interface using SPI
q AT25HP512 is a 65,536 byte serial EEPROM.

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


slower speeds down to 1.8v.

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

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

q The device also offers various degrees of write protection and a hold pin
EEPROM interface using SPI
EEPROM interface using SPI
q The device is enabled by pulling the Chip Select (CS) pin low.

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

q 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.
EEPROM interface using SPI
q Connect EEPROM pins 3, 7 and 8 to 5v and pin 4 to ground.

q 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).
EEPROM interface using SPI
q The first step is setting up our pre-processor directives.
q 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.
EEPROM interface using SPI
q char buffer [128] this is a 128 byte array we will be using to store the data
for the EEPROM write:
EEPROM interface using SPI
q First we initialize our serial connection, set our input and output pin
modes and set the SLAVESELECT line high to start.
q This deselects the device and avoids any false transmission messages
due to line noise:
EEPROM interface using SPI
q Now we set the SPI Control register (SPCR) to the binary value 01010000.
In the control register each bit sets a different functionality.

q 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).

q 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:
EEPROM interface using SPI
EEPROM interface using SPI
q The EEPROM MUST be write enabled before every write instruction.
q To send the instruction we pull the SLAVESELECT line low, enabling the
device, and then send the instruction using the spi_transfer function.
q 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:
EEPROM interface using SPI
q Now we pull the SLAVESELECT line low to select the device again after a
brief delay.
q We send a WRITE instruction to tell the EEPROM we will be sending data to
record into memory.
q We send the 16 bit address to begin writing at in two bytes, Most
Significant Bit first.
q Next we send our 128 bytes of data from our buffer array, one byte after
another without pause.
q Finally we set the SLAVESELECT pin high to release the device and pause
to allow the EEPROM to write the data:
EEPROM interface using SPI
EEPROM interface using SPI
q 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.

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

q When the address increments to 128 we turn it back to 0 because we have only filled 128
addresses in the EEPROM with data.
EEPROM interface using SPI
q The spi_transfer function loads the output data into the data transmission register, thus starting the
SPI transmission.

q It polls a bit to the SPI Status register (SPSR) to detect when the transmission is complete using a bit
mask, SPIF.
q It then returns any data that has been shifted in to the data register by the EEPROM.
EEPROM interface using SPI
q The read_eeprom function allows us to read data back out of the EEPROM.

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

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

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

q Finally we pull the SLAVESELECT line high again to release the device after reading one byte, and
return the data.
EEPROM interface using SPI
SENSOR'S AND ACTUATORS

l Actuators
l DC Motor
l Servo Motor
l Stepper Motor
l Sensors
l Phototransistor
l Reflectance Sensor
l IR Distance Sensor
l Contact Switch
l Bend Sensor
l Other Sensors

BECE102L, Dr. C. Sridhar


1 Page 102
0
SENSOR'S AND ACTUATORS

l Actuators
l DC Motor
l Servo Motor
l Stepper Motor
l Sensors
l Phototransistor
l Reflectance Sensor
l IR Distance Sensor
l Contact Switch
l Bend Sensor
l Other Sensors

BECE102L, Dr. C. Sridhar


1 Page 103
0
SERVO MOTOR

l Servo motors are designed to be easy to use


l DC motor
l Gearing
l Analog shaft encoder
l Control circuitry
l High--current driver

l Three wires: 5V, GND, Control

l Turn from 0 to 180 degrees


l Position determined by pulses on control wire

BECE102L, Dr. C. Sridhar Page 104


SERVO PULSE WIDTH MODULATION

l Control position with 50 Hz (20 ms) pulses

l Pulse width modulation (PWM)

l 1 ms = 0○

l 1.5 ms = 90○

l 2 ms = 180○

BECE102L, Dr. C. Sridhar Page 105


SERVO MOTOR

l 4.0 – 7.2 V Operation

l At 4.8 V
l Speed: 0.12 sec / 60
degrees (83 RPM)
l Stall Torque: 16.7 oz--in

10
BECE102L,
6 Dr. C. Sridhar Page 106
ARDUINO INTERFACING

BECE102L, Dr. C. Sridhar Page 107


STEPPER MOTOR
l Stepper motors are also popular
l Motor advances in discrete steps
l Input pulses indicate when to
advance
l Example: Pololu 1207 Stepper Motor
l 1.8○ steps (200 steps/revolution)
l 280 mA @ 7.4 V
l 9 oz--in holding torque
l Needs H--Bridge driver
l Ground C and D
l Alternate pulses to A and B

BECE102L, Dr. C. Sridhar Page 108


ULTRASONIC SENSOR

• An ultrasonic sensor comprises several


essential components that work
together to measure distances or detect
objects using high-frequency sound
waves.

• At its core is the transducer, typically


made of a piezoelectric crystal, which
both emits and receives ultrasonic
waves

BECE102L, Dr. C. Sridhar Page 109


ULTRASONIC SENSOR
distance = duration * 0.034 / 2;
the speed of sound in air
(approximately 0.034
centimeters per
microsecond)

The receiver converts


the reflected waves
into electrical signals

BECE102L, Dr. C. Sridhar Page 110


ULTRASONIC SENSOR
WITH LCD INTERFACING

BECE102L, Dr. C. Sridhar Page 111


int trig = 11;
int echo = 10;
float timeduration;
float distance;
void setup()
{
Serial.begin(9600); int potvalue;
pinMode(trig, OUTPUT); void setup()
pinMode(echo, INPUT); {
} pinMode(A0, INPUT);
void loop() pinMode(6, OUTPUT);
{ }

digitalWrite(trig, LOW);
delayMicroseconds(2); void loop()
digitalWrite(trig, HIGH); {
delayMicroseconds(10); potvalue=analogRead(A0)/4;
digitalWrite(trig, LOW); analogWrite (6, potvalue);
timeduration = pulseIn(echo, HIGH); }
distance = 0.034 * timeduration / 2;
Serial.print("Distance in cm: ");
Serial.println(distance);
delay(500);
}
Program & output Design

11
Sensor & Actuators

07/08/24 114
Sensor & Actuators

07/08/24 115
PHOTOTRANSISTOR

l Converts light to electrical current

l Vishay BPW77NA NPN Phototransistor


l Dark current: 1 – 100 nA
l Angle of half sensitivity: ±10 ○

jameco.com

BECE102L, Dr. C. Sridhar


vishay.com 18 Page 116
PHOTOTRANSISTOR CIRCUIT

l Leave base terminal unconnected

l Vout = 5 – Iphoto ´ 330 kW


l In dark, Vout ≈ 5 V
l For Iphoto > 15 µA, Vout drops to ~0

l Large resistor gives sensitivity to weak light

BECE102L, Dr. C. Sridhar


11 Page 117
7
OTHER LIGHT SENSORS

l Photodiodes
l Similar to phototransistors
l Lower sensitivity

l Cadmium Sulfide (CDS) Cell


l Resistance changes with light
l From > 1 MW in dark to 200
W in full light
l Slow response time

BECE102L, Dr. C. Sridhar


11 Page 118
8
SENSOR READ CODE
#define PHOTO_TRANS 19

void setup()
{
Saerial.begin(9600);

// configure sensors
pinMode(PHOTO_TRANS,INPUT);
}

void loop()
{
int sensor;
// test sensors
sensor = analogRead(PHOTO_TRANS); // analogRead uses analog port #
Serial.print("Reflectance sensor: ");
Serial.println(sensor);
delay(500);
}
BECE102L, Dr. C. Sridhar
11 Page 119
9
SUPPLIERS
l Engineering Stockroom

l Robu.in

l Hobbyist
l Pegasus Hobbies
l 5515 Moreno St., Montclair, an easy bike ride from campus
l Sparkfun
l Pololu
l Jameco
l All Electronics, Futurlec, Inventables, Goldmine Electronics, …

l Professional
l DigiKey (very wide selection, fewer hobby parts, higher cost)

BECE102L, Dr. C. Sridhar


12 Page 120
0

You might also like