Saturday, December 29, 2012

Breakout PCBs from OSH Park for the MCP9808

About a month ago I put in an order for some PCBs from OSHPark.  They were nothing special, just a break out board for the MCP9808 temperature sensor from Microchip, the order was more about testing out the service and getting some practice designing PCBs.  Well, they finally turned up a couple of days ago and I couldn't be happier.  So I though I'd document the design and final product.

The board is pretty basic.  It's only 0.75 x 0.50 inches with a couple of ESD protection diodes, current limiting resistors and a capacitor to filter the power supply.

MCP9808-Breakout_ProjectFiles.zip
MCP9808_Breakout_Schematic.pdf
MCP9808-Breakout_Gerbers.zip

Printed Circuit Board
PCB Top Layer
Printed Circuit Board
PCB Bottom Layer

I'm a bit of a cheapskate, so I designed the board in Kicad.  I'm familiar with Altium designer so Kicad took a bit of getting used to, but it wasn't too bad.

PCB Layout
Final PCB Design in Kicad

There's a built in viewer in Kicad, but I prefer to use gerbv to get an idea of the final result.

PCB Layout
gerbv Render of the Top Layer

PCB Layout
gerbv Render of the Bottom Layer

The MCP9808 temperature sensor comes in two different packages, an 8 lead DFN, and an 8 lead MSOP.  Ideally in my design I want to use the 8 lead DFN but I'm not sure if I'll be able to solder it successfully, so in the event that I can't, I've put footprints for both packages on the PCB in parallel.  That way if I can't solder the DFN, I can still test the design on the board with the MSOP package.

Printed Circuit Board
Duplicate Footprints

When designing the board, I was worried about a couple of things related to alignment.  One of them was the alignment of the drill holes with the copper layers.  I followed the tolerances specified and was happy with the result.  The picture below shows a slight misalignment of the of the hole, but it's within spec.

Printed Circuit Board
Drill Hole Alignment

The other thing I was worried about was the solder mask between the pins of the DFN package.  There's only 0.2 mm between the pads, and in case I want to reflow the board using solder paste, having some solder mask in there is going to come in handy to prevent bridges.

You can kind of see it the photo below, but there does seem to be a pretty good amount of solder mask between the pads. Further inspection with a jewellers loupe confirms this.

Printed Circuit Board
Solder mask between DFN pads

I've also had a little bit of practice placing the parts on the board.  Not easy, but doable.

Printed Circuit Board
Parts in position

Printed Circuit Board
Parts in position
So overall I'm pretty impressed, but there are a couple of problems with the board.  The silkscreen wasn't exactly as I'd expected, but that was my fault for not leaving enough clearance around it.  There's also the standard ever so slight misalignment problems between the copper, solder mask, drills, and silkscreen, but nothing outside of the norm and what you expect when you design a board.  In the future I need to pay attention to this a little more and alter my design accordingly.

The other major issue is delivery time.  Stupidly I went for the cheap postage forgetting that it would take about 5 weeks to get to Australia, next time I'm going to fork out the extra couple of buck for the faster option.  Why on Earth does it take so long to get through Australian customs?  It's excruciating.

Overall I'm thrilled with the price and quality of the boards, the service was great and I'm definitely going to be using OSH Park again.  I've designed boards by committee before and the results were pretty average, this is the first time I've had a go myself and I really enjoyed the experience.  I'm still amazed at the process though, send off some design files and a few weeks later the PCBs turn up in the mail.  Barely had to leave the house.  I think my logo on the board below sums up my feelings pretty well.

Printed Circuit Board
Neville logo

Tuesday, December 18, 2012

Driving Two LEDs With One Microcontroller Pin

So you've designed your latest widget and you want to add some status indicators, but you've only got one pin left on your micro-controller to drive a couple of LEDs, what do you do?  There are a couple of options you can try, each with their own pros and cons.  The first option is simple, and will suffice for most situations.

LED Circuit
2 centre driven LEDs

The centre driven arrangement is an easy way to drive two LEDs from one pin.  R1 and R2 are selected to limit the current through D1 and D2 respectively.  Vin is connected to a micro-controller pin which typically has three states, output high, output low, and high impedance input which effectively disconnects Vin.  What happens to the LEDs in each of these cases?

  • When Vin is driven high, current will flow through D2 but not D1, lighting D2.
  • When Vin is driven low, current will flow through D1 but not D2, lighting D1.
  • When Vin is disconnected, current will flow through R1, R2, D1, D2.  In this case the forward voltage of both LEDs, and the increased total resistance will cause the current through the LEDs to be reduced, this means that the light output from the LEDs will be reduced, giving a dim glow.

With this arrangement you can drive either LED, turning on one or the other, but what if you want to turn off both LEDs?  Well, you can't.  The best you can do is set the micro-controller pin as an input and have both LEDs dimly lit.  However, by adding one resistor to the circuit above it's possible to turn on either LED or turn them both off.

LED Circuit
2 centre driven LEDs with resistor bypass

The circuit basically operates as before, but in this case when Vin is disconnected the small current that flowed though D1 an D2 causing them to dimly light, is now shunted though R3.  Component selection is critical though.  The voltage across R3 has to be sufficiently low, otherwise current would start to flow through D1 and D2.  Working out the required resistor values isn't too complicated, but three parameters need to be defined for each LED.

  • Vhigh, the operating voltage of the LED when fully lit.
  • Vlow,  the highest voltage across the LED that won't cause any discernible illumination.
  • On current, the operating current of the LED when fully lit.

With these values the circuit can be analysed for each possible input condition.  The expression for each result has been rearranged to give a formula for a particular resistor.  The reason for this will become clear later on.

Nodal analysis of an LED circuit
Vin is driven low

When Vin is low, current will flow though D1, R1, R2, R3, but not D2 as it will be reversed biased.

Nodal analysis of an LED circuit
Vin is driven high

When Vin is high, current will flow though D2, R1, R2, R3, but not D1 as it will be reversed biased.

Nodal analysis of an LED circuit
Vin is disconnected

When Vin isn't connected we want the voltage across R3 to be equal to the combined low voltages of D1 and D2.  This will ensure that they don't turn on.  The circuit could be designed to use a smaller resistor, but doing so would increase the current in all three states.

The problem is now defined by three equations in three unknowns R1, R2, R3.  Algebraic substitution could be used to solve the system of simultaneous equations, but the problem is equally solvable numerically.  The first step is to calculate approximate values for R1 and R2,  This can be done by subtracting the Vhigh value for the LED from Vcc and dividing the result by the On current.  These values of R1 and R2 are then used to calculate R3.  An iterative process is used where you use the value of two resistors to calculate the value of the third.  After a couple of iterations a well formed problem will quickly converge to an answer.

To assist with these calculations I have put together a spreadsheet that allows you to enter the LED parameters and Vcc to obtain values for R1, R2, and R3.

2LEDsResistorCalculator.ods

To demonstrate the process I assembled a small circuit with parts I had on hand.  The parameters of the LEDs I had were as follows.

LED 1 - Red
  • Vhigh  -  1.89 V
  • Vlow  -  1.35 V
  • OnCurrent  -  1 mA
LED 2 - Orange
  • Vhigh  -  1.76 V
  • Vlow  -  1.40 V
  • OnCurrent  - 1.5 mA
Entering this information into the spreadsheet returned three resistor values.  I didn't have these exact values, but using a combination of other resistors I was able to obtain the values in the brackets.
  • R1  -  2462 ohm  (2350 ohm)
  • R2  -  1876 ohm  (1950 ohm)
  • R3  -  5302 ohm  (4700 ohm)
Entering the values of the actual resistors I used gave the following results.

circuit measurements
Simulation results

The circuit was then assembled and the operating values measured.

LED circuit on a breadboard
Input disconnected. Both LEDs off
LED circuit on a breadboard
Input high.  LED 2, Orange On
LED circuit on a breadboard
Input low.  LED 1, Red On

  • LED1 on current  -  1.07 mA
  • LED2 on current  -  1.46 mA
  • Supply current LED1 On  - 1.36 mA
  • Supply current LED2 On  - 1.70 mA
  • Supply current LEDs Off  - 0.56 mA
