0% found this document useful (0 votes)
14 views31 pages

Expt 1,2,3,6

The document outlines a series of tasks for familiarizing students with the Arduino UNO Learning Kit, including connecting the board, using the IDE, and writing sketches to perform operations like adding and multiplying hexadecimal numbers. It also covers tasks for blinking an LED, using the Serial Monitor, and collecting necessary components for lab work. Additionally, it introduces input and output port programming to control an LED with a switch.

Uploaded by

4vmfv28shx
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)
14 views31 pages

Expt 1,2,3,6

The document outlines a series of tasks for familiarizing students with the Arduino UNO Learning Kit, including connecting the board, using the IDE, and writing sketches to perform operations like adding and multiplying hexadecimal numbers. It also covers tasks for blinking an LED, using the Serial Monitor, and collecting necessary components for lab work. Additionally, it introduces input and output port programming to control an LED with a switch.

Uploaded by

4vmfv28shx
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/ 31

Experiment – 1

Familiarization with the Operation of Arduino UNO Learning Kit


Built-in LED Device and Serial Monitor
Task-1.1 Operation of Arduino UNO Learning Kit
(1) Collect the following Arduino UNO Board and USB cable from Lab Attendant.

Figure-1.1: Repackaged version UNO Learning Kit Figure-1.2: USB cable

(2) Connect the UNO Board with PC using the USB Cable.
(3) Create the following folder under D-drive of your PC.
D:\A1XXX A1 : The Section name XXX : Last 3-digit of student ID
(4) Check that the following icon is present in the Desktop and then click on it; otherwise, down
load Arduino-1.8.19-widows (IDE Interface), install it and then click on the following icon.

1
(5) Observe that the following window (Fig-1.3) has appeared on Desktop. It is known as
Integrated Development Environment (IDE) Interface. The IDE allows creating source
codes/sketch, compile sketch, and upload binary code of sketch into Flash Memory of MCU.

Figure-1.3: IDE (Integrated Development Environment) Interface

(6) Observe that the following two functions: setup() and loop() have appeared in the IDE
of Fig-1.3 along with some extra texts. Delete the extra texts. Align the opening brace ({) and
closing brace (}) so that the functions appear as follows:
void setup()
{

void loop()
{

(7) Write the following texts (the comments) at the top of the setup() function of Step-6.
//this program will test the UNO Board by adding two hex (hexadecimal) numbers.

(8) Save sketch or source code or program (whatever has been typed in IDE) of Step-6 as:
D:\A1XX\E116. Do not give any extension after P116; it will be automatically given by IDE.
E116 stands for: Experiment-1, Task-1, Step-6.
(9) Now, let us put codes/instructions in the sketch for adding these two numbers: 0x31 and
0x29. The result (5A) will appear on Serial Monitor (Fig-1.4) at 1-sec interval.
Task(s) executed for once or for a number of times are put under setup() function. Tasks
executed repeatedly are put under loop() function. Type the following codes in the IDE.
byte sum = 0x00; //global variable declaration in Global Space to hold the result

void setup()
{
Serial.begin(9600); //Serial communication port is enabled at speed 9600 bits/sec

2
byte x1 = 0x31; //1st number; 0x indicates that the number has hexadecimal base (16)
byte x2 = 0x29; //2nd number
sum = x1 + x2; //sum = result = 5A (in hexadecimal base)
}

void loop()
{
Serial.print(sum, HEX); //show result in SM in hex form
Serial.println(); //command to insert Newline (ln)
delay(1000); //1000 ms = 1-sec delay is inserted using IDE’s built-in function
}

(10) Save/update the sketch. Press Cntrl+T to insert “indents” automatically for the code lines.
(11) Let us carry out the following step to detect the presence of the UNO Board.
Taskbar of IDE→Tools → Boards → Arduino AVR Boards  select Arduino UNO and click
(12) Let us carry out the following steps to select communication port (COMXX Port) for UNO.
Taskbar of IDE → Tools → Port → select Uno and click
(13) Check that the following message has appeared at the bottom-right corner of the IDE:
Arduino/Genuino Uno on COMXX (XX could be any number)
(14) If you don’t see the message of Step-13, call Lab Teacher to help you. The Lab Teacher will
carry out the following step:
Start→Device Manager → Ports → Arduino Uno (COMXX) → Update Driver Software → …
(15) Let us carry out the following step to compile the sketch and correct the coding/language
errors (syntax and semantic) if there is any with the help of Lab Teacher.
Taskbar of IDE → Sketch → Verify/Compile and click.
(16) Let us check that the message “Done compiling” has appeared. After that look for any
error/warning message in “message box” located at bottom of IDE. Let us correct the errors.
(17) Let us carry out the following step to upload the sketch into the MCU of the UNO Board:
Taskbar of IDE → Sketch → Upload and click.
(18) Let us check that the message “Done uploading” has appeared.
(19) Let us carry out the following step to bring in the Serial Monitor (Fig-1.4) on Desktop of PC
Goto right-top corner of IDE → click on Serial Monitor icon
(20) Check that the following window known as Serial Monitor (SM) has appeared on desktop.

Figure-1.4: Serial Monitor


3
(21) Select the Baud Rate (Bd) at 9600. (Data transmission rate is: 9600 bits/sec.)
(22) Press the RESET button of the UNO Board.
(23) Let us check that result (5A) has appeared on the OutputBox of SM at 1-sec interval.

Task-1.2 Create and upload sketch to multiply two hex numbers (0x31 and 0x29) and show the
hex result (the product = 07D9) on SM at 1-sec interval. Save sketch as E12. If the 4-digit result
does not appear on SM, ask Lab Teacher for help. Check that 7D9 has appeared on SM instead of
07D9; the leading 0 is missing. The print() method does not show “leading 0” on SM.

Task-1.3 In Tasks-1.2, the SM has not shown the “leading 0”. Now, put the following codes at
the beginning of loop() function of E12 to see leading 0 of result. Save the program as E13.
byte y = highByte(product); //separate higher 8-bit from the 16-bit result named prooduct
if(y < 0x10) //0x00 – 0x0F has leading 0s.
{
Serial.print(‘0’); //show 0 on SM; for a single digit or character use single quote
}
Serial.print(y);

Task-1.4 Look for a built-in LED (L, Fig-1.5) on UNO Board. There is a ready-made sketch in
IDE to blink L at 1-sec interval. Carry out the following steps to get that sketch and save as E14.
(1) Taskbar of IDE → File → Examples → Basics → Blink and click
(2) Delete all unnecessary comments from the sketch to keep only the following form.
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); //LED_BUILTIN has been defined in IDE as DPin-13 (Fig-1.5)
}

void loop()
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for 1000 ms = one second
}

(3) Verify/Compile and upload the sketch. Let us check that L is blinking at 1-sec interval.
(4) Study the following connection diagram for the built-in LED (L) of Arduino UNO Board.
MCU Pin Built-inL
Pin Signal
Arduino UNO
DPin (Digital Pin)
PORTB
pb5 13
19 PB5 L
PB5
PB5

