KEMBAR78
Microcontroller Interfacing Lab Manual Editing On | PDF | Arduino | Microcontroller
0% found this document useful (0 votes)
18 views65 pages

Microcontroller Interfacing Lab Manual Editing On

The document is a laboratory manual for an Embedded Systems and Interfacing Lab, detailing various experiments involving microcontrollers, particularly the Atmel 8051 and Arduino. It outlines course objectives, outcomes, and provides a comprehensive introduction to microcontroller programming, interfacing, and applications. The manual includes step-by-step instructions for using Keil software to program microcontrollers and perform specific tasks such as interfacing with sensors and actuators.
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)
18 views65 pages

Microcontroller Interfacing Lab Manual Editing On

The document is a laboratory manual for an Embedded Systems and Interfacing Lab, detailing various experiments involving microcontrollers, particularly the Atmel 8051 and Arduino. It outlines course objectives, outcomes, and provides a comprehensive introduction to microcontroller programming, interfacing, and applications. The manual includes step-by-step instructions for using Keil software to program microcontrollers and perform specific tasks such as interfacing with sensors and actuators.
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/ 65

EMBEDDED SYSTEMS &

INTERFACING LAB

LABORATORY MANUAL

Prepared by Approved By
Dr. Sathish GP HoD/Design & Automation

SCHOOL OF MECHANICAL ENGINEERING

VIT- A place to learn; A chance to grow!

June 2024

1
CONTENTS

S. No. Experiment Page


No.

Introduction

Programming With Keil Software

1. Microcontroller assembly language programming (Atmel 8051) 4

2. Interfacing push button, keypad and display with microcontroller (Atmel 9


8051)

3. Traffic light control using microcontroller (Atmel 8051) 24

4. Interfacing ultrasonic sensor with Arduino (AVR) 35

5. DC Motor control with Arduino (AVR) 38

6. Stepper motor control with Arduino Via Matlab (AVR) 44

7. Servo motor control with Arduino Via Matlab (AVR) 48

8. Wireless data transmission with Arduino Via MATLAB (AVR) 50

9. Temperature and Humidity Monitoring Via Raspberry Pi (ARM Cortex) 53

10. Data Communication between Two Micro Controllers via Wireless protocol 55
(ARM Cortex)

2
COURSE OBJECTIVES

1. To expose the students to fundamentals of microcontrollers.


2. To develop the ability to design and implement embedded software for
interfacing sensors, actuators and peripherals using real-time constraints.
3. To familiarize students with embedded communication protocols and real-time
operating systems (RTOS) for modern embedded system applications

COURSE OUTCOMES
At the end of the course, the student will be able to,
1. Demonstrate and interface microcontroller with sensors and actuators.
2. Develop speed control techniques using microcontroller.
3. Construct the simulation model in MATLAB.

3
INTRODUCTION TO 8051 MICROCONTROLLER

Introduction

Microcontroller consists of all features that are found in microprocessors with additional built-in
ROM, RAM, I/O ports, Serial ports, Timers, Interrupts, and Clock circuits. It is an entire computer
on a single chip that is embedded within applications. Microcontrollers are widely used in many
domestic (washing machines, VCD players, microwave oven, robotics, etc.) as well as industrial
and automobile areas.

The 8051 is the first microcontroller of the MCS-51 family developed by Intel Corporation in
1980. It was developed using N-type Metal-Oxide-Semiconductor (NMOS) technology and later it
came to be identified by a letter C in their names e.g. 80C51 which was developed with
Complementary Metal-Oxide-Semiconductor (CMOS) technology which consumes less power
than NMOS and made it better compatible for battery-powered applications.

Microcontrollers can be classified on the basis of their bit processing capability e.g.8-bit
microcontroller means it can read, write, and process 8-bit data. Basically, it specifies the size of
the data bus. Today microcontrollers are designed with much more compact, cheap, and
powerful specifications like AVR and PIC.

The Key features of the 8051 Microcontroller

 4 KB on-chip ROM (Program memory)

 128 bytes on-chip RAM (Data memory)

 The 8-bit data bus (bidirectional)

 16-bit address bus (unidirectional)

 Two 16-bit timers

 Instruction cycle of 1 microsecond with 12 MHz crystal

 Four 8-bit input/output ports

 128 user-defined flags

 Four register banks of 8 bit each

 16-byte bit-addressable RAM

 General purpose registers are 32 each is 8-bit

 Two external and three internal interrupts

 8051 microcontroller specifies some special function features like UARTs, ADC, Op-amp,
etc.

 16-bit program counter and data pointer

4
Block-Structure Diagram of 8051

8051 is one of the first and most popular microcontrollers also known as MCS-51. Intel
introduced it in the year 1981. Initially, it came out as an N-type metal-oxide-semiconductor
(NMOS) based microcontroller, but later versions were based on complementary metal-oxide-
semiconductor (CMOS) technology. It is an 8-bit microcontroller which means the data bus is 8-
bit. Therefore, it can process 8 bits at a time. It is used in a wide variety of embedded systems
like robotics, remote controls, the automotive industry, telecom applications, power tools, etc. It
is referred to as a System on a Chip (SoC) microcontroller because it is a chip circuit/integrated
circuit that holds many components of a computer together on a single chip. These components
include a CPU, memory, input-output ports(I/O ports), timers, and secondary storage. 8051μc
have family members 8052 microcontroller & 8031 microcontroller. 8052 μc has 8K bytes of on-
chip program ROM instead of 4K bytes, and 128 bytes of RAM. and 8031 μc has 0K bytes of on-
chip program ROM, and 128 bytes of RAM.

Pin Description of 8051

