Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Experiment No.

Aim: To study Arduino Uno IoT Kit with ATMega 328 Microcontroller.

Requirement (Hardware/Software): ATMEGA328P, Arduino IDE, Arduino Uno

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.

ATmega328 Pins Description:

• 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

• AVCC is a supply voltage pin for analog to digital converter.

• VCC is a digital voltage supply.


GND denotes Ground and it has a 0V.

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.

• When we upload code in Arduino UNO, it’s actually uploaded in the

• Atmega328 Microcontroller.

• ATmega328 is the microcontroller used in the Arduino UNO board .

• A software driver called bootloader is pre-installed in the flash memory of the


Atmega328 microcontroller, which makes it compatible with Arduino IDE.

• AVR Atmega328 attached on Arduino is shown in the figure given below


ATmega328 Pinout:

2|Page
Applications of Atmega328:
• A complete package including ATMega 328 and Arduino can be used in several
different real-life applications.

• It can be used in Embedded Systems Projects.

• It can also be used in robotics.

• 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.

The IDE is common to all available boards of Arduino.

ATmega328 Microcontroller- It is a single chip Microcontroller of the Atmel family. The


processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and Flash),
Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer , external and internal
interrupts, and oscillator.
ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.
Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.

Result : Thus, we learn about Arduino UNO kit using and ATmega 328 microcontroller .

4|Page
Experiment No. 2

Aim: Design a sketch for running of LEDs.


Requirement (Hardware/Software):

o 3 x red LED
o 3 x 220 Ohm Resistors
o Arduino UNO R3 board
o Jump wires
o Breadboard

We can use any colour LED as per our choice.

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

2. We will turn the LED ON/OFF individually.

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

Open the Arduino IDE and start with the coding,


which is given below:

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

• A momentary switch, button, or toggle switch

• 10k ohm resistor

• hook-up wires

• breadboard

Theory:

This example shows you how to monitor the state of a switch by


establishing serial communication between your Arduino and your computer over USB.

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:

// digital pin 2 has a pushbutton attached to it. Give it a name:

int pushButton = 2;

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// make the pushbutton's pin an input:

pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:

void loop() {

8|Page
// read the input pin:

int buttonState = digitalRead(pushButton);

// print out the state of the button:

Serial.println(buttonState);

delay(1); // delay in between reads for stability

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

Aim: Design a sketch to read analog value of potentiometer by establishing serial


communication between Arduino and computer.

Hardware Required

• Arduino Board

• 10k ohm Potentiometer

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() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// the loop routine runs over and over again forever:

void loop() {

11 | P a g e
// read the input on analog pin 0:

int sensorValue = analogRead(A0);

// print out the value you read:

Serial.println(sensorValue);

delay(1); // delay in between reads for stability

Result: We have successfully designed a sketch to read analog value of 10 K potentiometer


by establishing serial communication between Arduino and computer.

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.

*/

// constants won't change. Used here to set a pin number:


const int ledPin = LED_BUILTIN; // the number of the LED pin

// Variables will change:


int ledState = LOW; // ledState used to set the LED

// 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

// constants won't change:


const long interval = 1000; // interval at which to blink (milliseconds)

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();

if (currentMillis - previousMillis >= interval) {


// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:


if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:


digitalWrite(ledPin, ledState);
}
}

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

Aim : Functional Testing of Raspberry Pi


Flashing the OS on to the device into a stable functional state by porting
desktop environment with necessary packages.

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.

Set up your SD card


If you have an SD card that doesn’t have the Raspberry Pi OS operating system
on it yet, or if you want to reset your Raspberry Pi, you can easily install
Raspberry Pi OS yourself. To do so, you need a computer that has an SD card
port — most laptop and desktop computers have one.
The Raspberry Pi OS operating system via the Raspberry Pi Imager
Using the Raspberry Pi Imager is the easiest way to install Raspberry Pi OS on
your SD card. Note: More advanced users looking to install a particular
operating system should use this guide to installing operating system images.
Download and launch the Raspberry Pi Imager

o Visit the Raspberry Pi downloads page

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

Using the Raspberry Pi Imager


Anything that’s stored on the SD card will be overwritten during formatting. If
your SD card currently has any files on it, e.g. from an older version of
Raspberry Pi OS, you may wish to back up these files first to prevent you from
permanently losing them.

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

Connect your Raspberry Pi


Let’s connect up your Raspberry Pi and get it running.
o Check the slot on the underside of your Raspberry Pi to see whether an SD
card is inside. If no SD card is there, then insert an SD card with Raspbian
installed (via NOOBS).

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).

o Connect the keyboard in the same way.

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.

You could connect an optional second screen in the same way.

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

Your Raspberry Pi doesn’t have a power switch. As soon as you connect it to a


power outlet, it will turn on.

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.

After a few seconds the Raspberry Pi OS desktop will appear.

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.

Click Next to start the setup.


Set your Country, Language, and Timezone, then click Next again.

29 | P a g e
o Enter a new password for your Raspberry Pi and click Next.

o Connect to your WiFi network by selecting its name, entering


the password, and clicking 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

Now it’s time to take a tour of your Raspberry Pi.


o Do you see the raspberry symbol in the top left-hand corner? That’s where
you access the menu: click on it to find lots of applications.

31 | P a g e
o Click on Accessories, and then click on Text Editor.

o Type I just built a Raspberry Pi computer in the window that appears.

32 | P a g e
o Click on File, then choose Save, and then click on Desktop and save the file as

o You should see an icon named rp.txt appear on the desktop.

Your file has been saved to your Raspberry Pi’s SD card.


Close the text editor by clicking the X in the top right-hand corner of the
window.
Return to the menu, click on Shutdown, and then click on Reboot.
When Raspberry Pi has rebooted, your text file should still be there on the
desktop.
Raspberry Pi runs a version of an operating system called Linux
(Windows and macOS are other operating systems). This operating

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 In the window that appears, type:

o Click Next let the wizard check for updates to Raspbian and install them
(this might take a little while).

o Click Done or Reboot to finish the setup.

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.

o Now type this command to change directory to the Desktop:


cd Desktop

You have to press the Enter key after every command.


Then type:
ls

Can you see the text file you created?


o Close the terminal window by clicking on the X.
o Now drag to the Wastebasket on the desktop so the Raspberry Pi will
be tidy for the next person using it.

Browsing the web

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.

o Click the web browser icon and search for .

Configuring your Raspberry Pi


You can control most of your Raspberry Pi’s settings, such as the password,
through the Raspberry Pi Configuration application found in Preferences on
the menu.

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.

Camera — enable the Raspberry Pi Camera Module


SSH — allow remote access to your Raspberry Pi from another
computer using SSH
VNC — allow remote access to the Raspberry Pi Desktop from
another computer using VNC
SPI — enable the SPI GPIO pins
I2C — enable the I2C GPIO pins
Serial — enable the Serial (Rx, Tx) GPIO pins
1-Wire — enable the 1-Wire GPIO pin
Remote GPIO — allow access to your Raspberry Pi’s GPIO pins

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.

Overclock — change the CPU speed and voltage to increase performance

GPU Memory — change the allocation of memory given to the GPU


Localisation

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

Aim: GPIO Programming


Programming of available GPIO pins of the corresponding device using native
programming language. Interfacing of LED

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,

4. Save this file as blinky on the desktop,

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:

from gpiozero import LED


from time import sleep

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:

We need to measure the resistance of the photoresistor. The Raspberry pi acts


as the battery whereas the GPIO pin 1 provides 3.3 V to the photoresistor. Make
the GPIO pin 12 as the bidirectional pin ( input and output pin). When the
capacitor is charging it will take some time to reach a voltage that registers
as high. GPIO pin 6 is grounded which is connected to the negative side of the
capacitor (short end). Check how long it takes for the input pin to become high
and use the result to calculate the resistance of the photocell.

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

You might also like