Garage Door Opener - Python

27
International Burch University TITLE PROJECT: Garage Door Opener - Python Programming 1 Department of Electrical and Electronics Engineering International Burch University

description

This project is based on a DC motor that is used as a mechanism for control, that is, opening and closing the garage door.

Transcript of Garage Door Opener - Python

Page 1: Garage Door Opener - Python

International Burch University

TITLE PROJECT:

Garage Door Opener - Python

Programming 1

Department of Electrical and Electronics Engineering

International Burch University

Student : Đumić Dalibor Professor : Prof.dr. Abdülhamit Subaşi

December, 2014.

Page 2: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

Content:

1 Introduction.......................................................................................................................................... 3

2 Hardware part....................................................................................................................................... 4

2.1 Rasbperry Pi..........................................................................................................................................4

2.2 GPIO......................................................................................................................................................5

2.3 DC Motor...............................................................................................................................................6

2.4 L293D Driver.........................................................................................................................................7

2.5 4-channel RF Module............................................................................................................................7

2.6 LCD Display 2x16...................................................................................................................................8

3 Hardware realization............................................................................................................................. 9

3.1 Connecting LCD display with Raspberry Pi.............................................................................................9

3.2 Connecting L293D, DC motor and battery with Raspberry Pi..............................................................10

3.3 Connecting 4-channel RF module with Raspberry Pi...........................................................................11

3.4 Connecting everything together..........................................................................................................11

4 Software.............................................................................................................................................. 12

4.1 Raspbian.............................................................................................................................................12

4.2 Python 3..............................................................................................................................................12

5 Software realizing................................................................................................................................ 13

5.1 Installing Python and GPIO library to Raspbian...................................................................................13

5.2 Writting the code................................................................................................................................14

5.3 The code..............................................................................................................................................14

6 Results................................................................................................................................................ 19

7 Conclusion........................................................................................................................................... 19

8 References.......................................................................................................................................... 19

w w w . i b u . e d u . b a2

Page 3: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

1 Introduction

This project is based on a DC motor that is used as a mechanism for control, that is, opening and closing the garage door. In order to do this, to control the DC motor is needed a drive that works on the principle of H - bridge, in this case, reversing the direction of the DC motor and switching off the engine. The most suitable driver for such a project such as this, is L293D driver. As the main controller , in this project is used Raspberry Pi, which is among the smallest computer in the world. Raspberry Pi in hardware part communicates with the L293D driver via GPIO (General Purpose Input Output), which consists of 26 pins, and in software part communicates via one of the most popular programming languages - Python 3. For controlling by hand, here is remote wirelles device (RF remote) which has 4 buttons. For indication of status of the garage door, here is used LCD 2x16 display. In the continuation it will shown how to prepare the Raspberry Pi, bindings scheme and ,at the end, the program code.

w w w . i b u . e d u . b a3

Page 4: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

2 Hardware part

2.1 Rasbperry PiRPI is a mini computer on whose board the size of a credit card are Broadcom 32-bit processor with a clock speed of 700MHz, Broadcom Video Core IV GPU and 512 MB of RAM (Model B) or 256 MB RAM (Model A), which means that is at the heart of RPI's is the same processor that powered the iPhone 3G and Kindle 2nd Graphics is equivalent to that of the first generation Xbox. In addition to Broadcom's chips, there are also:

HDMI out,

RCA video adapter,

audio out

USB 2.0 port (Model B has 2,

Model A 1),

Ethernet port

slot for SD card.

USB micro port,

GPIO pins,

5 status LEDs.

RPI is an excellent choice for computers in the classroom when used as a platform for learning programming or digital communications between the external elements (robotics), and with a little effort can serve as a personal computer to access the Internet.

w w w . i b u . e d u . b a4

Figure 2. Configuration of Raspberry PI computer

Figure 1. Rasbperry PI model B

Page 5: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

2.2 GPIO

