MM_Lab Manual_LDCE_EC_SEM_IV_2025
MM_Lab Manual_LDCE_EC_SEM_IV_2025
CERTIFICATE
This is to certify that Mr/Miss
2024 to April-2025.
PSO 3 Ability to use modern tools to produce environment friendly ideas and products
Sr.
PEO PEO statement
No.
To make students capable for the post-graduate courses, research, successful
1 PEO 1 career in industries or entrepreneurship in the field of electronics and
communication engineering.
2 PEO 2 To design, draft, erect and maintain electronics production.
To acquire communication skills to effectively communicate problems and
3 PEO 3 solutions of electronics and communication engineering individually as well as
in a team.
To be engaged in lifelong learning and adapt changing technological and
4 PEO 4
social needs.
To be ethically and professionally sensitive to the consequence of work for
5 PEO 5
promising career.
Course outcomes:
Objectives:
To install Arduino open source software
To learn Arduino UNO and nano open source hardware
To learn programming of Arduino UNO and Nano boards
GND (3): Three GND (Ground) terminals are available to connect ground to
external circuit.
5V (4) & 3.3V (5): The 5V pin supplies 5 volt and the 3.3V pin supplies 3.3
volt of power. External circuit which requires 5V or 3.3 V DC can be
connected at these pins.
Analog (6): Six Analog pins are available which is labelled as A0 through A5.
These pins can read the signal from an analog sensor (like a temperature
sensor, light sensor, weight sensor (strain gauge) and convert it into a digital
value which can be displayed on LCD. We can process this analog signal and
take decision based on value of it.
Digital I/O: Digital input/output pins (0 through 13 on the UNO) are
available. These pins can be used for both digital input (like telling if a
button is pushed) and digital output (like powering an LED).
This pin mapping is useful if students want to write program using ATMEL
Studio or other tools and want to program Arduino board.
// the loop function runs over and over again forever just like while(1)
void loop()
{
digitalWrite(13, HIGH); // turn the LED on connected at PB5
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off
delay(1000); // wait for a second
}
void setup()
{
DDRB=DDRB | (1<<5); //Set pin PB5 as output pin (LED is connected)
}
void loop()
{
PORTB=PORTB | (1<<5); //Set PB5 pin
delay(100);
PORTB=PORTB & ~(1<<5); //Clear PB5 pin
delay(100);
}
To start with we can use Arduino UNO or Arduino NANO board. Pin diagram
of Arduino NANO is given below.
:: WORKSHEET::
Activity #1:
Write instructions to initialize PB0, PD0 and PD1 pins as output pins. Write
program to blink LEDs connected on these pins.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #2:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #3:
Write program to blink Arduino LED (Connected at pin 13, PB5 pin) in
increasing and decreasing rate continuously. Rate should increase from 1
second to 5 second and then rate should decrease from 5 second to 1
second. Monotonically increasing and decreasing rate. Write program either
using Arduino instructions or C language(Hint: Use For loop or While loop
for this purpose)
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
We will need male/female wires to connect I/O board with Arduino card.
void setup() {
void loop() {
if(PINB & (1<<SW))
{
PORTB=PORTB & ~(1<<LED);
}
else
{
PORTB=PORTB | (1<<LED);
}
}
:: WORKSHEET::
Activity #1: Connect 4 LEDs of DAIO module with Arduino digital pins 2 to
5. Write following program, compile, upload and execute the program and
observe LEDs.
void setup()
{
DDRD = 0x3C; //Pins PD2 to PD5 output pins
}
void loop() {
PORTD=0x3C;
delay(1000);
PORTD=0x00;
delay(1000);
}
_____________________________________________________________________
_____________________________________________________________________
____________________________________________________________________
Explain program:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
____________________________________________________________________
Activity #2: Modify program given in activity 1 such that all 4 LED becomes
ON one by one after 1 second and then turn OFF one by one. Write program
again. Compile and execute this program in Arduino board.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Microprocessor & Microcontroller (3141008) Page 22
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #3: Connect +Vcc and Gnd of Arduino board with DAIO module.
Digital pin 2 with Buzzer pin of DAIO module. Write program to sound
buzzer for 1 second and OFF for 2 second. Repeat this five times.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #4: Modify program given in activity 3 such that when SW1 of
DAIO module is pressed, Buzzer should sound otherwise it should remain
OFF. Connect Buzzer of DAIO module with Digital pin 7.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #5: Prepare OR gate with SW1 and SW2. Write program such that
when SW1 or SW2 or both are pressed, internal LED of Arduino board
should glow. Connected SW1 with pin 6 and SW2 with pin 7 of Arduino
board. Write program in following space.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
AIM: Generate PWM waveform and change intensity of LED connected with
Arduino board
Objectives:
To generate PWM waveform
Know importance of PWM for light dimmer and speed control
Program 1:
/*
Change Intensity of LED by PWM
Two LEDs of General purpose I/O board is connected to Pin 9 and
10.
Brightness of one LED will increase while other will decrease and
vice-varsa */
{
pinMode(LED1, OUTPUT); // set pin 9 as output.
pinMode(LED2, OUTPUT); // set pin 10 as output.
}
void loop()
{
for (i=minBrightness; i<=maxBrightness; i=i+1)
{
analogWrite(LED1,i);
analogWrite(LED2,255-i);
delay(10);
}
delay(1000);
}
void loop()
{
sws1=digitalRead(SW1);
sws2=digitalRead(SW2);
if(sws1==0)
{
analogWrite(LED,200);
}
delay(100);
if(sws2==0)
{
analogWrite(LED,100);
}
delay(100);
}
Observe waveforms on Pin 9 for by pressing SW1 and SW2 one by one by
one and then draw waveforms.
Waveform when SW1 is pressed:
:: WORKSHEET::
Activity #1: Connect three color LED with pins 9,10 and 11. Write program
to produce 8 different colors. Write your program in following space. (If three
color LED not available then connect three separate LEDs and produce eight
different binary patterns from 000 to 111)
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
ON ON ON WHITE
ON ON OFF YELLOW
ON OFF ON MAGENTA
OFF ON ON CYAN
Objectives:
To measure temperature using LM35
Understand specifications of LM35
Transmit values using serial communication
Specifications of LM35:
void loop() {
int sum = 0; // Variable to store sum of temperature readings
float temperature;
for (int i = 0; i<100; i++)
{
sum = sum + (long)(analogRead(sensorValue));// read one value and add
it to var sum.
delay(10);// wait for some time before taking next sample.
}
// sum now contains total of 100 readings. Divide it by 100 to get average
value.
sum = sum /100L;
temperature = (((float)(sum) )* 5000.0 /1023.00 )/10.0; // convert int temp
into voltage in mV. Then divide it by 10 to get temperature in degree Celcius.
if(temperature>30)
digitalWrite(alarm, HIGH);
else
digitalWrite(alarm, LOW);
// print temperature value to the serial monitor:
Serial.println(temperature);
delay(1000); // Take reading at every 1 second
}
void loop() {
// put your main code here, to run repeatedly:
int SWSTATUS;
SWSTATUS=digitalRead(SW);
Serial.println(SWSTATUS);
if(SWSTATUS==1)
{
Serial.println("DOOR IS OPEN");
digitalWrite(LED,HIGH);
}
else if(SWSTATUS==0)
{
Serial.println("DOOR IS CLOSE");
digitalWrite(LED,LOW);
}
delay(100);
Serial.flush();
}
#include <SPI.h>
#include <SD.h>
/*
Analog input, analog output, serial output
Connect Analog Input at pin 0
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(alarm,OUTPUT);
myFile = SD.open("d:/tempdata.txt", FILE_WRITE);
}
void loop() {
int count=0;
while(count<100)
{
int sum = 0; // Variable to store sum of temperature readings
float temperature;
for (int i = 0; i<100; i++)
{
sum = sum + (long)(analogRead(sensorValue
delay(10);// wait for some time before taking next sample.
}
sum = sum /100L;
temperature = (((float)(sum) )* 5000.0 /1023.00 )/10.0;
if(temperature>30)
digitalWrite(alarm, HIGH);
else
digitalWrite(alarm, LOW);
// print the results to the serial monitor:
Serial.print("TEMPERATURE = " );
Serial.println(temperature);
myFile.write(temperature); //Write temperature data in file
delay(1000);
count++;
}
myFile.close();
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
AIM: Write and execute Arduino program to display message on LCD &
GLCD
Objectives:
Understand interfacing of LCD & GLCD with Arduino UNO Board.
Write Arduino program to display message on LCD & GLCD
Write Arduino program to display counter on LCD & GLCD
Pin connections:
LCD ARDUINO
3 Contrast (GND)
5 Read/write (GND)
6 Enable 10 Digital 10
7 D0 (Keep open)
8 D1 (Keep open)
9 D2 (Keep open)
10 D3 (Keep open)
11 D4 5 Digital 5
12 D5 4 Digital 4
13 D6 3 Digital 3
14 D7 2 Digital 2
Pin connections:
Program:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("GEC RAJKOT!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
int AnalogPin = 0;
int POTValue1 = 0;
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);// LCD in 4 bit mode
void setup() {
lcd.begin(16, 2);
lcd.print("EC Department MI LAB");
for(i=0;i<10;i++)
{
lcd.scrollDisplayLeft();
delay(500);
}
lcd.clear();
delay(1000);
}
void loop() {
lcd.setCursor(0,1);
lcd.print("Value:");
lcd.setCursor(6, 1);
POTValue= analogRead(AnalogPin));
lcd.print(POTValue);
delay(100);
}
Observation:
_____________________________________________________________________
_____________________________________________________________________
____________________________________________________________________
Explain program:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
___________________________________________________________________
AIM: Sense temperature using LM35 sensor and display temperature value
on LCD
Objectives:
Write program to read temperature using LM35 and display it on LCD
for Arduino.
Connections:
Connect LCD as per experiment No. 5
Connect Temperature sensor LM35 output of DAIO board to A0 pin of
Arduino
Program:
#include <LiquidCrystal.h>
int sensorPin1 = 0; // select the input pin for the potentiometer
int count,i,count_char=0;
char message[16];
float temperature;
long sum;
const int ledPin = 13;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 5, 4, 3, 2);// LCD in 4 bit mode
void setup() {
// set up the LCD's number of columns and rows:
i=0;
temperature=0;
sum=0;
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
if(temperature>70)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.setCursor(6, 1);
lcd.print(temperature);
lcd.setCursor(13,1);
lcd.print("Deg.C.:");
delay(100);
}
:: WORKSHEET::
Activity #1: In this experiment set up remove LM35 output connection with
Arduino board and connect LDR with A0 pin of Arduino board.
Observation:
Activity #2: Write Arduino program such that LED connected with Pin 13
(On board LED) should turn OFF when light falls on LDR and it should turn
ON in dark condition.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EC Department, L.D. College of Engineering, Ahmedabad Page 39
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #3: Connect LCD Data lines D7 to D4 with pins 2 to 5. RS with pin
11 and Enable with pin 10. (As per earlier experiment) Connect Switch with
pin number 7. Write program to display number on LCD depending on
number of times switch is pressed. It should display “count value”
corresponding to number of times switch is pressed.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Objectives:
To install ATMEL Studio
To learn assembly language instructions for AVR Microcontroller
Program 1: Write and assemble a program to load a value into each location
of R20 – R23. Complement the value in each register. Use the simulator to
single-step and examine the flags and register content after the execution of
each instruction
.INCLUDE "M32DEF.INC"
Note:
LDI Instruction is used to load immediate 8 bit data to general
purpose registers.
COM Instruction is used to complement content of registers
There are 32 general purpose registers in AVR microcontroller known
as R0,R1,R2 …. R31.
LDI instruction cannot be used to load data in registers R0 to R15. It
can be used for registers R16 to R31
Semicolon (;) can be used to write comments (Anything written after ;
will be ignored by compiler). It is necessary for readability of programs
0x sign before the number is used to represent hexadecimal number
i.e. 0xFF is hexadecimal number whose decimal equivalent is 255.
Alternately $ sign can be used to represent hexadecimal number.
.INCLUDE "M32DEF.INC"
Note:
$ sign is used to represent hexadecimal value (Option of 0x )
MOV instruction is used to copy one register value into other …
(Source and destination can be any register R0 to R31)
ADD instruction used to add content of specified operands
SUB instruction used to subtract content of specified operands
RJMP is unconditional relative jump instruction used to create infinite
loop at the end of the program.
.INCLUDE "M32DEF.INC"
LDI R16, 0xFF ; Set Port B as an output Port
OUT DDRB, R16 ;
LDI R16, 0x08 ; Define Stack pointer 0x085F
OUT SPH,R16
LDI R16, 0x5F
OUT SPL,R16
AGAIN:
LDI R16, 0x55 ;Copy data 0x55 to register R16
OUT PORTB, R16 ; Turn alternate LEDs ON
RCALL DELAY
LDI R16, 0xAA ;Copy data 0x55 to register R16
OUT PORTB, R16 ; Make ON LED OFF and OFF LED ON
RCALL DELAY
RJMP AGAIN
DELAY:
LDI R20,0x05
L3: LDI R21,0x01 ;Load 0xFF for hardware
L2: LDI R22,0x01 ;Load 0xFF for hardware
L1: DEC R22
BRNE L1 ; Brach if Z=0 i.e. if register content=0
DEC R21
Note:
DDRB defines direction of PORT B. If we transfer 0x00 to DDRB, port
becomes input port and if we transfer 0xFF to DDRB, it becomes
output port.
We are connecting 8 LEDs with port B so 0xFF is first loaded into R16
and then R16 is copied to DDRB to initialize PORT B as an output
port.
This program uses “DELAY” subroutine
When “DELAY” subroutine is called from Main program using RCALL
(Relative call) instruction, value of Program counter is stored in stack
memory pointed by SP(Stack pointer) at 0x085F
DELAY routine uses three counters using registers R20, R21 and R22,
it decrements these registers until it becomes ZERO. For simulation
use 0x01 for R21 and R22 (Less delay) and for hardware use 0xFF to
get enough observation period for flashing of LEDs
.INCLUDE "M32DEF.INC"
CBI DDRB, 2 ;Set PB2 pin as an input pin
LDI R16, 0xFF ;Set Port C as an output Port
OUT DDRC, R16 ;
SBI DDRD, 3 ; Make PD3 pin output
AGAIN:
SBIS PINB, 2 ; SKIP next instruction (i.e. exit loop) if
PB2 is high (SET)
RJMP AGAIN
LDI R16, 0x45
OUT PORTC, R16
SBI PORTD, 3
CBI PORTD, 3
HERE: RJMP HERE
Note:
CBI instruction used to clear bit immediately CBI DDRB,2 clears PB2
bit to use it input pin (Because switch is connected)
SBI instruction is used to set bit immediately SBI DDRD,3 set pin PD3
to make it output pin (because we want to generate pulse at PD3)
DELAY:
LDI R20,0x05
L3: LDI R21,0xFF
L2: LDI R22,0xFF
L1: DEC R22
BRNE L1
DEC R21
BRNE L2
DEC R20
BRNE L3
RET
:: WORKSHEET::
Activity #1:Write and assemble the following program. Use the simulator
to single-step and examine the flags and register content after the
execution of each instruction.
.INCLUDE "M32DEF.INC"
LDI R20,$27
LDI R21,$15
SUB R20, R21
LDI R20,$20
LDI R21,$15
SUB R20, R21
LDI R24,95
LDI R25,95
SUB R24, R25
LDI R22,50
LDI R23,70
SUB R22, R23
L1: RJMP L1
Activity #2: Examine status of carry (C) flag after execution of following
instructions in single stepping mode.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #6: W.A.P. to convert packed BCD no. to two ASCII no. and place
them in R21 and R22.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
AVRDude:
AVRdude is command line tool to program HEX (Binary) file from PC to AVR
microcontroller.
HFUSE and LFUSE is for AVR fuse bits. Fuse bits are used to choose
features of AVR microcontroller. ATMega32 has two fuse bytes: hfuse and
lfuse. (Refer Chapter 8 of text book for more details)
E PB2
RWPB1
(Through
PCF8591
ADC/DAC)
1
RN1
470
9
8
7
6
5
4
3
2
10
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
PA2/ADC2 PC7/TOSC2
37
PA3/ADC3
36 14
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
# include <avr/io.h>
void mydelay(int count)
{
int i,j;
for(int i=0;i<=count;i++)
{
for(int j=0;j<=100;j++);
}
}
int main(void)
{
DDRD=0xFF; //Port D Output Port
PORTD=0xFF;
EC Department, L.D. College of Engineering, Ahmedabad Page 49
while(1)
{
PORTD=0xFF;
mydelay(1000);
PORTD=0x00;
mydelay(1000);
}
return 0;
}
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
OFF ON PC4/TDO
16 1 40 27
PA0/ADC0 PC5/TDI
15 2 39 28
PA1/ADC1 PC6/TOSC1
14 3 38 29
PA2/ADC2 PC7/TOSC2
13 4 37
PA3/ADC3
12 5 36 14
PA4/ADC4 PD0/RXD
11 6 35 15
PA5/ADC5 PD1/TXD
10 7 34 16
PA6/ADC6 PD2/INT0
9 8 33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
# include <avr/io.h>
void mydelay(int count)
int main(void)
{
DDRD=0xFF; //Port D Output Port
DDRA=0x00; //Port A Input
unsigned char ssdata[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,
0xF8,0x80,0x90,0xC8,0x83,0xC6,0xA1,0x86,
0x8E,0x00};
unsigned char switchdata;
while(1)
{
switchdata=PINA;
switchdata=switchdata&0x0F;
PORTD=ssdata[switchdata];
}
}
# include <avr/io.h>
void mydelay(int count)
{
int i,j;
for(int i=0;i<=count;i++)
{
for(int j=0;j<=100;j++);
}
}
int main(void)
{
unsigned char count i=0;
DDRD=0xFF; //Port D Output Port
DDRA=0x00; //Port A Input
unsigned char ssdata[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,
# include <avr/io.h>
void delay_ms(int count)
{
for(int i=0;i<=count;i++)
{
for(int j=0;j<=1000;j++);
}
}
int main(void)
{
DDRD=0xFF; //Port D Output Port
DDRB=DDRB|(1<<5); //Inbuilt Arduino LED connected to PB5 pin
DDRD=0xFF;
while(1)
{
PORTB=PORTB|(1<<5);
delay_ms(5000);
PORTB=PORTB&~(1<<5);
delay_ms(5000);
}
return 0;
}
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #2:
Modify program given in Activity #1 in such a way that LED should glow one
by one from left to right and than from right to left continuously…
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #3:
Write assembly language program to glow LEDs one by one at the interval of
1 second.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #4: Write program for down-counter from 9 to 0 and display it on
seven segment display.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #5:
Write program to display number 1234 on four seven segment display one
by one at the interval of 1 second.
(Hint: Send seven segment hex code corresponds to 1 on PORTD, make PB0
pin high, after 1 second send seven segment hex code of number 2 on
PORTD and make PB1 high and so on for number 3 (PB2 high) and 4 (PB3
high).
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Read switch positions connected at PORT A pins PA4 to PA7. Display its 1’s
complemented value on seven segment display.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Interfacing diagram:
LCD1
LM016L
VDD
VSS
VEE
RW
RS
D0
D1
D2
D3
D4
D5
D6
D7
E
9 22
RESET PC0/SCL
10
12
13
14
1
2
3
4
5
6
7
8
9
11
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
26
PC4/TDO
40 27
PA0/ADC0 PC5/TDI
39 28
PA1/ADC1 PC6/TOSC1
38 29
PA2/ADC2 PC7/TOSC2
37
PA3/ADC3
36 14
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
C Program:
# include <avr/io.h>
# include <util/delay.h>
#define LCD_DATA PORTD
#define LCD_DDR DDRD
#define LCD_CMD PORTB
#define LCD_CDDR DDRB
#define RS 0
#define RW 1
#define EN 2
C Program:
#define F_CPU 8000000UL /* Define CPU clock Frequency 8MHz */
#include "Font_Header.h"
int main(void)
{
my_GLCD_String(0,"Microprocessor");
while(1);
Activity #2: Write program in C language to display different message on the LCD
depending on position of switches. If SW1 ON: First Floor SW2 ON: Second Floor SW3 ON:
Third Floor and so on….
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Microprocessor & Microcontroller (3141008) Page 66
EXPERIMENT NO. 10
AIM: Write and execute program to rotate stepper motor clockwise and
anticlockwise with half step and full step mode
Objectives:
Understand Interfacing stepper motor with microcontroller
Write program to rotate stepper motor in anticlockwise and clockwise
direction.
Write program to rotate stepper motor at different speeds.
Introduction:
Stepper motor is brushless synchronous motor which runs in discrete
steps. It divides full rotation into discrete steps. Stepper motor is
manufactured with steps per revolution of 12, 24, 72, 144, 180, 200 and
400 which results into step angle of 300, 150, 50, 2.50, 20, 1.80, and 0.90 per
step. Stepper motor can be controlled precisely with or without feedback. It
converts electrical pulses into mechanical rotation in steps.
A
U1
9 22
RESET PC0/SCL
23
PC1/SDA
13 24
XTAL1 PC2/TCK
12 25
XTAL2 PC3/TMS
40
PA0/ADC0
PC4/TDO
PC5/TDI
26
27 D B
39 28
38
PA1/ADC1 PC6/TOSC1
29 U2
PA2/ADC2 PC7/TOSC2
37 10
PA3/ADC3 COM
36
35
PA4/ADC4
PA5/ADC5
PD0/RXD
PD1/TXD
14
15
1
2
1B
2B
1C
2C
18
17 C
34 16 3 16
PA6/ADC6 PD2/INT0 3B 3C
33 17 4 15
PA7/ADC7 PD3/INT1 4B 4C
18 5 14
PD4/OC1B 5B 5C
1 19 6 13
PB0/T0/XCK PD5/OC1A 6B 6C
2 20 7 12
PB1/T1 PD6/ICP1 7B 7C
3 21 8 11
PB2/AIN0/INT2 PD7/OC2 8B 8C
4
PB3/AIN1/OC0
5 ULN2803
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
470 470
PD0 Q5 PD3 Q5
BC557 BC557
470 470
Q1 Q3 Q1 Q3
BC547 BC547 BC547 BC547
PD1 Winding
PD4 Winding
Red Black
Yellow Brown/
470 470 Orange
Q2 Q4 Q2 Q4
BC547 BC547 BC547 BC547
PD2 PD5
IC L293D has inbuilt two H-bridge so it can be used to drive bi-polar stepper
motor.
Arduino Program:
[1] Rotate motor in full step uni-polar mode
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
speed = 500;
#include <avr/io.h>
#include <avr/delay.h>
int main(void)
{
unsigned char stepdata=0x01;
DDRD=0x0F; //PD0-PD3 output to drive stepper motor
while(1)
{
PORTD=stepdata;
_delay_ms(1000);
stepdata<<=1;
if(stepdata==0x10) stepdata=0x01;
}
}
Circuit Diagram:
U1 10
COM
9 22 1 18
RESET PC0/SCL 1B 1C
23 2 17
PC1/SDA 2B 2C
13 24 3 16
XTAL1 PC2/TCK 3B 3C
12 25 4 15
XTAL2 PC3/TMS 4B 4C
26 5 14
PC4/TDO 5B 5C
40 27 6 13
PA0/ADC0 PC5/TDI 6B 6C
39 28 7 12
PA1/ADC1 PC6/TOSC1 7B 7C
38 29 8 11 +88.8
PA2/ADC2 PC7/TOSC2 8B 8C
37
PA3/ADC3
36 14 ULN2803
PA4/ADC4 PD0/RXD
35 15
PA5/ADC5 PD1/TXD
34 16
PA6/ADC6 PD2/INT0
33 17
PA7/ADC7 PD3/INT1
18
PD4/OC1B
1 19
PB0/T0/XCK PD5/OC1A
2 20
PB1/T1 PD6/ICP1
3 21
PB2/AIN0/INT2 PD7/OC2
4
PB3/AIN1/OC0
5 Anti-clockwise Clockwise
PB4/SS
6
PB5/MOSI
7 32
PB6/MISO AREF
8 30
PB7/SCK AVCC
ATMEGA32
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
unsigned char stepdata=0x01;
int main(void)
{
DDRC=0x0F; //PC0-PC3 output to drive stepper motor
PORTD=1<<2; //Pull up of PD2 activated
PORTD=1<<3;
MCUCR=0x02; //Negative Edge Trigerred
GICR=0xE0;
sei(); //SET interrupt flag in SREG
while(1);
}
ISR(INT0_vect)
{
sei();
while(1)
{
PORTC=stepdata;
_delay_ms(200);
stepdata<<=1;
if(stepdata==0x10) stepdata=0x01;
EC Department, L.D. College of Engineering, Ahmedabad Page 71
}
}
ISR(INT1_vect)
{
sei();
while(1)
{
PORTC=stepdata;
_delay_ms(200);
stepdata>>=1;
if(stepdata==0x00) stepdata=0x08;
}
}
int main(void)
{
unsigned char stepdata[]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09,0x00};
unsigned char i=0;
DDRC=0x0F; //PD0-PD3 output to drive stepper motor
while(1)
{
PORTC=stepdata[i];
_delay_ms(1000);
i++;
if(stepdata[i]==0) i=0;
}
}
:: WORKSHEET::
Activity #1:Draw interfacing diagram to drive stepper motor with PC0-PC3 pins of
ATMega 32 microcontroller. Connect switch with INT0 pin. Write and execute C
language programme to rotate stepper motor in full step mode continuously. Speed
of the motor should increase when switch connected with INT0 is pressed.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EC Department, L.D. College of Engineering, Ahmedabad Page 73
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #2:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #3:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
AIM: Write and execute program to rotate DC motor of Robot for forward,
reverse, left and right motion. Simulate program using PROTEUS
Software
Objectives:
Understand Interfacing DC motor with microcontroller
Understand use of DC motor for robot motion control
Write program to rotate DC motor in anticlockwise and clockwise
direction to move robot in forward and reverse direction
Write program to rotate DC motor at different speeds using PWM.
Interfacing Details:
DC Motors are driven by L293D (Two H-bridge driver circuit).
U1
30 97
RESET PF0/ADC0
96
PF1/ADC1
34 95
XTAL1 PF2/ADC2
33 94
XTAL2 PF3/ADC3
93
U2 PF4/ADC4/TCK
8 16 78 92
PA0/AD0 PF5/ADC5/TMS
77 91
PA1/AD1 PF6/ADC6/TDO
3 2 76 90
OUT1 VS VSS IN1 PA2/AD2 PF7/ADC7/TDI
6 7 75
OUT2 IN2 PA3/AD3
+88.8 1 74 51
EN1 PA4/AD4 PG0/WR
73 52
PA5/AD5 PG1/RD
72 70
PA6/AD6 PG2/ALE
9 71 28
EN2 PA7/AD7 PG3/TOSC2
11 10 29
OUT3 IN3 PG4/TOSC1
14 15 19 1
OUT4 GND GND IN4 PB0/SS/PCINT0 PG5/OC0B
20
PB1/SCK/PCINT1
+88.8 21 12
PB2/MOSI/PCINT2 PH0/RXD2
L293D 22 13
PB3/MISO/PCINT3 PH1/TXD2
23 14
PB4/OC2A/PCINT4 PH2/XCK2
24 15
PB5/OC1A/PCINT5 PH3/OC4A
25 16
PB6/OC1B/PCINT6 PH4/OC4B
26 17
PB7/OC0A/OC1C/PCINT7 PH5/OC4C
18
PH6/OC2B
53 27
PC0/A8 PH7/T4
54
PC1/A9
55 63
PC2/A10 PJ0/RXD3/PCINT9
56 64
PC3/A11 PJ1/TXD3/PCINT10
57 65
PC4/A12 PJ2/XCK3/PCINT11
58 66
PC5/A13 PJ3/PCINT12
59 67
PC6/A14 PJ4/PCINT13
60 68
PC7/A15 PJ5/PCINT14
69
PJ6/PCINT15
43 79
PD0/SCL/INT0 PJ7
44
PD1/SDA/INT1
45 89
PD2/RXD1/INT2 PK0/ADC8/PCINT16
46 88
PD3/TXD1/INT3 PK1/ADC9/PCINT17
47 87
PD4/ICP1 PK2/ADC10/PCINT18
48 86
PD5/XCK1 PK3/ADC11/PCINT19
49 85
PD6/T1 PK4/ADC12/PCINT20
50 84
PD7/T0 PK5/ADC13/PCINT21
83
PK6/ADC14/PCINT22
2 82
PE0/RXD0/PCINT8/PDI PK7/ADC15/PCINT23
3
PE1/TXD0/PDO
4 35
PE2/XCK0/AIN0 PL0/ICP4
5 36
PE3/OC3A/AIN1 PL1/ICP5
6 37
PE4/OC3B/INT4 PL2/T5
7 38
PE5/OC3C/INT5 PL3/OC5A
8 39
PE6/T3/INT6 PL4/OC5B
9 40
PE7/ICP3/CLKO/INT7 PL5/OC5C
41
PL6
98 42
AREF PL7
100
AVCC
ATMEGA2560
}
}
(To take right turn, stop right motor and rotate left motor. Similarly to take
left turn, stop left motor and rotate right motor)
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
• Pin1&2 : Enable
• Pin3&4 : Input1
• Pin5&6 : Input2
• Pin7&8 : Direction Switch
• Pin19&20 is GND
Write Arduino program to rotate motor in clockwise direction and
anticlockwise direction. Draw connection diagram and run program.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Servo Motor:
Introduction of I2C:
I2C is two wire serial data and control bus
Implemented with one serial data (SDA) and one clock (SCL) line.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
Introduction of SPI:
SPI is system for serial communication.
There are four wires: one for transmission, second for reception, third
for clock (synchronization) and fourth for selecting device for
communication.
SPI is used in many devices such as ADC, DAC, EEPROM, RTC etc.
PB5 SCK
PB4MISO
PB3MOSI
PB2SS
#include <SPI.h>
Activity #1: Find out any one application of interfacing IO device with Arduino
using SPI protocol. Draw its circuit diagram (Connection diagram) and write code
for it.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Activity #2: Find out any one application of interfacing sensor with Arduino
using I2C protocol. Draw its circuit diagram (Connection diagram) and write code
for it.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EC Department, L.D. College of Engineering, Ahmedabad Page 85
EXPERIMENT NO. 13
AIM: Write Arduino program to receive IR Signal from IR remote and
operate Electrical device based on switch pressed.
Objectives:
Write program to receive IR code transmitted by IR Remote
Understand TSOP 1738 sensor
Understand how to operate electrical device with Arduino board using relay.
Program:
/* This program detects types of IR Remote
* Tuning is required for different remote.
* Assuming that devices are connected at PD4,PD5,PD6 and PD7
* IR Sensor connected at PD2*/
#include <IRremote.h>
const int RECV_PIN = 2;
const int DEVICE1=4;
const int DEVICE2=5;
const int DEVICE3=6;
const int DEVICE4=7;
const int POWERON=8;
int myflag=0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true);
pinMode(DEVICE1,OUTPUT);
pinMode(DEVICE2,OUTPUT);
pinMode(DEVICE3,OUTPUT);
pinMode(DEVICE4,OUTPUT);
pinMode(POWERON,OUTPUT);
digitalWrite(DEVICE1,LOW); /* If common anode then HIGH */
digitalWrite(DEVICE2,LOW);
digitalWrite(DEVICE3,LOW);
digitalWrite(DEVICE4,HIGH);
digitalWrite(POWERON,HIGH);
Serial.println("IR Remote testing Program: ");
}
void loop() {
if (irrecv.decode(&results)) {
if (results.decode_type == NEC) {
Serial.print("NEC: ");
} else if (results.decode_type == SONY) {
Serial.print("SONY: ");
} else if (results.decode_type == RC5) {
Serial.print("RC5: ");
} else if (results.decode_type == RC6) {
Serial.print("RC6: ");
} else if (results.decode_type == UNKNOWN) {
Serial.print("UNKNOWN: ");
}
if(results.value==0x80C)
{
Serial.println("POWER ON BUTTON PRESSED");
digitalWrite(POWERON,LOW);
myflag=~myflag;
if(!myflag)
{
digitalWrite(DEVICE1,HIGH);
digitalWrite(DEVICE2,HIGH);
digitalWrite(DEVICE3,HIGH);
digitalWrite(DEVICE4,LOW);
digitalWrite(POWERON,LOW);
}
}
if(results.value==0x801)
{
Serial.println("1 is pressed");
digitalWrite(DEVICE1,HIGH);
digitalWrite(DEVICE2,LOW);
digitalWrite(DEVICE3,LOW);
}
if(results.value==0x802)
{
Serial.println("2 is pressed");
digitalWrite(DEVICE1,LOW);
digitalWrite(DEVICE2,HIGH);
digitalWrite(DEVICE3,LOW);
}
if(results.value==0x803)
{
Serial.println("3 is pressed");
digitalWrite(DEVICE1,LOW);
digitalWrite(DEVICE2,LOW);
digitalWrite(DEVICE3,HIGH);
}
if(results.value==0x804)
{
Serial.println("4 is pressed");
digitalWrite(DEVICE4,LOW);
}
}
}