Professional Documents
Culture Documents
IOT AI&DS Lab Manual
IOT AI&DS Lab Manual
Aim: To study Arduino Uno IoT Kit with ATMega 328 Microcontroller.
Theory:
Introduction to ATmega328:
1. ATmega328 is an 8-bit, 28-Pin AVR Microcontroller, manufactured by Microchip,
follows RISC Architecture and has a flash-type program memory of 32KB.
2. Atmega328 is the microcontroller, used in basic Arduino boards i.e. Arduino UNO,
Arduino Pro Mini and Arduino Nano.
3. It has an EEPROM memory of 1KB and its SRAM memory is 2KB.
4. It has 8 Pins for ADC operations, which all combine to form PortA (PA0 –PA7)
5. It also has 3 built-in Timers, two of them are 8 Bit timers while the third one is 16-Bit
Timer.
6. You must have heard of Arduino UNO; UNO is based on atmega328
7. Microcontroller. It’s UNO’s heart.
8. It operates ranging from 3.3V to 5.5V but normally we use 5V as a standard.
9. Its excellent features include cost-efficiency, low power dissipation, programming lock
for security purposes, real timer counter with separate oscillator
10. It’s normally used in Embedded Systems applications. You should have a look at these
Real Life Examples of Embedded Systems, we can design all of them using this
Microcontroller.
• Functions associated with the pins must be known in order to use the device
appropriately.
• ATmega-328 pins are divided into different ports which are given in detail below
1|Page
• Port A consists of the pins from PA0 to PA7. These pins serve as an analog input to
analog to digital converters. If analog to digital converter is not used, port A acts as an
eight (8) bit bidirectional input/output port.
• Atmega328 Microcontroller.
2|Page
Applications of Atmega328:
• A complete package including ATMega 328 and Arduino can be used in several
different real-life applications.
• Quad-copter and even small aero-plane can also be designed through it.
• Power monitoring and management systems can also be prepared using this device.
• I have designed this Home Security System using Arduino UNO, you should have a
look at it.
Arduino UNO:
The Arduino UNO is a standard board of Arduino. Here UNO means 'one' in Italian. It was
named as UNO to label the first release of Arduino Software. It was also the first USB board
released by Arduino. It is considered as the powerful board used in various projects.
Arduino.cc developed the Arduino UNO board. Arduino UNO is based on an ATmega328P
microcontroller. It is easy to use compared to other boards, such as the Arduino Mega board,
etc. The board consists of digital and analog Input/output pins (I/O), shields, and other
circuits.
The Arduino UNO includes 6 analog pin inputs, 14 digital pins, a USB connector, a power
jack, and an ICSP (In-Circuit Serial Programming) header. It is programmed based on IDE,
which stands for Integrated Development Environment. It can run on both online and offline
3|Page
platforms.
Result : Thus, we learn about Arduino UNO kit using and ATmega 328 microcontroller .
4|Page
Experiment No. 2
o 3 x red LED
o 3 x 220 Ohm Resistors
o Arduino UNO R3 board
o Jump wires
o Breadboard
Connection:
1. We will connect the three LEDs to pins 13, 8, and 4 of the Arduino board. The limiting
value of resistance should be between 220 and 330 ohms to set the optimal current
through the LEDs. The required resistance is enough to light up an LED without
damaging the board and the LED
3. The structure clearly shows the pinout of the UNO board and the three LEDs with
resistors in series are connected to the board. It is shown below:
Sketch
void setup()
{
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
5|Page
pinMode(4, OUTPUT);
}
void loop()
{
// the first LED is made to blink one time
digitalWrite(13, HIGH);
delay(1000); // delay time in milliseconds
digitalWrite(13,LOW);
delay(1000);
// the second LED will blink two times
digitalWrite(8, HIGH);
delay(500); // the duration is 0.5 seconds
digitalWrite(8, LOW);
delay(500);
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8, LOW);
delay(500);
}
}
Output :
Result : Thus , we have successfully perform Design a sketch for running of LEDs.
6|Page
Experiment No 3
Aim: Design a sketch to monitor state of switch by establishing serial communication between
a Arduino and computer.
Requirement (Hardware/Software):
• Arduino Board
• hook-up wires
• breadboard
Theory:
Connect three wires to the board. The first two, red and black, connect to the two long
vertical rows on the side of the breadboard to provide access to the 5 volt supply and
ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg
of the button connects through a pull-down
resistor (here 10k ohm) to ground. The other leg of the button connects to the 5 volt supply.
Pushbuttons or switches connect two points in a circuit when you press them. When the
pushbutton is open (unpressed) there is no connection between the two legs of the
pushbutton, so the pin is connected to ground (through the pull-down resistor) and reads
as LOW, or 0. When the button is closed (pressed), it makes a connection between its two
legs, connecting the pin to 5 volts, so that the pin reads as HIGH, or 1.
7|Page
If you disconnect the digital i/o pin from everything, its reading may change erratically.
This is because the input is "floating" - that is, it doesn't have a solid connection to voltage
or ground, and it will randomly return either HIGH or LOW. That's why you need a pull-
down resistor in the circuit.
Schematic
Sketch
Open the Arduino IDE and start with the coding, which is given below:
int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
8|Page
// read the input pin:
Serial.println(buttonState);
Result: Thus , we have successfully perform design a sketch to read analog value of
potentiometer by establishing serial communication between Arduino and computer.
9|Page
Experiment No 4
Hardware Required
• Arduino Board
Theory:
This example shows you how to read analog input from the physical world using a
potentiometer. A potentiometer is a simple mechanical device that provides a varying
amount of resistance when its shaft is turned. By passing voltage through a potentiometer
and into an analog input on your board, it is possible to measure the amount of resistance
produced by a potentiometer (or pot for short) as an analog value. In this example you will
monitor the state of your potentiometer after establishing serial communication between
your Arduino and your computer running the Arduino Software (IDE).
Circuit
Connect the three wires from the potentiometer to your board. The first goes from one of
the outer pins of the potentiometer to ground. The second goes from the other outer pin of
the potentiometer to 5 volts. The third goes from the middle pin of the potentiometer to the
analog pin A0.
By turning the shaft of the potentiometer, you change the amount of resistance on either
side of the wiper, which is connected to the center pin of the potentiometer. This changes
the voltage at the center pin. When the resistance between the center and the side connected
to 5 volts is close to zero (and the resistance on the other side is close to 10k ohm), the
voltage at the center pin nears 5 volts. When the resistances are reversed, the voltage at the
center pin nears 0 volts, or ground. This voltage is the analog
10 | P a g e
voltage that you're reading as an input.
The Arduino boards have a circuit inside called an analog-to-digital converter or ADC that
reads this changing voltage and converts it to a number between 0 and 1023. When the
shaft is turned all the way in one direction, there are 0 volts going to the pin, and the input
value is 0. When the shaft is turned all the way in the opposite direction, there are 5 volts
going to the pin and the input value is 1023. In between, analogRead() returns a number
between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.
Schematic
Sketch
Open the Arduino IDE and start with the coding, which is given below:
void setup() {
Serial.begin(9600);
void loop() {
11 | P a g e
// read the input on analog pin 0:
Serial.println(sensorValue);
12 | P a g e
Experiment No 05
Aim: Design a sketch for blinking LED’s without using Delay.
Hardware Required
• Arduino Board
• 220 ohm resistance
• LEDs
Theory:
This sketch demonstrates how to blink an LED without using delay(). It turns the LED on
and then makes note of the time. Then, each time through loop(), it checks to see if the desired
blink time has passed. If it has, it toggles the LED on or off and makes note of the new time.
In this way the LED blinks continuously while the sketch execution never lags on a single
instruction.
To build the circuit, connect one end of the resistor to pin 13 of the board. Connect the long
leg of the LED (the positive leg, called the anode) to the other end of the resistor. Connect
the short leg of the LED (the negative leg, called the cathode) to the board GND, as shown
in the diagram above and the schematic below.
Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run
this example with no hardware attached, you should see that LED blink.
13 | P a g e
Sketch:
/*
Blink without Delay
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.
*/
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
14 | P a g e
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
Result: We have successfully designed a sketch to blink the LED without delay function.
15 | P a g e
Experiment No 06
Aim: Design a sketch to develop switch based binary LED counter. Also observe output on serial
monitor.
Hardware Required
• Arduino Board
• 220-ohm resistance
• LEDs
• Switch
Theory:
In digital logic and computing, a Counter is a device which stores (and sometimes displays) the
number of times a particular event or process has occurred, often in relationship to a clock signal.
Counters are used in digital electronics for counting purpose, they can count specific event
happening in the circuit. For example, in UP counter a counter increases count for every rising
edge of clock. Not only counting, a counter can follow the certain sequence based on our design
like any random sequence 0,1,3,2.
16 | P a g e
Sketch:
int buttonState = 0;
int count = 0;
void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop()
{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
// digitalWrite(7, LOW);
buttonState = digitalRead(2);
if(buttonState == LOW) {
}
else { count++; delay(200);
Serial.print (count);
Serial.print ("\n");
if((count % 2) > 0) { digitalWrite(3, HIGH); } else { digitalWrite(3, LOW); }
if((count % 4) > 1) { digitalWrite(4, HIGH); } else { digitalWrite(4, LOW); }
if((count % 8) > 3) { digitalWrite(5, HIGH); } else { digitalWrite(5, LOW); }
if((count % 16) > 7) { digitalWrite(6, HIGH); } else { digitalWrite(6, LOW); }
//if((count % 32) > 15) { digitalWrite(7, HIGH); } else { digitalWrite(7, LOW); }
delay(200);
}
17 | P a g e
}
Result: We have successfully designed a sketch to develop switch based 4 bit binary LED
counter and also observe the counter output on serial monitor.
18 | P a g e
Experiment No 7
Raspberry Pi
You are going to take a first look at Raspberry Pi! You should have a
Raspberry Pi computer in front of you for this. The computer shouldn’t be
connected to anything yet.
o Look at your Raspberry Pi. Can you find all the things labelled on the diagram?
USB ports — these are used to connect a mouse and keyboard. You
can also connect other components, such as a USB drive.
SD card slot — you can slot the SD card in here. This is where the
operating system software and your files are stored.
Ethernet port — this is used to connect Raspberry Pi to a network with
a cable. Raspberry Pi can also connect to a network via wireless LAN.
Audio jack — you can connect headphones or speakers here.
HDMI port — this is where you connect the monitor (or projector) that
you are using to display the output from the Raspberry Pi. If your
monitor has speakers, you can also use them to hear sound.
Micro USB power connector — this is where you connect a power
supply. You should always do this last, after you have connected all your
19 | P a g e
other components.
GPIO ports — these allow you to connect electronic components such
as LEDs and buttons to Raspberry Pi.
o Click on the link for the Raspberry Pi Imager that matches your operating system
20 | P a g e
o When the download finishes, click it to launch the installer
21 | P a g e
When you launch the installer, your operating system may try to block you from
running it. For example, on Windows I receive the following message:
If this pops up, click on More info and then Run anyway
Follow the instructions to install and run the Raspberry Pi Imager
Insert your SD card into the computer or laptop SD card slot
In the Raspberry Pi Imager, select the OS that you want to install and
the SD card you would like to install it on
Note: You will need to be connected to the internet the first time for the the
Raspberry Pi Imager to download the OS that you choose. That OS will then be
stored for future offline use. Being online for later uses means that the
Raspberry Pi imager will always give you the latest version.
22 | P a g e
23 | P a g e
Then simply click the WRITE button
Wait for the Raspberry Pi Imager to finish writing
Once you get the following message, you can eject your SD card
24 | P a g e
Note: Many microSD cards come inside a larger adapter — you can slide the
smaller card out using the lip at the bottom.
o Find the USB connector end of your mouse’s cable, and connect the mouse
to a USB port on your Raspberry Pi (it doesn’t matter which port you use).
25 | P a g e
o Make sure your screen is plugged into a wall socket and switched on.
o Look at the HDMI port(s) on your Raspberry Pi — notice that they have a flat side
on top.
o Use a cable to connect the screen to the Raspberry Pi’s HDMI port — use
an adapter if necessary.
Raspberry Pi 4
Connect your screen to the first of Raspberry Pi 4’s HDMI ports, labelled HDMI0.
26 | P a g e
Raspberry Pi 1, 2, 3
Connect your screen to the single HDMI port.
Note: nothing will display on the screen, because the Raspberry Pi is not running yet.
o If you want to connect the Pi to the internet via Ethernet, use an Ethernet
cable to connect the Ethernet port on the Raspberry Pi to an Ethernet socket
on the wall or on your internet router. You don’t need to do this if you want
to use wireless connectivity, or if you don’t want to connect to the internet.
o If your screen has speakers, your Raspberry Pi can play sound through these.
Or you could connect headphones or speakers to the audio port.
27 | P a g e
Start up your Raspberry Pi
o Plug the power supply into a socket and connect it to your Raspberry Pi’s power
port.
You should see a red LED light up on the Raspberry Pi, which indicates that
Raspberry Pi is connected to power. As it starts up (this is also called booting),
you will see raspberries appear in the top left-hand corner of your screen.
28 | P a g e
Finish the setup
When you start your Raspberry Pi for the first time, the Welcome to
Raspberry Pi application will pop up and guide you through the initial setup.
29 | P a g e
o Enter a new password for your Raspberry Pi and click Next.
30 | P a g e
Note: if your Raspberry Pi model doesn’t have wireless connectivity, you
won’t see this screen.
A tour of Raspberry Pi
31 | P a g e
o Click on Accessories, and then click on Text Editor.
32 | P a g e
o Click on File, then choose Save, and then click on Desktop and save the file as
33 | P a g e
system allows you to make things happen by typing in commands
instead of clicking on menu options. To try this out, click on the
Terminal symbol at the top of the screen:
o Click Next let the wizard check for updates to Raspbian and install them
(this might take a little while).
34 | P a g e
Note: you will only need to reboot if that’s necessary to complete an update.
ls
and then press Enter on the keyboard.
You can now see a list of the files and folders in your directory.
You might want to connect your Raspberry Pi to the internet. If you didn’t
plug in an ethernet cable or connect to a WiFi network during the setup, then
you can connect now.
o Click the icon with red crosses in the top right-hand corner of the screen,
35 | P a g e
and select your network from the drop-down menu. You may need to ask an
adult which network you should choose.
o Type in the password for your wireless network, or ask an adult to type
it for you, then click OK.
o When your Pi is connected to the internet, you will see a wireless LAN
symbol instead of the red crosses.
36 | P a g e
System
In this tab you can change basic system settings of your Raspberry Pi.
Password — set the password of the pi user (it is a good idea to change
the password from the factory default ‘raspberry’)
Boot — select to show the Desktop or CLI (command line interface)
when your Raspberry Pi starts
Auto Login — enabling this option will make the Raspberry Pi
automatically log in whenever it starts
37 | P a g e
Network at Boot — selecting this option will cause your Raspberry Pi
to wait until a network connection is available before starting
Splash Screen — choose whether or not to show the splash (startup)
screen when your Raspberry Pi boots
Interfaces
You can link devices and components to your Raspberry Pi using a lot of
different types of connections. The Interfaces tab is where you turn these
different connections on or off, so that your Raspberry Pi recognises that you’ve
linked something to it via a particular type of connection.
38 | P a g e
from another computer
Performance
If you need to do so for a particular project you want to work on, you can
change the performance settings of your Raspberry Pi in this tab.
Warning: Changing your Raspberry Pi’s performance settings may result in it
behaving erratically or not working.
39 | P a g e
This tab allows you to change your Raspberry Pi settings to be specific to a
country or location.
Locale — set the language, country, and character set used by your Raspberry Pi
Timezone — set the time zone
Keyboard — change your keyboard layout
WiFi Country — set the WiFi country code
40 | P a g e
Experiment No. 8
GPIO Pins :
As shown in above figure, there are 40output pins for the PI. But when you look
at the second figure, you can see not all 40 pin out can be programmed to our
use. These are only 26 GPIO pins which can be programmed. These pins go
from GPIO2 to GPIO27.
These 26 GPIO pins can be programmed as per need. Some of these pins also
perform some special functions, we will discuss about that later. With special
GPIO put aside, we have 17 GPIO remaining (Light green Cirl).
41 | P a g e
Each of these 17 GPIO pins can deliver a maximum of 15mA current. And the
sum of currents from all GPIO cannot exceed 50mA. So we can draw a
maximum of 3mA in average
from each of these GPIO pins. So one should not tamper with these things
unless you know what you are doing.
Components Required
Here we are using Raspberry Pi 2 Model B with Raspbian Jessie OS. All the
basic Hardware and Software requirements are previously discussed, you
can look it up in the Raspberry Pi Introduction, other than that we need:
▪ Connecting pins
▪ 220Ω or 1KΩresistor
▪ LED
42 | P a g e
▪ Bread Board
Circuit Diagram:
Steps:
1. On the desktop, go the Start Menu and choose for the PYTHON 3, as
shown in figure below.
2. After that, PYHON will run and you will see a window as shown in below figure.
43 | P a g e
3. After that, click on New File in File Menu, You will see a new Window open,
44 | P a g e
5. After that write the program for blinky as given below and execute the
program by clicking on “RUN” on ‘DEBUG’ option.
If the program has no errors in it, you will see a “>>>”, which means the
program is executed successfully. By this time you should see the LED blinking
three times. If there were any errors in the program, the execution tells to correct
it. Once the error is corrected execute the program again.
45 | P a g e
Program:
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
46 | P a g e
Experiment No. 9
Aim : ON/OFF Control Based On Light Intensity Using the light sensors, monitor the
surrounding light intensity & automatically turn ON/OFF the high intensity LED's by
taking some pre-defined threshold light intensity value.
Measure the intensity of light in a room using a single photocell and a capacitor
connected to the raspberry pi with a bit of code in python.
What is Photocell?
The Photocell is a light sensor in which the resistance varies according to the
intensity of light. The resistance reduces when it is in brighter surroundings.
We have to set up a threshold value for the measurements of the intensity
because it cannot give the precise measurements. If the measurements are
below the threshold then it is dark, else it is bright.
Role of a Capacitor
A Capacitor is an electrical component that can store electrical energy
temporarily. It is measured in Farads which is characterized by capacitance.
The capacitor consists of 2 conductors that can hold the electric charge and
when it is fully charged the capacitor starts discharging. This kind of alternative
behavior is used to generate AC.
47 | P a g e
When the switch is pressed the current starts flowing and the capacitor starts
charging up. The capacitor stops charging when the voltage at its end reaches
the voltage of the battery. Then as there is no potential difference in the upper
half of the circuit, no current flows there. Things needed
• A Raspberry pi
• 1 x breadboard
• A Photocell
• A Resistor
• A Capacitor ( 1 microfarad)
Circuit:
48 | P a g e
• Insert a photocell in a breadboard.
• Connect the GPIO pin 1 (3.3 V) to the resistor which is connected
serial to the Photocell.
• Connect the other end of the photocell to the GPIO pin 12 and the
Capacitor as shown in the diagram.
• GPIO pin 6 ( ground) is connected to the other end of the capacitor ( short end ).
Code
#measuring the light intensity using a photocell
import RPi.GPIO as GPIO,time,os
#im
port the libraries DEBUG=1
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
def RCtime(RCpin): #
function start
reading=0
GPIO.setup(RCpin,G
PIO.OUT)
GPIO.output(RCpin,
GPIO.LOW)
time.sleep(2) # time to discharge capacitor
GPIO.setup(RCpin,GPIO.IN)
49 | P a g e
while (GPIO.input(RCpin) == GPIO.LOW): # the loop will run till the capacitor is charged
reading += 1
# measuring time which in turn is measuring resistance return reading
# function
while True:
print RCtime(12) # calling the function
Output
1. With light:
2. Without light:
50 | P a g e