U5B RN2A : 1k
GND 22

This circuit within dotted oval is on the UNO Board

Figure-1.5: Connection diagram of built-in LED (L) of UNO Board


4
Task – 1.5 [Blink L of Fig-1.5 without using delay() function] When the delay(1000)
function is called, the MCU is blocked 1-sec time. The MCU cannot do any other job until the
1-sec time delay has elapsed. This is not a good situation as there might be a breakout of fire
within this 1-sec time which for sure will go unnoticed. Let us see how we can blink the LED/L
without using the delay(1000) function. The process is known as “Blink without Delay (function)”.
In this process, the MCU will carry two jobs simultaneously ― blinking the LED at 1-sec interval
and checking the Fire Alarm.
(1) Upload the following sketch. Save the sketch as E151.
unsigned long prMillis = 0;
byte ledState = HIGH; //initially LED is ON

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, ledState); //LED is ON
prMillis = millis(); //millis() function works on TC0's interrupt
}

void loop()
{
if (millis() - prMillis >= 1000)//1000 ms = 1-sec is over
{
ledState = !ledState; //ledState is reversed from ON to OFF
digitalWrite(13, ledState); //LED toggles
prMillis = millis();
}
else
{
//do something else; check Fire Alarm
}
}

(2) Working Principle of millis() Function: The above program uses millis() function and
millisCounter to account for the elapse of 1000 ms ( sec) time period. The millisCounter
starts with initial value of 0 (zero) when the uploading process of a sketch is finished in the
UNO. After that it is incremented by the elapse of every 1ms time. The execution of
millis() function reads the current content of the millisCounter, which says how much
ms (millisecond) time has elapsed since the UNO has started. The millisCounter is a 32-bit
unsigned counter (composed of four consecutive RAM locations) and can hold 232 ms time
(49-day 17-hour 2-minute 47-second 296-millisecond) before overflow occurs. For example:
The millis() function can be used in the following ways to observe if 2000 ms (2 sec) has
elapsed from the current instant.
unsigned long prMillis = millis(); //present/current content of millisCounter
do
{
; //do nothing
}
while(millis – prMillis >= 2000)!

//prMillis is fixed; millis() gives current time of millisCounter which continuously advances
by 1 ms.

5
Task – 1.6 [Serial Monitor] In this task, we will program the ATmega328P MCU of the
UNO Board to receive characters from the InputBox of Serial Monitor of Fig-1.4 and to
send the characters to the OutputBox of the Serial Monitor
(1) Upload the following sketch into UNO. Save the sketch as E161.
void setup()
{
Serial.begin(9600); //Serial/UART Port is active. Data rate is: 9600 bits/sec
}

void loop()
{
byte n = Serial.available(); //n = 0 means that no character has arrived from InputBox
if(n != 0)
{
char y = Serial.read(); //one character has come an save it in variable y
Serial.print(y); //show received character on OutputBox of SM
}
}

(2) Open the Serial Monitor.


(3) Select No line ending option for the Line ending tab.
(4) Enter AUST in the InputBox of SM and then click on the Send button.
(5) Check that AUST has appeared on the OutputBox of Serial Monitor.
(6) Enter UNO in the InputBox of SM and then click on the Send button.
(7) Check that UNO has appeared on the OutputBox of Serial Monitor just after the previous
AUST as follows:
AUSTUNO
(8) What we can do to see the words AUSTUNO as follows:?
ASUT
UNO
(a) Click on the Clear output button to delete the messages from the OutputBox of SM.
(b) Select Newline option for the Line ending tab.
(c) Now send ASUT and then send UNO from the InputBox of SM. Check that they have
appeared as follows on the OutputBox of the Serial Monitor.
ASUT
UNO

Task – 1.7 [Collecting your own Arduino UNO and other parts]
Collect the parts as per following parts list, give them to Lab Attendant who will assemble the Learning Kit
for you. You pay the money for the board, plastic box and labor to the Lab Attendant.

6
Parts Needed for Lab Work using UNO
1. Arduino UNO with Cable (1) 6. Four-in-One 7-seg Display Unit (2)