The 8051 microcontroller is a popular 8-bit microcontroller widely used in embedded systems.
It is a single-chip microcontroller with a Harvard architecture that includes a CPU, RAM, ROM,
and several peripherals. The 8051 microcontroller has a 40-pin dual in-line package (DIP) that
provides various inputs and outputs for communication with external devices. The 40 pins serve
different functions like read, write, I/O operations, interrupts etc. 8051 has four I/O ports
wherein each port has 8 pins which can be configured as input or output depending upon the

5
logic state of the pins. Therefore, 32 out of these 40 pins are dedicated to I/O ports. The rest of
the pins are dedicated to VCC, GND, XTAL1, XTAL2, RST, ALE, EA’ and PSEN’.

Application of Microcontrollers

 Automation: The 8051 microcontroller is widely used in automation applications. They


are widely used automation of machine operations, product tracking in assembly and
inspection systems with appropriate sensors.

 Industrial Control Systems: The 8051 microcontroller is used in industrial control


systems, such as process control systems and manufacturing equipment, to control and
monitor various processes and operations.

 Energy management: Competent measurement systems support energy consumption


calculations in home and industrial environments. These measuring systems are prepared
by integrating a microcontroller.

 Touch Screen: Many microcontroller vendors incorporate touch functionality into their
designs. Portable devices such as media players and gaming devices.

 Embedded Systems: The 8051 microcontroller is commonly used in embedded systems,


such as home automation systems, security systems, and industrial control systems. Its

6
low cost, small size, and ease of programming make it an ideal choice for these
applications.

 Automotive Systems: The 8051 microcontroller is used in automotive systems, such as


engine control units, anti-lock braking systems, and airbag systems, to control various
functions and ensure safe and efficient operation.

 Robotics: The 8051 microcontroller is used in robotics applications to control the


movement and operation of robots. It is commonly used to control the motors, sensors,
and other peripherals of the robot.

 Environmental monitoring: Convenient environmental devices such as humidity,


temperature and pollution monitors contain microcontrollers to display measurements in
real-time.

 Consumer Electronics: The 8051 microcontroller is used in a variety of consumer


electronics products, such as digital cameras, gaming systems, and audio players. It is
commonly used to control the user interface, display, and other peripherals of these
devices.

 Communication Systems: The 8051 microcontroller is used in communication systems,


such as modems, routers, and switches, to control the data transfer and communication
protocols.

 Medical Devices: The 8051 microcontroller is used in medical devices, such as insulin
pumps, heart monitors, and blood glucose meters, to control various functions and
provide accurate and reliable results.

7
General Functional Blocks of 8051 Development Board

4x4 TEMPERATURE
SERVO KEYPAD SENSOR

ULN2B03

BUZZER

ZIGBEE

MICROCONTROLLER
MEMORY

USB

POWER
LED
DC 8051
MOTOR
RTC

RELAY
4
PUSHBUTTON
SEVEN
SEGMENT
ISP

2X16 LCD

12V AC/DC
INPUT
L239D
MOTOR ON / OFF
DRIVER SWITCH

8
PROGRAMMING WITH KEIL SOFTWARE

Embedded system means some combination of computer hardware and programmable


software which is specially designed for a particular task like displaying message on LCD. One of
the important parts in making an embedded system is loading the software/program we develop
into the microcontroller. Usually it is called “burning software” into the controller. Before
“burning a program” into a controller, we must do certain prerequisite operations with the
program. This includes writing the program in assembly language or C language, compiling the
program in a compiler and finally generating the hex code and bin code from the compiled
program. Keil MicroVision is free software which solves many of the pain points for an
embedded program developer. This software is an integrated development environment (IDE),
which integrated a text editor to write programs, a compiler and it will convert your source code
to hex files too. Here is simple guide to start working with Keil uVision which can be used for
writing programs in C/C++ or Assembly language, compiling and assembling programs,
debugging program, creating Hex and Bin file, and testing program without real hardware
(Simulator Mode)

Working with KEIL MICRO VISION software


STEP 1: Create new folder in the name of FLASHING LED, and copy lcd.c and lcd.h from From CD
Embedded lab experiment code FLA SHING LED code Main files: FLASHING LED.c Sub files: lcd.c,
lcd.h
STEP 2: Click for KEIL µVISION4 Icon .Which appearing after Installing KEIL µVISION4

After opening Keil uV4, go to Project tab and Create new uVision project

9
Create the project in FLASHING LED folder created in Step1 and give save.

STEP 3: Select Target Device Vendor (i.e.).NUVOTON& click ok

10
STEP 4: Then select specific chip W78E052 in NUVOTON.

Then click ok, you will see following window click yes

Note: Code wills Not Run without Startup.s

STEP 5: Then you will see following window

11
STEP 6: Now you see Startups is already added which is necessary for running code for Keil.
Startup.s is available in C:\Keil\ARM\Startup\Philips.
The startup-code executes immediately upon reset of the target system and performs the
following operations:

We are going to add C file in two ways


Method 1: Creating a new c file for FLASHING LED.C
STEP 7: Now Click on File Menu and Click on New.

Write Code for FLASHING LED in C and save it as FileName.c here FLASHING LED.c
Write Code for flashing led in C and SAVE as FLASHING LED.c

12
STEP 8: Once the code is written it has to be added to the project to do that right click on
source group and select “ADD FILES TO SOURCE GROUP” option AND add FILES FLASHING
LED.c, lcd.c.
1. Main files: FLASHING LED.c
Sub files: lcd.c, lcd.h

OR

METHOD 2: Using existing file from copied folder


Copy FLASHING LED.c file from
CD Embedded lab experiment code FLASHING LED code
copy to FLASHING LED folder

Now add FLASHING LED.c , lcd.c, to source group from FLASHING LED FOLDER

