Expt 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

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.

(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.
1
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
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)
}

2
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


(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.

3
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.

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


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)”.
4
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 2 32 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.

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()

5
{
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.

Parts Needed for Lab Work using UNO


1. Arduino UNO with Cable (1)

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

8. LM35 Analog Temperature Sensor (2)

2. Arduino NANO with Cable (1) 9. DS18B20 digital Temperature Sensor (2)
Quantity: 1

10. DS3231 Real Time Clock Module (1)


3. 4x 20 Lines LCD (1)

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

4. I2C Controller (1)

12. 5V-I2C BME280 PTH (1)

5. Common Cathode 7-segment Display (6)

13. 2N7000 MOSFET Transistor (6)

6. Four-in-One 7-seg Display Unit (2)


14. Level Shifter (1)

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

25. Male-female Jumpers


.

16. 4x4 Keypad (1) 26. HC-05 Bluetooth Module (2)

17. Push Buttons (5) 27. SG-90 Stepper Motor (2)

18. Red LED (5)

19. Green LED (5)

20. 2.2k Resistor (20)


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

You might also like