One powerful feature of the Raspberry Pi is the row of GPIO (general purpose input/output) pins along the edge of the board, next to the yellow video out socket.

These pins are a physical interface between the Pi and the outside world. They are used to turn on or off (input) or by Pi turn on / off (output). Seventeen of the 26 pins are GPIO pins; the others are power or ground pins.

w w w . i b u . e d u . b a5

Figure 3. General Purpose Input / Output

Figure 4. The layout of GPIO pins

Page 6: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

2.3 DC Motor

A DC motor relies on the fact that like magnet poles repel and unlike magnetic poles attract each other. A coil of wire with a current running through it generates an electromagnetic field aligned with the center of the coil.

By switching the current on or off in a coil its magnetic field can be switched on or off or by switching the direction of the current in the coil the direction of the generated magnetic field can be switched 180°.

A simple DC motor typically has a stationary set of magnets in the stator and an armature with a series of two or more windings of wire wrapped in insulated stack slots around iron pole pieces (called stack teeth) with the ends of the wires terminating on a commutator. The armature includes the mounting bearings that keep it in the center of the motor and the power shaft of the motor and the commutator connections. The winding in the armature continues to loop all the way around the armature and uses either single or parallel conductors (wires), and can circle several times around the stack teeth.

The total amount of current sent to the coil, the coil's size and what it's wrapped around dictate the strength of the electromagnetic field created. The sequence of turning a particular coil on or off dictates what direction the effective electromagnetic fields are pointed. By turning on and off coils in sequence a rotating magnetic field can be created.

These rotating magnetic fields interact with the magnetic fields of the magnets (permanent or electromagnets) in the stationary part of the motor (stator) to create a force on the armature which causes it to rotate. In some DC motor designs the stator fields use electromagnets to create their magnetic fields which allow greater control over the motor. At high power levels, DC motors are almost always cooled using forced air.

w w w . i b u . e d u . b a6

Figure 5. DC motor

Figure 6. Simple DC motor

Page 7: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

2.4 L293D Driver

L293D is a dual H-bridge motor driver integrated circuit (IC). Motor drivers act as current amplifiers since they take a low-current control signal and provide a higher-current signal. This higher current signal is used to drive the motors.

L293D contains two inbuilt H-bridge driver circuits. In its common mode of operation, two DC motors can be driven simultaneously, both in forward and reverse direction. The motor operations of two motors can be controlled by input logic at pins 2 & 7 and 10 & 15. Input logic 00 or 11 will stop the corresponding motor. Logic 01 and 10 will rotate it in clockwise and anticlockwise directions, respectively.

Enable pins 1 and 9 (corresponding to the two motors) must be high for motors to start operating. When an enable input is high, the associated driver gets enabled. As a result, the outputs become active and work in phase with their inputs. Similarly, when the enable input is low, that driver is disabled, and their outputs are off and in the high-impedance state

2.5 4-channel RF ModuleRF modules offer the capability to wireless transmite datta on electronic and robotics projects, this modules are extremly easy to use and are quite cheap wich make them ideal for projects. The XY-DJM-5V module has 7 pins, the first two GND and 5V will be connected to ground and power pins on the RPi GPIO.

The D0,D1,D2,D3 pins respond to a digital output from the toggle of the buttons on the remote controll, when a button is pressed the pin goes from GND to HIGH, conecting each one on this to the analog ports of the Arduino board will give the needed feedback.

2.6 LCD Display 2x16

w w w . i b u . e d u . b a7

Figure 7. RF module

Figure 6. Pins layout of L293D driver

Page 8: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

A register select (RS) pin that controls where in the LCD's memory can be writed data.

A Read/Write (R/W) pin that selects reading mode or writing mode. An Enable pin that enables writing to the registers 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that it is

writed to a register when you write, or readed the values.

There's also a display constrast pin (Vo), power supply pins (+5V and Gnd) and LED Backlight (Bklt+ and BKlt-) pins that can be used to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively. The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register.

Figure 8. Pins layout of LCD display