13
Copy flashing led .c and lcd.c from Embedded lab experiment code

Now our folder contains flashing led.c and lcd.c.


Add files to Source Group

14
Now add lcd file to the source

15
Hex file and bin files are not created unless we follow particular steps. Steps to be
followed for creating hex file and bin files are mentioned below

Creating Hex file


The Intel hex (ihex) generally known as hex file, is a format used to store
machine language code in hexadecimal form. It is widely used format to store programs
to be transferred to microcontrollers, ROM and EEPROM. The compilers convert the
programs written in assembly, C etc into corresponding hex files, which are dumped into
the controllers using burners/programmers.

STEP 1: Go to Options for Target 1‟.

16
STEP 2: Go to output Tab

STEP 3: Click on Check Box “Create HEX File”.

STEP 5: Then Click on Rebuild All Target Files

Once the project is build result can be seen at the bottom. If any errors are
present list of errors will be displayed or if project has no errors build is said to
be successful.

17
STEP 6: Now you see 0 Error(s), 0 Warning (s). Then Hex File will create in Specific
Folder. Now to download it for you target hardware

Burning Hexfile In to the Chip


STEP 1: Connect USB CABLE in the 8051 kit to the system via serial cable.
STEP 2: Click the windows buton and search for NUVOTON ISP-ICP UTILITY and click on it.

STEP 3: NUVOTON ISP-ICP UTILITY WINDOW WILL OPEN

18
STEP 4: CLICK as shown below

STEP 5: Note the port connected, here its connected to com2.

19
STEP 5: Select the COM port which is connected in the system.

STEP 6: Select the IC Number “W78E052D”

20
STEP7: Click The ‘LOAD FILE’ button

STEP8: Select the hex file created in step6 in hex file creation and click the open button.

21
STEP 9: Click the “UPDATE CHIP ”button

STEP10: While uploading the data you should be click the reset button.

The reset button is located in 8051 kit.

22
STEP10: Just Click OK

STEP11: when the upload was completed the popup window shown.in the below

23
1. MICROCONTROLLER ASSEMBLY LANGUAGE PROGRAMMING
(ATMEL 8051)

Objective
To program a microcontroller with assembly language.

Hardware and Software


1. Computer
2. KEIL Software

Features of MC-8051
 MC-8051 is an 8-bit Microcontroller.
 It is a 40 pin IC Chip.
 It has RAM(On Chip) of 128 bytes.
 It has ROM(On Chip) of 4K bytes.
 8051-MC Works with 12 MHz clock and a single +5V supply.
 It has 111 instructions: 49 single byte, 45 two byte and 17 three byte

Program for 8-bit Addition


;program for 8-bit addition in assembly language programming
ORG 0000
MOV A,#5
MOV B,#5
ADD A,B
MOV 50H,A
MOV R0,50H
END

Program Description
 ORG 0000H means it set the statement at memory address 0000H.
 MOV A,#5 using immediate Addressing mode we are transferring hexadecimal ‘5’
to accumulator.
 MOV B,#5 Same as above we are transferring hexadecimal 5 to b.
 ADD A,B here add opcode will adds the data of a and b
 MOV 50H,A this line uses to store the output to the memory address 50h from
accumulator.

24
 MOV R0,50H this line uses to store the output to the register from memory 50h.
 END This ‘end’ opcode to stops the program
 To Write comments in Assembly language we use a “;“.

Flowchart of program

To write a 8-bit addition program using assembly language programming, we need a


IDE to write and run it. In this case we are using Keil uVision software.

Steps to use Keil uVision Software


After Installing the Keil software follow the below steps to perform the 8-bit Addition
1. Open the software and click on the Project option which is settled at the top of the
window, Then it will shows a popup like this below:

25
Keil uVision Software

And then click on the “New uVision Project” option then it will opens and explorer to
save it like below:

Saving the Project

26
Give a name to your Project and save it in your desired location. Then a popup will
come to select the device of Microcontroller here in this case we are going to select
the ‘AT89C51’ Microcontroller to run the program. In the popup there will be a search
bar type the above name then select it as below:

Select the MC AT89C51

After selecting the MC click on “OK” Option ‘AT89C51’ is a Microcontroller that


designed by ‘ATML’ it is 8051-MC. Then you will get a dialog box with yes or no like
below:

Click on NO

Click on ‘NO’ we don’t have need of that file so that click on NO but that’s up to you if
you have any use cases with that file you can click on YES Then we have successfully
created a project file like below:

27
Then right click on Source Group 1 and click on the option called “add new item to
Group ‘Source Group 1′” and select the file type and enter the file name also like
below:

Then click on Add that create an .asm file in your project. Now the ‘asm’ file will be
open in the editor to type the program so that Enter the above Program of 8-bit
Addition. You can paste the above Program in your code editor after completing the
code click on “F7” key to Build the target file or right click on the asm file and select
Build target option from there.

28
In the below at Build output box you will get like below:
Build started: Project: addition
Build target 'Target 1'
assembling add.asm...
linking...
Program Size: data=8.0 xdata=0 code=11
".\Objects\addition" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:00:01

So when we are getting the output as 0 Errors we can proceed to run the Program by
debugging.
To debug the program we can simply click ‘ctrl + F5’ or there will be a option to do
that at the top of the window.

we can debug the program by step wise or complete program at a time by clicking on
keys, By clicking on F11 Key we can achieve step wise debugging
At starting the register values are like below:

29
At starting all the registers are empty by pressing the F11 key we can check how the
values of the accumulator, b are changing.

We can in the above picture that the value #5 came into Accumulator and B also added,
the result was stored in Accumulator as per the program, as in the program we are
added that to store the value in r0 register and 50h address see the result in the
below:

As you can see the result was stored in R0 Register and 50h address. so that we can
change the program to test your own values and make sure to rebuilt it again by
clicking on F7 key .

30
Applications of Assembly Language Programming
Assembly Language Programming have lot more applications here are some :
1. Used to design Real-time Systems
2. Used to design Embedded Systems
3. Used in Writing OS
4. Used in Writing FrimWare
5. Used to develop compilers and interpreters, etc.

Advantages and Disadvantages of using Assembly Language in 8051


There are some list of Advantages and Disadvantages of using assembly language in
8051 given below :

Advantages of using Assembly Language in 8051


1. It is faster than using a high-level language.
2. It works more efficiently in terms of memory usage.
3. It gives the programmer more control over the code.

Disadvantages of using Assembly Language in 8051


1. It is difficult to write and maintain.
2. It is not portable to other microcontrollers.

31
Additional Programs

1. TRANSFER OF BLOCK OF DATA BYTES FROM SOURCE MEMORY TO DESTINATION


MEMORY USING 8051

Program:

MOV R0,#50H // Initialize the source memory pointer


MOV R1,#60H // Initialize the destination memory pointer
MOV R2, #05H // Initialize Iteration counter
BACK: MOV A,@R0 // Get the data from source memory pointer
MOV @R1,A // Store the data into destination memory pointer
INC R0 // Increment the source memory pointer
INC R1 // Increment the destination memory pointer
DJNZ R2, BACK // Decrement iteration count and if it
// is not zero, go to relative Address and
// repeat the same process until count become
// zero.
END

Memory Window:

Before execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 00 00 00 00 00 00

After execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 22 AB 3D 44 55 00

32
2. 16 BIT ADDITION

Program:

MOV R0,#51H // Initialize input1 memory pointer


MOV R1,#61H /* Initialize input2 memory pointer and store output also same */
MOV R2,#02H // Initialize iteration count
CLR C
BACK: MOV A,@R0 /*Get lower bytes data in first iteration, upper bytes data in second
iteration, add them with carry and store in memory pointer2.*/
ADDC A,@R1
MOV @R1,A
DEC R0 // Increment memory pointer1 & 2 to get upper bytes
DEC R1
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, go to relative address
and repeat the same process until count become zero.*/
JNC FINISH
MOV @R1,#01H
FINISH:SJMP $
END

Memory Window:

Before execution:
D:0x50H: FD 07 00 00 00 00
D:0X60H: FF 5F 00 00 00 00

After execution:
D:0x50H: FD 07 00 00 00 00
D:0X5FH: 01 FC 66 00 00 00

33
3. ARRANGEMENT OF 8-BIT NUMBERS IN ASCENDING ORDER.

Program:

MOV R2, #05H // Initialize the iteration counter


DEC R2 // Decrement the iteration count
BACK1: MOV R0, #50H // Initialize memory pointer1
MOV R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1
MOV B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP /* Compare if not equal go to relative address (LOOP)*/
LOOP: JC LOOP1 /* If carry generates, go to relative address (LOOP1)*/
MOV @R0,B // Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK // Decrement inner loop count if not zero go to
back
DJNZ R2, BACK1 // Decrement outer loop count if not zero go to
back1
END

Memory Window:

Before execution:
D:0x50H: 06 04 03 07 02 01

After execution:
D:0x50H: 01 02 03 04 06 07

34
2. PUSH BUTTON AND LED INTERFACE WITH MICROCONTROLLER
(ATMEL 8051)

The objective is to create the simplest experiment by using 4 pushbutton and led
interface with 8051 microcontroller. Basically, you connect a few things easily, write
some code, and upload it to the 8051 microcontroller. With that, the Push buttons or
switches connect two points in a circuit when you press them, LED gets ON and OFF.

Circuit Connection

From above the figure explained that how to make connections between 8051
microcontroller and 4 bush buttons and LED through wire connections as a
corresponding pin described in table.

8051 PINS LED AND SWITCH PINS


P1.0 SW1
P1.1 SW2
P1.2 SW3
P1.3 SW4
P3.4 LED1
P3.5 LED2
P3.6 LED3
P3.7 LED4

35
Procedure

• Set up the circuit as described in the “Circuit Connection” section.


• Connect the 8051 MICROCONTROLLER to your computer using a USB cable.
• Open the KEIL UVISION4 software on your computer.
• Write the code for 4 pushbutton and led interface in 8051 MICROCONTROLLER.
• Create a hex file as a instructions given.
• Open the NUVUTON ISP-ICP utility software on your computer.
• Click the upload button and upload the hex file which is created by the KEIL
software.
• Verify and upload the code to the 8051 MICROCONTROLLER.
• When the button is not pressed, the LED will turn off.
• When the button is press the LED will turn on.

PROGRAM

#include<reg51.h>
sbit sw1=P1^0;
sbit sw2=P1^1;
sbit sw3=P1^2;
sbit sw4=P1^3;
sbit led1=P3^4;
sbit led2=P3^5;
sbit led3=P3^6;
sbit led4=P3^7;

int main()
{
P1=0xFF;
while(1)
{
if(sw1==0)
{
led1=0;
}
else
{

36
led1=1;
}
if(sw2==0)
{
led2=0;
}
else
{
led2=1;
}
if(sw3==0)
{
led3=0;
}
else
{
led3=1;
}
if(sw4==0)
{
led4=0;
}
else
{
led4=1;
}
}
return 0;
}

The 8051 MICROCONTROLLER successfully blinks led on if pushbutton is high,


led off if pushbutton low, Here by following the instructions and uploading the provided
code. The fundamentals of utilizing an 8051 microcontroller to control hardware
components are clarified through this experiment.

