Tuesday, 16 February 2016

Design and make a custom PCB

Converting bread board or soldered board to a printed circuit board has many benefits and a few downsides.

  • Professional looking product
  • No ugly wires all over the place
  • Can be replicated many times
  • Can be the size and shape of your choice
  • Usually have to order more than one board per design
  • Can be expensive for one board
  • If you get the design wrong the board could be scrap.
  • Can take a week or so to get the board
  • Shipping costs can be more than the price of the board (to Me in Australia)

Software for designing a custom PCB

There are a number of programs to create PBC's. I found that almost all fabrication companies accept Eagle Gerber files. So I used Eagle and there is a free version.

Eagle free version limitations are listed here http://www.cadsoftusa.com/download-eagle/freeware/  but here is the basics

  • The usable board area is limited to 100 x 80 mm (4 x 3.2 inches).
  • Only two signal layers can be used (Top and Bottom).
  • The schematic editor can only create two sheets.
  • Support is only available via email or through our forum (no fax or phone support).
  • Use is limited to non-profit applications or evaluation purposes.
Just download the software and install then click the freeware license option.

How to use Eagle

First create a new project. File -> New Project and rename it to a name of your choice.

Eagle Libraries

Next you will probably need to upload some libraries for the device that you are using. 
A good start is Sparkfun Libraries https://github.com/sparkfun/SparkFun-Eagle-Libraries 
Then for specific devices there are a lot available on Element 14 see below screen shot

There are a lot of manufacturer Eagle libraries to down load but not all each device that has a library will have link like the ones I highlighted in green boxes.

Building a Schematic

Once you have some Items you can start to build the schematic. Right click on the project and select New->Schematic 

Then minimize and go back to the main Eagle control panel and expand the libraries until you find the item you require and right click Add to Schematic.

The drawing shows the device schematic view on the left and physical view. Below the drawings is a list of connection types usually through hole and surface mount versions of the same device.
Add all the items you require then connect the items together on the Schematic. This takes some time and you should be careful to get it right. Right click gives you options and you can label wire connections. The net tool is the easiest way to connect items as you can then re-route them as required.

I recommend using headers to connect external items like LCD screens, sensors etc. This helps if you need to change connections or replace items and saves the need to re-soldering everything. 

Then check the connections with the ERC (Electrical Rule Check) tool the ERC errors will be shown. If you are happy with an error just approve it and the error will be ignored or fix the issue. I did not have a use for all the inputs one 74HC165N chip so I left them unconnected and approved these errors. 

PCB layout and wiring

Once the schematic is ready change to board view by clicking on the button from the schematic this will generate the basic PCB board and dump all the components on the page.

Move each item on to the outlined PCB. This process is a lot of trial and error so just do your best to get everything lined up where you think it will work best. Don't worry about the wiring lines the will be done automatically latter.
To move items click on the cross then move them into position right click will rotate an item if required.

Check the layout with the ERC and fix or approve any errors.
If you want wider solder traces than standard or bigger pads change settings in the DRC (Design Rules Check). The DRC will check that items are not too close together and general layout of the board. Once you have everything how you want it, use the autorouter to run all the wires. This can take some time. 

Once finished check the layout. You will probably want to reduce Vias by moving and rotating items. A via is where the connection has go Via another PCB layer to get to its destination. This below example shows some of the 75 vias that is not good.

Then move items around and find the best layout you can taking into account of mounting screws and connections. Here is the same PCB design with a better layout and just 4 vias.

I recommend using headers to connect things like LCD screens, sensors etc this means you can work on or replace the board without re-soldering everything each time. You can see that I have a header for sensor inputs, relay outputs, LCD screen, keypad, real time clock and a big one in the middle for the Raspberry PI.

Creating Gerber Files

To get a board built you will need Gerber files to send to the company that builds the boards. From the Board view click the CAM processor button.

Then from the dialog select File->Open->Job and select the gerb274x.cam

Create a folder to store the gerber files then Click on the File button on each tab and select the new folder (I add a gerber folder under the project folder) 

Check the path has changed on each tab. Click the Process Job button