w w w . i b u . e d u . b a8

Page 9: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

3 Hardware realization

In this project these items are used:

2 breadboards; Raspberry Pi RPi Cobbler and jumper wire DC motor 3 V 4-channel RF module and 6 resistors (three 1.5 kΩ and three 3kΩ) L293D driver

3.1 Connecting LCD display with Raspberry Pi

Figure 9. Connecting LCD display with Raspberry Pi

There is used potentiometer with resistance 10kΩ which is used to controll contrast of the LCD display. Pin 1 , 4 and 16 are connected to the ground (GND). Pin 2 and 15 are connected to power 5V from Raspberry Pi. Pin 3 is connected to the potentiometer. RS pin is connected to 3 of GPIO. R/W pin is connected to the GND. EN pin is connected to 18 pin. D0,D1,D2 and D3 are not connected. D4, D5, D6 and D7 are connected to 16, 11, 13 and 15. Pin 15 and 16 are connected to 5V and GND.

w w w . i b u . e d u . b a9

Page 10: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

3.2 Connecting L293D, DC motor and battery with Raspberry Pi

Figure 10. Connecting L293D, Dc motor and battery with Raspberry Pi

The reason why is used battery is that Raspberry Pi doesn't have enough power to run LCD display, driver, DC motor and RF module at the same time. DC motor spends a lot of power so battery is used to supply DC motor. Pin Vcc1 is connected to 5V. Pin In4 is connected to 26 pin of GPIO. Pin Out4 is connected to the first pin of DC motor. Pin Out3 is connected to the second pin of the DC motor. Pin In3 is connected to pin 24 of GPIO. Pin En1 is connected to pin 10 of GPIO. The Vcc2 pin is connected to pin 12 of GPIO.

For controlling the speed of rotation of DC mtoor, for that is used Pulse-Width Modulation which works by pulsing in time intervals.

w w w . i b u . e d u . b a10

Page 11: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

3.3 Connecting 4-channel RF module with Raspberry Pi

Figure 11. Connecting 4-channel RF module with Raspberry Pi

RF module XY-DJM-5V has 7 pins. Two of them are for supply ( 5V ) and ground, other four are for signalling and last one is for indication output status. First pin is connected to GND. Second pin is connected to 5V. Pins D0, D1, D2 and D3 are connected to pins 10, 22 , 8 and 19 on GPIO. Resistor are used to divide the outputs from RF module ( from 5V to 3.3V ) to converting normal signal to logical "1" (3.3V).

3.4 Connecting everything together

Figure 12. Connecting everything together

w w w . i b u . e d u . b a11

Page 12: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

4 Software

4.1 Raspbian

Raspbian is a free operating system based on Debian optimized for the Raspberry Pi hardware. An operating system is the set of basic programs and utilities that make your Raspberry Pi run. However, Raspbian provides more than a pure OS: it comes with over 35,000 packages, pre-compiled software bundled in a nice format for easy installation on your Raspberry Pi.

The initial build of over 35,000 Raspbian packages, optimized for best performance on the Raspberry Pi, was completed in June of 2012. However, Raspbian is still under active development with an emphasis on improving the stability and performance of as many Debian packages as possible.

Figure 12. www.raspbian.org

4.2 Python 3

Python is a widely-used high level programming language. Its elegant syntax allows you to clearly define application behaviour using fewer lines of code than would be required in other languages like VB. It supports multiple programming paradigms including imperative, functional and object oriented styles, allowing a wide range of tasks to be performed.

Figure 13. Offical logo of Python

w w w . i b u . e d u . b a12

Page 13: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

5 Software realizing

5.1 Installing Python and GPIO library to Raspbian

The installation of Python is perfomed by this command and it must be runned in LXTerminal ( Linux Console) :

sudo apt-get install python-dev

Figure 14. Installing Python

The installation of GPIO library is performed by this command :

sudo apt-get install python-rpi.gpio

w w w . i b u . e d u . b a13

Page 14: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

