Top Banner
 #9/3,Shree Lakshmi complex,2 nd Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085 .www.tenettech.com  2014  Author: P N KIRAN Email ID:[email protected] Controlling Bulb using GUI in Raspberry pi 
14

Controlling Bulb Using GUI in Raspberry Pi with python code and c code

Jun 02, 2018

Download

Documents

P N KIRAN
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 1/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

2014 

 Author: P N KIRAN

Email ID:[email protected] 

Controlling Bulb using GUI in Raspberry pi 

Page 2: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 2/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

Introduction: 

This easy step by step guide will help you set up your raspberry pi and get

started with Controlling Bulb using GUI in Raspberry pi.

Hardware requriment:

Raspberry pi board

8GB sd card

blub

relay

12v power supply break out board

Software requriment  : 

Raspbian.

Raspberry pi:

The raspberry pi is a credit-card size computer that plugs into your TV and a keyboard.it is a

capbable little computer which can be used in electronics projects and for many of the things

that your desktop pc does,like spreadsheets, word-processing, games and it also plays high-

definition video. More info please click here 

Relay:

A relay is an electrically operated switch.  Many relays use an electromagnet to mechanically

operate a switch, but other operating principles are also used, such as  solid-state relays. Relaysare used where it is necessary to control a circuit by a low-power signal (with complete

electrical isolation between control and controlled circuits), or where several circuits must be

controlled by one signal. The first relays were used in long distance  telegraph circuits as

amplifiers: they repeated the signal coming in from one circuit and re-transmitted it on another

Page 3: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 3/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

circuit. Relays were used extensively in telephone exchanges and early computers to perform

logical operations. more info please click here 

Pin out

A microcontroller's GPIO (general purpose input/output) pins cannot handle higher power

requirements. An LED was easy enough, but large power items such as light bulbs, toaster

ovens, and blenders required more sneaky circuitry. Something sneaky called a relay.

This board contains the relay, transistor, diode, resistor and activation LED. The board requires

5V and GND to operate.

Page 4: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 4/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

12v power supply break out board:

A power supply is an electronic device that supplies electric energy to an electrical load. The

primary function of a power supply is to convert one form of electrical energy to another and,

as a result, power supplies are sometimes referred to as electric power converters. Here we

provide one breakout board which supply you 3.3V,5V,12V for various applications

Booting with raspbian:

Step 1: Download the desired raspbian image from the downloads section of the raspberry

pi. for downloading this operating system please click here. 

Step 2: SD card setup (copying the image to an SD card using on windows)

  Extract the image file 2013-05-25-wheezy-raspbian.img from the downloaded .zip file.

  Download the Win32DiskImager utility.

  Extract the executable from the zip file and run the Win32DiskImager utility. You may

need to run the utility as Administrator

  Insert the SD card into your SD card reader and check what drive letter it was assigned.

Page 5: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 5/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

  You can easily see the drive letter (for example F:) by looking in the left column of

Windows Explorer.

  If the card is not new, you should format it; otherwise Win32DiskImager may hang.

  Select the 2013-05-25-wheezy-raspbian.img image file you extracted earlier

  Select the drive letter of the SD card in the device box.

  Be careful to select the correct drive; if you get the wrong one you can destroy your

computer's hard disk! .

  Click Write

  And wait for the write to complete.

Page 6: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 6/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

  Exit the imager and eject the SD card.

  Insert the card in the Raspberry Pi, power it on, and it should boot up. There is an option

in the configure script that comes up to expand the partitions to use all of the SD card if

you have used one larger than 4GB.

  Default login and password

User name: pi

Password: raspberry

For entering GUI window type

pi@raspberrypi ~ $ startx

Page 7: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 7/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

Connect ethernet cabel to raspberry pi

Step 3 : 

open a text editor by giving command as sudo nano led.py

led =is file name

.py=is python extention

Now we can write a python script that will on/off bulb using relay . Here’s the script to make that

happen: 

The default keyboard settings are for a generic keyboard in a UK-style layout. If you want they

keys to do what they’re labeled to do. we can change the key board layout to US-style.

Give command as sudo nano /etc/defult/keybord make change as

Page 8: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 8/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

Code :

import RPi.GPIO as GPIO ## Import GPIO library

from time import sleep

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD) ## Use board pin numbering

GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT

while 1:

GPIO.output(7,True) #pin 7 is now high

sleep(2)

