Minor 5sem Iot Manual w22
Minor 5sem Iot Manual w22
LABORATORY MANUAL
IoT Embedded Hardware
Subject Code: 115AIO1
B.E. SEM. V Year 2022-23 (Odd Semester)
CERTIFICATE
Date of submission:
INDEX
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.
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 */
}
}
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
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.
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 */
}
}
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.
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).
0 0 Normal
0 1 CTC (Clear timer on Compare
Match)
1 0 PWM, Phase correct
1 1 Fast PWM
0 1 Reserved Reserved
1 0 Non-inverted Clear OC0 on compare match, set OC0
at TOP
11
2. When WGM00: WGM01= 10 i.e. Phase correct PWM. Compare Output
Mode
waveform generator on OC0 pin
1COM Description
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%.
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
*/
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
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
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
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