37
3. Traffic light control using microcontroller (Atmel 8051)

The objective is to create the simplest experiment by using 4x4 keypad and lcd interface
8051 MICROCONTROLLER. Basically, you connect a few things easily, write some code,
and upload it to the 8051 MICROCONTROLLER. With that if you

CIRCUIT CONNECTION

GND GND
+5V VCC
CON CON
P2.7 RS
P2.6 R/W
P2.5 EN
P0.0 D0
P0.1 D1
P0.2 D2
P0.3 D3
P0.4 D4
P0.5 D5
P0.6 D6
P0.7 D7
L+ LCD(light +)
L- LCD(light-)
P1.0 R1 (KEYPAD R=ROW,C=COLUM)
P1.1 R2
P1.2 R3
P1.3 R4
P1.4 C1
P1.5 C2
P1.6 C3
P1.7 C4

38
PROCEDURE:

• Set up the circuit as described in the “Circuit Connection” section.


• Connect the 8051 MICROCONTROLLER to your computer using a USB cable.
• Open the KEIL UVISION4 software on your computer.
• Write the code for 4x4 keypad and lcd interface 8051 MICROCONTROLLER.
• Create a hex file as a instructions given.
• Open the NUVUTON ISP-ICP utility software on your computer.
• Click the upload button and upload the hex file which is created by the KEIL
software.
• Verify and upload the code to the 8051 MICROCONTROLLER.
• When the input is given with the help of 4*4 keypad the lcd screen displays the
output.

PROGRAM
#include <stdio.h> //Define I/O Functions
#include <reg52.h> //Define 8051 Registers
#define DATA P0 //Define DATA to Port1
#define SCAN P1
void lcd_init(void); //LCD Initialization
void lcd_cmd(unsigned char); //LCD Command Function
void lcd_display(unsigned char); //LCD Display Function
void Key_Scan(void);//KeyScan Function
void DelayMs(int); //DelayMs Function
sbit RS = P2^7; //Register Select

39
sbit RW = P2^6; //LCD Read/Write
sbit lcd_e = P2^5; //LCD Enable
unsigned char R,C,ch;
unsigned int i=0;

unsigned char Key[4][4] = {'*','7','4','1',


'0','8','5','2',
'#','9','6','3',
'D','C','B','A',
};

code unsigned char msg[] = (" 4X4 KEYPAD TEST");//Display the Message
code unsigned char msg1[] = ("KEY PRESSED - _");

//-------------------------------
// Main Program
//-------------------------------

void main()
{
lcd_init();
while(1)
{
Key_Scan();
ch = Key[C][R]; //Assign Key value to ch;
//SBUF=ch; //[C][R] denotes Column
DelayMs(35); //and Row Value of Keypad
lcd_cmd(0xCE);
lcd_display(ch);
DelayMs(35);
}
}

//-------------------------------
// Key Scan Function
//-------------------------------

void Key_Scan(void)
{
unsigned int i = 0;
//Scanning for Row Value
SCAN = 0x0F; //Initialize Port2 to 0Fh
while(SCAN == 0x0F);
if(SCAN == 0x0E) //Checking for Row0
R = 0;

40
else if(SCAN == 0x0D) //Checking for Row1
R = 1;
else if(SCAN == 0x0B) //Checking for Row2
R = 2;
else if(SCAN == 0x07) //Checking for Row3
R = 3;
//Scanning for Column Value
SCAN = 0xF0; //Initialize Port2 to F0h
while(SCAN == 0xF0);
if(SCAN == 0xE0) //Checking for Column0
C = 0;
else if(SCAN == 0xD0) //Checking for Column1
C = 1;
else if(SCAN == 0xB0) //Checking for Column2
C = 2;
else if(SCAN == 0x70) //Checking for Column3
C = 3;
DelayMs(50);
}

//-------------------------------
// LCD command Function
//-------------------------------
void lcd_cmd(unsigned char cmnd)
{
DATA = cmnd;
RS = 0;//RS:Register Select
RW = 0; //RW:Read/Write
lcd_e = 1; //LCD Enable
DelayMs(35);
lcd_e = 0;
}

//-------------------------------
// LCD Data Function
//-------------------------------
void lcd_display(unsigned char dat)
{
DATA = dat;
RS = 1;//RS:Register Select
RW = 0; //RW:Read/Write
lcd_e = 1;
DelayMs(35);
lcd_e = 0;
}

41
//-------------------------------
// LCD Initialization
//-------------------------------

void lcd_init(void)
{
unsigned char i;
lcd_cmd(0x38); //2x16 Character 5x7 dot
DelayMs(15); //matrix LCD,8-bit format
lcd_cmd(0x0c); //Display On, cursor off
DelayMs(15);
lcd_cmd(0x06); //Shift Cursor to right
DelayMs(15);
lcd_cmd(0x01); //Clear display screen
DelayMs(15);

//------------------------------------
// First Line Message Display
//------------------------------------
lcd_cmd(0x80); //First Line Initialization
DelayMs(35);
i=0;
while(msg[i]!='\0')
{
lcd_display(msg[i]);
i++;
}
DelayMs(50);

//------------------------------------
// Second Line Message Display
//------------------------------------

lcd_cmd(0xc0); //Second Line Initialization


DelayMs(35);
i=0;
while(msg1[i]!='\0')
{
lcd_display(msg1[i]);
i++;
}
DelayMs(50);
}

42
//--------------------------
// DelayMs Function
//--------------------------

void DelayMs(int k)
{
unsigned int a;
for(a=0;a<=k;a++);
}

