KEMBAR78
Embedded System Lab Manual | PDF | Electric Motor | Microcontroller
0% found this document useful (0 votes)
12 views38 pages

Embedded System Lab Manual

Uploaded by

Aman Raj
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)
12 views38 pages

Embedded System Lab Manual

Uploaded by

Aman Raj
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/ 38

1

Gyan Ganga Institute of Technology and Sciences, Jabalpur

Lab Manual

For

MICROCONTROLLER & EMBEDDED SYSTEM LAB

(EC-606)

Name: …………………………………………………….

Enrollment No. …………………………………………..

Branch: Electronics & Communication Engineering

Semester: 6th
.

LABORATORY REGULATIONS AND SAFETY RULES

The following Regulations and Safety Rules must be observed in all concerned laboratory locations.

1. It is the duty of all concerned parties who use any electrical laboratory to take all reasonable steps to safeguard the HEALTH
and SAFETY of themselves and all other users and visitors.
2. Make sure that all equipment is properly working before using them for laboratory exercises. Any defective equipment must be
reported immediately to the Lab. Instructors or Lab. Technical Staff.
3. Students are allowed to use only the equipment provided in the experiment manual or equipment used for senior project
laboratory.
4. Power supply terminals connected to any circuit are only energized with the presence of the Instructor or Lab. Staff.
5. Students should keep a safe distance from the circuit breakers, electric circuits or any moving parts during the experiment.
6. Avoid any part of your body to be connected to the energized circuit and ground.
7. Switch off the equipment and disconnect the power supplies from the circuit before leaving the laboratory.
8. Observe cleanliness and proper laboratory housekeeping of the equipment and other related accessories.
9. Wear proper clothes and safety gloves or goggles required in working areas that involves fabrications of printed circuit boards,
chemicals process control system, antenna communication equipment and laser facility laboratories.
10. Double check your circuit connections specifically in handling electrical power machines, AC motors and generators before
switching “ON” the power supply.
11. Make sure that the last connection to be made in your circuit is the power supply and first thing to be disconnected is also the
power supply.
12. Equipment should not be removed, transferred to any location without permission from the laboratory staff.
13. Software installation in any computer laboratory is not allowed without the permission from the Laboratory Staff.
14. Computer games are strictly prohibited in the computer laboratory.
15. Students are not allowed to use any equipment without proper orientation and actual hands on equipment operation.
16. Smoking and drinking in the laboratory are not permitted.
3

MICROCONTROLLER & EMBEDDED SYSTEM LAB

(EC-606)

DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING

OUTCOMES OF THE COURSE

OUTCOMES

Upon completion of the lab course, students will be able to:

[
Familiarize with the assembly level and embedded C programming using 8051.

Familiarize with the assembly level programming using 8051.

Familiarize with the Keil µVision-3/4 and IAR Embedded Workbench tools.

Design circuits for various applications using microcontrollers.

Apply the concepts on real- time applications.


LIST OF EXPERIMENTS AS PRESCRIBED IN RGPV SYLLABUS

S.No. NAME OF EXPERIMENT PAGE DATE GRADING SIGN


.
NO.

1. To write an assembly language program to 9


perform the addition of two 16-bit numbers
using 8051.

2. To write an assembly language program to 10


perform the multiplication of two 16-bit
numbers using 8051.

3. To Write an assembly language program 12


to perform logical operations AND, OR,
XOR on two eight bit numbers stored in
internal RAM locations 21h,22h.

4. To Write an assembly language program to 13


generate a square wave using 8051 timer.

5. To conduct an experiment to configure 14


8051 microcontroller to transmit
characters to a PC using the serial port and
display on the serial window.

6. To write a program to transfer data using P1 of 19


8051.

7. To Write a C program to interface DC motor 20

8. To Write a C program to Gas Sensor with ESP- 22


32 to detect gas Leakage.

9. To Write a C program to interface Stepper 24


Motor with 8051 Microcontroller.

LIST OF EXPERIMENTS WITH ENHANCEMENT BY THE INSTITUTE

10. To Write a C program for Traffic light system 29


using 8051 Micro-Controller
ABOUT SOFTWARE/HARDWARE

INTRODUCTION:
Earlier to Microcontrollers, Microprocessors were greatly used for each and every
purpose. Microprocessors were containing ALU, general purpose register, stack pointer,
program counter, clock counter and so many other features which the today’s Microcontroller
also possesses. But the difference between them exists with respect to the number of
instructions, access times, size, reliability, PCB size and so on. Microprocessor contains
large instruction set called as CISC processor whereas Microcontroller contains less
number of instructions and is called as RISC processor. The access time is less in case of
microcontrollers compared to microprocessors and the PCB size reduces in case of
microcontrollers.
There are many versions of microcontrollers 8051, 80528751, AT8951 from Atmel
Corporation and many more. In this manual we will study about the 8051 architecture,
its features, programming and interfacing.
MCS 8051 is an 8-bit single chip microcontroller with many built-in functions and is
the core for all MCS-51 devices.
The main features of the 8051 core are:
 Operates with single Power Supply +5V.
 8-bit CPU optimized for control applications.
 16-bit program counter (PC) and 16-bit data pointer (DPTR).
 8-bit program status word (PSW).
 8-bit stack pointer (SP).
 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).
 128 bytes of On-Chip Data Memory (Internal RAM):
