0% found this document useful (0 votes)
30 views25 pages

Minor 5sem Iot Manual w22

The document describes experiments to interface different input and output devices like LEDs, switches, and temperature sensors with an ATmega32 microcontroller. It includes the theory, circuit diagrams, and programs for blinking an LED, reading a switch, and measuring temperature with an LM35 sensor.

Uploaded by

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

Minor 5sem Iot Manual w22

The document describes experiments to interface different input and output devices like LEDs, switches, and temperature sensors with an ATmega32 microcontroller. It includes the theory, circuit diagrams, and programs for blinking an LED, reading a switch, and measuring temperature with an LM35 sensor.

Uploaded by

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

Enrollment No:

LABORATORY MANUAL
IoT Embedded Hardware
Subject Code: 115AIO1
B.E. SEM. V Year 2022-23 (Odd Semester)

AI Minor/Honors - Internet of Things

Gujarat Power Engineering and Research Institute, Mehsana


(A constituent college of Gujarat Technological University)
Near Toll Booth, Ahmedabad-Mehsana Express way, Village-Mewad
District: Mehsana - 382710 (Gujarat)
Contact No. +91-2762-285875/71
GUJARAT POWER ENGINEERING AND RESEARCH
INSTITUTE, MEHSANA
(A constituent college of Gujarat Technological University)

AI Minor/Honors – Internet of Things

CERTIFICATE

This is to certify that Mr./Ms.


Enrollment No. of semester has
satisfactorily completed the laboratory work in the course
within the four walls of the Institute.

Date of submission:

Faculty In-charge Head of Department


B.E. SEM. 5(AI-Minor IoT) Year: 2022-23 (Odd Sem.)
115AI01 IoT Embedded Hardware

INDEX

No. Title Page Date Sign Remark

1 Write c programs for ATMega32


Microcontroller and simulate using
ATMEL Studio
2 Write programs related to I/O applications
along with IoT

3 Generate PWM waveform and


change intensity of LED
connected with ATMega32
Microcontroller board through
cloud commands
4 Write a program for LDR to vary
the light intensity of LED using
Arduino.
5 Write a program in Python to blink an
LED on and off every second.

6 Write a program in Python for


Connecting a passive infrared (PIR) motion
sensor to the Raspberry Pi.
EXPERIMENT 1

Aim: Write c programs for ATMega32 Microcontroller and simulate using


ATMEL Studio. (LED INTERFACING ).

Theory:
ATMEGA ports are 8 bit wide. Each port has 3 eight bit registers associated. Each
bit in these registers configures pins of associated port. Bit 0 of these registers is
associated with Pin 0 of the port, Bit1 of these registers is associated with Pin1 and
so on. These three registers are – DDRx register – PORTx register – PINx register
X may be replaced by A,B,C or D based on the PORT you are using.

DDRx register

DDRx (Data Direction Register) configures data direction of the port pins. Which,
writing 0 to a bit in DDRx makes corresponding port pin as input, while writing 1 to
a bit in DDRx makes the corresponding port pin as output. EXAMPLE: • to make
all pins of port B as input, DDRA = 0b00000000; • to make all pins of port A as
output pins : DDRB= 0b11111111; • to make lower nibble of port B as output and
higher nibble as input : DDRB = 0b00001111; In hexadecimal representation, it can
be written as DDRB = 0x0F;

Procedure:
1. Develop the program using ATMEL STUDIO, and build its hex

file.

2. Implement the circuit as shown in figure 1.1 and give the power.

3. Load the hex file into ATMega32 chip using Load firmware.

4. Observe the result.

1
Figure 1. Circuit diagram of Blinking LED using ATmega32

Program:
/* LED blinking */

#include <avr/io.h>
#define F_CPU 16000000L
#include<util/delay.h>

int main(void)
{
/* the code here is executed only once */

DDRB=0xff; /*Setting PORTB as OUTPUT*/


while (1) /*Infinite loop*/
{
PORTB=0xff; /*make all PORTB HIGH*/
_delay_ms(100); /*make it HIGH for 100ms*/
PORTB=0x00; /*make all PORTB LOW*/
_delay_ms(100);/*make if off for 100ms*/

}
}