The 8051 Microcontroller displays the output that that entered using keypad,
here by following the instructions and uploading the provided code. The fundamentals
of utilizing an 8051 MICROCONTROLLER to control hardware components are clarified
through this experiment.

43
4. SENSOR INTERFACE WITH ARDUINO

Ultrasonic sensors are widely used for distance measurement applications. This manual
guides you through interfacing the HC-SR04 Ultrasonic Distance Sensor with an Arduino
Uno board. The setup also includes a buzzer to alert when an object is detected within a
specified range.

CIRCUIT CONNECTION

HC-SR04 Ultrasonic Sensor Pins

Pin Name Description


VCC +5V Power Supply
GND Ground
Trig Trigger Input (to send sound pulse)
Echo Echo Output (receives reflected pulse)

Arduino Pins Connection Table


Arduino Pins Ultrasonic Sensor Pins
GND GND
+5V VCC
Digital Pin 9 Trig
Digital Pin 10 Echo

Arduino to Buzzer Connection


Arduino Pins Buzzer Wires
Digital Pin 11 Signal Wire (usually red)
GND GND (usually black)

Note: Make sure to connect the buzzer's positive terminal to digital pin 11 and the
negative to GND.

44
Wiring Diagram

Arduino Code

#define trigPin 9
#define echoPin 8
#define buzzerPin 7

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);

45
}

void loop() {
// Send trigger pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read echo pulse duration


duration = pulseIn(echoPin, HIGH, 30000); // Timeout to avoid freeze
distance = duration * 0.034 / 2; // Convert to cm

// Print distance in Serial Monitor


Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// If no object detected (0 cm or > 200 cm), buzzer OFF


if (distance == 0 || distance > 200) {
noTone(buzzerPin);
}
else {
// Object detected — closer means faster beeps
int beepDelay = map(distance, 1, 100, 50, 500); // 50ms (close) → 500ms (far)
tone(buzzerPin, 1000); // 1kHz tone
delay(beepDelay);
noTone(buzzerPin);
delay(beepDelay);
}

delay(50); // Small loop delay


}

Procedure
The Trig pin sends an ultrasonic pulse. The Echo pin receives the reflected pulse after

46
hitting an object. The Arduino calculates the time taken for the pulse to return and
converts it into distance using the speed of sound. If the object is detected within 10 cm,
the buzzer is turned on.

Applications
 Obstacle detection
 Smart parking systems
 Robotic distance sensing
 Object counters and automation

47
5. DC MOTOR INTERFACE WITH ARDUINO

This manual provides a detailed guide for interfacing a DC motor with an Arduino Uno
using a motor driver module. The setup enables control of the motor's operation
through digital output signals from the Arduino.

CIRCUIT CONNECTION

From below figure explained that how to make connections among Arduino , and
DC motor through wire connections as a corresponding pin described in table.

Component Pin Name Connection Description


DC Motor Driver Board IN1 Connected to Arduino
Digital Pin 9
DC Motor Driver Board IN2 Connected to Arduino
Digital Pin 8
DC Motor Driver Board VCC Connected to Arduino 5V
DC Motor Driver Board GND Connected to Arduino GND

DC Motor Red Wire Connected to Motor Driver


Output Terminal
DC Motor Blue Wire Connected to Motor Driver
Output Terminal

48
PROCEDURE
• Set up the circuit as described in the “Circuit Connection” section.
• Connect the Arduino to your computer using a USB cable.
• Open the Arduino IDE/ Arduino online Editor software on your computer.
• Write the code for Dc motor and motor driver interface with Arduino.
• Click the compile button and get error cleared and upload the code to Arduino by
clicking the Upload button,

PROGRAM

// Define motor control pins


const int in1 = 8; // Direction pin 1
const int in2 = 9; // Direction pin 2
const int ena = 10; // Enable pin (PWM speed control)

void setup() {
// Set all control pins as outputs
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ena, OUTPUT);
}

void loop() {
// Rotate motor forward at half speed
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(ena, 128); // Half speed (range: 0 to 255)
delay(2000); // Run for 2 seconds

// Stop motor
analogWrite(ena, 0);
delay(1000); // Pause for 1 second

// Rotate motor backward at full speed


digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

49
analogWrite(ena, 255); // Full speed
delay(2000); // Run for 2 seconds

// Stop motor again


analogWrite(ena, 0);
delay(1000); // Pause before repeating
}

The ARDUINO successfully control the speed of the DC motor as programed. Here
by following the instructions and uploading the provided code. The fundamentals of
utilizing an Arduino to control hardware components are clarified through this
experiment

6. STEPPER MOTOR CONTROL WITH MATLAB VIA ARDUINO

50
The objective is to control the speed of the stepper motor by simply controlling
the input voltage to the motor and the most common method of doing that is by using
PWM signal. Basically, you connect a few things easily, write some code, and upload it to
the Arduino.

CIRCUIT CONNECTION

From below the figure explained that how to make connections among 8051
microcontroller and stepper motor through wire connections as a corresponding pin
described in table.

Arduino Uno Pin ULN2003 IN Pin Stepper Motor Coil (color)


D8 IN1 Blue
D9 IN2 Pink
D10 IN3 Yellow
D11 IN4 Orange
GND GND —
5V VCC (on ULN2003) Red (Power to motor)

PROCEDURE

51
• Set up the circuit as described in the “Circuit Connection” section.
• Connect the Arduino to your computer using a USB cable.
• Open the Matlab software on your computer.
• Write the code for Stepper motor interface with Arduino.
• Save the file.
• Click the Run button and check the code is loading in the Arduino.
• The Stepper will function as coded.

PROGRAM

% Clear any previous connection


clear;
clc;

% Connect to Arduino (change COM port accordingly)


a = arduino('COM3', 'Uno');

