Arduino Basics: Sensors, Actuators & IDE
Arduino Basics: Sensors, Actuators & IDE
Date :
Experiment-1
AIM: Introduction to Arduino platform and programming and Introduction to various actuators
& its applications.
Theory:
Arduino pins are multiplexed meaning that most of the pins are multi-function. Also the 6-pin header
at the bottom and also at the top right corner near the USB socket are multifunction.
Function PIN PIN Function
D GND
In this experiment we have learnt about the arduino Uno platform, the pin diagram, types of
sensors and actuators which will be quite often used in the IoT experiment.
Viva Questions
Experiment 2
AIM : Introduction with running a blinking LED and fading LED with PWM
Objective:
i. Learn the arduino platform and the use of operators, variables and functions
ii. Develop a program to blink and fade the LED light
Requirements:
Introduction:
LED – The LED is a special type of diode and have similar electrical characteristics to a PN
junction diode. Hence the LED allows the flow of current in the forward direction and blocks
the current in the reverse direction. It emits light when in forward biased.
LED Types: LED are single or multi-color. Single color LED have two terminals/legs whereas multi-
color LED have more than 2 legs. For example figure below shows 3-leg LED that can display
combination of two colors whereas 4-leg LED emit color which is a combination of three colors RGB.
We can control the LED Brightness using the digitalWrite() and AnalogWrite() functions.
DigitalWrite() is very simple as it require to connecting the LED pin to any of the digital I/O pins of
the arduino.
Controlling LED brightness (Fading of LED) :
Operations like fading of LED is done using the PWM pins of the arduino. On an Arduino Uno, PWM
output is possible on digital I/O pins 3, 5, 6, 9, 10 and 11. On these pins the analogWrite function is
used to set the duty cycle of a PWM pulse train that operates at approximately 500 Hz . Thus, with a
frequency fc = 500 Hz, the period is τc = 1/fc ∼ 2 ms. As with conventional digital I/O, the pinMode
function must be called first to configure the digital
pin for output.
Thus, PWM_out_level = 255 × τo / τc = 255 × Veff / Vs Figure : Scaling relationships for PWM parameters
PWM out level is the 8-bit value used as the second parameter to the analogWrite() function.
Therefore,
Code Explanation
1. A global variable LED_pin of type int is defined in the global area. It’s scope is the entire
program. Declaring it at the top allows us to use the LED_pin for the pin number 4.
2. The set up function will execute only once. It uses two functions-
a. pinMode(LED_pin, OUTPUT); to instructs the arduino to use LED_pin i.e. pin 4 as
Output.
b. digitalWrite(LED_pin, HIGH); to instructs the arduino to keep the LED_pin i.e. pin 4
glowing.
3. Loop() function runs continuously in the loop executing all instructions inside its scope from
beginning till the end and repeats this process as long as the device is powered ON.
a. The first line declares a variable pause of type int an assigns it a value 1000. This
variable is passed in the delay function. 1000 here will correspond to 1 seconds delay.
b. The LED (connected on pin 4) which was initially ON will be put OFF by the
function digitalWrite(LED_pin, LOW); and then ON by digitalWrite(LED_pin,
HIGH);
4. The LED Blinks after every 1 seconds interval.
5. LED Brightness control Using a Potentiometer
Screenshot:
Connect the potentiometer center leg to analog pin A0 and the other two pins to 5V and GND,
Connect an led to digital pin number 9
Code Explanation:
Output
Upload the sketch and watch the LED connected on pin 9 to fade as potentiometer is rotated.
Summary
In this experiment we learnt two examples of controlling the LED. In the first example we used
simple digitalWrite(pin, LOW) and digitalWrite(pin, HIGH) with delay function to blink the LED. In the
second example we learnt how to use PWM signal to control the brightness of the LED using
analogWrite(pin, value) function.
Viva Questions
1. What are the digital i/o pins which are also used as analog output pin?
Date:
Experiment-3
AIM : Arduino IDE, Operators and Frequently Used Functions
Features
Verify / Upload - compile and upload your code to your Arduino Board.
Select Board & Port - detected Arduino boards automatically show up here, along with the
port number.
Sketchbook - here you will find all of your sketches locally stored on your computer.
Additionally, you can sync with the Arduino Cloud, and also obtain your sketches from the
online environment.
Boards Manager - browse through Arduino & third party packages that can be installed. For
example, using a MKR WiFi 1010 board requires the Arduino SAMD Boards package
installed.
Library Manager - browse through thousands of Arduino libraries, made by Arduino & its
community.
Debugger - test and debug programs in real time.
Search - search for keywords in your code.
Open Serial Monitor - opens the Serial Monitor tool, as a new tab in the console
Sketchbook
The Arduino Software (IDE) uses the concept of a sketchbook: a standard place to store your
programs (or sketches). The sketches in your sketchbook can be opened from the File >
Sketchbook menu or from the Open button on the toolbar. The first time you run the Arduino
software, it will automatically create a directory for your sketchbook. You can view or change the
location of the sketchbook location from with the Preferences dialog.
Beginning with version 1.0, files are saved with a .ino file extension. Previous versions use the .pde
extension. You may still open .pde named files in version 1.0 and later, the software will
automatically rename the extension to .ino.
Tabs, Multiple Files, and Compilation
Allows you to manage sketches with more than one file (each of which appears in its own tab). These
can be normal Arduino code files (no visible extension), C files (.c extension), C++ files (.cpp), or
header files (.h).
Operators and Frequently Used Functions
We have learnt how to open the arduino IDE and explore its various tabs to configure and create
sketch. Arduino use the operators, variables and the function in the similar way we use in C
programming language. However there are certain built-in functions which cater to the need of the
arduino platform and boards. We can select one of many different types board including arduino Uno,
nano, sense-BLE, ESP and so on.
Viva Questions
2. Name the digital and analog input output functions used in arduino
Control Structures
As the code to be written in arduino IDE is a “C” program, you are all already familiar with the syntax
of the following structures. Here we will use these structures in the context of the hardware.
In the following examples we will study the application of all the above structures.
void loop() {
analogWrite(LED_pin, brightness);
brightness = brightness + FadeAmount;
if (brightness <=0 || brightness >= 255)
FadeAmount = -FadeAmount
FadeAmount = FadeAmount; // if condition is false then this line will execute
delay(5);
}
b) if...else
void loop() {
analogWrite(LED_pin, brightness);
brightness = brightness + FadeAmount;
if (brightness <=0 || brightness >= 255)
FadeAmount = -FadeAmount;
else
FadeAmount = FadeAmount;
delay(5);
}
2. for loop : This structure is used to execute a line of code a number of times as specified in the
control condition of the for() loop. Here is an example.
a) A simple for() loop
int timer = 100; // The higher the number, the slower the timing.
void setup() { // use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
for (int thisPin = 2; thisPin < 8; thisPin++)
{ // loop from the lowest pin to the highest:
b) Array
This example demonstrates the use of an array to hold pin numbers in order to iterate
over the pins in a sequence. We can choose the pins in the array to be in any random
order, the example uses 2 through 7 i/o pins for LED.
int timer = 100; // for delay time.
void setup() {
pinMode(ledPins[thisPin], OUTPUT);
void loop() {
delay(timer);
3. switch case
Demonstrates the use of a switch statement. The switch statement allows you to choose from
among a set of discrete values of a variable. It's like a series of if [Link] this example we will
map the PWM value of the potentiometer
void setup() {
// initialize serial communication:
[Link](9600);
}
void loop() {
int sensorReading = analogRead(A0); // read the sensor:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
// The map function maps the sensor range to a range of four options (0,1,2,3):
4. while(condition)
Ref : [Link]
We have learnt how to write our own functions for well structuring of the programs. We also learnt
how to use different control structures in developing arduino programs.
ViVa questions
2. Write the format and syntax of the for, if, while control structures
4. Name the special inbuilt functions used in above theory and write their syntax
and purpose.
Date:
Experiment-5
AIM : Interfacing button to arduino digital pin and using it to control actuators (LED).
Objective:
i. To understand the concept of PULL-UP and PULL_DOWN in interfacing of a switch
with arduino
ii. To read the position of the switch to control actuators (LED)
Requirement: Arduino IDE, Push Button, 5mm LED, 220 Ω resistor
Theory:
We generally need a resistance at the terminals of the push button to limit the current through the
board pin. This resistance can be an internal built into the board or an external resistance. The way
this resistance is connected is the internal pull-up or external pull-up or pull down resistance.
With a pull-up resistor and with the button un-pressed you make a logic state ON and with the
button pressed you make a logic OFF.
With a pull - down resistor and a pressed button you make an ON logic state and OFF logic state
when its unpressed.
To prevent the unknown state a internal pull-up, external pull-up or external pull-down resistor
will ensure the state on the pin is low. Add a resistor of approx. 5 K to 10K Ω to the circuit as
shown below.
In the figure on left we initialize digital pin 2 as an input with the internal pull-up resistor enabled
using INPUT_PULLUP as 2nd argument to the pinMode () function.
When your button is not pressed, the internal pull-up resistor connects to 5 volts. This causes the
Arduino to report "1" or HIGH. When the button is pressed, the Arduino pin is pulled to ground,
causing the Arduino report a "0", or LOW.
Ex-1: Interface a switch using INTERNAL_PULLUP with the arduino board and WAP to
ON / OFF the LED as per the position of the switch. Use the internal LED connected to pin
13 of arduino
Connect a switch to pin 2 and GND in internal_pullup as shown in the figure(extreme left).
In this program we use initialize digital pin 2 as an input with the internal pull-up resistor
enabled using INPUT_PULLUP as 2nd argument to the pinMode () function
When your button is not pressed, the internal pull-up resistor connects to 5 volts. This causes the
Arduino to report "1" or HIGH. When the button is pressed, the Arduino pin is pulled to ground,
causing the Arduino report a "0", or LOW.
Code Explanation:
Arduino Sketch
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
void loop() {
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the button's
//pressed, and off when it's not:
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
In this lab we have learnt the use button with out pull up and with the use of internal, external
pull up and external pull down. We also saw how the problem of flickering of LED is
resolved using the pull up or pull down resistors.
Viva question:
2. Explain what happens if don’t use a pull-up resistor at the input of a button /
switch.
3. Write the button input state when button connected in (i). with internal pull up,
(ii). With external pull up, and (iii) with external pull down resistor.
4. Draw the figure to configure the button without pull up / pull down and with
the help of internal and external pull up and external pull down.
Date:
Experiment-6
Aim : Measure humidity and temperature
Objective: At the end of this experiment we will be able to:
Requirement: PC with Arduino IDR, Arduino Uno board, DHT-11 module and 03 jumper
wire.
Theory
DHT11 Sensor
The DHT-11 is a digital-output relative humidity and temperature sensor. It uses a capacitive
humidity sensor and a thermistor to measure the surrounding air. It is smaller and less
expensive but this sensor is less precise, less accurate and works in a smaller range of
temperature and humidity when compared with DHT22. DHT11 has a sampling rate of 1Hz,
which means it can provide new data once every second. Comes with 4 pin or 3 pin. Figure
below shows the pin names. However before interfacing just check the pin names on the PCP
of DHT-11 module.
Humidity
Sensor:
The humidity
sensing
component
has two electrodes
with a moisture-
holding
substrate (usually a salt or conductive plastic polymer) in between.
As the humidity rises, the substrate absorbs water vapour, resulting in the release of ions
and a decrease in the resistance between the two electrodes.
The sensor also includes an 8-bit SOIC-14 packaged IC. This IC measures and processes the
analog signal using stored calibration coefficients, converts the analog signal to digital, and
outputs a digital signal containing the temperature and humidity.
DHT11 can measure temperature from 0°C to 50°C with a ±2.0°C accuracy, and humidity from
20 to 80% with a 5% accuracy.
Code:
Program to measure humidity and
temperature and display on the
serial monitor
#include <DHT.h>
//Define Constants
#define DHTPIN 2 // data input pin where DHT 11 connected to
//Variables
int chk;
void setup()
[Link](9600);
[Link]();
void loop()
hum = [Link]();
temp= [Link]();
Summary:
In this experiment we have learnt the basics of the DHT-11, its interfacing with arduino
board and the programming to measure the humidity and temperature and display the
reading on the serial monitor.
ViVa Questions:
1. What is the purpose of DHT-11 sensor module and explain its working.