o Four Register Banks, each containing 8 registers (R0 to R7) [Total 32 reg]
o 16-bytes of bit addressable memory.
o 80 bytes of general-purpose data memory (Scratch Pad Area).
 Special Function Registers (SFR) to configure/operate microcontroller.
 32 bit bi-directional I/O Lines (4 ports P0 to P3).
 Two 16-bit timers/counters (T0 and T1).
 Full duplex UART (Universal Asynchronous Receiver/Transmitter).
 On-Chip oscillator and clock circuitry.
Fig.1. General Block Diagram of 8051 Microcontroller Architecture

STEPS TO CREATE AND COMPILE Keil µVision-3/4 PROJECT:

1. Double Click on the icon on the desktop.


2. Close any previous projects that were opened using – Project -> Close.
3. Start Project – New Project, and select the CPU from the device database
(Database-Atmel- AT89C51ED2 or AT89C51RD2 as per the board).On clicking
‘OK’, the following option is displayed. Choose ‘No’.
4. Create a source file (using File->New), type in the assembly or C program and save this
(filename.asm/filename.c) and add this source file to the project using either one of the
following two methods. (i) Project->Manage->Components, Environment Books-
>addfiles-> browse to the required file -> OK
“OR” ii) right click on the Source Group in the Project Window and the Add

Files to Group option.


5. Set the Target options using -> Project – Options for Target opens the
µ Options for Target – Target configuration dialog. Set the Xtal
(Crystal frequency) frequency as 11.0592 MHz, and also the Options for Target
– Debug – use either Simulator / Keil Monitor- 51 driver.

6. If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the
COM Port to which the board is connected and select the baud rate as 19200 or 9600
(recommended). Enable Serial Interrupt option if the user application is not using on-
chip UART, to stop program execution.
7. Build the project; using Project -> Build Project.
application and links. Any errors in the code are indicated by – “Target not
created” in the Build window, along with the error line. Debug the errors. After an error
free, to build go to Debug mode.

8. Now user can enter into Debug mode with Debug- Start / Stop Debug session

dialog. Or by clicking in the icon.


9. The program is run using the Debug-Run command & halted using Debug-Stop Running.

Also the (reset, run, halt) icons can be used. Additional icons are

(step, step over, and step into, run till cursor).


10. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led
status, etc. If it is a part-A program, the appropriate memory window is opened using
View -> memory window (for data RAM & XRAM locations), Watch window (for
timer program), serial window, etc.

11. Note: To access data RAM area type address as D: 0020h. Similarly to access the DPTR region
(XRAM-present on chip in AT89C51ED2) say 9000h location type in X: 09000H.
Experiment no. 1
Aim : To Write an assembly language program to perform the addition of two
16-bit numbers.

Requirement: Keil development tools for the 8051 Microcontroller

mov r0,#88h //lower nibble of No.1


mov r1,#33h //higher nibble of No.1
mov r2,#45ch //lower nibble of No.2
mov r3,#33h //higher nibble of No.2
clr c
mov a,r0
add a,r2
mov 41h,a
mov a,r1
addc a,r3
mov 40h,a
mov 00h,c
end

Result: The result of addition of two 16 bit numbers 3388 H and 3345 H is 66CD,Shown in the
output window on the memory location 0x40 and 0x41.
Experiment no. 2

Aim : To Write an assembly language program to perform the multiplication of


two 16-bit numbers.

Requirement: Keil development tools for the 8051 Microcontroller

Program:

mov r0,#34h // 5678*1234


mov r1,#12h
mov r2,#78h
mov r3,#56h
mov a,r0
mov b,r2
mul ab
mov 33h,a
mov r4,b
mov a,r0
mov b,r3
mul ab
add a,r4
mov r5,a
mov a,b
addc a,#00h
mov r6,a
mov a,r1
mov b,r2
mul ab
add a,r5
mov 32h,a
mov a,b
addc a,r6
mov 00h,c
mov r7,a
mov a,r3
mov b,r1
mul ab
add a,r7
mov 31h,a
mov a,b
addc a,20h
mov 30h,a
end
Result:

Note: Write the logic of the program. Try with some other logic.
Experiment no. 3

Aim: Write an assembly language program to perform logical operations AND,


OR, XOR on two eight bit numbers stored in internal RAM locations 21h,22h

Requirement: Keil development tools for the 8051 Microcontroller

Program:

mov a, 21h //do not use #, as data ram 21h is to be accessed


anl a, 22h //logical and operation
mov 30h, a //and operation result stored in 30h
mov a, 21h
orl a,22h //logical or operation
mov 31h, a //or operation result stored in 31h
mov a,21h
xrl a,22h //logical xor operation
mov 32h,a // xor operation result stored in 32h end

Result:
Before Execution: D: 21H = 22H =

After Execution: D: 030H = 20 //AND operation


D: 031H = 23 //OR operation
D: 032H = 03 //XOR operation
Experiment no.4

Aim To write an assembly language program to generate a square wave using 8051 timer

Requirement: Keil development tools for the 8051 Microcontroller

Theory:

Mode 1 Programming: The following are the characteristics and operations of mode 1: 1. It is a
16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the timer’s register
TL and TH. 2. After TH and TL are loaded with a 16-bit initial value, the timer must be started.
This is done by SETB TR0 for timer 0 and SETB TR1 for timer 1. 3. After the timer is started, it
starts to count up. It counts up until it reaches its limit of FFFFH. When it rolls over from
FFFFH to 0000, it sets high a flag bit called TF (timer flag). Each timer has its own timer flag:
TF0 for timer 0 and TF1 for timer 1. This timer flag can be monitored. When this timer flag is
raised, one option would be to stop the timer with the instructions CLR TR0 or CLR TR1, for
timer 0 and timer 1, respectively. 4. After the timer reaches its limit and rolls over, in order to
repeat the process. TH and TL must be reloaded with the original value, and TF must be
reloaded to 0.