% Define ULN2003 input pins connected to Arduino


IN1 = 'D8';
IN2 = 'D9';
IN3 = 'D10';
IN4 = 'D11';

% Full-step sequence (no microstepping)


sequence = [
1 0 1 0;
0 1 1 0;
0 1 0 1;
1 0 0 1;
];

% Speed Control
rpm = 15; % Desired speed (faster than half-step)
stepsPerRev = 2048; % Full-step count for 28BYJ-48
delay = 60 / (rpm * stepsPerRev); % Delay between steps

52
% Infinite loop for forward and backward rotation
while true
% Forward direction
for i = 1:stepsPerRev
idx = mod(i-1, 4) + 1;
writeDigitalPin(a, IN1, sequence(idx, 1));
writeDigitalPin(a, IN2, sequence(idx, 2));
writeDigitalPin(a, IN3, sequence(idx, 3));
writeDigitalPin(a, IN4, sequence(idx, 4));
pause(delay);
end

pause(0.5); % Optional pause

% Backward direction
for i = stepsPerRev:-1:1
idx = mod(i-1, 4) + 1;
writeDigitalPin(a, IN1, sequence(idx, 1));
writeDigitalPin(a, IN2, sequence(idx, 2));
writeDigitalPin(a, IN3, sequence(idx, 3));
writeDigitalPin(a, IN4, sequence(idx, 4));
pause(delay);
end

pause(0.5); % Optional pause


end

The Arduino successfully control the speed of the DC motor as programed. Here by
following the instructions and uploading the provided code. The fundamentals of
utilizing an Arduino to control hardware components are clarified through this
experiment

53
7. SERVO MOTOR CONTROL WITH ARDUINO VIA MATLAB

The objective is to control the servo motor in two ways and Sweep (the shaft of a
RC servo motor back and forth across 180 degrees).is by using PWM signal. Basically,
you connect a few things easily, write some code, and upload it to the Arduino.

CIRCUIT CONNECTION

From below the figure explained that how to make connections among Arduino
and servo motor through wire connections as a corresponding pin described in table.

Servo Wire Colour Function Arduino Pin


Orange/Yellow PWM D2
Red +5V +5V
Brown/ Black GND GND
.

PROCEDURE
• Set up the circuit as described in the “Circuit Connection” section.
• Connect the Arduino to your computer using a USB cable.
• Open the Matlab software on your computer.
• Write the code for Servo motor interface with Arduino.
• Save the file.
• Run the code and check the output on the hardware function.

54
PROGRAM
% === Setup ===
clc;
clear;

% Connect to Arduino
a = arduino('COM5', 'Uno', 'Libraries', 'Servo'); % Replace 'COM3' with your actual COM
port

% Attach servo to pin D9


s = servo(a, 'D2'); % 'D9' is the PWM pin

% === Move Servo ===


disp('Moving servo to 0°');
writePosition(s, 0); % 0°
pause(1);

disp('Moving servo to 90°');


writePosition(s, 0.5); % 90°
pause(1);

disp('Moving servo to 180°');


writePosition(s, 1); % 180°
pause(1);

disp('Sweeping servo from 0° to 180°...');


for angle = 0:0.5:1
writePosition(s, angle); % angle from 0 to 1 (0° to 180°)
pause(1); % small delay for smooth movement
end

disp('Sweeping servo from 180° to 0°...');


for angle = 1:-0.5:0
writePosition(s, angle);

55
pause(1);
end

disp('Sweep complete.');

The servo motor is controlled successfully with the code written to Arduino. Here
by following the instructions and uploading the provided code. The fundamentals of
utilizing an Arduino to control hardware components are clarified through this
experiment.

56
8. WIRELESS DATA TRANSMISSION WITH ARDUINO VIA MATLAB
(AVR)

The objective is to control the traffic lights with the Arduino. The connections
among Arduino, Bluetooth module interface through wire connections as a
corresponding pin are described in table.

CIRCUIT CONNECTION

From below the figure explained that how to make connections among Arduino
and Hc-05 Bluetooth module through wire connections as a corresponding pin
described in table.

Component Pin Name Arduino UNO (Master) Arduino UNO (Slave)

HC-05 Module VCC 5V 5V


GND GND GND

TXD D10 (via voltage divider) D10 (via voltage divider)

RXD D11 D11

57
PROCEDURE
• Set up the circuit as described in the “Circuit Connection for Transmitter circuit”
section.
• Connect the Arduino-1 to your computer using a USB cable.
• Open the MATlab software on your computer.
• Write the code for Bluetooth module interface with Arduino-1.
• Set up the circuit as described in the “Circuit Connection for Receiver circuit”
section.
• Connect the Arduino-2 to your computer using a USB cable.
• Open the MATlab software on your computer.
• Write the code for Bluetooth module interface with Arduino-2.
• Run both codes to see the output.

PROGRAM
Matlab Code for Master:

master = serialport("COM7", 9600);


slave = serialport("COM8", 9600);

disp("Connected to both Master and Slave");

for i = 1:10
% Send to Master
write(master, "MATLAB → Master", "string");

% Send to Slave
write(slave, "MATLAB → Slave", "string");

pause(1);

% Read responses
if master.NumBytesAvailable > 0
msgM = readline(master);
fprintf("From Master: %s\n", msgM);
end

58
if slave.NumBytesAvailable > 0
msgS = readline(slave);
fprintf("From Slave: %s\n", msgS);
end
end

clear master slave;


disp("Done");

Wireless Data Transmission with Arduino Via Matlab successfully carried out
with the Arduino-1 and Arduino-2 and by uploading the MATlab code. The fundamentals
of utilizing an Arduino as master and slave to control hardware components are clarified
through this experiment.