Result: The circuit has been implemented, program has been developed and
created hex code, the hex code is burned on the microcontroller. The LEDs are ON
for 100ms and OFF for 100ms

2
Experiment 2

Aim: Interface Digital/Analog input and output devices with ATMega32


Microcontroller and write programs related to I/O applications along with
IoT.

Switch:

Theory:
A switch is an electrical component that can disconnect or connect the
conducting path in an electrical circuit, interrupting the electric current or
diverting it from one conductor to another. The most common type of switch
is an electromechanical device consisting of one or more sets of movable
electrical contacts connected to external circuits. When a pair of contacts is
touching current can pass between them, while when the contacts are
separated no current can flow.

Procedure:
To use a push button, switch with a microcontroller, first you should
configure the corresponding pin as input. Then we can easily read the status
of that input pin and make required decisions. There are two types of push
button switches Push To On and Push To Off, here we are using Push To On
switch. In this tutorial a press at the switch turns ON the LED for 3 seconds.
16 MHz crystal is used to provide clock to the Atmega32 microcontroller.
10µF capacitor and 10KΩ resistor is used to provide Power On Reset (POR)
during the startup of microcontroller. LED is connected to the first pin of
PORTC (PC0) of the microcontroller and a resistor is used to limit current
through it. Push Button Switch is connected to the first pin of PORTD (PD0)
of the microcontroller and a pull down resistor is provided to make the input
LOW whenever the switch remain unpressed.

3
Figure 2 .Circuit Diagram of Switch connected to ATmega32

Program:
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRC |= (1<<PC0); //Nakes first pin of PORTC as Output
// OR DDRC = 0x01;
DDRD &= ~(1<<PD0);//Makes firs pin of PORTD as Input
// OR DDRD = 0x00; //Makes all pins of PORTD input
while(1) //infinite loop
{
if(PIND & (1<<PD0) == 1) //If switch is pressed
{
PORTC |= (1<<PC0); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTC &= ~(1<<PC0); //Turns OFF LED
}
}
}

Temperature sensor:

Theory:
A thermistor is a variable resistance element, whose resistance changes with
a change in temperature. The resistance increases with an increase in
temperature for PTC (Positive Temperature Coefficient) type thermistors.
The resistance decreases with an increase in temperature for NTC (Negative

4
Temperature Coefficient) type thermistors. They can be used as current
limiters, temperature sensors, overcurrent protectors, etc. The change in
resistance value is a measure of the temperature.
By using the thermistor in series with a fixed resistance forming a simple
voltage divider network, we can find out the temperature by the change in
voltage of the voltage divider network due to a change in temperature.
Procedure:
LM35 is a temperature sensor that can measure temperature in the range of -
55°C to 150°C.It is a 3-terminal device that provides an analog voltage
proportional to the temperature. The higher the temperature, the higher is the
output voltage. The output analog voltage can be converted to digital form
using ADC so that a microcontroller can process it.
Let’s interface the LM35 temperature sensor with ATmega16 and display the
surrounding temperature on the LCD16x2 display.LM35 gives output in the
analog form so connect out pin of a sensor to one of the ADC channels of
ATmega16/ATmega32.

Figure 3 .Circuit Diagram for Temperature sensor connected to ATmega32


Program
/*
*ATmega32_Thermister.c
*/
#define F_CPU 16000000UL
#include <avr/io.h>

5
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "LCD16x2_4bit.h"

int val;
long R;
double Thermister;

void ADC_Init()
{
DDRA=0x00; /* Make ADC port as input */
ADCSRA = 0x87; /* Enable ADC, fr/128 */
}

int adc()
{
ADMUX = 0x40; /* Vref: Avcc, ADC channel: 0 */
ADCSRA |= (1<<ADSC); /* start conversion */
while ((ADCSRA &(1<<ADIF))==0); /* monitor end of conversion interrupt
flag */
ADCSRA |=(1<<ADIF); /* set the ADIF bit of ADCSRA register */
return(ADCW); /* return the ADCW */
}