Figure 15. Installing GPIO library

5.2 Writting the codeCreating new file is perfomed by this command:

sudo nano garage-door.py

In the empty place there can be written code.

5.3 The code#!/usr/bin/python

#import

import RPi.GPIO as GPIO

import time

import datetime

# Define GPIO to LCD mapping

LCD_RS = 3

LCD_E = 18

LCD_D4 = 16

LCD_D5 = 11

LCD_D6 = 13

LCD_D7 = 15

# Define some device constants

LCD_WIDTH = 16 # Maximum characters per line

LCD_CHR = True

LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line

LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

# Timing constants

E_PULSE = 0.00005

E_DELAY = 0.00005

def main():

# Main program block

GPIO.setmode(GPIO.BOARD) # Use BCM GPIO numbers

GPIO.setup(LCD_E, GPIO.OUT) # E

GPIO.setup(LCD_RS, GPIO.OUT) # RS

GPIO.setup(LCD_D4, GPIO.OUT) # DB4

w w w . i b u . e d u . b a14

Page 15: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

GPIO.setup(LCD_D5, GPIO.OUT) # DB5

GPIO.setup(LCD_D6, GPIO.OUT) # DB6

GPIO.setup(LCD_D7, GPIO.OUT) # DB7

GPIO.setup(26,GPIO.OUT) #Pin4 L293D

GPIO.setup(24,GPIO.OUT) #Pin3 L293D

GPIO.setup(12,GPIO.OUT) #EnablePin L293D

GPIO.setup(10,GPIO.IN) #RF module pin D0 B button

GPIO.setup(8,GPIO.IN) #RF module pin D1 c button

GPIO.setup(22,GPIO.IN) #RF module pin D2 D button

GPIO.setup(19,GPIO.IN) #RF module pin D3 A button

motor = GPIO.PWM(12,10) #setting Pulse-Width Modulation

motor.start(10) #start motor with 10 pulses in time interval

GPIO.output(26,0)

GPIO.output(24,0)

# Initialise display

lcd_init()

lcd_byte(LCD_LINE_1,LCD_CMD)

lcd_string(" Welcome to the ")

lcd_byte(LCD_LINE_2,LCD_CMD)

lcd_string("Garage Door Opener")

time.sleep(3)

lcd_byte(LCD_LINE_1,LCD_CMD)

lcd_string("Press A for open")

lcd_byte(LCD_LINE_2,LCD_CMD)

lcd_string("B for close, C ")

time.sleep(3)

lcd_byte(LCD_LINE_1,LCD_CMD)

lcd_string("for date and D ")

lcd_byte(LCD_LINE_2,LCD_CMD)

lcd_string(" for exit! :)")

time.sleep(3)

while True:

#if pressed D button

w w w . i b u . e d u . b a15

Page 16: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

if (GPIO.input(22)):

lcd_byte(LCD_LINE_1,LCD_CMD)

lcd_string(" Thank you for ")

lcd_byte(LCD_LINE_2,LCD_CMD)

lcd_string(" watching ! :) ")

time.sleep(5)

#if pressed C button

if (GPIO.input(8)) :

GPIO.output(12,1)

lcd_byte(LCD_LINE_1,LCD_CMD)

lcd_string("Date:"+time.strftime("%d %b %Y"))

lcd_byte(LCD_LINE_2,LCD_CMD)

lcd_string(" Time : "+time.strftime("%H:%M"))

time.sleep(3)

#if pressed A button

if (GPIO.input(19)):

GPIO.output(26,1)

GPIO.output(24,0)

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string(" STATUS : ")

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string(" opening... ")

time.sleep(5)

GPIO.output(26,0)

lcd_byte(LCD_LINE_1,LCD_CMD)

lcd_string(" STATUS : ")

lcd_byte(LCD_LINE_2,LCD_CMD)

lcd_string(" opened ")

#if pressed B button

if (GPIO.input(10)) :

GPIO.output(26,0)

