23oct 44IOTG Lab 1
23oct 44IOTG Lab 1
School of Engineering
Internet of Things (IoT) 44IOTG-012628
OBJECTIVES
Setup Arduino IDE for the target MCU
Understand the Arduino environment
Understand IO pins on the target MCU board
Use the Serial monitor for program testing and debugging
Observe the problem of delay() for a responsive system
Try out a responsive demo program
EQUIPMENT
Sign out for the semester: ESP32 IoT kitset + LED-PB board + Grove potentiometer + USB cable
I2C Expansion board with OLED, RTC & MPU6050 for use during the lab session only
AC lamp with Grove control (very limited units)
PREPARATION
Hardware
The hardware includes system mechanical casing, power and various connections, if any, user input (e.g.
buttons, keyboard, touch screen) and outputs (LED, displays).
Below is a “teardown” of a mobile phone which is a often part of an IoT system. It is both an embedded
system and a powerful computer. It has connectivity through WiFi and Bluetooth. There are printed circuit
boards (PCBs) that connect components together, wiring and mechanical parts.
Software
Once you have a reliable hardware, the system intelligence and reliability are determined by the software
run by the processor. Software is an extremely important part of modern electronic systems.
Software makes the system very versatile. It can be easily updated without changing the hardware to
enhance the system behaviour, performance, or remove software bugs (errors). On the other hand, any
hardware change is very costly involving a recall and physical modifications that require specialized skills.
CPU
Many modern SOC has multiple CPUs in a single chip, allowing multiple tasks to be run in parallel
simultaneously. These are called multi-core processor. This feature speeds up the execution of programs,
improves the system performance. More advanced unit has multi-core GPUs (Graphics Processing Unit).
Memory
There are two types of memory: ROM, a read-only memory, is typically a “Flash Memory”, and RAM, a
read-write memory. ROM are non-volatile, i.e. its contents remains when the power is switched off. RAM
are volatile as its contents are lost when the power is removed.
WEB SEARCH:
RAM is a read-write memory. The name “RAM” stands for __________________________
You may want to find out the differences between CPU and GPU.
The Apple A15 Bionic has ___________ 64-bit CPU cores and ________ GPU cores.
C LED-PB board
P23 P19 P18
A D
LED_R LED_Y LED_G
E
UART
F
I2C
G
I Push
Plug into here PB1 PB2 buttons
H P27 P25
A ESP32 DevKitC module; the processor is enclosed in the metallic case (WROOM32 module) with PCB
antenna.
B to F are 4-pin Grove connectors with pins shown in table below.
Grove Purpose Pin 1 2 3 4
Connectors
G2 (D) Analog in, digital IO or Touch A4 (P32) *P27 (T7#) 3V3 GND
I2C (F) or G4 I2C or digital IO SCL (P22) SDA (P21) 3V3 GND
* LED-PB board uses P25 and P27. # These pins can be used as touch inputs
G The LED-PB board is plug into this 8-pin header socket.
H Capacitive Touch (pin P4)
I MCU Reset Button. Press this to restart the program in the MCU. No need to do Arduino upload.
ESP32 Target
Processor platform
Arduino IDE
The Arduino IDE allows us to edit the program, build (compile …) and upload the codes generated to the
target processor board. It also allows the developer to interact with the target board through the “Serial
Monitor”, a useful tool to verify correct program execution, and investigation error (debugging).
There are other IDEs that support Arduino programming. Examples are “Atom” and “Visual Studio Code”.
The Libraries
There are 3 sets of libraries in the Arduino IDE environment. You don’t need to bother with the first 2.
1. Standard Arduino Libraries that comes with the Arduino IDE. These libraries are general and is usable
for different hardware platforms. Example: C:\arduino-1.8.19\libraries.
2. Platform specific library that comes with the target processor installed. These libraries are specific to
the platform. In our lab, the installed ESP32 libraries are stored in (xxx is user name)
C:\Users\xxx\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5\libraries
3. User installed library which are downloaded from various sources. These libraries can be general or
platform specific. In our lab it will be located in D:\arduino-iot\libraries
Each of the above libraries come with the C++ source codes and often with examples. It is a great learning
experience studying these codes. You can easily extend the C++ classes to meet your needs.
How to access a library in a program? Just use the include statement to include the header file in the
library subfolder. Example below includes the header found in “D:\arduino-iot\libraries\_delays”.
#include <delays.h>
Sketch = Program
The programs we write in Arduino are called “sketch” in Arduino. An usual name.
The sketches has file extension .ino and must be stored in a folder of a similar name.
For example, folder “…\xyz” must have sketch name “…\xyz\xyz.ino”
QUESTION:
Why different processor needs different compiler?
_________________________________________________________________________________
What is a cross compiler?
_________________________________________________________________________________
Where will be the location of the user library in our lab?
_______________________________________________
A program written in Arduino IDE is call a ______________________________
CHECKPOINT 1: Show to your supervisor the preparation section ________________
Rotary potentiometer
b. Connect the ESP32 kitset to the laptop USB port to power the unit.
c. Rotate the rotary from one extreme end to another and the 3 LEDs starts to “run” from left to
right.
d. Adjust the rotary to increase the running speed for easier observation for the next step. If the
speed can’t be changed by the rotary, repeat the above by resetting the MCU.
e. Press all these to start/stop the running LEDs: 2 push-buttons (near to the LEDs) and the touchpad.
f. Press the MCU reset button. Ensure that the program is re-started.
The above confirms that the following are working: the rotary potentiometer, 3 LEDs, 2 PBs, touchpad
and the MCU reset button. It is important that all these are working.
C. Arduino IDE
You used Arduino in the past semesters. If your version is already 1.8.19 skip this section. We will not use
the recently available version 2.0 which has some major differences. You may try that on your own.
If your version is not 2.0.14, you may uninstall the older version and do the following, otherwise skip this
section. (The installation below can co-exist with the older version, however, it may be confusing.)
4. Download the IDE zip file: Go here to get the latest version of Windows Arduino IDE installation file:
https://www.arduino.cc/en/software. Scroll down to look for version 1.8.19 and download the ZIP file.
5. Open the zip file using Windows File Explorer. Do not use WinRAR, WinZip nor 7zip. If necessary, right
click the zip file and select Windows File Explorer. The content is a folder shown below.
Open the ZIP file using
Windows File explorer
The content in the ZIP file is a
folder containing Arduino IDE
Select that single content “arduino-1.8.19”. Use Ctrl-C to copy (to Windows clipboard). Go to C:\ and
paste (Ctrl-V). You get “c:\arduino-1.8.19\”. We assume that your IDE is in this folder from now.
If your Arduino is running, quit by pressing Ctrl-Q and test the shortcut at the taskbar to start Arduino.
a. Set the Additional Board Manager URL: In menu “File > Preferences”, click the icon at the
very end of "Additional Boards Manager URLs:" text box and enter (below is a single line):
“https://espressif.github.io/arduino-esp32/package_esp32_index.json”.
Check to ensure that your URL above is correct first by opening the URL in your browser. You
should observe a JSON file, otherwise, your URL is incorrect probably due to typo.
If you already have an ESP32 URL in your Arduino preference but is different, change it!
If you already have other URLs that are not for ESP32, add the above in a new line.
3. Check this
https://
espressif.githu
b. Go to menu “Tools > Board: ... > Board manager …”. It may take a little while to update.
c. In the Boards Manager dialog, and enter “esp32” and press ENTER. Select “esp32 by Espressif
Systems version …” (version 2.0.5 or higher). Click “Install” (or “update” if your version is older).
IMPORTANT: The older ESP32 version 1.0.x is not compatible with some of codes used in the lab.
This will take a while to complete as it has to download the ESP32 compilers (there’re different
versions of ESP32) which are quite large, examples programs, etc.
Site below is for your reference only. No need to watch the video there, nor “Testing the installation”.
https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/
F. My Sketchbook location
Arduino uses the term “sketch” to refer to a program.
9. Create Folder for our sketches: Create a folder d:\arduino-iot (or c:) that will be our default folder to
store our programs.
10. Sketchbook Location in Preferences: Select menu File > Preferences. Set “sketchbook location” to
“d:\arduino-iot”, then OK.
11. Quit Arduino IDE (Ctrl-Q) & Start the IDE again: This will cause Arduino to create a default user library
location for you – see next section.
FI: You can also download a library as a zip file from e.g. github and install through: “Sketch” > “Include
Library” > “Manage Libraries …” > “Add .ZIP library …”. We won’t do that.
Procedures above allow installation of a single library at a time, but we have many libraries.
14. Download “IOT-libraries.zip” file from the BrightSpace under “Module Information” > “Lab Exercises”
> “Lab 1”
Use Windows File Explorer to open the zip file and you should see something shown below. Select all
the items in there. Ctrl-C to copy them all to Windows clipboard.
IOTG Lab 1 Oct 2023 Page 10 of 18
Open the ZIP file in
Windows File Explorer
Contents of the ZIP file with many more
others libraries. Copy them all.
Go to folder d:\arduino-iot\libraries and use Ctrl-V to paste all in there. You should now have these:
15. Restart Arduino IDE in order for these libraries to be recognized. Reason: Manually copying libraries
into the folder is not recognized by the IDE until it is restarted.
16. List the .h and .cpp files found in d:\arduino-iot\libraries\_input\
_________________________________________________________
A reference on library installation: https://learn.sparkfun.com/tutorials/installing-an-arduino-library/all
Yours?
DO NOT choose ESP32S3, ESP32C2 nor ESP32S2. THEY ARE DIFFERENT BOARDS!
22. Choose COM port: Select “Tools” > “Port” > followed by the COM port which you recorded earlier.
Observe the board and COM port info at the bottom right of the Arduino window.
Can I run the “blink” sketch now? Yes. We had selected the correct board and COM port.
Do not change the original program. That is, the variable “led” remains assigned with 13.
What is the unit for time (s, ms or us?) indicated as 1000 in “delay(1000);”? ________________
Study the program and summarize what it does:
___________________________________________________________________________________
___________________________________________________________________________________
Upload the program using one of the methods below. It will build (compile …) first and if there’s no
error, upload the generated codes to the board. The compilation may take some time! Be patient.
- “Sketch” > “Upload”
- Ctrl-U (shortcut, which is the most convenient)
23. Serial Monitor: Open the Serial monitor by clicking icon at the top right (or menu “Tools > Serial
Monitor” or short cut Ctrl-Shift-M). The Serial Monitor Window is shown below. Make sure that the
baud rate is set to 115200.
25. Modify the Blink sketch to that as shown below. We only added in the Serial object functions to tell us
what is going on in the program.
Note that we have a delay(500) immediately after Serial.begin() for the Serial hardware to settle. If
not, the initial program messages sent by Serial may not appear at the Serial Monitor.
int led = 13;
void setup() {
Serial.begin(115200); // communicate with serial monitor at 115200bps
delay(500); // wait for the Serial hardware to settle
Serial.print("\nProgram started");
Serial.print("\nSet pin ");
Serial.print(led);
Serial.print(" to OUTPUT");
// Above 4 statements can be done using single Serial.printf()below
// Serial.printf("\nSet pin %d to OUTPUT",led);
void loop() {
Serial.printf("\nOutput HIGH to pin %d for 1s",led);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
Serial.printf("\nOutput LOW to pin %d for 1s",led);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
You should observe the messages at the Serial Monitor window non-stop:
Note that the program Serial object bit rate must match the Serial Monitor bit rate at 115200bps.
26. Comment/Uncomment lines: To comment out a line or multiple lines, place the cursor at the line or
select multiple lines, then press Ctrl-/. To uncomment, do the same (toggle effect).
Comment out the 4 Serial.print() statements in setup() and uncomment the Serial.printf() statement.
Test the program. Do you get the same output at the Serial Monitor? ____________
27. Serial Printing functions: The program above uses Serial.print() and Serial.printf() functions.
(Serial.println() which does the same as Serial.print() but adds a newline at the end of printing.)
What are the differences for Serial.print() and Serial.printf()?
The printf() is the standard C/C++ formatted print function allowing printing of text and data in a single
statement. The format specifier (e.g. “%d”) must match the data type. (If mismatch? …)
Question: What printf() format specifier to use when printing a float _______, to print a string _______
On the other hand, the print() and println() functions are very special in the sense that they
understands the data type received and print it appropriately. In the above example, the print() was
used to print string and integer. If you pass a float to print(), it will also print correctly. It can print
complex data types like IP address object, e.g. “192.168.1.92”.
FI only: Serial object uses C++ “function overloading”. Behind the scene, there are actually many
print() functions implemented for different number of input data and data types. During compilation,
the compiler automatically picks up the right one to use based on the input parameters. This C++
feature is not available in C.
So which one to use? print() or printf()?
For convenience, printf() is preferred if we want to print data with text. However, we have to be
careful with the data format specifiers. Some Arduino platforms (e.g. Uno) do not support printf().
Use print() if we want to print special object types like an IP address
28. Correct the LED pin number: From the messages at the Serial Monitor, the program looks fine but the
LED pin number is incorrect. Change the led value from 13 to 23. Test the program.
Which LED is blinking now? __________________.
Which LED is blinking? ____________________ LED_Y = _______ the pin number for that LED.
What are these values? LED_Y_ON_VAL = ________________ LED_Y_OFF_VAL = ________________
32. Examine npsoe_iot_kit.h: Determine & record the exact path of the header file npsoe_iot_kit.h. (Hint:
Look at the libraries folder)
___________________________________________________________________________________
Open the file using a text editor (not Word) and determine these pin numbers for the 3 LEDs:
LED_R = _______________ LED_Y = ________________ LED_G = ___________________
What is the value for these LED to be turned on? _________ Thus, they are turn off with value _______
Push buttons: PB1 = _________ PB2 = ________ They are connected to input/output (delete one) pins.
What is the value at the push button when it is pressed? __________ Thus, when released, it is ______
34. Add in another 2 functions and update the setup() and loop(). The intention of the program is to blink
3 LEDs independently at different rates. Ensure that your program is neatly formatted with proper
indentation, another important programming practice. Use Cltr-T to auto-indent when necessary.
Remember to use copy-paste-modify to be productive.
- setup() will initialize the pins for Red and Yellow LED as OUTPUT.
- Function blinkR1() will blink the Red LED once with 0.5s on and 0.5s off.
- Function blinkG1() that will blink the Green LED once with 0.25s on and 0.25s off.
Modify loop() to call ONLY blinkG1(). Test the program. What is the blink rate for the Green LED?
That is, How many blinks per second? _________
You can estimate from observation & the codes in blinkG1() function.
35. Modify loop() to call all the 3 blink functions.
Do the 3 LEDs blink at as their intended rates (e.g. Green LED blink rate same as before)? ___________
When delay(1000) is called, how long will it take for it to complete and return back? ____________
How long do these functions take before returning back to loop() after been called?
blinkY1(): __________________ blinkR1(): __________________ blinkG1(): __________________
Due to the use of delay(), all the 3 functions are “blocking” as they don’t return quickly. Thus the 3
LEDs cannot blink independently at the intended rates. The three functions have to WAIT for one
another to complete before its turn to blink once!
We will show you how to overcome this problem in the next lab. The demo program in the next section
is an example of a responsive system.
CHECKPOINT 3: Show to your supervisor your new “non-working” program ______________________
OLED RTC
37. Open iotg_esp32Demo sketch in lab1 folder. Ensure that the Serial Monitor is open and is beside the
Arduino IDE window.
Notice that this program consists of multiple files. Each of the .cpp file serves specific purpose –
modular programming by splitting a program into multiple files.
Upload the program. It may take quite some time to build the program.
Refer to the OLED display: IP address given by the ASUS_0603 Router: __________________________
If “NO CONNECTION” shown, check with your supervisor to resolve the issue.
Last resort: Use your own mobile phone hotspot SSID & password by changing these 2 lines in
iotg_esp32Demo file.
const char *r_ssid = "ASUS_0603"; // router or your a hotspot
const char *r_password = "asus0603";
Run and note the IP address given to ESP32 shown at the OLED.
Observation at the yellow LED: _____________________________________________
Connect your laptop WiFi to ASUS_0603 router (or a hotspot). At the Web browser enter the ESP32 IP
address as the URL. The ESP32 is running a web server and may take time to respond. Press F5 to
refresh if necessary. If hotspot is used, minimize the use of data on your laptop! You may also try to
use browser on your mobile phone connected to the same SSID.