GPIO.output(7,False)#pin7 is now low

sleep(2)

Save the program press clt+x it will ask to save the changes type y and the press enter.

compile and run the program by giving command sudo python led.py 

The code is pretty straight forward. It starts by initializing the GPIO library. Then it sets the

GPIO PIN7 to be an output. Next the code goes into an infinite loop. The loop turns the bulb off,

then sleeps for a second, then turns the bulb on, and finally sleeps for one second

Controlling BULB using GUI

  For GUI based applications we have to install Tkinter

Tkinter: (Tk)

  Python interface to GUI tool kit

  The Tkinter module ("Tk interface") is the standard Python interface to the Tk GUI tool

kit from Scriptics (formerly developed by Sun Labs).

  Both Tk and Tkinter are available on most Unix platforms, as well as on Windows and

Macintosh systems.

Page 9: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 9/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

  The Tk interface is located in a binary module named _tkinter (this was tkinter in earlier

versions).

  This module contains the low-level interface to Tk, and should never be used directly by

application programmers.

  It is usually a shared library (or DLL), but might in some cases be statically linked withthe Python interpreter.

  In addition to the Tk interface module, Tkinter includes a number of Python modules.

  The two most important modules are the Tkinter module itself, and a module called

Tkconstants.

  The former automatically imports the latter, so to use Tkinter, all you need to do is to

import one module:

  Type the following command to activate Tkinter:

sudo apt-get install python python-tkidle python-pmwpython-imaging

open a text editor by giving command as sudo nano tkinter.py

code :

# -*- coding: utf-8 -*-import RPi.GPIO as GPIO

import sys

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.OUT)

from Tkinter import *

def ledon():

GPIO.output(7,True)

def ledoff():

GPIO.output(7,False)

root =Tk()

button1 = Button(root,fg= 'BLACK',text="ON",command=ledon)

Page 10: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 10/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

button2 = Button(root,fg= 'RED',text="OFF",command=ledoff)

button1.pack (side = LEFT )

button2.pack (side = RIGHT)

root.mainloop()

Save the program press clt+x it will ask to save the changes type y and the press enter.

compile and run the program by giving command sudo python tkinter.py then gui ON AND

OFF button will be pop up on you console pressing ON button on console blub will be glowing

pressing OFF button on console blub will be off state

For c code :

Download and Install

open a text editor and giving command as

WiringPi is now maintained under GIT for ease of change tracking,

If you do not have GIT installed, then under the Raspbian you can install it with:

sudo apt-get install git-core

If you get any errors here, make sure your Pi is up to date with the latest versions of Raspbian:

sudo apt-get update

Page 11: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 11/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

To obtain WiringPi using GIT:

git clone git://git.drogon.net/wiringPi

If you have already used the clone operation for the first time, then

cd wiringPi

Will fetch an updated version then you can run the build script below.

To build/install there is a new simplified script:

./build

open a text editor by giving command as sudo nano relay.c type a code 

Code

#include <stdio.h>

#include <wiringPi.h>

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.

#define LED 0

int main (void)

{

Page 12: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 12/13

 

#9/3,Shree Lakshmi complex,2nd

Floor,Opp,To Vivekananda Park,Girinagar,Bangalore-560085.www.tenettech.com 

printf ("Raspberry Pi blink\n") ;

wiringPiSetup () ;

pinMode (LED, OUTPUT) ;

for (;;)

{

digitalWrite (LED, HIGH) ; // On

delay (2000) ; // mS

digitalWrite (LED, LOW) ; // Off

delay (2000) ;

}

return 0 ;

}

Save the program clt+x it will ask to save the changes type y and the press enter.

compile the program by giving command gcc -Wall blink.c -o blink –lwiringPi

run the program by giving command ./blink

Then it sets the GPIO PIN11 to be an output. Next the code goes into an infinite loop. The loop

turns the bulb off, then sleeps for a second, then turns the bulb on, and finally sleeps for one

second

Page 13: Controlling Bulb Using GUI in Raspberry Pi with python code and c code

8/10/2019 Controlling Bulb Using GUI in Raspberry Pi with python code and c code

http://slidepdf.com/reader/full/controlling-bulb-using-gui-in-raspberry-pi-with-python-code-and-c-code 13/13

 

nd

Circuit Diagram

Training

Daily and weekly Deals 

Youtube channel- 

For queries:[email protected] 

Contact:080-26722726