TP1_IoT_Intro_Arduino
TP1_IoT_Intro_Arduino
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.
• 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 :
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> ».
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:
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.
The Hello World app will ensure that you have Visual Studio code installed
correctly with PlatformIO and set up for microcontroller development.
The first step is to create a new project using PlatformIO configured for the
Arduino UNO.
2. Launch VS Code
Select this menu item, then select PIO Home -> Open
ii. From the Board dropdown, type in UNO to filter the boards, and
select Arduino Uno
iv. Either leave the Use default location checkbox checked, or uncheck
it and select a location for your project
PlatformIO will download the components it needs to compile code for the
Arduino Uno and create your project. This may take a few minutes.
The VS Code explorer will show a number of files and folders created by the
PlatformIO wizard.
Folders
• 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() {
void loop() {
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:
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
When you have finished writing your code, connect your Arduino UNO to your
computer, and upload your code to it using the following steps:
If your program works, call the Professor and show him your work.
• A sensor circuit:
Photoresistor
5V Analog Input
• An actuator circuit: (careful, the long leg of the LED is the positive one)
Digital Output
For the digital output, use the pin 5 of the Arduino, and for the analog input use
pin A0.
Start wiring the sensor and actuator circuit, with the Arduino UNO
disconnected from the USB, and call the professor when you are done.
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.