0% found this document useful (0 votes)
11 views37 pages

04.1.development Environment and Configuration

Uploaded by

borislavb000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views37 pages

04.1.development Environment and Configuration

Uploaded by

borislavb000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Development Environment and

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

4.Debugging and Serial Communication


 Serial Monitor

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

Save Verify if the Upload the


the sketch is sketch on the
sketch compiling microcontroller

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)

 Rollover – when variables exceed their


byte x; byte x;
maximum
x = 0; capacity, they
x ="roll
255; over" back to
their
x = x minimum capacityx and
- 1; // x = 255 = x +vice
1; //versa
x = 0
21
Variable Types
Size
Type Value Range
(Bytes)
boolean True or False 1
char -128 to 127 1
byte 0 to 255 1
int -32768 to 32767 2
unsigned int 0 to 65535 2
long -2147483648 to 2147483647 4
unsigned
0 to 4294967295 4
long
-3.4028235E+38 to
float 4
3.4028235E+38
22
Libraries
 Libraries provide extra functionality for use in
sketches:
 To connect with sensors
 To manipulate data, etc.
 Use a library from:
 Sketch > Include Library
 #include<> statement will
be added to
#include <Ser-
the sketch
vo.h> 23
Problem: Internal LED Blink
 Make the blue internal LED blink by keeping it ON for 1
seconds and OFF for 2 second
 Warning: NodeMCU ESP-12E module has 2 build-in LEDs – on
GPIO 2 and GPIO 16 (find the one, shown on the picture)
 Connect the module to the computer and configure Arduino
IDE to the correct COM port
 Write the code
 Verify if the code is compiling
 Upload the sketch to the board
Note: The internal LED is ON on LOW voltage and OFF on
HIGH voltage 24
Solution: Internal LED Blink
 You can load sample code from:
void setup() {
File > Examples > ESP8266 pinMode(16,
> Blink OUTPUT);
}
 In the example is used the built-in
variable LED_BUILTIN, whichvoidis loop() {
digitalWrite(16,
on GPIO2 LOW);
 In this case we use the LED ondelay(1000);
GPIO16 digitalWrite(16,
HIGH);
delay(2000);
25
Problem: Double Blink
 Make the Built-in and the Onboard internal LEDs
blink together
 First turn them ON and then OFF on 2 seconds
intervals
 After each blink reduce the interval with 50 ms
 When the interval becomes 100 ms, begin again
from 2 s

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

Use the button to


go through the
loop() method
Breakpoi
Read the Buttons for
debuggernt on pause and
line 6
instructions step over lines

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy