Arduino Introduction
Arduino Introduction
Lab 5-6
Arduino Introduction
Introduction
The Arduino can be used for many applications involving sensors and actuators. For this lab,
we will focus on a lighting application, where we use a Light Dependent Resistor (LDR) as an
input to sense light, and a Light Emitting Diode (LED) as an output to emit light that depends
on the sensor input. This hardware is introduced next.
Note: Arduino boards will be available for use during the lab, but students are free
(encouraged) to purchase their own for their own development projects. Also, the LDR/LED
circuit that you will need will also be provided during the lab. See the prototyping board in
Fig. 2.
Figure 2: Prototyping board. We will use the LDR to turn the LEDs on/off.
APSC 177 Engineering Computation and Instrumentation
Lab 5-6
Arduino Introduction
Hardware
Arduino UNO: The Arduino UNO is a basic instrumentation tool that consists of input and
output pins (see Fig. 1). It is programmed using C/C++. It can read inputs from sensors and
perform a variety of calculations on the read values to interact with the real world. The
Arduino UNO has 3 different types of inputs/outputs. In this lab, we will focus on digital
output, analog input, and serial input.
Digital Output: A digital output is a signal that can take two states: A HIGH voltage state
(typically 5V or 3.3V on the Arduino UNO), and a LOW voltage state (0V). This can be used to
control relays and other switch-like devices, as well as other low-power devices such as LEDs.
Analog Input: An analog input reads ‘continuous’ voltage levels between 0V and 5V. This is
then represented as a 10-bit input signal with a value between 0 and 1023 (where 0,1,…,1023
5
represents values from 0V to 5V with increments of 1023V). This is ideal for reading the voltage
across a sensor, such as an LDR.
Serial Input: A serial input allows the Arduino to communicate with other devices such as a
computer. This can be useful for debugging and displaying data. In our application, we will
use this to form an understanding of what is happening by plotting the values
measured/calculated by the Arduino.
Light Dependent Resistor (LDR): A resistor is a component that limits the current passing
through and the voltage across parts of an electronic circuit. An LDR is a variable resistor
whose value (resistance) increases/decreases depending on the intensity of light shining on
its surface. The change in resistance will lead to a change in voltage across the resistor, which
we can read to measure light intensity. This way, the LDR can be used as a light sensor.
Light Emitting Diode (LED): An LED is a component that lights up when current passes through
it. LEDs range widely in brightness, power rating, and color and are subsequently used in
various applications ranging from room lights to large-scale displays. An LED’s brightness can
be controlled by changing its voltage or current. In the Arduino, this can be realized using
pulse width modulation (PWM) which is explained next.
Pulse With Modulation (PWM): PWM is a technique used to simulate varying voltage levels.
This is done by changing the value of an output pin from 0V to 5V over a given duration of
time, so that the average voltage over that duration is the desired value we want to achieve.
Fig. 3 shows an example of a PWM signal. To realize 0V, we can switch the value of an output
pin to 0V all the time. To realize 1.25V, we can switch its value to 5V for 25% of the time and
to 0% for the rest of the time. Note that this leads to an average voltage of 1.25V since the
average is 0.25*5V+0.75*0V=1.25V. The 25% here is called a duty cycle. To realize 2.5V, we
APSC 177 Engineering Computation and Instrumentation
Lab 5-6
Arduino Introduction
https://support.arduino.cc/hc/en-
us/articles/360019833020-Download-and-install-
Figure 4: Board selection in the Arduino IDE.
Arduino-IDE
Once you install the IDE, start it and connect the Arduino board. The IDE will then allow you
to select the board by clicking on the ‘Select Board’ dropdown menu. It should say ‘Arduino
UNO’ which you can then select (Fig. 4). You will see four buttons as follows:
Serial Monitor: Used to display the serial data received by the Arduino in text form,
useful for debugging.
Serial Plotter: Used to plot the data received from the Arduino on a graph, useful for
visualizing what is happening.
APSC 177 Engineering Computation and Instrumentation
Lab 5-6
Arduino Introduction
The serial plotter is a valuable debugging tool built into the Arduino IDE. It can help monitor
variables, control actions, and understand what a program is doing or why it might not work
as expected.
When you start the Arduino IDE, it will show you two functions: a void setup() and a
void loop() function (see below). In the void setup() function, we will declare
physical pins on the Arduino board as input or output pins and connect them to useful variable
names such as led_1. This function will only run once at the start of your program. Then,
execution will move on to the void loop() function. As the name suggests, this will run
indefinitely in a loop as long as the Arduino is connected to power.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Useful Functions
pinMode(pin,mode)
This is used to set up a pin as either INPUT, OUTPUT or INPUT_PULLUP.
• pin: Is any digital or analog pin number labelled on the board (e.g. A0)
• mode: can be either INPUT or OUTPUT.
This is used in the function void setup() where you can specify which pins to use and
their mode.
Serial.begin(baudRate)
Is used to initialize serial communication between the computer and the Arduino.
• baudRate can take values from 9600, 14400, 19200, etc.
The baud rate is the rate at which this communication takes place (in bits per second). The
baud rate can be 9600 bits/second for lower-speed Arduino projects. But we can change this
to 14400 bits/second and even up to 57600 bits/second if we need to increase the rate at
which we read and process data. The Downside of using a higher baud rate is that it will use
both more energy and resources, which the processer could otherwise use to run more
complex computations.
This is used in the function void setup() to set up the serial monitor to allow the Arduino
to communicate with the computer.
APSC 177 Engineering Computation and Instrumentation
Lab 5-6
Arduino Introduction
analogRead(pin)
This function reads an input from an analog input pin. The read value is a 10-bit binary number,
i.e., is a value that ranges from 0 to 1023.
• pin is one of the analog input pins, such as A0.
Serial.print(data)
This function is used to send data to the serial port to visualize the given data on the computer.
• data can be filled with variables, strings, and many other data types. This is like the
‘cout’ statement. Strings are in quotation marks, while variables and numbers are not.
This function uses spaces and commas as delimiters, which indicate the end of one data to
be plotted, and the start of a new one. A good style is to print a string to be used as a legend
in the plot, followed by the value to be printed, followed by a comma, such as
Serial.println(data)
It is the same as Serial.print but adds a new line at the end of the data (same function
as “endl” after a “cout” statement).
• data can be filled with variables, strings, and many other data types. This is like the
“cout” statement. Strings are in quotation marks, while variables and numbers are not.
analogWrite(pin,data)
This function is used to send a PWM signal to a pin.
• pin is any PWM capable digital pin on the Arduino.
• data is an integer between 0 and 255.
Map(Value,FromRangeLow,FromRangeHigh,ToRangeLow,ToRangeHigh)
This function is used to remap values from one range to another, such as a 10-bit signal to an
8-bit signal.
This function is used to scale values from range to another range. In general, the mapping of
a value 𝑥 ∈ [𝑎, 𝑏] to a value 𝑦 ∈ [𝑐, 𝑑] is done by applying the formula
𝑑−𝑐
𝑦 = (𝑥 − 𝑎) × + 𝑐.
𝑏−𝑎
For example, if we have a value in the range [0,5] which is equal to 2, this function will map it
to the range [0,10] as 4. As another example, the value 3 in the range [1,5] is mapped to the
range [0,10] as 5.
This function is often used to map from the resolution of the analog pins (10 bits) to the
resolution of the PWM pins (8-bits), but can be used for other scaling needs as we will see in
Lab 6.