Then do the same process but use the excellon.cam job and put the file in the same folder.
This will create a file for drill holes that some board manufacturers require

The files should look like the below

Then select the folder and zip the files into one file. Then check you file is good here http://www.gerber-viewer.com/default.aspx by uploading your zip file.

Now all you need to do is go to a PCB manufacturer and get your board built.

I have used http://www.seeedstudio.com/service/index.php?r=pcb the price was reasonable and they delivered the boards in about a week to Australia. I used a 2 layer board 10 cm x 10 cm there is a minimum order of 5 boards. In the US and Europe there is more choice of PCB manufacturers so you are likely to get a good price locally.

My custom PCB from .Seeed Studio

Here is the board almost fully populated with components.

Thank you for reading and I hope this information helps you build the board of your dreams.

Monday, 8 February 2016

Raspberry Pi - All things Date and Time in Python

When writing python code you often want to compare times dates and add delays etc. Here are a few things I have learnt that may help you in your projects.

Time delays

These are easy to use and are very useful all you need is to define time then use the sleep function.


print "Waiting 10 seconds"
print "Running again"

The sleep function can also do delays less than a second for example 
time.sleep(0.01) this is sometimes used for some GPIO that may need a moment to complete in another thread.

Formatting Date time

You can convert datetime to various parts that you want to display

Here I create a date time to format:-
import datetime
MyDateTime = datetime.datetime(2016,1,3,8,30,20)

Simple date and time extractions
print "Day of month :", MyDateTime.day
print "Month :", MyDateTime.month
print "year  :", MyDateTime.year
print "Weekday Monday to Sunday (0 to 6) :", MyDateTime.weekday()
print "Weekday Monday to Sunday (1 to 7) :", MyDateTime.isoweekday()
Program output:-

Formatted date outputs
print "ISO Formatted date    :", MyDateTime.isoformat()
print "Long format date time :", MyDateTime.ctime()
Program output:-

String formatted dates 

print MyDateTime.strftime("Time       : %H:%M:%S")

print MyDateTime.strftime("Short Date : %d/%m/%y")

print MyDateTime.strftime("Long Date  : %d/%m/%Y")

print MyDateTime.strftime("D/Month/Y  : %d-%b-%y")
print MyDateTime.strftime("Full Date  : %A %d %B %Y")
Program output:-

For the complete reference of strftime click this link http://strftime.org/

How to compare dates and times and just the time

To compare first you need to input a date, time or both. To input a full date and time use the datetime function with year,day,month,hour,minute,second. Use the if statement with greater than (>) or less than (<) operators (never use == as the time is measured in microseconds so its unlikely match exactly).
import datetime
MyDateTime = datetime.datetime(2016,1,3,8,30,20)
print "Entered Date time is ", MyDateTime

if MyDateTime > datetime.datetime.now():
print "Current datetime is bigger"
print "My datetime is bigger"

To compare time only use the time function and compare the time only. can be used to do some operation the same time every day.

MyTimeOnly = datetime.time(8,31,53)
print "Entered time is ", MyTimeOnly

if MyTimeOnly > datetime.time():
print "Current time is bigger"
print "My time is bigger"
Program output:-

What is UTC time and what does it mean?

UTC means Coordinated Universal Time (This used to be known as Greenwich Mean Time, or GMT). UTC is the common time standard across the world.

So why UTC and not CUT?
The Official Abbreviation

The official abbreviation for Coordinated Universal Time is UTC. It came about as a compromise between English and French speakers.
  • Coordinated Universal Time in English would normally be abbreviated CUT.
  • Temps Universel Coordonné in French would normally be abbreviated TUC.
How to display the current time and UTC time
import datetime
print "The current local date time is ",datetime.datetime.now()

print "The current UTC date time is   ",datetime.datetime.utcnow()
Program output:-

The resolution of time is to the microsecond. The date is the February 2nd 2016 (My time zone is Sydney Australia so UTC -11 hours on that date).

To get the current time zone UTC offset do the following
import time
print "The UTC offset is",time.altzone/3600,"Hours"
Program output:-

The function time.altzone returns seconds so divide by 3600 to get hours offset