double getTemp()
{
val = adc(); /* store adc value on val register */
R=((10230000/val) - 10000);/* calculate the resistance */
Thermister = log(R); /* calculate natural log of resistance
*/ /* Steinhart-Hart Thermistor Equation: */
/* Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3) */ /* where
A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8 */ Thermister =
1 / (0.001129148 + (0.000234125 * Thermister) +
(0.0000000876741 * Thermister * Thermister * Thermister));
Thermister = Thermister - 273.15;/* convert kelvin to °C */

return Thermister;
}

int main(void)
{
char array[20],ohm=0xF4;
double temp;
LCD_Init(); /* initialize 16x2 LCD */
ADC_Init(); /* initialize ADC */
LCD_Clear(); /* clear LCD */
LCD_String_xy(0, 0,"Temp: ");

8
LCD_String_xy(1, 0, "R: ");
while(1)
{
temp = getTemp();/* store temperature value on temp resistor */
memset(array,0,20);
dtostrf(temp,3,2,array);
LCD_String_xy(0, 7,array);
6
LCD_Char(0xDF); /* ASCII value of '°' */
LCD_String("C ");

memset(array,0,20);
sprintf(array,"%ld %c ",R,ohm);
LCD_String_xy(1, 3,array);
_delay_ms(1000);/* wait for 1 second */
}
}

Program for DC Motor:


/*
* ATmega32 DC Motor control
*/

#define F_CPU 16000000UL /* Define CPU Frequency 16MHz */ #include


<avr/io.h> /* Include AVR std. library file */ #include <avr/interrupt.h>
#include <stdio.h> /* Include std. library file */ #include
<util/delay.h> /* Include Delay header file */

volatile uint8_t Direction = 0;

void ADC_Init() /* ADC Initialization function */ {


DDRA = 0x00; /* Make ADC port as input */
ADCSRA = 0x87; /* Enable ADC, with freq/128 */ ADMUX = 0x40; /*
Vref: Avcc, ADC channel: 0 */
}

int ADC_Read(char channel) /* ADC Read function */


{
ADMUX = 0x40 | (channel & 0x07);/* set input channel to read */
ADCSRA |= (1<<ADSC); /* Start ADC conversion */
while (!(ADCSRA & (1<<ADIF))); /* Wait until end of conversion */
ADCSRA |= (1<<ADIF); /* Clear interrupt flag */
_delay_ms(1); /* Wait a little bit */
return ADCW; /* Return ADC word */
}

ISR(INT0_vect)
{
Direction = ~Direction; /* Toggle Direction */
_delay_ms(50); /* Software de-bouncing control delay */
}

int main(void)
{
DDRC = 0xFF; /* Make PORTC as output Port */
DDRD &= ~(1<<PD2); /* Make INT0 pin as Input */
DDRB |= (1<<PB3); /* Make OC0 pin as Output */
GICR = (1<<INT0); /* Enable INT0*/
MCUCR = ((1<<ISC00)|(1<<ISC01));/* Trigger INT0 on Rising Edge
triggered */
sei(); /* Enable Global Interrupt */
ADC_Init(); /* Initialize ADC */
TCNT0 = 0; /* Set timer0 count zero */
TCCR0 = (1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00)|(1<<CS01);/* Set

7
Fast PWM with Fosc/64 Timer0 clock */
while(1)
{
if (Direction !=0) /* Rotate DC motor Clockwise */
PORTC = 1;
else /* Else rotate DC motor Anticlockwise */
PORTC = 2;

17
OCR0 = (ADC_Read(0)/4); /* Read ADC and map it into 0-255 to
write in OCR0 register */
}
}

8
Experiment 3

Aim: Generate PWM waveform and change intensity of LED connected with
ATMega32 Microcontroller board through cloud commands.

Theory:
Pulse Width Modulation (PWM) is a technique by which the width of a pulse is
varied while keeping the frequency constant.
Why do we need to do this? Let’s take an example of controlling DC motor speed,
more the Pulse width more the speed. Also, there are applications like controlling
light intensity by PWM.
A period of a pulse consists of an ON cycle (5V) and an OFF cycle (0V). The
fraction for which the signal is ON over a period is known as the duty cycle.

Duty Cycle (In %) =


E.g. Consider a pulse with a period of 10ms which remains ON (high) for 2ms.The
duty cycle of this pulse will be
D = 2ms / 10ms = 20%
Through the PWM technique, we can control the power delivered to the load by
using the ON-OFF signal.
Pulse Width Modulated signals with different duty cycle are shown below

Figure 7.PWM Duty Cycle Waveforms


9
AVR ATmega PWM
ATmega has an inbuilt PWM unit. As we know, ATmega has 3 Timers T0, T1, and
T2 which can be used for PWM generation. Mainly there are two modes in PWM.

1. Fast PWM
2. Phase correct PWM

We need to configure the Timer Register for generating PWM. PWM output will be
generated on the corresponding Timer’s output compare pin (OCx).

Figure 8.PWM out Pins on AVR ATmega16/32


Configuring Timer0 for PWM generation
It is simple to configure PWM mode in Timer. We just need to set some bits in the
TCCR0 register.
TCCR0: Timer Counter Control Register 0
10
Bit 7- FOC0: Force compare match
Write only bit, which can be used while generating a wave. Writing 1 to this bit will
force the wave generator to act as if a compare match has occurred.
Bit 6, 3 - WGM00, WGM01: Waveform Generation Mode
WGM00 WGM01 Timer0 mode selection bit

0 0 Normal
0 1 CTC (Clear timer on Compare
Match)
1 0 PWM, Phase correct
1 1 Fast PWM

Bit 5:4 - COM01:00:

1. When WGM00: WGM01= 11 i.e. Fast PWM. Compare Output Mode

waveform generator on OC0 pin

1COM Mode Name Description

0 0 Disconnected The normal port operation, OC0


disconnected

0 1 Reserved Reserved
1 0 Non-inverted Clear OC0 on compare match, set OC0
at TOP

1 1 Inverted Set OC0 on compare match, clear OC0


PWM at TOP

11
2. When WGM00: WGM01= 10 i.e. Phase correct PWM. Compare Output
Mode
waveform generator on OC0 pin
1COM Description

0 0 The normal port operation, OC0 disconnected


0 1 Reserved
1 0 Clear OC0 on compare match when up-counting, set OC0
on compare match when down-counting

1 1 Set OC0 on compare match when up-counting, Clear OC0


on compare match when down-counting

Bit 2:0 - CS02:CS00: Clock Source Select


These bits are used to select a clock source. When CS02: CS00 = 000, then timer is
stopped. As it gets a value between 001 to 101, it gets a clock source and starts as
the timer.
CS02 CS01 CS00 Description
0 0 0 No clock source (Timer / Counter stopped)
0 0 1 clk (no pre-scaling)
0 1 0 clk / 8
0 1 1 clk / 64
1 0 0 clk / 256
1 0 1 clk / 1024
1 1 0 External clock source on T0 pin. clock on
falling edge

1 1 1 External clock source on T0 pin. clock on


rising edge.
12
Fast PWM mode
To set Fast PWM mode, we have to set WGM00: 01= 11. To generate a PWM
waveform on the OC0 pin, we need to set COM01:00= 10 or 11.
COM01:00= 10 will generate Noninverting PWM output waveform and
COM01:00= 11 will generate Inverting PWM output waveform

void PWM_init()
{
/*set fast PWM mode with non-inverted output*/
TCCR0 = (1<<WGM00) | (1<<WGM01) | (1<<COM01) | (1<<CS00);
DDRB|=(1<<PB3); /*set OC0 pin as output*/
}

Setting Duty cycle: we have to load value in the OCR0 register to set the duty
cycle.
255 value for 100% duty cycle and 0 for 0% duty cycle. Accordingly, if we load
value 127 in OCR0, the Duty cycle will be 50%.

Figure 9.Inverted Fast PWM

13
Figure 10.Non-Inverted Fast PWM

The advantage of using PWM mode in AVR is that it is an inbuilt hardware unit for
waveform generation and once we set the PWM mode and duty cycle, this unit
starts generating PWM and the controller can do other work.

Program:
/*
AVR ATmega32 PWM to control LED brightness
*/