Steps to program in mode 1: To generate a time delay, using timer in mode 1, following are the
steps: 1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used
and which timer mode (0 or 1) is selected. 2. Load registers TL and TH with initial count value.
3. Start the timer. 4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to
see if it is raised. Get out of the loop when TF becomes high. 5. Stop the timer. 6. Clear the TF
flag for the next round. 7. Go back to Step 2 to load TH and TL again.
Program:

Mov Tmod, # 01 ; Timer 0, Mode 1(16-Bit Mode)


Here: Mov Tl0, # 0f2h ; Tl0=F2h, The Low Byte
Mov Th0, # 0ffh ; Th0=Ffh, The High Byte
Cpl P1.5 ; Toggle P1.5
Acall Delay
Sjmp Here

Delay:
Setb Tr0 ; Start The Timer 0
Again : Jnb Tf0, Again ; Monitor Timer Flag 0 Until It Rolls Over
Clr Tr0 ; Stop Timer 0
Clr Tf0 ; Clear Timer 0 Flag
Ret

RESULT: A Square wave is generated using keil compiler under the spectrum analyzer tab.
Generated square wave as shown in the figure.
Experiment no. 5

Aim: To Conduct an experiment to configure 8051 microcontroller to transmit


characters to a PC using the serial port and display on the serial window.

Requirement: Keil development tools for the 8051 Microcontroller

Theory:
The microcontroller MCS51 has an inbuilt UART for carrying out serial communication.
The serial communication is done in the asynchronous mode. A serial port, like other PC
ports, is a physical interface to establish data transfer between a computer and external
hardware or device. This transfer, through a serial port, takes place bit by bit.
Bit Addressable: We can assign the values bit by bit. For example, for a single bit, we
can set weather 1 or 0.
Byte Addressable: We cant assign the values bit by bit. We can set only byte by byte.
First, we will see the SFRs.
Registers used for UART
SCON (Serial Control Register) – Bit Addressable
SBUF (Serial Buffer Register) – Byte Addressable
PCON (Power Control Register) – Byte Addressable
SCON (Serial Control Register)
This register is a bit addressable.
SM0 SM1 SM2 REN TB8 RB8 TI RI

SM0: Serial Port Mode Specifier 1 bit.


SM1: Serial port Mode Specifier 2 bit
These Two bits are used to select the Mode of the UART.
SM0 SM1 Mode Baudrate
Shift Register
0 0 Fosc/12
(Mode 0)
8-bit UART Variable (Set by
0 1
(Mode 1) Timer 1)
9-bit UART
1 0 Fosc/32 or Fosc/64
(Mode 2)
1 1 9-bit UART Variable (Set by
(Mode 3) Timer 1)

SM2: Multiprocessor communications bit. Set/cleared by the program to enable


multiprocessor communications in modes 2 and 3. When set to 1 an interrupt is generated
if bit 9 of the received data is a 1; no interrupt is generated if bit 9 is a 0. If set to 1 for
mode 1, no interrupt will be generated unless a valid stop bit is received. Clear to 0 if
mode 0 is in use.
REN: Receive enable bit. Set to 1 to enable reception; cleared to 0 to disable reception.
TB8: Transmitted bit 8. Set/cleared by the program in modes 2 and 3.
RB8: Received bit 8. Bit 8 of received data in modes 2 and 3; stop bit in mode1. Not
used in mode 0.
TI: Transmit Interrupt flag. Set to one at the end of bit 7 time in mode 0, and at the
beginning of the stop bit for other modes. Must be cleared by the program.
RI: Receive Interrupt flag. Set to one at the end of bit 7 time in mode 0, and halfway
through the stop bit for other moves. Must be cleared by the program.
Explanation
The first four bits (bits 4 through 7) are configuration bits. Bits SM0 and SM1 let us set
the serial mode to a value between 0 and 3, inclusive. The four modes are defined in the
chart immediately above. As you can see, selecting the Serial Mode selects the mode of
operation (8-bit/9-bit, UART or Shift Register) and also determines how the baud rate
will be calculated. In modes 0 and 2 the baud rate is fixed based on the oscillator’s
frequency. In modes 1 and 3 the baud rate is variable based on how often Timer 1
overflows. We’ll talk more about the various Serial Modes in a moment.
The next bit, SM2, is a flag for “Multiprocessor communication.” Generally, whenever a
byte has been received the 8051 will set the “RI” (Receive Interrupt) flag. This lets the
program know that a byte has been received and that it needs to be processed. However,
when SM2 is set the “RI” flag will only be triggered if the 9th bit received was a “1”.
That is to say, if SM2 is set and a byte is received whose 9th bit is clear, the RI flag will
never be set. This can be useful in certain advanced serial applications. For now it is safe
to say that you will almost always want to clear this bit so that the flag is set upon
reception of any character.
The next bit, REN, is “Receiver Enable.” This bit is very straightforward: If you want to
receive data via the serial port, set this bit. You will almost always want to set this bit.
The last four bits (bits 0 through 3) are operational bits. They are used when actually
sending and receiving data. They are not used to configure the serial port.
The TB8 bit is used in modes 2 and 3. In modes 2 and 3, a total of nine data bits are
transmitted. The first 8 data bits are the 8 bits of the main value, and the ninth bit is taken
from TB8. If TB8 is set and a value is written to the serial port, the data’s bits will be
written to the serial line followed by a “set” ninth bit. If TB8 is clear the ninth bit will be
“clear.”The RB8 also operates in modes 2 and 3 and functions essentially the same way
as TB8, but on the reception side. When a byte is received in modes 2 or 3, a total of nine
bits are received. In this case, the first eight bits received are the data of the serial byte
received and the value of the ninth bit received will be placed in RB8.
TI means “Transmit Interrupt.” When a program writes a value to the serial port, a
certain amount of time will pass before the individual bits of the byte are “clocked out”
the serial port. If the program were to write another byte to the serial port before the first
byte was completely output, the data being sent would be garbled. Thus, the 8051 lets the
program know that it has “clocked out” the last byte by setting the TI bit. When the TI bit
is set, the program may assume that the serial port is “free” and ready to send the next
byte.Finally, the RI bit means “Receive Interrupt.” It functions similarly to the “TI” bit,
but it indicates that a byte has been received. That is to say, whenever the 8051 has
received a complete byte it will trigger the RI bit to let the program know that it needs to
read the value quickly, before another byte is read.
SBUF (Serial Buffer Register)
SBUF Register: For a byte of data to be transferred via the TxD line, it must be placed in
the SBUF.
SBUF holds the byte of data when it is received by the MCS51’s RxD line.