59
11. TEMPERATURE AND HUMIDITY MONITORING VIA
RASPBERRY PI (ARM CORTEX)

The objective is to connect Sensor module and monitoring the Temperature and
Humidity with the help Arduino.

CIRCUIT CONNECTION

The connections among Pico, Temperature and Humidity sensor and LCD display
interface through wire connections as a corresponding pin are described in table.

Component Pin Raspberry Pi Pico Pin


DHT11 VCC 3.3V (Pin 36)
GND GND (Pin 38)
Data GPIO 15 (Pin 21)
SSD1306 OLED VCC 3.3V (Pin 36)
GND GND (Pin 38)
SCL GPIO 5 (Pin 7)
SDA GPIO 4 (Pin 6)

60
PROCEDURE
 Connect the Temperature and humidity sensor and SSD1306 OLED to the
Raspberry Pi Pico as per the provided pin configuration table.
 Connect the Raspberry Pi Pico to your computer via a micro-USB cable.
 Install and open the Thonny IDE on your computer.
 Ensure MicroPython is installed on your Pico:
 Go to Tools > Options > Interpreter.
 Select MicroPython (Raspberry Pi Pico).
 Click Install or update firmware if required.
 Write the MicroPython code in the Thonny editor to interface with the DHT11
sensor and SSD1306 OLED display.
 Save or upload required libraries (ssd1306.py) to the Raspberry Pi Pico using File
> Save As > MicroPython device.
 Save your main code as main.py so it runs automatically on boot.
 The temperature and humidity data from the DHT11 sensor should be displayed
on the OLED screen.
 Safely disconnect and reboot the Pico to confirm that the script runs on power-
up.

PROGRAM

from machine import Pin, I2C


import utime
import dht
import ssd1306

# Initialize DHT11
dht_sensor = dht.DHT11(Pin(15)) # GPIO15

# Initialize I2C for OLED (GPIO4 = SDA, GPIO5 = SCL)


i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)

# Initialize OLED
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

61
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()

print("Temperature:", temp, "°C")


print("Humidity:", hum, "%")

oled.fill(0)
oled.text("Temp: {} C".format(temp), 0, 0)
oled.text("Hum : {} %".format(hum), 0, 10)
oled.show()

except OSError as e:
print("Sensor error:", e)

utime.sleep(2)

Temperature an humidity sensor module gets successfully connected with the


Pico, and also can see the Values in the OLED. Here by following the instructions and
uploading the provided code. The fundamentals of utilizing an Pico to control hardware
components are clarified through this experiment.

62
12. Data Communication between Two Micro Controllers via Wireless
protocol (ARM Cortex)

Objective:
To develop appropriate Data Communication between Two Micro Controllers via Wireless
protocol (ARM Cortex) to be controlled in MATLAB for analysis.

CIRCUIT CONNECTION

The connections among Pico and Arduino with Bluetooth module, interface
through wire connections as a corresponding pin are described in table.

Raspberry Pi Pico + HC-05 Bluetooth Module (Master)

HC-05 Pin Pico Pin Function


VCC 3.3V (Pin 36) or 5V with converter Power
GND GND (Pin 38) Ground
TXD GP1 (Pin 2) HC-05 → Pico (RX)
RXD GP0 (Pin 1) via voltage divider Pico → HC-05 (TX)
EN (optional) 3.3V (Pull High if required)

Arduino UNO + HC-05 Bluetooth Module (Slave)


HC-05 Pin Arduino Pin Function
VCC 5V Power
GND GND Ground
TXD RX (Pin 0) HC-05 → Arduino
RXD TX (Pin 1) via voltage divider Arduino → HC-05
EN (optional) 3.3V (Pull High if required)

63
PROCEDURE
 Connect HC-05 Bluetooth module to Raspberry Pi Pico and Arduino UNO
according to their master-slave roles.
 Ensure a voltage divider is used on Pico TX to HC-05 RX for safe operation.
 Power both microcontrollers appropriately.
 Connect the Raspberry Pi Pico to your computer using a USB cable.
 MicroPython Programming for Pico (Master):
 Open Thonny IDE or any MicroPython-compatible interface on your computer.
 Write the MicroPython code for the Pico to act as Bluetooth Master:
 Save and run the script on your Raspberry Pi Pico (e.g., save as main.py so it runs
on boot).

 Arduino Programming (Slave):


 Open Arduino IDE on your computer.
 Write the Arduino sketch for reading data from the Pico via HC-05:
Upload the sketch to your Arduino UNO via USB.
Important: Disconnect HC-05 module from TX/RX pins while uploading, then
reconnect.
 Testing via Serial Monitor:
 Open the Arduino Serial Monitor (baud rate: 9600) to see the messages received
from the Pico.
 Messages like Hello Arduino #0, Hello Arduino #1, etc., should appear every 2
seconds.

64
PROGRAM

Pico + HC05
from machine import UART, Pin
import utime

uart_bt = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))

print("Pico Bluetooth Master Ready")

counter = 0
while True:
msg = f"Hello Arduino #{counter}\n"
uart_bt.write(msg)
print(f"Sent: {msg}")
counter += 1
utime.sleep(2)

Arduino +HC05
void setup() {
Serial.begin(9600);
Serial.println("Arduino Bluetooth Slave Ready");
}

void loop() {
if (Serial.available()) {
String msg = Serial.readStringUntil('\n');
Serial.print("Received from Pico: ");
Serial.println(msg);
}
}
Bluetooth module gets successfully connected with Pico and Arduino, and also
help to control the components. Here by following the instructions and uploading the
provided code. The fundamentals of utilizing an Pico and Aduino to control hardware
components are clarified through this experiment.

65

You might also like