Microcontroller Interfacing Lab Manual Editing On
Microcontroller Interfacing Lab Manual Editing On
INTERFACING LAB
LABORATORY MANUAL
Prepared by Approved By
Dr. Sathish GP HoD/Design & Automation
June 2024
1
CONTENTS
Introduction
10. Data Communication between Two Micro Controllers via Wireless protocol 55
(ARM Cortex)
2
COURSE OBJECTIVES
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.
8051 microcontroller specifies some special function features like UARTs, ADC, Op-amp,
etc.
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.
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
Touch Screen: Many microcontroller vendors incorporate touch functionality into their
designs. Portable devices such as media players and gaming devices.
6
low cost, small size, and ease of programming make it an ideal choice for these
applications.
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
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.
10
STEP 4: Then select specific chip W78E052 in NUVOTON.
Then click ok, you will see following window click yes
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:
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
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
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
16
STEP 2: Go to output Tab
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
18
STEP 4: CLICK as shown below
19
STEP 5: Select the COM port which is connected in the system.
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.
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.
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 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
25
Keil uVision Software
And then click on the “New uVision Project” option then it will opens and explorer to
save it like below:
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:
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.
31
Additional Programs
Program:
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:
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:
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.
35
Procedure
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;
}
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:
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;
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
//------------------------------------
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
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);
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.
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
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
49
analogWrite(ena, 255); // Full speed
delay(2000); // Run for 2 seconds
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
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.
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
% 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
% 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
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.
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
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.
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:
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
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.
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
# Initialize DHT11
dht_sensor = dht.DHT11(Pin(15)) # GPIO15
# Initialize OLED
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
61
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
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)
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.
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).
64
PROGRAM
Pico + HC05
from machine import UART, Pin
import utime
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