• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
TP 1
Introduction to Arduino
Objectives :
The goal of this TP is to install the different tools needed to program an Arduino
board, and to do first a hello world program, before implementing a simple night
detector project that puts the light on when it detects low light conditions.
We will do the following tasks:
• Installation and Configuration of the build environment.
• Writing the first Hello World program and execute it on the Arduino UNO
board.
• Implement the night detector and run it on the Arduino UNO board.
Important :
This lab involves using electronic equipment and wiring them together.
Incorrect wiring may cause short circuits that may damage the boards.
Make sure:
1. Always disconnect the USB port of the Arduino board when wiring.
2. In the first labs, always call the professor to verify the wiring
before putting the power on by connecting the USB cable.
Evaluation :
These TPs have two parts that are evaluated:
1. Experimental part: call the Professor to verify what you did when it tells
you to do so.
2. Report: at the end of the class, follow the instructions of the assignment
section of the TP, and send your report to your TP teacher before
midnight the day of the TP:
a. Ouassim KARRAKCHOU ouassim.karrakchou@uir.ac.ma
b. Nouhaila FRAIHI nouhaila.fraihi@uir.ac.ma
The TP must be done in groups of students. The groups must stay the same for all
the next TPs. When you send your code, put the following subject on the email
« [IoT] TP1-Group <put your names here> ».
UIR 2024/2025 Page 1
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
I- Equipment needed:
In the TPs of the IoT course, we will use the IoT boxes provided. Make sure you
put back everything where you found it at the end of the class. In this TP, we will
use the following equipment:
• The Arduino UNO board
• 1x USB cable to connect the Arduino board to the computer
• 1x 220 ohm resistance
• 1x 1K ohm resistance
• 1x red LED
• 1x Photoresistor
• Jumper wires
II- Installation and configuration of the build environment:
Install the required software:
1. Install Visual Studio Code (VS Code). This is the editor you will be using to
write your device code in C/C++. Refer to the VS Code documentation for
instructions on installing VS Code.
2. Install the VS Code PlatformIO extension. This is an extension for VS Code
that supports programming microcontrollers in C/C++. Refer to
the PlatformIO extension documentation for instructions on installing this
extension in VS Code. This extension depends on the Microsoft C/C++
extension to work with C and C++ code, and the C/C++ extension is
installed automatically when you install PlatformIO.
II- Create the Hello World program:
It is traditional when starting out with a new programming language or
technology to create a 'Hello World' application - a small application that outputs
something like the text "Hello World" to show that all the tools are correctly
configured.
The Hello World app will ensure that you have Visual Studio code installed
correctly with PlatformIO and set up for microcontroller development.
Create a PlatformIO project
The first step is to create a new project using PlatformIO configured for the
Arduino UNO.
UIR 2024/2025 Page 2
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
Task - create a PlatformIO project
Create the PlatformIO project.
1. Connect the Arduino UNO to your computer
2. Launch VS Code
3. The PlatformIO icon will be on the side menu bar:
Select this menu item, then select PIO Home -> Open
4. From the welcome screen, select the + New Project button
UIR 2024/2025 Page 3
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
5. Configure the project in the Project Wizard:
i. Name your project helloWorld
ii. From the Board dropdown, type in UNO to filter the boards, and
select Arduino Uno
iii. Leave the Framework as Arduino
iv. Either leave the Use default location checkbox checked, or uncheck
it and select a location for your project
v. Select the Finish button
PlatformIO will download the components it needs to compile code for the
Arduino Uno and create your project. This may take a few minutes.
UIR 2024/2025 Page 4
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
Investigate the PlatformIO project
The VS Code explorer will show a number of files and folders created by the
PlatformIO wizard.
Folders
• .pio - this folder contains temporary data needed by PlatformIO such as
libraries or compiled code. It is recreated automatically if deleted, and you
don't need to add this to source code control if you are sharing your
project on sites such as GitHub.
• .vscode - this folder contains the configuration used by PlatformIO and VS
Code. It is recreated automatically if deleted, and you don't need to add
this to source code control if you are sharing your project on sites such as
GitHub.
• include - this folder is for external header files needed when adding
additional libraries to your code.
• lib - this folder is for external libraries that you want to call from your
code.
• src - this folder contains the main source code for your application.
Initially, it will contain a single file - main.cpp.
• test - this folder is where you would put any unit tests for your code
Files
• main.cpp - this file in the src folder contains the entry point for your
application. Open this file, and it will contain the following code:
#include <Arduino.h>
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:
UIR 2024/2025 Page 5
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
When the device starts up, the Arduino framework will run the setup function
once, then run the loop function repeatedly until the device is turned off.
• .gitignore - this file lists the files and directories to be ignored when adding
your code to git source code control, such as uploading to a repository on
GitHub.
• platformio.ini - this file contains configuration for your device and app.
Open this file, and it will contain the following code:
[env:uno]
platform = atmelavr
board = uno
framework = arduino
The [env:uno] section has configuration for the Arduino Uno. You can have
multiple env sections so your code can be compiled for multiple boards.
The other values match the configuration from the project wizard:
o platform = atmelavr defines the hardware that the Wio Terminal
uses (an ATSAMD51-based microcontroller)
o board = uno defines the type of microcontroller board (the Arduino
Uno board)
o framework = arduino defines that this project is using the
Arduino framework.
Write the Hello World program
The Hello World program will write every second the string “Hello World!” to the
serial link. The serial link is a special bidirectional communication interface with
the Arduino board. It can be opened with any serial terminal in your machine,
but here in this TP we will use the PlatformIO serial monitor.
To create the Hello World program modify the main.cpp file accordingly. You can
use the following functions:
• Serial.begin(9600): initializes the serial link with a speed of 9600 bauds (the
default speed used by the serial monitor)
• Serial.print(“Text”): sends “Text” to the serial connection
• Serial.println(“Text”): sends “Text” followed by a line break to the serial
connection
• delay(500): wait for 500 milliseconds
UIR 2024/2025 Page 6
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
When you have finished writing your code, connect your Arduino UNO to your
computer, and upload your code to it using the following steps:
• Click on the PlatformIO icon, then on “Upload and Monitor”
If your program works, call the Professor and show him your work.
II- Create the Night Detector program:
You will now create a new program that will detect if it is the night by measuring
the ambient luminosity, and if the luminosity is low enough, it will put light on.
Wire the circuits on the breadboard
For this you need to put two circuits on the breadboard:
• A sensor circuit:
Photoresistor
5V Analog Input
UIR 2024/2025 Page 7
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
• An actuator circuit: (careful, the long leg of the LED is the positive one)
Digital Output
To do this, you need to put the components on the breadboard, and
connect them to using jumper wires, as well as the connections inside the
breadboard (all the points connected in the image below are electrically
connected:
For the digital output, use the pin 5 of the Arduino, and for the analog input use
pin A0.
UIR 2024/2025 Page 8
• TP 1 : Introduction to Arduino
• Professor: Ouassim Karrakchou
Start wiring the sensor and actuator circuit, with the Arduino UNO
disconnected from the USB, and call the professor when you are done.
Write the nightDetector program
Create a new platform IO project called “nightDetector”, and modify the
main.cpp file accordingly to implement the night detector behavior. We want
following requirements:
1. Measure the sensor value every 500 milliseconds
2. If the sensor value is below a certain threshold, put the LED on
3. Else, put the LED off
You need to specify in your setup code the configuration of the pins you use
using the pinMode function: configure pin A0 as input, and pin 5 as output. You
can then use the analogRead and digitalWrite functions in your code. Look at
their documentation in https://www.arduino.cc/reference
The threshold value depends of the light conditions, so measure the sensor value
when it is covered and uncovered by your hand to experimentally determine the
correct threshold value.
Once you code is finished upload and monitor and check that it works. If it works
call the professor.
III-Assignment
Write a report showing the different steps you did in the TP to create your night
detector IoT device. Use screenshots and attach the code you used.
UIR 2024/2025 Page 9