Initialize the UART (Configuration)


That’s all about Registers. When using the integrated serial port, obviously configure it.
This lets us tell the 8051 how many data bits we want, the baud rate we will be using, and
how the baud rate will be determined.
Here we are going to use Mode 1. Because that is 8-bit UART and we can generate
Baudrate using Timer 1. If you dont know about the timer please click here for the
reference.
So Mode 1 means we have to give 0x50 value to the SCON Register.
SCON = 0x50;
Generating Baudrate using Timer 1
Once the Serial Port Mode has been configured, as explained above, the program must
configure the serial port’s baud rate. This only applies to Serial Port modes 1 and 3.
The Baud Rate is determined based on the oscillator’s frequency when in mode 0 and 2.
In mode 0, the baud rate is always the oscillator frequency divided by 12. This means if
you’re crystal is 11.0592Mhz, mode 0 baud rate will always be 921,583 baud. In mode 2
the baud rate is always the oscillator frequency divided by 64, so a 11.059Mhz crystal
speed will yield a baud rate of 172,797.
In modes 1 and 3, the baud rate is determined by how frequently timer 1 overflows. The
more frequently timer 1 overflows, the higher the baud rate. There are many ways one
can cause timer 1 to overflow at a rate that determines a baud rate, but the most common
method is to put timer 1 in 8-bit auto-reload mode (timer mode 2) and set a reload value
(TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate.
To determine the value that must be placed in TH1 to generate a given baud rate, we may
use the following equation (assuming PCON.7 is clear).
Calculation
TH1 = 256 - ((Crystal / 384) / Baud)
If PCON.7 is set then the baud rate is effectively doubled, thus the equation becomes:
TH1 = 256 - ((Crystal / 192) / Baud)
For example, if we have an 11.0592Mhz crystal and we want to configure the serial port
to 19,200 baud we try plugging it in the first equation:
TH1 = 256 - ((Crystal / 384) / Baud)
TH1 = 256 - ((11059000 / 384) / 19200 )
TH1 = 256 - ((28,799) / 19200)
TH1 = 256 - 1.5 = 254.5
As you can see, to obtain 19,200 baud on a 11.059Mhz crystal we’d have to set TH1 to
254.5. If we set it to 254 we will have achieved 14,400 baud and if we set it to 255 we
will have achieved 28,800 baud. Thus we’re stuck…
But not quite… to achieve 19,200 baud we simply need to set PCON.7 (SMOD). When
we do this we double the baud rate and utilize the second equation mentioned above.
Thus we have:
TH1 = 256 - ((Crystal / 192) / Baud)
TH1 = 256 - ((11059000 / 192) / 19200)
TH1 = 256 - ((57699) / 19200)
TH1 = 256 - 3 = 253
Here we are able to calculate a nice, even TH1 value. Therefore, to obtain 19,200 baud
with an 11.059MHz crystal we must:
Configure Serial Port mode 1 or 3.
Configure Timer 1 to timer mode 2 (8-bit auto-reload).
Set TH1 to 253 to reflect the correct frequency for 19,200 baud.
Set PCON.7 (SMOD) to double the baud rate.

Program :

# include<reg51.h>

void send(unsigned char *s)

{ while(*s) {

SBUF=*s++;
while(TI==0);

TI=0;

void main()

unsigned int i;

SCON=0x50;

TMOD=0x20;

TH1=TL1=0xfd;

TR1=1;

while(1) {

send("Embetronicx ");

for(i=0; i<=35000; i++);

Result: We have verified the operation of Serial Port by sending a Character on Serial Window.
Experiment no. 6

Aim: To write a program to transfer data using P1 of 8051.

Requirement: Keil development tools for the 8051 Microcontroller

Theory:
Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or output. In
contrast to port 0, this port does not need any pull-up resistors since it already has pull-up
resistors internally. Upon reset, port 1 is configured as an input port. The following code
will continuously send out to port 1 the alternating values 55H and AAH.

Program:

MOV A,#FF H
BACK: MOV P0,A
ACALL DELAY
CPL A
ACALL DELAY
SJMP BACK

DELAY: MOV R3, #23


Here1: MOV R2, #255
Here2: MOV R1, #255
Here: DJNZ R1, Here
DJNZ R2, Here2
DJNZ R3, Here1
RET

Result : Following pattern is displayed on P1 Continuously.


P1 = 11111111 After 1 sec P1 = 00000000
Experiment no.7: Motor Interfacing

Aim: To Write a C program to interface DC motor


Requirement: Keil development tools for the 8051 Microcontroller, Proteus software
Block Diagram:

8 P 3.2
0 (INCR) DC
5 P 3.3 Motor
1 (DECR)

Theory:
DC motors are used in many applications like process control and automation in an
industry, robotics, consumer electronics, office automation equipment like printers and
scanners etc. One can consider the use of a DC motor wherever there is need to control the
motion of an object. Speed control of the motor is important in the applications involving
them. For example, in an audio system, the DC motor that drives the cassette should
always run at a fixed speed. Like wise, there are applications where the speed of
the DC motor has to change according to some defined conditions. The DC motor used in
this interface module is a 12V, 4W motor that can be seen in many electronic equipments. The
circuit to control the speed of the motor follows a general concept and can be applied to DC
motors of higher capacity also.
The pulse width modulation technique is used to vary the speed of the DC
motor. The frequency of the pulses is 120Hz. Keeping the frequency constant, the
width of the pulses is used to change the speed. When the pulse width is minimum, the speed
is minimum and when the width is maximum, the speed is maximum (2400rpm). The
ramp and pedestal technique is used to change the pulse width and thereby the speed.

Result: Proteus Simulator Diagram shows the Interfacing of DC Motor with 8051, the
working operation of dc Motor is tested by simulation. Both the Motor is rotate in clockwise
direction.
Experiment no. 8:
Aim: To Write a C program to interface Gas Sensor to detect LPG Leakage using ESP 32.
Requirement: MQ2 Gas Sensor, ESP- 32,Arduino IDE and Connecting Wires.

Block Diagram:

Theory:

A gas leak detection system is a device or set of devices that detect the presence of gas in a
specific area or location. These systems can use various technologies to detect gas leaks, such as
infrared sensors, ultrasonic sensors, or electrochemical sensors. They are commonly used in
industrial settings, such as oil and gas refineries, chemical plants, and natural gas distribution
systems, as well as in residential and commercial buildings. The goal of a gas leak detection
system is to detect gas leaks as quickly as possible in order to prevent fires, explosions, and other
hazards.
An IoT-enabled gas leak detection system using the ESP32 microcontroller would utilize the
capabilities of the ESP32 to connect to the internet and send data from gas sensors to a remote
location for monitoring. The ESP32 is a low-cost, low-power microcontroller with built-in Wi-Fi
and Bluetooth capabilities, making it a suitable choice for IoT projects. The system could be
programmed to send alerts to a control center or to a specific person’s device in the event of a gas
leak, and can also be set up to automatically shut off gas valves or perform other safety actions.
Additionally, the ESP32‘s ability to connect to other devices and sensors could enable the system
to integrate with other safety systems and devices.

The ESP32 is a low-cost, low-power microcontroller with built-in Wi-Fi and Bluetooth
capabilities. It is a popular choice for IoT projects and is commonly used for a variety of
applications such as home automation, wireless control, and sensor data logging.
The ESP32 features a dual-core processor, a rich set of peripherals, and support for a wide range
of protocols. It can be programmed using the Arduino IDE and various other programming
languages such as C, C++, and MicroPython.
Additionally, the ESP32 has a wide range of features including:
 A high-performance processor with a clock speed of up to 240 MHz

 Support for various types of wireless connectivity such as Wi-Fi, Bluetooth, and
Bluetooth Low Energy (BLE)

 Multiple communication interfaces such as I2C, SPI, UART, and I2S

 A large number of GPIO pins to connect to external devices and sensors

 A built-in security module for secure communication


The ESP32 is often used in projects where a low-cost, low-power device with Wi-Fi and
Bluetooth capabilities is needed, and it is commonly used with other sensors and devices to build
IoT projects, home automation systems, wireless control systems, and data logging systems.

The MQ-4 sensor is a type of gas sensor that is commonly used to detect methane, natural gas,
and propane. When used with an ESP32 microcontroller, the MQ-4 sensor can be connected to
the ESP32‘s analog input pins and the ESP32 can be programmed to read the sensor’s output and
determine the concentration of gas in the air. The ESP32 can then send this data over the internet
to a remote location for monitoring and analysis.
The system can be designed to send an alert if the gas concentration exceeds a certain threshold,
and can also be set up to automatically shut off gas valves or perform other safety actions.
Additionally, the ESP32‘s ability to connect to other devices and sensors could enable the system
to integrate with other safety systems and devices.

Program :

//int Buzzer = 6; // used for Arduino


//int Gas_analog = A0; // used for Arduino
//int Gas_digital = 7; // used for Arduino

//int Buzzer = D2; // used for ESP8266


//int Gas_analog = A0; // used for ESP8266
//int Gas_digital = D1; // used for ESP8266

int Buzzer = 32; // used for ESP32


int Gas_analog = 4; // used for ESP32
int Gas_digital = 2; // used for ESP32

void setup() {
Serial.begin(115200);
pinMode(Buzzer, OUTPUT);
pinMode(Gas_digital, INPUT);
}

void loop() {
int gassensorAnalog = analogRead(Gas_analog);
int gassensorDigital = digitalRead(Gas_digital);

Serial.print("Gas Sensor: ");


Serial.print(gassensorAnalog);
Serial.print("\t");
Serial.print("Gas Class: ");
Serial.print(gassensorDigital);
Serial.print("\t");
Serial.print("\t");
if (gassensorAnalog > 1000) {
Serial.println("Gas");
digitalWrite (Buzzer, HIGH) ; //send tone
delay(1000);
digitalWrite (Buzzer, LOW) ; //no tone
}
else {
Serial.println("No Gas");
}
delay(100);
}
}

Result: Verified the operation of Gas Leakage detection system


Experiment no. 9:
Aim: To Write a C program to interface Stepper Motor with 8051 Microcontroller.
Requirement: Keil and Proteous Plateform .

Theory:

Stepper motor is brushless DC motor, which can be rotated in small angles, these angles are
called steps. Generally stepper motor use 200 steps to complete 360 degree rotation, means its
rotate 1.8 degree per step. Stepper motor used in many devices which needs precise rotational
movement like robots, antennas, hard drives etc. We can rotate stepper motor to any particular
angle by giving it proper instructions.

Stepper motors are basically two types: Unipolar and Bipolar. Unipolar stepper motor
generally has five or six wire, in which four wires are one end of four stator coils, and other end
of the all four coils is tied together which represents fifth wire, this is called common wire
(common point). Generally there are two common wire, formed by connecting one end of the
two-two coils as shown in below figure. Unipolar stepper motor is very common and popular
because of its ease of use.

In Bipolar stepper motor there is just four wires coming out from two sets of coils, means there
are no common wire.

Stepper motor is made up of a stator and a rotator. Stator represents the four electromagnet coils
which remain stationary around the rotator, and rotator represents permanent magnet which
rotates. Whenever the coils energised by applying the current, the electromagnetic field is created,
resulting the rotation of rotator (permanent magnet). Coils should be energised in a particular
sequence to make the rotator rotate. On the basis of this “sequence” we can divide the working
method of Unipolar stepper motor in three modes: Wave drive mode, full step drive mode and
half step drive mode.

Wave drive mode: In this mode one coil is energised at a time, all four coil are energised one
after another. It produces less torque in compare with Full step drive mode but power
consumption is less. Following is the table for producing this mode using microcontroller, means
we need to give Logic 1 to the coils in the sequential manner.

Steps A B C D

1 1 0 0 0

2 0 1 0 0

3 0 0 1 0

4 0 0 0 1

Full Drive mode: In this, two coil are energised at the same time producing high torque. Power
consumption is higher. We need to give Logic 1 to two coils at the same time, then to the next
two coils and so on.
Steps A B C D

1 1 1 0 0

2 0 1 1 0

3 0 0 1 1

4 1 0 0 1

Half Drive mode: In this mode one and two coils are energised alternatively, means firstly one
coil is energised then two coils are energised then again one coil is energised then again two, and
so on. This is combination of full and wave drive mode, and used to increase the angular rotation
of the motor.

Steps A B C D

1 1 0 0 0

2 1 1 0 0

3 0 1 0 0

4 0 1 1 0

5 0 0 1 0

6 0 0 1 1

7 0 0 0 1

8 1 0 0 1

Interfacing Stepper Motor with 8051 Microcontroller


Interfacing with 8051 is very easy we just need to give the 0 and 1 to the four wires of stepper
motor according to the above tables depending on which mode we want to run the stepper motor.
And rest two wires should be connected to a proper 12v supply (depending on the stepper motor).
Here we have used the unipolar stepper motor. We have connected four ends of the coils to the
first four pins of port 2 of 8051 through the ULN2003A.
8051 doesn’t provide enough current to drive the coils so we need to use a current driver IC
that is ULN2003A. ULN2003A is the array of seven NPN Darlington transistor pairs. Darlington
pair is constructed by connecting two bipolar transistors to achieve high current amplification. In
ULN2003A, 7 pins are input pins and 7 pins are output pins, two pins are for Vcc (power supply)
and Ground. Here we are using four input and four output pins. We can also use L293D IC in
place of ULN2003A for current amplification.

You need to find out four coil wires and two common wires very carefully otherwise motor will
not rotate. You can find it out by measuring resistance using multimeter, multimeter won’t show
any readings between the wires of two phases. Common wire and the other two wire in the same
phase should show the same resistance, and the two end points of the two coils in the same phase
will show the twice resistance in compared with resistance between common point and one end
point.

Troubleshooting
If your motor is not rotating OR vibrating but not rotating, then you must check the following
checklist:

1. First check the circuit connections and code.

2. If the circuit and code is ok, then check that the stepper motor gets proper supply voltage
(generally 12v), otherwise it just vibrate but not rotate.

3. If supply is fine, then check the four coil end points which in connected to ULN2003A.
First find the two common end points and connect them to 12v, then connect the
remaining four wires to ULN2003A and try every possible combination until motor get
started. If you wouldn’t connect them in proper order then the motor just vibrate instead
of rotating.
Here is the code for Wave step mode and full wave step mode, you can easily calculate the value
for PORT P2 for the half wave mode.
Code:

/ Wave drive Mode

#include<reg51.h>

void msdelay(unsigned int time)

unsigned i,j ;

for(i=0;i<time;i++)

for(j=0;j<1275;j++);

void main()

while(1)

P2=0x01; // 0001 P2_0=1,P2_1=0,P2_2=0,P2_3=0

msdelay(1);

P2=0x02; //0010

msdelay(1);

P2=0x04; //0100

msdelay(1);

P2=0x08; //1000

msdelay(1);

// Full drive Mode

#include<reg51.h>

void msdelay(unsigned int time)

unsigned i,j ;

for(i=0;i<time;i++)
for(j=0;j<1275;j++);

void main()

while(1)

P2 = 0x03; //0011 P2_0=1,P2_1=1,P2_2=0,P2_3=0

msdelay(1);

P2 = 0x06; //0110

msdelay(1);

P2 = 0x0C; //1100

msdelay(1);

P2 = 0x09; //1001

msdelay(1);
}

Result: : Proteus Simulator Diagram shows the Interfacing of Stepper Motor with 8051
Microcontroller. Working Operation of Stepper Motor are get verified using different drive
modes.
Experiment no.10

Aim: To Write a C program for Traffic light system using 8051 Micro-Controller
Requirement: Keil development tools for the 8051 Microcontroller, Proteus software

Traffic Light Control

Traffic lights, which may also be known as stoplights, traffic lamps, traffic signals, signal lights,
robots or semaphore, are signaling devices positioned at road intersections, pedestrian crossings
and other locations to control competing flows of traffic.About the colors of Traffic Light Control
Traffic lights alternate the right of way of road users by displaying lights of a standard color (red,
yellow/amber, and green), using a universal color code (and a precise sequence to enable
comprehension by those who are color blind).In the typical sequence of colored lights:
☞Illumination of the green light allows traffic to proceed in the direction denoted,

☞Illumination of the yellow/amber light denoting, if safe to do so, prepare to stop short of the
intersection, and

☞Illumination of the red signal prohibits any traffic from proceeding.

Usually, the red light contains some orange in its hue, and the green light contains some blue, for
the benefit of people with red-green color blindness, and “green” lights in many areas are in fact
blue lenses on a yellow light (which together appear green).

Interfacing Traffic Light with 8051

The Traffic light controller section consists of 12 Nos. point LEDS are arranged by 4Lanes in
8051 Development Board. Each lane has Go (Green), Listen (Yellow) and Stop (Red) LED is
being placed.

Algorithm:
• In this circuit , used three traffic light. signal 1(P2.0 to P2.2) and signal 2
(P2.3 to P2.5) are connected to port 2 and signal 3(P3.0 to P3.2) is connected
to port 3.Traffic light gets turned on as per the sequence provided in the
program.
C Program to Traffic Light using 8051

#include<reg51.h>

//signal 1
sbit tl1r = P2^0; // red
sbit tl1o = P2^1; // orange
sbit tl1g = P2^2; // green

//signal 2
sbit tl2r = P2^3; // red
sbit tl2o = P2^4; // orange
sbit tl2g = P2^5; // green

//signal 3
sbit tl3r = P3^0; // red
sbit tl3o = P3^1; // orange
sbit tl3g = P3^2; // green

void delay(int t);


void trafficlight(void);
void main() // main program
{
P2=0x00; // turned off the lights
P3=0x00; // turned off the lights

while(1)
{
trafficlight();
}

}
void delay(unsigned long int t) // delay routine
{
while(t>0)
{
unsigned long int i;
for(i=1;i<10*1275;i++);
t--;
}
}
void trafficlight(void) // traffic light system program
{
P2= 0x11; // traffic signal control data
P3= 0x04;
/*
tl1r=1; // signal 1
tl1o=0;
tl1g=0;

tl2r=0; // signal 2
tl2o=1;
tl2g=0;
tl3r=0; // signal 3
tl3o=0;
tl3g=1;

*/
delay(100); // delay
P2= 0x0c; // traffic signal control data
P3= 0x02;
/*
tl1r=0; // signal 1
tl1o=0;
tl1g=1;

tl2r=1; // signal 2
tl2o=0;
tl2g=0;

tl3r=0; // signal 3
tl3o=1;
tl3g=0;
*/
delay(100); // delay
P2= 0x22; // traffic signal control data
P3= 0x01;
/*
tl1r=0; // signal 1
tl1o=1;
tl1g=0;

tl2r=0; // signal 2
tl2o=0;
tl2g=1;

tl3r=1; // signal 3
tl3o=0;
tl3g=0;
*/
delay(100); // delay
}
Result: : Proteus Simulator Diagram shows the Interfacing of traffic signal with 8051
.Operation of different Signals are get verified using different time-delays.
Viva Questions

1. What do you mean by Embedded System? Give examples.


2. Why are embedded Systems useful?
3. What are the segments of Embedded System?
4. What is Embedded Controller?
5. What is Microcontroller?
6. List out the differences between Microcontroller and Microprocessor.
7. How are Microcontrollers more suitable than Microprocessor for Real Time
Applications?
8. What are the General Features of Microcontroller?
9. Explain briefly the classification of Microcontroller.
10. Explain briefly the Embedded Tools.
11. Explain the general features of 8051 Microcontroller.
12. How many pin the 8051 has?
13. Differentiate between Program Memory and Data Memory.
14. What is the size of the Program and Data memory?
15. Write a note on internal RAM. What is the necessity of register banks? Explain.
16. How many address lines are required to address 4K of memory? Show the
necessary calculations.
17. What is the function of accumulator?
18. What are SFR’s? Explain briefly.
19. What is the program counter? What is its use?
20. What is the size of the PC?
21. What is a stack pointer (SP)?
22. What is the size of SP?
23. What is the PSW? And briefly describe the function of its fields.
24. What is the difference between PC and DPTR?
25. What is the difference between PC and SP?
26. What is ALE? Explain the functions of the ALE in 8051.
27. Describe the 8051 oscillator and clock.
28. What are the disadvantages of the ceramic resonator?
29. What is the function of the capacitors in the oscillator circuit?
30. Show with an example, how the time taken to execute an instruction can be
calculated.
31. What is the Data Pointer register? What is its use in the 8051?
32. Explain how the 8051 implement the Harvard Architecture?
33. Explain briefly the difference between the Von Neumann and the Harvard
Architecture.
34. Describe in detail how the register banks are organized.
35. What are the bit addressable registers and what is the need?
36. What is the need for the general purpose RAM area?
37. Write a note on the Stack and the Stack Pointer.
38. Why should the stack be placed high in internal RAM?
39. Explain briefly how internal and external ROM gets accessed.
40. What are the different addressing modes supported by 8051 Microcontroller ?
41. Explain the Immediate Addressing Mode.
42. Explain the Register Addressing Mode.
43. Explain the Direct Addressing Mode.
44. Explain the Indirect Addressing Mode.
45. Explain the Code Addressing Mode.
46. Explain in detail the Functional Classification of 8051 Instruction set
47. What are the instructions used to operate stack?
48. What are Accumulator specific transfer instructions?
49. What is the difference between INC and ADD instructions?
50. What is the difference between DEC and SUBB instructions?
51. What is the use of OV flag in MUL and DIV instructions?
52. What are single and two operand instructions?
53. Explain Unconditional and Conditional JMP and CALL instructions.
54. Explain the different types of RETURN instructions.
55. What is a software delay?
56. What are the factors to be considered while deciding a software delay?
57. What is a Machine cycle?
58. What is a State?
59. Explain the need for Hardware Timers and Counters?
60. Give a brief introduction on Timers/Counter.
61. What is the difference between Timer and Counter operation?
62. How many Timers are there in 8051?
63. What are the three functions of Timers?
64. What are the different modes of operation of timer/counter?
65. Give a brief introduction on the various Modes.
66. What is the count rate of timer operation?
67. What is the difference between mode 0 and mode 1?
68. What is the difference Modes 0,1,2 and 3?
69. How do you differentiate between Timers and Counters?
70. Explain the function of the TMOD register and its various fields?
71. How do you control the timer/counter operation?
72. What is the function of TF0/TF1 bit
73. Explain the function of the TCON register and its various fields?
74. Explain how the Timer/Counter Interrupts work.
75. Explain how the 8051 counts using Timers and Counters.
76. Explain Counting operation in detail in the 8051.
77. Explain why there is limit to the maximum external frequency that can be counted.
78. What’s the benefit of the auto-reload mode?
79. Write a short note on Serial and Parallel communication and highlight their
advantages and disadvantages.
80. Explain Synchronous Serial Data Communication.
81. Explain Asynchronous Serial Data Communication.
82. Explain Simplex data transmission with examples.
83. Explain Half Duplex data transmission with examples.
84. Explain Full Duplex data transmission with examples.
85. What is Baud rate?
86. What is a Modem?
87. What are the various registers and pins in the 8051 required for Serial
communication? Explain briefly.
88. Explain SCON register and the various fields.
89. Explain serial communication in general (synchronous and asynchronous). Also
explain the use of the parity bit.
90. Explain the function of the PCON register during serial data communication.
91. How the Serial data interrupts are generated?
92. How is data transmitted serially in the 8051? Explain briefly.
93. How is data received serially in the 8051? Explain briefly.
94. What are the various modes of Serial Data Transmission? Explain each mode
briefly.
95. Explain with a timing diagram the shift register mode in the 8051.
96. What is the use of the serial communication mode 0 in the 8051?
97. Explain in detail the Serial Data Mode 1 in the 8051.
98. Explain how the Baud rate is calculated for the Serial Data Mode 1.
99. How is the Baud rate for the Multiprocessor communication Mode calculated?
100. Explain in detail the Multiprocessor communication Mode in the 8051.
101. Explain the significance of the 9th bit in the Multiprocessor communication
Mode.
102. Explain the Serial data mode 3 in the 8051.
103. What are interrupts and how are they useful in Real Time Programming?
104. Briefly describe the Interrupt structure in the 8051.
105. Explain about vectored and non-vectored interrupts in general.
106. What are the five interrupts provided in the 8051?
107. What are the three registers that control and operate the interrupts in 8051?
108. Describe the Interrupt Enable (IE) special function register and its various bits.
109. Describe the Interrupt Priority (IP) special function register and its need.
110. Explain in detail how the Timer Flag interrupts are generated.
111. Explain in detail how the Serial Flag interrupt is generated.
112. Explain in detail how the External Flag interrupts are generated.
113. What happens when a high logic is applied on the Reset pin?
114. Why the Reset interrupt is called a non-maskable interrupt?
115. Why do we require a reset pin?
116. How can you enable/disable some or all the interrupts?
117. Explain how interrupt priorities are set? And how interrupts that occur
simultaneously are handled.
118. What Events can trigger interrupts, and where do they go after getting
triggered?
119. What are the actions taken when an Interrupt Occurs?
110. What are Software generated interrupts and how are they generated?
111. What is RS232 and MAX232?
112. What is the function of RS and E pins in an LCD?
113. What is the use of R/W pin in an LCD?
114. What is the significance of DA instruction?
115. What is packed and unpacked BCD?
116. What is the difference between CY and OV flag?
117. When will the OV flag be set?
118. What is an ASCII code?

END OF LAB MANUAL

You might also like