The circuit operated correctly and the measured values agreed well with the calculations in the spreadsheet. 

LED circuit on a breadboard
Bypass resistor removed.  Input disconnected.

To illustrate the importance of the bypass resistor, R3, I removed it from the circuit and disconnected the input.  Without the bypass resistor the current passes through the LEDs illuminating both at once.

This circuit is reasonably flexible.  You can turn off both LEDs, turn on either, and if you wanted both lit up at once, just toggle the input between low and high.  The only downside to this circuit is that there is always a current flowing.  Even when the LEDs are off there's a small current, which may make this unsuitable for any type of battery powered device.  Ideally you should plan ahead and make sure you have enough output pins to control any indicator LEDs, but if you're in a jam, or just don't want to move up to the next device with a higher pin count, this will do the job.

Friday, December 7, 2012

Different Earth Models and Quick Spreadsheet Calculations

In my last couple of posts I've been exploring the physics thought experiment called The Gravity Train.  For simplicity I'm only interested in a path that goes directly through the centre of the Earth.  Initially I used a simple constant density model of Earth to arrive at an analytic solution, although easy to solve, it's essentially wrong.  To see what the results would be using some more realistic models I've done some extra simulations.  Ideally I should have fired up Octave or another similar numerical package to model the scenarios, but for a quick ballpark answer I decided to just use a spreadsheet.  It's less accurate but it's quick and easy.

The 3 Earth models simulated are as follows.

1.  Constant Density
        Mass     5.9736 E+10^24 kg
        Radius   6371 km

2.  Linear Density
        The density of the planet varies linearly in a radial direction.
        Mass               5.9736 E+10^24 kg
        Radius             6371 km
        Inner Density      13000 kg/m^3

3.  Preliminary Reference Earth Model
        Uses the Preliminary Reference Earth Model (PREM) (Dziewonski & Anderson, 1981)
        to model the Earth as a sphere of radius 6371 km.


Graph of Density Inside the Earth
Earth's density at different depths

The graph above shows the density of the Earth as you travel from one side to the other, with the middle of the graph being the centre of the Earth.   The uniform density model is just a straight line representing a density of about 5500 kg/m^3.  The linear density model starts at about 3000 kg/m^3 at the surface and rises to around 13000 kg/m^3 at the Earth's core.  The Preliminary Reference Earth Model (PREM), the most realistic model I could find, shows distinct areas of different density that correspond to layers such as the core, inner core, and mantle.


The spreadsheet models the situation as a finite difference time domain problem, where an object is accelerated by the Earth with different accelerations at different depths.  I won't go though the equations used in the spreadsheet but they're based on the formula in my last post, Mass Of a Spherical Shell With a Linearly Varying Density.  The time taken for the object to reach the other side of the planet is noted and in theory the object should reach 0 velocity just at it reaches the other side.  However, due to the inherent errors of numerical modelling, the velocity wasn't zero, but it was within a reasonable margin of error.  As a point of comparison, using the data provided for the constant density model, the equation derived in my first post on this topic yields a travel time of 42 minutes and 10 seconds.

Graph of Gravity Train Travel Times
Displacement vs Time for Different Models

The times to travel from one side to the other side of Earth using different models are as follows.

Uniform   41 minutes 48.0 seconds
Linear    38 minutes 13.2 seconds
PREM      37 minutes 43.6 seconds

These results show a time decrease for the more realistic models which is what you'd expect.  With more of the mass concentrated at the centre of the Earth, it can accelerate an object for a longer period of time.  I'm fairly confident with those numbers.  The time difference between the uniform numerical and analytic models is only 22 seconds, which is a reasonable amount of error for the simulation method.

An extra piece of information that can be extracted from the data is the gravitational acceleration of the Earth vs depth.

Graph of Gravitational Acceleration Inside Earth
Gravitational Acceleration Inside Earth

Unlike the uniform density model I used in my first post, the acceleration inside Earth actually goes up before falling to zero in the other models.  For the realistic PREM model there is a point approximately halfway to the core of the Earth where gravitational acceleration peaks at around 10.6 m/s^2 that about 1.1g's.  Didn't know that.