7. Breadboard (Size (6.5”x2.25”) (1)

8. LM35 Analog Temperature Sensor (2)


2. Arduino NANO with Cable (1)
Quantity: 1

9. DS18B20 digital Temperature Sensor (2)

3. 4x 20 Lines LCD (1)

10. DS3231 Real Time Clock Module (1)

4. I2C Controller (1)

11. 3.3V-I2C/SPI BMP280 PT (1)

5. Common Cathode 7-segment Display (6)

12. 5V-I2C BME280 PTH (1)

7
13. 2N7000 MOSFET Transistor (6) 19. Green LED (5)

14. Level Shifter (1) 20. 2.2k Resistor (20)


21. 10k Resistor (20)
22. 4.7 k Resistor (20)
23. 2k Pot (3)

15. OLED Display Unit (1) 24. Male-male Jumper

25. Male-female Jumpers


.

26. HC-05 Bluetooth Module (2)


16. 4x4 Keypad (1)

27. SG-90 Stepper Motor (2)


17. Push Buttons (5)

18. Red LED (5)

8
Experiment – 2
Input and Output Port Programming to drive LED
Switch and 7-segment Display Device
Task-2.1 [Switch and LED] Place R1-LED1 circuit of Fig-2.1 on the breadboard, and
connect it with the UNO via DPin-8. Also, Place R2-K1 circuit on the breadboard, and
connect it with UNO via DPin-A3/17. Now, create sketch so that LED1 will be ON only
when K1 is found closed. (If you no switch K1, then use a jumper as a switch.)

M CU Pin ATmega328P M CU M CU Pin


Arduino UN O
Edge Connector H oles
Port-D 5V 21 AREF
AREF
7 13 pd7 7 To be placed on
Rip Port-C
6 12 pd6 5 pc5 28 A5/19 External Breadboard
D Pin (D igital Pins)

5 11 pd5 S1 pc4 27 A4/18


K1
4 6 pd4 26 A3/17/PI N C3 5V
3 5 pd3 pc2 25 A2/16
8 M Hz R2 = 2.2k
2 4 pd2 pc1 24 A1/15 (external pull-down
Osc
1 3 pd1 0 pc0 23 A0/14 resistor)
0 2 pd0 0 GN D /0V
UN O
10 L
X2(pb6) fosc Edge Connector H oles
Y1 Port-B R2N A:
16 M H z Osc 1k
9 16 M H z 5 PB5 19 13 GN D
Externally short

X1(pb7)
RESET 1 4 pb4 18 12
RESET/(PC6)
5V 20 3 pb3 17 11 This can be found on
AV cc
by UN O

5V 7 D V cc 2 pb2 16 10 UN O Board
GN D 22 AGN D internally 1 pb1 15 9
R1 = 2.2k LED 1
GN D 8 D GN D I n the M CU 0 PB0 14 8 GN D

To be placed on
portI O/50Lxy D Pin-8 (D igital Pin-8)
Figure-2.1: Connection of IO (input/output) devices with Arduino External
UNO Breadboard

(1) Open IDE.


(2) Type the following codes (replication of codes of Section-B in sketch form) to create sketch
and save as P21A.
void setup()
{
pinMode(A3, INPUT);
pinMode(8, OUTPUT);
L3: int n = digitalRead(A3);
if( n != HIGH)
{
goto L3;
}
digitalWrite(8, HIGH); //turn ON LED1
}

void loop()
{

}
(3) Upload the sketch. Gently, press K1.
(5) Check that LED1 is ON.
(6) Re-write program P21A by replacing goto statement with do-while structure as use of goto
statement in programming is not a good practice. Save the program as P21B. Upload the
program. Check that LED1 becomes ON when you just press K1 switch.
bool n;

void setup()
{
pinMode(A3, INPUT);
pinMode(8, OUTPUT);

do
{
n = digitalRead(A3);
}
while(n != HIGH);
digitalWrite(8, HIGH); //LED1 is ON
}

void loop()
{

(7) Re-write program E21B using while-do structure. Save sketch as P21C.

Task – 2.2 [Programming CC-type (common cathode) 7-segment Display Device]


(1) Study Internal Structure of CC-type (common cathode) 7-segment Display Device of Fig-2.2.

10 9 8 7 6
g f GND/ cc a b

f a a

e g b
LED
f b

d c
p cahode
g

6 e c

pp
cc-pin anode d
1 2
34
5 e
2
d GND/ cc c
4
p

(a) Pictorial View (b)1 Internal


3
Structure
5 280

Figure-2.2: Structure of Common Cathode Type 7-Segment Display Device


(2) Create Common Cathode Codes for digit 3 using following Table-2.1.
Table-2.1 : Character vs CC-code Table
Characte p g f e d c b a cc-code in hex format
r
0 0 0 1 1 1 1 1 1 3F
2 0 1 0 1 1 0 1 1 5B
------- ------ ----- ----- ----- ----- ----- ----- ----- ------
F 0 1 1 1 0 0 0 1 71

(3) Build circuit of Fig-2.3 on breadboard. Place display device at left-most side of the
breadboard. Make connections between display devices and UNO as per Fig-2.3. If there is
shortage of resistors, you can use the simplified circuit of Fig-2.4.
7segOneX
UNO Segment Pin CC7SD 2
Digital Pin
DP0
RN1

p g f e d c b a

A0

Dpins: 7 6 13 12 11 10 9 8

Figure-2.3: Connection diagram between cc-type 7-segment display device and UNO

7segOneXY
Segment Pin CC7SD
UNO Digital Pin
DP0
Port-D
7 5
PD6 p
6 10
PD6 g a
13 9
PB5 f
1
PB4
12 e f g b
11 2
PB3 d e c
10 4
PB2 c p
9 6 d
PB1 b
8 7
PB0 a
Port-B RN1:8x2.2k cc0
Port-C
A0/14 3
PC0
R1 = 2.2k
Current Limiting Resistors Segment Signals

Figure-2.4:

(4) Create the following sketch in the IDE. Save the sketch as P22
void setup()
{
//set the directions of the IO lines using 9 pinMode() functions or using for() loop.
//use digitalWrite() function to turn on required segments to see 2.
//Assert LOW on Dpin-14.
}

void loop()
{

Task – 2.3 Create sketch to add two hexadecimal numbers (range: 0x00 – 0x78) 0x31
and 0x29 and show the result (5A) on DP0 position of Fig-2.3. First, 5 will appear on the
display and will remain ON for 1-sec and then A will appear and will stay ON for 1-sec

3
and then 5 and then A…. Save the sketch as P25. Use the Lookup Table of Fig-2.5 to
get the cc-codes for the digits of the result.
Look up Table (LUT): lupTable[16]
Array Location cc-code in hex format digit
lupTable[0] 3F 0
lupTable[1] 06 1
lupTable[2] 5B 2
lupTable[3] 4F 3
lupTable[4] 66 4
lupTable[5] 6D 5
M ember-1 of lupTable[16]
lupTable[6] 7D 6
M ember-1 w ith index-1
lupTable[7] 07 7
lupTable[8] 7F 8
lupTable[9] 6F 9
lupTable[A] 77 A
lupTable[B] 7C B
lupTable[C] 39 C
lupTable[D] 5E D
lupTable[E] 79 E
lupTable[F] 71 F
Figure-2.5: Lookup Table (LUT) for the cc-codes of digits/characters
lutd2ccq 0–F

(1) Create the following sketch in the IDE. Save the sketch as P23.
byte lupTable[] = //This array is the C++ code for the Lookup Table of Fig-2.5
{
0x3F, 0x06, 0x5B, 0x4F,0x66, 0x6D, 0x7D, 0x07, //CC-codes of: 0 - 9
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 //CC-codes of: A - F
}; // array declaration/definition

byte ccd0; //cccd0 will hold cc-code for the left digit of sum = 0x31 + 0x29 = 0x5A = d0d1
byte ccd1;

void setup()
{
//---set direction of IO lines

byte sum = 0x31 + 0x29; //sum = 5A = d0d1

//--extract digit 5 from sum using >> operator


byte d0 = sum >> 4; //d0 = 05 = 5

//---extract digit A from sum by deleting 5


byte d1 = sum & 0x0F; //d1 = 0A = A

//--get cc-code of d0, d1 using Lookup Table (declared as array in Global Area)
ccd0 = lupTable[d0]; //ccd0 = 0x6D
ccd1 = lupTable[d1]; //ccd1 = 0x77
}

{
//--- show 5
PORTB = ccd0; //6-bit of ccd0 has gone to PORTB
digitalWrite(6, bitRead(ccd0, 6));
digitalWrite(7, bitRead(ccd1, 7));
digitalWrite(14, LOW);

4
delay(1000);
//---show A-------------------------

PORTB = ccd1; //6-bit of ccd0 has gone to PORTB


digitalWrite(6, bitRead(ccd1, 6));
digitalWrite(7, bitRead(ccd1, 7));
digitalWrite(14, LOW);
delay(1000);
//----------------------------------------------
}

(2) Compile and upload sketch of Step-1. Check that 5 and A are appearing on the display
device one-after-another.
(3) Re-write sketch of Step-1 so that loop() function contains lesser number of code lines. Save
te sketch as P23A

Task – 2.4 Create sketch to show 0, 1, 2, …, F, 0, 1, .. on DP0 of Fig-2.3 at 1-sec


interval. Get the cc-codes of the characters to be displayed from the array declaration of
Task-2.3. Save the sketch as P24.

5
Experiment – 3
Programming of Multiplexed 7-segment Display Unit and I2CLCD
Task-3.1 [2-digit Multiplexed Display Unit]
(1) Build the circuit of Fig-3.1 (2-digit cc-type 7-segment multiplexed display unit) on the
breadboard. In a multiplexed display unit, the segment pins of the identical segments are shorted
together except the cc-pins. Write program to show simultaneously 2 at DP0 and 5 at DP1
position of the display unit. Save the program as E31.
7segTwo Segment Pin
UNO Digital Pin CC7SD

DP0 DP1
PORTB/PORTD
7 5
PD7 p
6 10 a a
PD6 g
13 9
PB5 f f g b f g b
PB4 12 1
e
PB3 11 2 c c
d e e
PB2 10 4
c p p
PB1 9 6 d d
b
PB0 8 7
a
RN1:8x2.2k
cc0 cc1
PORTC
A0/14 3 3
PC0
A1/15
PC1

Figure-3.1: Connection diagram between 2-digit cc-type 7-segment multiplexed display unit and UNO

(2) Solution
void setup()
{
Set directions of PD7-PD0 lines as output
Set direction of PB5-PB01 line as output
Set direction of PC1-PC0 line as output
}

void loop()
{
Send cc-code of character 2 onto segment DPins
Assert LL on cc0-pin of DPO
Assert LH on cc1-pin of DP1
delay(10); //so that 2 remains visible for a while
//---------------------------------------------------------------------------
Send cc-code of character 5 onto segment DPins
Assert LH on cc0-pin of DPO
Assert Ll on cc1-pin of DP1
delay(10);
}

Codes:
void setup()
{
for (int i = 6; i <= 15; i++)
{
pinMode(i, OUTPUT); //DPins: 7-6, 13-8, A1-A0 work as output lines
}
}
1
void loop()
{
byte y = 0x5B; //cc-code of 2; y7 y6 y5 y4 y3 y2 y1 y0 = 0 1 0 1 1 0 1 1
PORTB = y; //PORTB accepts lower 6-bit and puts on DPins:13-8

bool m = bitRead(y, 6); //y6 = 1 is in variable m


digitalWrite(6, m); //m = y6 = 1 goes on DPin-6

digitalWrite(7, bitRead(y, 7)); //y7 is put on DPin-7; nested instruction


//------------------------------------------------------------------------------
digitalWrite(A0, LOW); //DP0 is ON; 2 appears on DP0 position
digitalWrite(A1, HIGH); //DP1 is OFF ; nothing appears on DP1 position
delay(10); //10 ms wait for a while so that eye/brain can remember 2
//----------------------------------------------------------------------------
byte z = 0x6D; //cc-code of 5
PORTB = z;
int n = bitRead(z, 6);
digitalWrite(6, n);
digitalWrite(7, bitRead(z, 7));
//---------------------------------------
digitalWrite(A0, HIGH); //DP0 is OFF; noting appears on DP0 position
digitalWrite(A1, LOW); //DP1 is ON; 5 appears on DP1 position
delay(10);
}

(3) Upload sketch of Step-2. Check that 25 has appeared on the display.

Task-3.2 Repeat Task-3.1 using for() loop to reduce number of code lines. Save sketch as E32.
Solution Hints:
void loop()
{
for (int i = 0; i < 2; i++)
{
byte x = 0b11111111; //0b means base 2 (binary); to activate cc-pin of display devices
bitClear(x, i); //x = 0b11111110; Bit-0 will be sent at DPin-A0 of display to see 2
//------------------------------------------------------------
byte y = ccCode[i]; //cc-code of 2 and then 5
PORTB = y; //PORTB accepts lower 6-bit and puts on DPins:13-8
bool m = bitRead(y, 6);
digitalWrite(6, m); //y6 is put on DPin-6
digitalWrite(7, bitRead(y, 7)); //y7 is put on DPin-7
//-----------------------------------------------------------
digitalWrite(A0, bitRead(x, 0));//DP0 is ON and then OFF in the next pass
digitalWrite(A1, bitRead(x, 1));//DP1 is ON and then OFF
//------------------------------------------------------------
delay(10); //time delay for the digit to remain ON for a while so that eye can remember it
}
}

Task-3.3 Write sketch to add 0x23 and 0x42. Show result (65) on display unit of Fig-3.1. Use
LUT of Fig-2.5 to get cc-codes for digits 6 and 5 of result. Use right-sift operator to get indices for
digits 6 and 5 of result. The cc-code of 6 is 0x7D and the cc-code of 5 is 0x6D. Save sketch as E33.
(1) Codes to get the cc-codes from LUT Table for the digits of the result:
byte lupTable[16] =
{
0x3F, 0x06, 0x5B, 0x4F,0x66, 0x6D, 0x7D, 0x07, //cc-codes for: 0 -9
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 //cc-code for: A - F
};

2
byte y1 = 0x23;
byte y2 = 0x42;
byte sum = y1 + y2; // sum = 65

//----- extract 6 from 65 using right-sift operator and save as 06---------


byte digitDP0 = (sum >> 4); // shift sum to right by 4-bit; we get: 0000 1010 = 06 = (sum >> 4)
byte ccDP0 = lupTable[digitDP0]; //ccDP0 = cc-code for the digit of DP0 position = 0x7D

//------ now find the ccCodeDP1-------------------------


byte ccDP1 = luTable[(sum & 0x0F)];
//sum & 0x0F ==> 0x65 & 0x0F ==> 1010 0101 & 0000 1111 ==> 0000 0101=05

(2) Upload the sketch and check that the display shows: 65

Task-3.4 Repeat Task-3.3 where the indices for the digits of sum are to be found using modulus
(%) and division (/) operators. Save sketch as E34.
Solution Hints:
(1) The sum is: 65 in hex base.
byte sum = number1 + number2; //0x23 + 0x42 = 0x65.
(2) Get the indices 06 and 05 for the digits 6 and 5.
byte idxDP0 (index of digit to be displayed on DP0) = sum%16 (idxDP0 = 0x05)
sum = sum/16 (quotient 60 is in sum)
byte ccDP0 = lupTable[idxDP0] ((ccDP0 = cc-code for the digit to show on DP0 = 0x6D)
//-----------------------------------------------------
byte idxDP1 = sum%16 (idxDP1 = index for the digit to be displayed on DP1 = 06)
byte ccDP1 = lupTable[ccDP1] (ccDP1 = cc-code for the digit to show on DP1 = 0x7D)

(3) Upload the sketch and check that the display show: 65.

Task-3.5 Create sketch to show 25 on the display unit of Fig-3.1 using readymade codes,
routines, functions, LUT of this library: SevSeg.h. Save program as P118.
(1) The Library named SevSeg.h contains ready-made codes, routines, functions, and LUT
tables written by someone else and it can be used to drive multiplexed 7-segment Display Unit.
Now, the user is not required to execute pinMode(), digitalWrite(), delay(), for(), LUT and
other commands in the sketch. The Library must be down loaded from the internet and then it
has to be included in the sketch and IDE. In this subsection, we will use SevSeg.h Library to
show 2 and 5 on the display unit of Fig-3.1.

The Library contains following functions and declarations to drive multiplexed 7-segment
display unit.
(a) sevSeg.begin(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
 sevSeg.begin(displayType, numDigits, ccDPins, segDPins, softCurrentLimitResistor,
updateWithDelays, leadingZeros, disableDecPoint);

arg1= Display type : Value = COMMON_CATHODE or COMMON_ANODE.


When COMMOM_CATHODE is selected, the program automatically consults digit-vs-
ccCode LUT Table (Fig-1.7) to collect the ccCodes for the digits of display unit.

arg2 = Number of digits to be shown: Value = 1 to 8


3
arg3 = an array that contains DPins with which the cc-pins of the digits are to be connected
arg4 = array that contains DPins with which segments-pins of the digits are to be connected

arg5 = SoftcurrentLimitResistor (Software controlled): Value = true or false


true means software controlled current limit resistor;
false means that the current limiting resistors are installed manually.

arg6 = updateWithDelays : true or false (choose false; why? No idea.)

arg7 = leadingZeros : Value = true or false;


true means keep the leading zeroes like: 05

arg8 = disableDecPoint : Value = true or false;


false means that we can show decimal point at any position of the display unit. The position
is determined by sevseg.SetNumber() function.
 sevseg.begin(COMMON_CATHODE, 2, ccDPins, segDPins, false, false, false, true); //no point

(b) sevenSeg.SetNumber(arg1, arg2, arg3);


 sevenSeg.SetNumber(numberToShow, placeOfDecimalPoint, base);

arg1 = integer value to be displayed


Declaration:
byte valueToShow = 25; //range: 0 to 255;
unsigned int valueToShow = 0 to 35535;

arg2 = after how many digits (counting from right), the decimal point is to be placed
Value: 1 //23.5 an example of a 3-digit display with decimal point before 1-digit from right

arg3 = base in which the “number of arg1” is to be shown; Value = HIGH or LOW
HIGH means that the hexadecimal digits (0 – 9, A- F) will appear on display unit.
LOW means that decimal digits (0 – 9) will appear on the display unit.

(c) sevSeg. refreshDisplay(); //performs the action of display refreshing


As the digits of a multiplexed display unit share common segment lines, we must show the digits
one after another with some delay in-between; otherwise, the display unit will be frozen to a
single digit. This process is known as refreshing. The sevSeg.refresh() function performs this
refreshing job. The C++ codes of this function are within the SevSeg,h Library.

(d) Array declaration for the DPins of segment-lines of the digits of display unit
byte segDPins[] = {DPin-a, DPin-b, DPin-c, DPin-d, DPin-e, DPin-f, DPin-g, DPin-p};
 byte segDPins[] = {8, 9, 10, 11, 12, 13, 6, 7};

(e) Array declaration for the DPins of cc-lines of the digits of display unit
byte ccDPins[] = {DPin-cc0, DPin-cc1}; //DPin-cc0 = DPin with which cc0-pin of DP0 is connected
 byte segDPins[] = {A0, A1};

(f) Procedures to include SevSeg.h Library with IDE

4
(i) Download SevSeg.h Library (zip file) from the following this link:
https://github.com/DeanIsMe/SevSeg
(ii) Open IDE.
(iii) Tool Bar → Sketch → Include Library → Add .ZIP Library… and then follow menu.

(2) Upload the following sketch. Save as E35.


#include<SevSeg.h>
SevSeg sevSeg; //create object sevSeg

byte numberToShow = 25;

void setup()
{
byte segDPins[] = {8, 9, 10, 11, 12, 13, 6, 7}; //DPin-8 = seg-a, …, DPin-7 = seg-p
byte ccDPins[] = {A0, A1}; //DPin-A0 = cc0, DPin-A1 = cc1
sevSeg.begin(COMMON_CATHODE, 2, ccDPins, segDPins, false, false, false, true);
sevSeg.setNumber(numberToShow, 0, LOW);
}

void loop()
{
sevSeg.refreshDisplay();
}

(3) Check that 25 has appeared on the display unit.


Task-3.6 Repeat Task-1.3.5 so that the display unit of Fig-3.1 shows 2.5. Save program as E36.

Task-3.7 If you include this line: sevseg.setNumber(valueToShow, 0, HIGH);, in the


program of Task-3.6., what will appear on the display unit? Save program as E37.

Task-3.8: Building 3-digit Thermometer using LM35 Temperature Sensor


(1) Build the following circuit of Fig-3.2 on the breadboard.

Figure-3.2: LM35 Sensor based Thermometer with 3-digit multiplexed display unit

5
(1) LM35 is an analog type temperature sensor. It produces 250 mV signal at its output terminal
when room temperature is 250C; it produces 400 mV signal when room temperature is 400C. The
precision of the sensor is 1-digit after decimal point and the resolution/accuracy is ± 0.5 0C.

(2) If room temperature is T0C and the corresponding output signal of LM35 sensor is VDT (VDT
stands for “DC Voltage when temperature is T”), then relation between T and VDT stands as:
T = 100*VDT //where VDT is in volt

(3) The analog signal VDT is converted into 10-bit digital signal by an internal ADC (analog to
digital converter) of the MCU. The ADC output can be put into variable x by executing the
following command/code:
unsigned int x = ananlogRead(A4); //where A4 is the UNO pin with which LM35 is connected

(4) The relation between VDT and x of Step-3 can be established as:
VDT = (1.1/1023)*x
 VDT = (1.1/1023)*analogRead(A4)

(5) Finally, we can have the following relationship between T and “the output of LM35 Sensor”:
T = 100*(1.1/1023)*analogRead(A4) //T is floating point number with integer and fractional
parts

(6) The Sketch/Program in Text/Pseudo Codes


1. Create Lookup Table that will hold cc-codes for the digits: 0 – 9 and A - F
void setup()
{
2. Set directions of IO lines as outputs.
3. Select 1.1V for VREF pin of the ADC.
}
void loop()
{
4. Wait for 2-sec.
5. Acquire Temperature Signal from LM35 and save its digital form into variable x.
6. Compute T (Temperature in floating number form) from x. (For example: 25.3697xx…)
7. Multiply T by 100.0 to get 4-digit integer and many-digit fractional parts (2536.97xx)
8. Get only the integer part from 2536.97xx.. (2536)
9. Convert 2563 into 02 05 03 06
10. Get cc-code from Lookup Table of for digits: 2, 5, 3, 6 and save them in an array.
11. Add decimal point with digit-5 so that display shows 25.3.
12. Send cc-code of 2 on display device to see 2 at DP0 position.
12. Send cc-code of 5 on display device to see 5 at DP1 position.
13. Send cc-code of 7 on display device to see 3 at DP2 position.
}
(7) Programming Codes for the Pseudo Codes of Step-6 (LM35CC7Seg.ino)
1. byte lupTable [] =
{
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,

6
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71
}; //Digit-vs-cccode: 0 - 9, A - F

2. for (int i = 6; i < 17; i++)//a=8,b=9,c=10,d=11e=12,f=13,g=6,p=7;A0=cc0,A1=cc1..


{
pinMode(i, OUTPUT); //directions are as outputs
}

3. analogReference(INTERNAL); //1.1V Vref of ADC

4. unsigned long prMillis = millis();


while (millis() - prMillis < 2000)
{
refreshDisplay(); //multiplexed display must be refreshed periodically
}

5. unsigned int x = analogRead(A4);

6. float temp = (float)100 * ( 1.1 / 1023) * x;


 float temp = (float)100 * ( 1.1 / 1023)*analogRead(A4); //temp = 25.3697xx…

7. float temp1 = temp * 100.00; //temp1 = 25.36.97xx…

8. unsigned int myTemp = ((unsigned int)temp1); //temp1 = 2536

9. byte myDigit[4];
for (int i = 0; i < 4; i++)
{
myDigit[i] = myTemp % 10; //06 comes out from 2536 and is saved in myDigit[0] ...
myTemp = myTemp / 10;
}

10. byte ccCode[3];


ccCode[0] = lupTable[myDigit[3]]; //ccCode[0] contains cc-code of 2 of 3176
ccCode[1] = lupTable[myDigit[2]]; //ccCode[1] contains cc-code of 5 of 3176
ccCode[2] = lupTable[myDigit[1]]; //ccCode[1] contains cc-code of 3 of 3176
11.`ccCode[1] = ccCode[1] | 0x80; //add decimal point with DP1 digit
12. void refreshDisplay()
{
for (int i = 0; i < 4; i++)
{
byte y = 0b11111111; //to activate DP0, then DP1, then DP2 and then DP3
byte x = ccCode[i];
PORTB = x; //x5 – x0 go onto f – a
digitalWrite(6, bitRead(x, 6)); //x6 goes on g
digitalWrite(7, bitRead(x, 7)); //x7 goes on p
//--------------------------------
bitClear(y, i); //y = 1111110
digitalWrite(A0, bitRead(y, 0));
digitalWrite(A1, bitRead(y, 1));
digitalWrite(A2, bitRead(y, 2));
digitalWrite(A3, bitRead(y, 3));
//--------------------------------
delay(1); //so that digit remains visible on DP0..DP3 for a while
}
}

(8) The Complete Sketch (Version-1) Save as E388 or LM35CC7Seg


unsigned int myTemp; //variable to hold integer part of temperature
byte myDigit[4]; //to hold indices for the digits of myTemp
7
byte ccCode[4]; //to hold cc-codes for the digits of myTemp
byte lupTable [] =
{
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71

};//Digit-vs-cccode: 0 - 9, A - F

void setup()
{
Serial.begin(9600);
for (int i = 6; i < 17; i++)//a=8,b=9,c=10,d=11e=12,f=13,g=6,p=7;A0=cc0,A1=cc1..
{
pinMode(i, OUTPUT); //directions are as outputs
}
analogReference(INTERNAL); //1.1V Vref of ADC
}

void loop()
{
unsigned long prMillis = millis();
while (millis() - prMillis < 2000)
{
refreshDisplay();
}
//---------------------
myTemp = acqTemp();//myTemp = 25.3697xx
getccCode(); //convert dogits of myTemp into their cc-codes
refreshDisplay();
}

unsigned int acqTemp()


{
float temp = (float)100 * ( 1.1 / 1023) * analogRead(A4); //temp = 25.36
Serial.println(temp, 2); //Serial monitor shows: 25.36
float temp1 = temp * 100.00; //temp1 = 2536.xx
return ((unsigned int)temp1); //returning: 2536
}

void getccCode()
{
for (int i = 0; i < 4; i++)
{
myDigit[i] = myTemp % 10; //06 comes out from 3176 and is saved in myDigit[0] ...
myTemp = myTemp / 10;
}
ccCode[0] = lupTable[myDigit[3]]; //ccCode[0] contains cc-code of 2 of 3176
ccCode[1] = lupTable[myDigit[2]]; //ccCode[1] contains cc-code of 5 of 3176
ccCode[1] = ccCode[1] | 0x80; //add decimal point with DP1 digit
ccCode[2] = lupTable[myDigit[1]]; //ccCode[2] contains cc-code of 3 of 3176
ccCode[3] = lupTable[myDigit[0]]; //ccCode[3] contains cc-code of 6 of 3176
}

void refreshDisplay()
{
for (int i = 0; i < 4; i++)
{
byte y = 0b11111111; //to activate DP0, then DP1, then DP2 and then DP3
byte x = ccCode[i];
PORTB = x; //x5 – x0 go onto f – a
digitalWrite(6, bitRead(x, 6)); //x6 goes on g
digitalWrite(7, bitRead(x, 7)); //x7 goes on p

8
//--------------------------------
bitClear(y, i); //y = 1111110
digitalWrite(A0, bitRead(y, 0));
digitalWrite(A1, bitRead(y, 1));
digitalWrite(A2, bitRead(y, 2));
digitalWrite(A3, bitRead(y, 3));
//--------------------------------
delay(1); //so that digit remains visible on DP0..DP3 for a while
}
}

(9) Upload sketch of Step-8 and check that display unit shows room temperature.
(10) The Complete Sketch (Version-2) using SevSeg.h Library. Save as E3810 or LM35SevSeg.
#include <SevSeg.h>
SevSeg sevSeg;

void setup()
{
Serial.begin(9600);
byte ccDPins[] = {A0, A1, A2, A3}; //A0 = cc0-pin, A1 = cc1-pin, ...
byte segDPins[] = {8, 9, 10, 11, 12, 13, 6, 7}; //8 = seg-a, 9 = seg-b ...
sevSeg.begin(COMMON_CATHODE, 4, ccDPins, segDPins, false, false, false, false);
analogReference(INTERNAL); //1.1V Vref for ADC
}

void loop()
{
unsigned long prMillis = millis();
while (millis() - prMillis < 2000) //wait for 2-second
{
sevSeg.refreshDisplay(); //keep refreshing display until 2-sec has elapsed
}
float rawTemp = 100 * ( 1.1 / 1023) * analogRead(A4);// 31.25xxxx...
rawTemp = rawTemp*100.00; //myTemp = 3125.xxxx...)
unsigned int myTemp = (unsigned int)rawTemp;//rawTemp = 3125(.xxx...)takes only integer part
sevSeg.setNumber(myTemp, 2, LOW); //shows: 31.2(5) point before 2-digit from right, base = 10
}

(11) Upload sketch of Step-10 and check that display unit shows room temperature.
(12) Touch LM35 sensor by fingers and check that display reading changes.

Task-3.9 Operation of I2C Controller Based LCD (I2CLCD) Liquid Crystal Display)
(1) Let us collect the following LCD Panel of Fig-1.1.

Figure-3.3: I2C Controller based LCD Panel


9
(a) There are 2 lines in the LCD ⎯TopLine and BotLine.
(b) In every line, there are 16 display blocks ⎯ DP0, DP1, …, DPF (DP15).
(c) Each display block can show a character of the English Language Alphabet (0 – 9, A –
Z, a – z, punctuation marks, and especial characters).
(d) A display block is a 7x5 dot matrix element.
(e) An I2C Controller has a 7-bit address and it is 0b0100111 (0x27) or 0x3F for this
controller.
(2) Connect the LCD Panel with UNO Kit as per following diagram of Fig-3.4.

UNO Kit I2C Bus I2C LCD

SDA(PC4) A4 SDA
SCL(PC5) A5 SCL
5V 5V Vcc
GND
GND GND
lcdx

Figure-3.4: Connection diagram between UNO Kit and LCD Panel

(3) Create sketch to add 0x21 and 0x86 and show the hex result (A7) at DP0DP1 position of the
Topline of LCD. Also, show the hex result on SM (Serial Monitor) at 1-sec interval. Save the
sketch under your folder as E393.
#include<Wire.h> //Library header file; it is needed for the I2C
Controller
#include<LiquidCrystal_I2C.h> //this header is needed for I2C Controller based LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); //lcd is an object; I2C address, 16 characters, 2 line

byte sum = 0x00;

void setup()
{
Serial.begin(9600); //needed for SM
//--next 3 lines are for I2C based LCD----
lcd.init();
lcd.backlight(); //this function brings light at the back of LCD
lcd.setCursor(0, 0); //1st argument refers display block 0; 2ndarg = 0 refers TopLine
//---add the numbers----------------------
byte x1 = 0x21;
byte x2 = 0x86;
sum = x1 + x2; //result = A7
//--- show result at DP0DP1 positions of the TopLine of LCD--------
lcd.print(sum, HEX);
}

Void loop()
{
//---show hex result on SM line after line---
Serial.println(sum, HEX); //Serial.print(sum, HEX) and Serial.println() are now combined
delay(1000);
}

(4) Compile and upload the sketch. Check that LCD and SM show the correct result. Show the
result to the Lab teacher.

10
Experiment – 6
UART Port Based Serial Data Communication
(When a Task/Step is done, put Tick Mark  on it)

Task – 6.1 Read the following Architecture of UART, SUART, and Serial Monitor

Figure-6.1: UART and SUART Ports of ATmega328P MCU

Task–6.2 Read the following Functions for UART Port based Serial Data Communication
Sn Function/Method Description Comment
1 serialEvent(); When a character arrives in receiver of UART, MCU is
interrupted and then goes to serialEvent() subroutine to collect
character from buffer. It does not work with Soft UART.
2 Serial.available(); The Serail.available() function allows to know how many bytes
of data are still there in the buffer.
3 Serial.read(); This code reads a data byte from buffer..
4 Serial.write(arg); This function writes single byte/multi byte data into buffer
Serial.write(array, size of array); for onward transmission.
5 Serial.print(arg); This function writes a character into buffer for onward
transmission to OutputBox of Serial Monitor..
6 Serial.println(); This code keeps two characters – CR (0x0D) first and line feed
(0x0A) for onward transmission to OutputBox of SM.
7 Serial.println(arg); This code keeps three byte data – value arg, CR, and LF for
onward transmission OutputBox of Serial Monitor..
8 Serial,begin(arg); Sets the Baud Rate (Bd) of the communication channel.
9 Serial.end(); Ends the serial communication. The TX and RX lines could be
used as general digital IO lines.
10 SoftwareSeral.h Header File which supports software UART )SURT) Port.
11 SoftwareSerial SUART(5, 6); Creates SUART Port with SRX Line attached at DPin-5 and
STX Line attached at DPin-6.

1
Task – 6.3 Exchanging Characters between Serial Monitor and UNO
(1) [Single Charcater] Open IDE and create the following sketch to receive character A from the
InputBox of Serial Monitor (Fig-6.1) by UNO and send it back to the OutputBox of the Serial
Monitor. Select ‘No line ending” option for the “Line ending tab” of Serial Monitor. Save the
program as P631.
void setup()
{
Serial.begin(9600);
}

void loop()
{
byte n = Serial.available(); //n = 1
if ( n != 0) //Buffer contains at least one data byte
{
char ch = Serial.read();
Serial.print(ch);
}
}

(2) [Multiple Characters] Open IDE and create sketch to receive the message “AUST” from
Serial Monitor by UNO , save it in an array named myArray[] and send it back to Serial Monitor.
Choose Newline option for the Line ending tab of the Serial Monitor. Save the program as P632.
char myArray[10];
int arrayIndex = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
byte n = Serial.available();
if(n != 0)
{
char ch = Serial.read();
if(ch != '\n') //not Newline charcater
{
myArray[arrayIndex] = ch;
Serial.print(ch);
arrayIndex++;
}
else
{
Serial.println();
myArray[arrayIndex] = '\0'; //insert null-charcater
Serial.println(myArray);
arrayIndex = 0; //reset
}
}
}

2
(3) [use of atoi() (ASCII TO Integer) function to extract decimal number from incoming ASCII coded
data] Create sketch to receive 1234 (with Newline option) from Serial Monitor, receive it and save
in an array, convert them into a numerical integer value to save in variable y, and then send y to
the OutputBox of the Serial Monitor. Save the program as P633.
char myData[10];

void setup()
{
Serial.begin(9600);//UNO's UART is active
}

void loop()
{
byte n = Serial.available(); //n = 1
if ( n != 0)
{
byte m = Serial.readBytesUntil('\n', myData, 10);//arg3 = arraySize
// m = number of charcaters stored in Buffer except Newline ;
int x = atoi(myData);
Serial.println(x, DEC);
}
}

(4) [use of atof()(ASCII TO Float) function to extract floating point number from incoming ASCII
coded data] Create sketch to receive 6712.56 (with Newline option) from Serial Monitor, receive it
and save in an array, convert them into a numerical floating point number to save in variable
fNumn, and then send the fNum to the OutputBox of the Serial Monitor. Save the program as
P634.
char myData[10];

void setup()
{
Serial.begin(9600);
}

void loop()
{
byte n = Serial.available(); //n = 1
if ( n != 0) //Buffer contains at least one data byte
{
//keep receiving data bytes until Newline is found
byte m = Serial.readBytesUntil('\n', myData, 10);//arg3 = arraySize
// m = number of charcaters stored in Buffer except Newline
float x = atof(myData);
Serial.println(x, 2);
}
}

(5) Create sketch to start reading characters from Serial Buffer when it has finished
accumulating only four characters. Save sketch as P635.
i. Open IDE and create sketch at Bd = 9600.
ii. Upload sketch.
iii. Open Serial Monitor (SM) at Bd = 9600.

3
v. Enter Ahsanullah in the InputBox of SM.
vi. Check that only Ahsa has appeared on the OutputBox of Serial Monitor.

Task - 6.4 Programming of the UART Port


Write sketch so that when you send message “Turn ON L” from SM of Fig-6.2 with Newline
option, then L of UNO will be ON. The message L is ON will also appear SM. Save sketch as P64.
uartBlk

Connector
Serial Monitor UNO

Turn ON L (InPutBox) Send FIFO BUFFer MCU


0 ?
L is ON TXD 1 u
(OutputBox) RXD Out TXD
L
9600
13
63
In RXD
Newline
(bits/sec)

Figure-6.2:

char recString[10]; //array to store received characters


int i = 0; //array index

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop()
{
byte n = Serial.available(); //check that a character has arrived
if(n !=0) //a character has arrived
{
char y = Serial.read(); //put the ASCII of character in y
if(y == ‘\n’) //C code for Newline character is found;
{
recString[i] = '\0'; //insert null-byte
bool m = strcmp(recString, "Turn ON L"); //comparing two arrays
if(m == 0x00) //strng matches
{
digitalWrite(13, HIGH); // L is ON
Serial.print("L is ON"); //message on Serial Monitor
while(1); //wait here
}
}
else
{
recString[i] = y; //Newline character has not arrived; keep saving characters
i++;
}
}
}

4
Task – 6.5 [use of SUART (Software UART Port) Port] to transmit 0x12 from UNO to
NANO. =w
wNANO receives the data byte and shows on SM2.
1. Connect UNO and NANO using SUART Port as per Fig-6.1.
2. Include the following lines in the sketch to create and activate SUART Port.
#include<SoftwareSerial.h>
SoftwareSerial SUART(SRX, STX);
SUART.begin(9600);
3. Upload the following sketch in UNO/Sender. Save sketch as E653M.
#include<SoftwareSerial.h>
SoftwareSerial SUART(6, 7); //SRX = DPin-6, STX = DPin-7

void setup()
{
Serial.begin(9600);
SUART.begin(9600);
}

void loop()
{
SUART.write(0x12);
delay(1000);
}
4. Upload the following sketch in NANO/Receiver. Save sketch as E654S.
#include<SoftwareSerial.h>
SoftwareSerial SUART(6, 7); //SRX = DPin-6, STX = DPin-7

void setup()
{
Serial.begin(9600);
SUART.begin(9600);
}

void loop()
{
byte n = SUART.available();
if (n != 0)
{
byte y = SUART.read(); //y = 0x12
//--converting 0x12 into 0x31 and 0x32 to display
byte z = y;
z = z >> 4;
z = z + 0x30; //z = 0x31 = ASCII code of 1
Serial.print(z); //shows: 1
//--------
y = y & 0b00001111; //y = 0x02
y = y + 0x30; // y = 0x32 = ASCII code of 2
Serial. prinln(y); //shows: 2
}
}
5. Check that 12 appear on SM2 at 1-sec interval.
6. Reduce codes of the loop() function of Step-4 by replacing by the two Serial.print()
commands by a single Serial.println() command.

5
Task – 6.6 [use of SUART Port] to transmit a string from UNO to NANO
(1) UNO will send this message "Forum" to NANO using SUART Port (Fig-6.3) at 1-sec interval

(2) The Start-Marker (STX = 0x02) will be sent first to mark the beginning of message.

(3) Then the string "Forum" will be sent.

(4) Then a checksum (CKHSUM) byte will be sent. CHKSUM is computed by adding all the
bytes of the message, discarding the carry, and then taking 2's complement of the remainder.

(5) Then send (ETX = 0x03) as the End-Marker of the message.

(6) At the receiver side, the NANO detects the STX first and then will collect data bytes of the
arrived message including the CHKSUM. Reception will end when ETX is detected. After that all
the bytes of the received message will be added, discard the carry, and then adds with the
received CHKSUM. The result should be zero indicating valid message and then NANO will
flash LED1 (Fig-1).

(7) Build the following circuit of Fig-1 using SUART Port (Software UART Port).

Figure-6.3:

(8) Upload the following sketch in UNO. Save the sketch as E668.
#include<SoftwareSerial.h> //Hedaer file that includes ready-made functons
SoftwareSerial SUART(2, 3); //SUART port is created with SRX = DPin-2, STX = DPin-3
#define STX 0x02 //STX = Start of Text
#define ETX 0x03 //ETX = End of Text

char myMsg[] = "Forum"; //Message/sting to be sent


byte CHKSUM = 0;

void setup()
{
Serial.begin(9600); //UART Port is enabled
SUART.begin(9600); //SUART port is enabled
}

6
void loop()
{
SUART.write(STX); //StartMarker is end
SUART.write(myMsg, sizeof myMsg - 1); //bytes of the message are sent
//--------------------------
for (int i = 0; i < sizeof myMsg - 1; i++)
{
CHKSUM += myMsg[i];
}
CHKSUM = ~CHKSUM + 1; //computing check sum = F7
SUART.write(CHKSUM); //sending CHKSUM to NANOCHKSUM = 0;
CHKSUM = 0;
//-----------------------------
SUART.write(ETX); //EndMarker is sent
delay(1000); //test interval
}

(9) Upload the following sketch in NANO. Save the sketch as E669.
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);
#define STX 0x02 //STX = Start of Text
#define ETX 0x03 //ETX = End of Text
char myMsg[10];
byte CHKSUM = 0;

void setup()
{
Serial.begin(9600);
SUART.begin(9600);
pinMode(4, OUTPUT);
}

void loop()
{
byte n = SUART.available();
if ((n == 1) && (SUART.read() == STX))//StartMarker is detected
{
byte m = SUART.readBytesUntil(ETX, myMsg, 10); //save all except ETX
//-----------------------
for (int i = 0; i < m - 1; i++)
{
CHKSUM += myMsg[i]; //add all the bytes of received message
}
if (CHKSUM + myMsg[5] == 0) //the result should be zero
{
digitalWrite(4, HIGH); //flash LED1 at DPin-4
delay(200);
digitalWrite(4, LOW);
delay(200);
}
CHKSUM = 0; //reset CHKSUM variable
}
}

(10) Observe that LED1 of Fig-6.3 flashes at 1-sec interval indicating reception of valid string.

7
Task – 6.7 [use of SUART Port] to transmit signal from NANO to UNO
Connect LM35 temperature sensor with NANO as per Fig-6.1. Now, perform the following steps:
(1) Create sketch for NANO to acquire temp signal from LM35 sensor and show it on SM2 with
1-digit precision. NANO will also send the temp signal to UNO using SUART(6, 7) Port. Save
program as P671nano.

(2) Create sketch for UNO to acquire temp signal from NANO using SUART(6, 7) Port and
show it on SM1 with 1-digit precision. Save program as P672uno.

You might also like