04.1.development Environment and Configuration
04.1.development Environment and Configuration
Software
Arduino IDE. Sketch. Functions. Debugging
Svetlin Nakov
SoftUni Buditel
Table of Contents
1.Arduino IDE
2.Sketch
3.Basic Arduino IDE Functions
Basic Functions. Variables. Libraries
5.Examples
2
Arduino IDE
Introduction. Main View. Toolbar
Arduino IDE
Arduino Integrated Development Environment (IDE)
is used to write and upload programs to Arduino
compatible boards
Supports the languages C and C++
Start with Arduino IDE:
Download and install from:
https://www.arduino.cc/en/software
Install needed Board Manager library
and selecting your board
Select the correct COM port for the board
Note: for detailed instructions read the provided guidelines! 4
Select Your Board: NodeMCU
1.0
5
Select the Correct COM Port
6
Arduino IDE – Main View
Arduino IDE
Sketch version
name
Menu
s Toolbar
buttons
Code
area Upload
status
message
Configur
Messag
es from ed
the board
system and
COM
port 7
Toolbar Buttons
Verify – check the code for errors
Upload – compile and upload the code to the
board
New – create a new sketch
Open – open a saved sketch
Save – save the sketch
Serial Monitor – display serial
data sent from the board 8
Sketch
Structure. Creating a Sketch
Sketch – Structure
The Sketch is the main code (program), created in
Arduino IDE
Generates a Hex File which is uploaded on the
board
void setup() {
Requires two basic functions:
// Initialization of variables, pin modes, li-
braries
// Runs only once after each power up or reset
}
void loop() {
// Loops the code consecutively
}
10
Creating a Sketch
Create new or
Edit open an Verify Upload
existing
sketch
Edit
here
11
Problem: Blink Example
Try out an example sketch!
Open the sketch from File->Examples-
>01.Basics->Blink
Compile (verify) the sketch
Upload the sketch to the ESP
Watch the light blink
12
Arduino Examples
Arduino IDE has built-in code samples in
Examples section:
13
Opening Sketch – "Blink"
Example
14
Compiling the "Blink" Example
15
Uploading the "Blink" Example
16
The "Blink" Example in Action
17
Basic Arduino IDE
Functions
Basic IDE Functions. Variables. Libraries
Hardware Related Functions
pinMode() – setup the I/O functions of hardware
pins
pinMode(2, OUTPUT) pinMode(D4, INPUT)
digitalWrite(16,
digitalWrite() HIGH) –/ set a pin to a digital level
digitalWrite(D0,
HIGH)
digitalRead(16)
digitalRead() – return the given pin value
/ digitalRead(D0)
delay(1000)
delay() // – pause
pause forfor the given milliseconds
1 second
19
Flow-Control: If-Else, for, Do-
While
Examples:
for loop for (int i = 0; i <= 255; i+
+) {
analogWrite(PWMpin, i);
if (x > 120) {
delay(10);
digitalWrite(13,
}
HIGH);
} else {
do-
digitalWrite(13, LOW); do { while
} delay(50);
if – else
constructi x =
on readSensors();
} while (x < 100);
20
Variables
Variables – used for storing data (from sensors
or a calculation)
Declaring variable – define its type and
(optionally) set an initial value
int onboardLED = 16;
(initialize the
byte brightness = 13;
variable)
26
Solution: Double Blink
int LED1 = 2; void loop() {
int LED2 = 16; while (interval > 100) {
int interval = 2000; digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
void setup() { delay(interval);
pinMode(LED1, OUTPUT); digitalWrite(LED1, HIGH);
pinMode(LED2, OUTPUT); digitalWrite(LED2, HIGH);
} delay(interval);
interval = interval - 50;
}
interval = 2000;
}
27
Problem: External LED Blink
Use the diagram to create a
circuit with an external LED
Use 100 Ohm resistor or higher
Program the LED to turn ON
and OFF every 0.5 s
After every fifth blink turn OFF
the LED for 2 s instead of 0.5 s
28
Solution: External LED Blink
void loop() {
blinkCounter++;
digitalWrite(LED, 1);
delay(500);
digitalWrite(LED, 0);
if (blinkCounter % 5 == 0)
{
delay(2000);
int LED = D7; // GPIO 13 }
else
int blinkCounter = 0; {
void setup() { delay(500);
pinMode(LED, OUTPUT); }
} }
29
Debugging and Serial
Communication
Serial Monitor
Debugging
Debugging in Arduino IDE is complicated
The code depends on outputs / inputs of a physical
device
Debugging a sketch includes:
Checking the hardware:
wires, components, etc.
Trying to compile the code
Look for syntax errors
Code debugging with Serial Monitor:
Read data from the device at each code step 31
Serial Monitor
Serial Monitor displays messages sent from the
microcontroller
Serial communications allow the microcontroller
to interact with the computer and other devices
Serial.begin(baud rate) – start serial
communication
baud rate – maximum transferred bits per
second
Serial.println("{message}") – print message
from the board 32
Serial Communication –
Example
Baud
void setup() {
rate in
Serial.begin(9600); the code
}
void loop() {
Serial.println("Hello
world!");
delay(1000);
}
Set the same
baud rate in
the IDE
33
Serial.println(): Printing Text
+ Number
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i=1; i<=100; i++) {
// This will fail!
Serial.println("i = " + i);
// Use: print("i = "); println(i);
delay(1000);
}
}
34
Tinkercad Simulator –
Debugger
ledValue
ledValue
==01
35
What Did We Learn Today?
Arduino IDE is software for programming
microcontroller boards
Sketch is the main program, uploaded to the
board. It needs 2 main methods:
void setup() – runs only once at the beginning
void loop() – runs repeatedly
Contains built-in libraries and functions for
programming controllers:
pinMode(), digitalRead(), digitalWrite(), delay()…
Debugging is done by using Serial Communication
and checking the messages in the Serial Monitor
tool. 36
Questions?
© SoftUni Buditel – https://buditel.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.