GPIO.output(24,1)

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string(" STATUS : ")

w w w . i b u . e d u . b a16

Page 17: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string(" closing... ")

time.sleep(5)

GPIO.output(24,0)

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string(" STATUS : ")

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string(" closed ")

def lcd_init():

# Initialise display

lcd_byte(0x33,LCD_CMD)

lcd_byte(0x32,LCD_CMD)

lcd_byte(0x28,LCD_CMD)

lcd_byte(0x0C,LCD_CMD)

lcd_byte(0x06,LCD_CMD)

lcd_byte(0x01,LCD_CMD)

def lcd_string(message):

# Send string to display

message = message.ljust(LCD_WIDTH," ")

for i in range(LCD_WIDTH):

lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):

# Send byte to data pins

# bits = data

# mode = True for character

# False for command

GPIO.output(LCD_RS, mode) # RS

# High bits

w w w . i b u . e d u . b a17

Page 18: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

GPIO.output(LCD_D4, False)

GPIO.output(LCD_D5, False)

GPIO.output(LCD_D6, False)

GPIO.output(LCD_D7, False)

if bits&0x10==0x10:

GPIO.output(LCD_D4, True)

if bits&0x20==0x20:

GPIO.output(LCD_D5, True)

if bits&0x40==0x40:

GPIO.output(LCD_D6, True)

if bits&0x80==0x80:

GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin

time.sleep(E_DELAY)

GPIO.output(LCD_E, True)

time.sleep(E_PULSE)

GPIO.output(LCD_E, False)

time.sleep(E_DELAY)

# Low bits

GPIO.output(LCD_D4, False)

GPIO.output(LCD_D5, False)

GPIO.output(LCD_D6, False)

GPIO.output(LCD_D7, False)

if bits&0x01==0x01:

GPIO.output(LCD_D4, True)

if bits&0x02==0x02:

GPIO.output(LCD_D5, True)

if bits&0x04==0x04:

GPIO.output(LCD_D6, True)

if bits&0x08==0x08:

GPIO.output(LCD_D7, True)

w w w . i b u . e d u . b a18

Page 19: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

# Toggle 'Enable' pin

time.sleep(E_DELAY)

GPIO.output(LCD_E, True)

time.sleep(E_PULSE)

GPIO.output(LCD_E, False)

time.sleep(E_DELAY)

if __name__ == '__main__':

main()

w w w . i b u . e d u . b a19

Page 20: Garage Door Opener - Python

Garage Door opener - Python Đumić Dalibor

6 Results

When is pressed for example button A, RF module generates signal of 5V, but 5V is not regular high signal and Raspberry Pi can't recognise this signal, so it's divided by resistors 1.5kΩ and 3kΩ to 3.3 V which is logical "1". Raspberry Pi recognise this divided signal and perfoms the action : from GPIO pin 26 send high signal or logical"1" to L293D driver and from GPIO pin 24 sends low signal or logical "0" to L293D driver. This means that L293D driver runs the DC motor in clock direction. Of course, when is recognised that divided signal is high, Python program sends some data string to LCD library and it outputs to LCD Display.

On the same way, B buttons run the same action, but runs the DC motor in clockwise direction.

7 Conclusion

The usage of DC motor is unlimited. It can be used everywhere. This garage door opener is one example of a lot. DC motors can be used in elevators, industry, building, technology and enternainment ( for joystick vibrators). It's very useful item.

8 References

DC MOTOR : http://en.wikipedia.org/wiki/DC_motor

CONTROLLING DC MOTOR: http://www.rs-online.com/designspark/designshare/eng/projects/313/view/stage/brief/

L293D driver : http://www.engineersgarage.com/electronic-components/l293d-motor-driver-ic

RF 4 channel module : http://www.libremechanics.com/?q=node/30

LCD display : http://arduino.cc/en/Tutorial/LiquidCrystal

Python and GPIO library: https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi/install-python-module-rpi-dot-gpio

w w w . i b u . e d u . b a20