Iot Unit 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

UNIT 4

OPEN PLATFORMS AND PROGRAMMIING

1.Arduino platform
An Arduino is actually a microcontroller based kit which can be either used directly by
purchasing from the vendor or can be made at home using the components, owing to its
open source hardware feature. It is basically used in communications and in controlling or
operating many devices. It was founded by Massimo Banzi and David Cuartielles in 2005.

Arduino Architecture:
Arduino’s processor basically uses the Harvard architecture where the program code and
program data have separate memory. It consists of two memories- Program memory and
the data memory.The code is stored in the flash program memory, whereas the data is
stored in the data memory. The Atmega328 has 32 KB of flash memory for storing code (of
which 0.5 KB is used for the bootloader), 2 KB of SRAM and 1 KB of EEPROM and
operates with a clock speed of 16MHz.

Arduino Pin Diagram


A typical example of Arduino board is Arduino Uno. It consists of ATmega328- a 28 pin
microcontroller.

Arduino Uno consists of 14 digital input/output pins (of which 6 can be used as PWM
outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an
ICSP header, and a reset button

Power Jack: Arduino can be power either from the pc through a USB or through external
source like adaptor or a battery. It can operate on a external supply of 7 to 12V. Power can
be applied externally through the pin Vin or by giving voltage reference through the IORef
pin.

Digital Inputs: It consists of 14 digital inputs/output pins, each of which provide or take up
40mA current. Some of them have special functions like pins 0 and 1, which act as Rx and
Tx respectively , for serial communication, pins 2 and 3-which are external interrupts, pins
3,5,6,9,11 which provides pwm output and pin 13 where LED is connected.

Analog inputs: It has 6 analog input/output pins, each providing a resolution of 10 bits.

ARef: It provides reference to the analog inputs

Reset: It resets the microcontroller when low.

How to program an Arduino?


The most important advantage with Arduino is the programs can be directly loaded to the
device without requiring any hardware programmer to burn the program. This is done
because of the presence of the 0.5KB of Bootloader which allows the program to be burned
into the circuit. All we have to do is to download the Arduino software and writing the code.

The Arduino tool window consists of the toolbar with the buttons like verify, upload, new,
open, save, serial monitor. It also consists of a text editor to write the code, a message area
which displays the feedback like showing the errors, the text console which displays the
output and a series of menus like the File, Edit, Tools menu.

5 Steps to program an Arduino


 Programs written in Arduino are known as sketches. A basic sketch consists of 3 parts
1. Declaration of Variables
2. Initialization: It is written in the setup () function.
3. Control code: It is written in the loop () function.

 The sketch is saved with .ino extension. Any operations like verifying, opening a sketch,
saving a sketch can be done using the buttons on the toolbar or using the tool menu.
 The sketch should be stored in the sketchbook directory.
 Chose the proper board from the tools menu and the serial port numbers.
 Click on the upload button or chose upload from the tools menu. Thus the code is
uploaded by the bootloader onto the microcontroller.
Few of basic Adruino functions are:
 digitalRead(pin): Reads the digital value at the given pin.
 digitalWrite(pin, value): Writes the digital value to the given pin.
 pinMode(pin, mode): Sets the pin to input or output mode.
 analogRead(pin): Reads and returns the value.
 analogWrite(pin, value): Writes the value to that pin.
 serial.begin(baud rate): Sets the beginning of serial communication by setting the bit
rate.
How to Design your own Arduino?
We can also design our own Arduino by following the schematic given by the Arduino
vendor and also available at the websites. All we need are the following components- A
breadboard, a led, a power jack, a IC socket, a microcontroller, few resistors, 2 regulators, 2
capacitors.

 The IC socket and the power jack are mounted on the board.
 Add the 5v and 3.3v regulator circuits using the combinations of regulators and
capacitors.
 Add proper power connections to the microcontroller pins.
 Connect the reset pin of the IC socket to a 10K resistor.
 Connect the crystal oscillators to pins 9 and 10
 Connect the led to the appropriate pin.
 Mount the female headers onto the board and connect them to the respective pins on the
chip.
 Mount the row of 6 male headers, which can be used as an alternative to upload
programs.
 Upload the program on the Microcontroller of the readymade Adruino and then pry it off
and place back on the user kit.
7 Reasons why Arduino is being preferred these days
1. It is inexpensive
2. It comes with an open source hardware feature which enables users to develop their own
kit using already available one as a reference source.
3. The Arduino software is compatible with all types of operating systems like Windows,
Linux, and Macintosh etc.
4. It also comes with open source software feature which enables experienced software
developers to use the Arduino code to merge with the existing programming language
libraries and can be extended and modified.
5. It is easy to use for beginners.
6. We can develop an Arduino based project which can be completely stand alone or
projects which involve direct communication with the software loaded in the computer.
7. It comes with an easy provision of connecting with the CPU of the computer using serial
communication over USB as it contains built in power and reset circuitry.
So this is some basic idea regarding an Arduino. You can use it for many types of
applications. For instance in applications involving controlling some actuators like motors,
generators, based on the input from sensors.

LED Blinking Using Arduino


Last Updated : 01 May, 2024


We will interface an LED (light-emitting diode) to the Arduino UNO board. An LED is a simple
diode that emits light in a forward bias. We will write an LED-blinking program on the Arduino
IDE and download it to the microcontroller board. The program simply turns ON and OFF LED with
some delay between them.
What is LED?
LEDs (Light Emitting Diodes) are becoming increasingly popular among a wide range of
people. When a voltage is given to a PN Junction Diode, electrons, and holes recombine
in the PN Junction and release energy in the form of light (Photons). An LED’s electrical
sign is comparable to that of a PN Junction Diode. When free electrons in the conduction
band recombine with holes in the valence band in forward bias, energy is released in the
form of light.
The flow of charge carriers (electrons and holes) across the P-N junction drives the
activity of an LED. When a forward voltage (anode positive in comparison to the cathode)
is applied, electrons and holes recombine at the junction, releasing energy in the form of
photons (light). The semiconductor chip is linked to external terminals known as
the anode (+) and the cathode (-). The anode is linked to the P-region, and the cathode to
the N-region.
Blinking an LED
Blinking an LED is an introductory Arduino project in which we control an LED using
Arduino. LED blinking refers to the process of continuously turning an LED (Light Emitting
Diode) and off in a repetitive pattern. It is a simple and common demonstration in
electronics and microcontroller-based projects.
Working Procedure
setup() and loop() are two fundamental Arduino functions for controlling the behavior of
your board. The Arduino framework automatically calls these functions, which form the
foundation of any Arduino program.
The setup() function is only called once when the Arduino board boots up or is reset. Its
goal is to set pin modes, initialize variables, and execute any other necessary setup tasks
before the main loop begins. This function can be used to configure settings that should
only be changed once over the board’s lifespan.
The loop() function is the heart of an Arduino program. After the setup() function is
executed, the loop() function starts running repeatedly until the Arduino is powered off or
reset. It contains the main code that performs the desired tasks, controls the board, user
input. Whatever is included in the loop() function will be executed in a continuous loop,
allowing the Arduino to perform its intended functions continuously.
In the code, we have declared two integers, LEDpin and delayT. LEDpin represents the
pin number of the Arduino where LEDs need to be connected, and delayT is an integer
variable for the delay() function. The delay() function accepts values in milliseconds.
Components Required
1. 1 X LED
2. 1 X Resistor, 330 Ohm
3. Breadboard
4. Arduino UNO R4 or earlier versions.
5. Jumper wires
Components Description
1. 1 X LED: We are controlling only one LED in this program.
2. 1 X Resistor, 330 Ohm: For every LED, we need one current limiting resistor.
3. Breadboard: A breadboard is a fundamental tool used in electronics and prototyping to
build and test circuits without soldering.
4. Arduino UNO R4 or earlier versions.
5. Jumper wires: Jumper wires are simple electrical wires with connectors on both ends
used to create connections between various electronic components or points on a
circuit on a breadboard.
Arduino Code
C++

int LEDpin = 13;


int delayT = 1000;
void setup() {
// put your setup code here, to run once:
pinMode(LEDpin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LEDpin, HIGH);
delay(delayT);
digitalWrite(LEDpin, LOW);
delay(delayT);
}
Circuit Diagram
In the circuit diagram, we used one 330-ohm resistor in series with the LED. This resistor
is also called a current-limiting resistor. The Anode of the LED (the longer pin) is
connected to one end of the resistor, and the cathode (the shorter pin) is connected to the
ground. The other end of the resistor is connected to the Arduino pin. A step-by-step
explanation is as follows:
1. LED Connections: Connect the LED to the breadboard. The LED has two legs, the
longer of which is the anode (positive) and the shorter of which is the cathode
(negative).
2. Resistor Connection: Insert one end of the resistor into the same row of the
breadboard as the LED’s Anode. The resistor’s other end should be connected to the
Arduino’s digital output pin.
3. Ground (GND) Connection: Connect a jumper wire from the same row as the LED’s
cathode to any Arduino board GND (Ground) pin. This connects the circuit to the
ground of the Arduino.
The circuit is now complete. Here’s how it works:
When you upload a simple Arduino program that controls the LED, the microcontroller on
the Arduino board executes the program, and the LED will blink according to the code you
wrote.
Applications and Uses of LED Blinking
The LED blinking project is an important and straightforward method that can be utilized
for a wide range of applications in microcontroller-based projects like :
 Security Systems: To check the status of security systems
 Warning Signals: In battery-operated devices, LED blinking can be used to indicate
low battery levels.
 In Testing and debugging.
 Status Indication: LEDs can be used to indicate different states of a system. For
example, in a home automation project, an LED might blink to indicate whether a
device is connected to an internet network or not.
GPIO

About

One of the most used and versatile peripheral in a microcontroller is the GPIO. The GPIO is commonly
used to write and read the pin state.

GPIO stands to General Purpose Input Output, and is responsible to control or read the state of a specific
pin in the digital world. For example, this peripheral is widely used to create the LED blinking or to read a
simple button.

GPIOs Modes

There are two different modes in the GPIO configuration:

 Input Mode
In this mode, the GPIO will receive the digital state from a specific device. This device could be a button
or a switch.

 Output Mode
For the output mode, the GPIO will change the GPIO digital state to a specific device. You can drive an
LED for example.

GPIO API

Here is the common functions used for the GPIO peripheral.

pinMode

The pinMode function is used to define the GPIO operation mode for a specific pin.

void pinMode(uint8_t pin, uint8_t mode);


 pin defines the GPIO pin number.
 mode sets operation mode.
The following modes are supported for the basic input and output:

 INPUT sets the GPIO as input without pullup or pulldown (high impedance).
 OUTPUT sets the GPIO as output/read mode.
 INPUT_PULLDOWN sets the GPIO as input with the internal pulldown.
 INPUT_PULLUP sets the GPIO as input with the internal pullup.
Internal Pullup and Pulldown

The ESP32 SoC families supports the internal pullup and pulldown throught a 45kR resistor, that can be
enabled when configuring the GPIO mode as INPUT mode. If the pullup or pulldown mode is not defined,
the pin will stay in the high impedance mode.

digitalWrite

The function digitalWrite sets the state of the selected GPIO to HIGH or LOW . This function is only used if
the pinMode was configured as OUTPUT .

void digitalWrite(uint8_t pin, uint8_t val);


 pin defines the GPIO pin number.
 val set the output digital state to HIGH or LOW .

digitalRead

To read the state of a given pin configured as INPUT , the function digitalRead is used.

int digitalRead(uint8_t pin);


 pin select GPIO
This function will return the logical state of the selected pin as HIGH or LOW .

Interrupts

The GPIO peripheral on the ESP32 supports interruptions.

attachInterrupt

The function attachInterrupt is used to attach the interrupt to the defined pin.

attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode);


 pin defines the GPIO pin number.
 handler set the handler function.
 mode set the interrupt mode.
Here are the supported interrupt modes:

 DISABLED
 RISING
 FALLING
 CHANGE
 ONLOW
 ONHIGH
 ONLOW_WE
 ONHIGH_WE
attachInterruptArg

The function attachInterruptArg is used to attach the interrupt to the defined pin using arguments.

attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void * arg, int mode);


 pin defines the GPIO pin number.
 handler set the handler function.
 arg pointer to the interrupt arguments.
 mode set the interrupt mode.
detachInterrupt

To detach the interruption from a specific pin, use the detachInterrupt function giving the GPIO to be
detached.

detachInterrupt(uint8_t pin);
 pin defines the GPIO pin number.

You might also like