#define F_CPU 16000000UL


#include "avr/io.h"
#include <util/delay.h>

void PWM_init()
{
/*set fast PWM mode with non-inverted output*/
TCCR0 = (1<<WGM00) | (1<<WGM01) | (1<<COM01) | (1<<CS00);
DDRB|=(1<<PB3); /*set OC0 pin as output*/
}

int main ()
{
unsigned char duty;

PWM_init();
while (1)
{
for(duty=0; duty<255; duty++)
{

14
OCR0=duty; /*increase the LED light intensity*/
_delay_ms(8);
}
for(duty=255; duty>1; duty--)
{
OCR0=duty; /*decrease the LED light intensity*/
_delay_ms(8);
}
}
}

15
Experiment 4

Aim: To write a program for LDR to vary the light intensity of LED using
Arduino.

Theory:
STEP1: Start the program.
STEP2: Start →Arduino 1.88[IDE].
STEP3: Enter the coding in Arduino software.
STEP4: Compile the coding in the Arduino software.
STEP5: From LDR light sensor module, connect VCC to power supply 5V and
connect to digital pin D3 and connect GND to ground gnd using jumper wires to
arduino board.
STEP6: For LED, connect D to digital pin D2 and connect GND to ground GND
using jumper wires to arduino board.
STEP7: Show the variance of lights intensity in LED we use LDR light sensor
module. STEP8: Stop the process.

Code:
const int ldr_pin = 3;
const int led_pin = 2;
void setup() {
pinMode(ldr_pin, INPUT);
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
}

16
void loop() {
if ( digitalRead( ldr_pin ) == 1) {
digitalWrite(led_pin, HIGH);
}
else {
digitalWrite(led_pin , LOW);
}
Serial.println(digitalRead( ldr_pin ));
delay(100);
}

Output:

Result:

Thus the output for LDR to vary the light intensity of LED using Arduino has
successfully executed.

17
Experiment 5

Aim: .Python to blink an LED on and off every second.

Theory:
● Connect a female-to-male jumper wire from the Raspberry Pi’s GND pin to
the negative rail of the breadboard.

● Place an LED into two holes on the breadboard that are next to each other but
not in the same row.

● Place the longer, positive leg of the LED into the hole on the right side.

● Place the shorter, negative leg of the LED into the hole on the left side.

● Place one end of a 330 Ω resistor into a hole in the same breadboard row as
the negative leg of the LED.

● Place the other end of the resistor into the negative rail of the breadboard

● Connect a female-to-male jumper wire from the Raspberry Pi’s GPIO4 pin to
a hole in the same breadboard row as the positive leg of the LED.

18
Code
from gpiozero import LED
from signal import pause

led = LED(4)
led.blink()

pause()

19
Experiment 6

Aim: .Connecting a passive infrared (PIR) motion sensor to the Raspberry


Pi.

Theory:
● When using a motion sensor, you may need to adjust how sensitive it is
to motion and how long it will send out a signal after motion is detected.
● You can make adjustments using two dials on the side of the sensor.
● Once you’ve adjusted the motion sensor, you’re ready to set up the
wiring. The motion sensor’s design doesn’t allow it to easily connect to a
breadboard.
● You’ll need to connect the Raspberry Pi’s GPIO pins directly to the pins
on the motion sensor with jumper wires.
● Connect a female-to-female jumper wire from the Raspberry Pi’s 5V pin
to the sensor’s VCC pin.
● Connect a female-to-female jumper wire from the Raspberry Pi’s GPIO4
pin to the sensor’s OUT pin.
● Connect a female-to-female jumper wire from the Raspberry Pi’s GND
pin to the sensor’s GND pin.

20
Code

from gpiozero import MotionSensor


from signal import pause

motion_sensor = MotionSensor(4)

def motion():
print("Motion detected")

def no_motion():
print("Motion stopped")

print("Readying sensor...")
motion_sensor.wait_for_no_motion()
print("Sensor ready")

motion_sensor.when_motion = motion
motion_sensor.when_no_motion = no_motion

21
pause()

Output:

Readying sensor...
Sensor ready

Motion detected

22

You might also like