www.NewsDownload.co.uk Page 27

Raspberry Pi Multi Channel Timer

2015-07-05 21:05 By Jason Birch

A multi channel timer module which could be used for central heating, lighting, appliance, ... timing. Running independently with numeric keypad and LCD display.

VIDEO
The video here demonstrates the project which this article describes how to build.

The article breaks the project down into several stages:

  • Software Python Lessons
  • Circuit IO Optimization
  • Build
  • Project Costing

Reference:

The Python Lessons
Use the Raspian OS distribution for this project, it's easy to install and it comes with Python installed and ready to use. Just copy the NOOBS files onto an SD card and power up the Raspberry Pi, with the SD card installed. See the Raspberry Pi Downloads page for details.

Open a terminal window and type this text, followed by a return key. A terminal window can be opened from the menu in the top left of the display:
echo "print(\"TEST\");" | python

Python should be installed by default and you should see the following text on the next line:
TEST

If the text is not displayed, you can make sure Python is installed by typing this text followed by a return key:
sudo apt-get install python

You can use any text editor to write Python programs, leafpad is the default text editor installed with Raspbian. It can be started from the menus at the top left of the display, or by typing the following in a terminal window:
leafpad &

The final application required when developing Python is a File Manager, this will provide a graphic view of the files in the Python project. You can start the file manager from the menus in the top left of the display, or by typing the following in a terminal window:
pcmanfm &

The source code is available to download from the GitHub web site at the following link:
Project GitHub Repository

I recommend running though the lessons on this web page, copying the code from the web page and pasting into a text editor on the Raspberry Pi. The code can then be saved and run. This also gives you an opportunity to read the comments in the source code and in conjunction with the lesson text, learn about how the Python code works.

Keypad Circuit

PiTimer

The first section is the software license which the source code is published under. The sections after are the lessons, a section can be opened or closed by clicking on it's header title. Each section contains a set of sub sections, which are each of the source code files. Each section contains the full source code for the lesson, so any lesson can be jumped to, but it would be simpler to follow by accessing each section in the order they are provided as the lessons progress in complexity.

LCD Circuit

Build
The circuit diagrams for this project are covered in the final three lessons Step 9 - Maintaining the system time, Step 10 - Numeric GPIO keypad and Step 11 - 20 x 4 LCD display. The build of the project is recommended at the point the hardware is implemented in the project, the prior steps cover the evolution of the software design and can all be performed on a Raspberry Pi without any hardware modifications required.

From a hardware point of view this is a very simple project, it consists of a few resistors, two transistors and two leds. This project requires the Raspberry Pi Battery Backed Real Time Clock project to be built too.

GNU GENERAL PUBLIC LICENSEClick to expand...

GETTING STARTEDClick to expand...

PiTimer - Step 1 - Programatically setting the system time.Click to expand...

PiTimer - Step 2 - Python Classes.Click to expand...

PiTimer - Step 3 - Schedule table.Click to expand...

PiTimer - Step 4 - Schedule functions.Click to expand...

PiTimer - Step 5 - Relay functions.Click to expand...

PiTimer - Step 6 - User interactions.Click to expand...

PiTimer - Step 7 - Save/Load application state.Click to expand...

PiTimer - Step 8 - Controlling physical relays.Click to expand...

PiTimer - Step 9 - Maintaining the system time.Click to expand...

PiTimer - Step 10 - Numeric GPIO keypad.Click to expand...

PiTimer - Step 11 - 20 x 4 LCD display.Click to expand...

Driving Switching Devices
A transistor is required to drive switching devices. On my test LED board the transistors are just driving LEDs. Some transistors are capable of driving high voltages and/or high currents. However care must be taken when selecting a transistor, as some will require more power to switch than the Raspberry Pi is capable of supplying. Darlington drivers are a good choice to use, as they can drive large currents and have very high gain, which means they require a small amount of current to switch a large amount of current.

Designing a driver circuit here is beyond the scope of this article, it is a large topic and will depend entirely on the type of device you are trying to power.

NOTE: When driving a relay at least a transistor must be used and care taken when selecting the transistor. A reverse bias diode is also required, to prevent back EMF from the inductance of the relay coils.


Tasks To Try
The object of this article is to assist someone who wan't to program in Python. Allowing them to jump in at a very basic level, or skip some of the earlier stages if a basic understanding has already been achieved. It introduces concepts of design and reasoning behind taking decisions along the application design process.

At this stage you should have the full application running. As a challenge, here are some ideas to try, if you would like to attempt to modify the code. This would be a good idea to do, as I learned to program by typing in programs from magazines, such as Sinclair Programs and Sinclair User. Then understanding how they work by modifying them. Eventually writing my own programs. The great thing about learning to program is, if the program stops running, try and understand what went wrong or reload the original version and try again. Remember when writing code keep regular backups, they will come in handy from time to time, ideally use a version control system such as GIT, which will allow you to go to a point in time of your development process.

  • When the application starts, after the previous schedule items have been loaded. The schedule items will be loaded at the point they where saved, this could by many days ago. They will be repeatedly triggered until they get to what their current state should be. This is fine for schedules which trigger once an hour and doesn't take very long. But if you have a schedule which triggers every second and the application was last used a week ago, it can take a very long time to catch up. Devise a way which will run forward though the missed triggers quickly, so if a long period has passed since the application was shut down, the application starts swiftly.

  • Modify the application to provide a web interface, which can be used over WiFi.
If you modify the application and redistribute it, under the terms of the GNU Public License, you must keep all license details in place and a credit to myself as the original author of the software in place.


Project Cost
Item Reference Qty Each Cost
Playing card case eBay 2 £1.25 £2.50
Stripboard 127mm x 95mm, 50 holes x 36 holes BitsBox 1 £1.67 £1.67
36-Way Right Angled Header Strip BitsBox 2 £0.39 £0.78
25-Way Single Row Socket BitsBox 3 £0.56 £1.68
470R Resistor Carbon film 1/4W 5% BitsBox 15 £0.05 £0.75
1K Resistor Carbon film 1/4W 5% BitsBox 2 £0.05 £0.10
10K skeleton trimmer BitsBox 1 £0.12 £0.12
20x4 LCD Display Module with Backlight BitsBox 1 £7.33 £7.33
3mm Red LED BitsBox 1 £0.10 £0.10
3mm Green LED BitsBox 1 £0.10 £0.10
Chromed Plastic 3mm LED Bezel BitsBox 2 £0.10 £0.20
BC337 Transistor BitsBox 2 £0.10 £0.20
KB304 - 12-key keypad BitsBox 1 £3.26 £3.26
Real Time Clock Module NewsDownload.co.uk 1 £6.68 £6.68
Raspberry Pi Model A CPC 1 £16.84 £16.84
8GB SD Card CPC 1 £9.92 £9.92
TOTAL £52.23

Loading...