Professional Documents
Culture Documents
Expt 1
Expt 1
Expt 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.
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.
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.
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
}
}
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
7. Breadboard (Size (6.5”x2.25”) (1)
2. Arduino NANO with Cable (1) 9. DS18B20 digital Temperature Sensor (2)
Quantity: 1
7
15. OLED Display Unit (1)
24. Male-male Jumper