Archive for January, 2011

Single pin relay control

Posted in electronics, i make things on January 16, 2011 by makingthingswork

EDIT: The below schematic and circuit description are slightly incorrect – the PNP and NPN transistors should be reversed.

I am cheap when it comes to micro-controller pins. I don’t like using them if i don’t have to. I also don’t like throwing current around unnecessarily.

I was working on a project which i wanted to control a bunch of relays with my micro-controller, but didn’t want to dedicate 2 pins or an h-bridge to each relay driver. My relays were fairly small, and i came up with the following way to control them with a single (tri-state) pin, and some external components.

If you have a strong-ish pin driver or a low-current relay coil, you don’t even need the totem pole arrangement.


Relays work by putting a certain amount of current through an inductor, setting up a magnetic field that pushes a switch. A normal relay requires that current to be always on. Once you stop feeding it, it goes back to ‘off’ (or on, whatever the default is).  A Latching relay doesn’t have the spring or whatever to force the switch back once current is released. It acts more like a light-switch.

So, for a latching relay you need to supply a certain amount of current to get it to switch, then another amount in reverse to get it to switch back.

Relays usually specify the current needed to switch them, and the internal resistance of the coils.

The idea:

Use a latching relay with a capacitor behind it, sized such that it can store enough current to switch the relay back:

control-pin -> relay coil -> capacitor -> GND

you send 5v or whatever though the coil and the capacitor will start to charge up. That current is enough to switch the relay, so it turns on. As you leave the pin high, the cap will charge up and the current will drop off to zero. Once you are ready to unswitch the relay, connect the pin to ground, and the cap will discharge across the coil and switch the relay back.

If you have a big relay, small pin driver, etc you can add a NPN and PNP transistor pair in a ‘totem pole’ configuration to switch a larger amount of current.

Bingo! one pin, no excess current loss, and 1 (or 3) extra components.

Here’s a picture:

The switch at left simulates the microcontroller pin. The resistor and inductor simulate the coil of the relay.

Want to try it? here’s the falstad simulator code (See this link for how)

$ 1 5.0E-6 31.41906602856942 54 5.0 50
c 304 208 304 256 0 9.999999999999999E-5 4.4916891133897625
g 304 256 304 288 0
w 240 112 304 112 0
t 208 96 240 96 0 1 0.0 0.5013938559564375 100.0
S 176 112 128 112 0 1 false 0
t 176 128 240 128 0 -1 5.0 0.5013938559564375 100.0
R 128 96 96 96 0 0 40.0 5.0 0.0 0.0 0.5
w 176 128 176 112 0
w 176 112 176 96 0
w 176 96 208 96 0
R 240 80 240 48 0 0 40.0 5.0 0.0 0.0 0.5
g 240 144 240 176 0
r 304 112 304 160 0 135.0
l 304 160 304 208 0 0.01 5.129834965258037E-5
g 112 128 112 144 0
w 112 128 128 128 0
x 61 75 155 79 0 18 control pin
x 251 77 360 81 0 18 “totem pole”
x 347 190 424 194 0 18 relay coil
o 12 64 0 291 2.5 0.025 0 -1

Depending on the control voltage, you may need to add more resistance inline with the coil.


low-fi circuit simulation

Posted in electronics, i make things on January 16, 2011 by makingthingswork

I often want to play with some circuit or other, and am not sure if it will work.

For those of us who aren’t super strong on electronics theory, this means either we buy a bunch of parts and test it out, possibly frying them, etc OR do some sort of simulator. Unfortunately most simulators are somewhat complex (spice, etc)

I was pointed to a really nice java simulator that is online for free:

He has lots of examples, and you can edit the circuits yourself, add parts etc. I have learned a lot there.

For example, here’s how to simulate my DC motor speed control circuit:
1) go to
2) right click in the black after java loads, and select file->import
3) paste in the following:

$ 1 5.0E-6 3.9121283998153213 53 5.0 50
R 112 224 64 224 0 2 400.0 2.5 2.5 0.0 0.5
g 320 256 320 272 0
d 208 128 208 192 1 0.3
t 176 112 208 112 0 1 -11.855891688647894 -0.016674669651078056 100.0
w 176 160 176 112 0
w 176 112 176 96 0
r 176 96 176 48 0 1200.0
R 176 48 128 48 0 0 40.0 12.0 0.0 0.0 0.5
w 176 48 208 48 0
w 208 48 208 96 0
w 208 128 272 128 0
f 272 128 320 128 0 1.5
w 320 144 320 256 0
w 208 48 320 48 0
f 176 224 208 224 0 1.5
l 320 112 320 48 0 4.0E-5 7.833859394512643E-8
w 320 48 384 48 0
w 320 112 384 112 0
d 384 112 384 48 1 0.805904783
r 384 48 432 48 0 2700.0
w 384 112 432 112 0
d 432 48 432 112 1 0.805904783
x 447 73 480 77 0 18 LED
x 266 167 355 171 0 18 Power FET
x 255 86 308 90 0 18 motor
I 112 224 160 224 0 0.5
w 160 224 176 224 0
x 7 260 111 264 0 18 PWM source
w 176 160 176 192 0
w 176 192 208 192 0
w 208 192 208 208 0
g 208 240 208 272 0
x 62 119 155 123 0 18 gate driver
x 345 136 470 140 0 18 flywheel diode

that’s it! pretty neat, huh?

You can easily make your own by doing file->import with a blank value, then right-click and add parts as you like. File->export will show the config you should save as a txt file if you want to save the circuit or come back later.

You can also copy the java class locally and run it without an internet connection. He updates it from time to time, so check back for new parts and features.

He also has some physics simulators which i haven’t tried, but are probably good, given the high-quality of his electronics site.

curve fitting and graphing.

Posted in Linux, solar on January 16, 2011 by makingthingswork

Here’s how i do the curve fits and resulting graphing.

I use the free program ‘gnuplot’ which is pretty powerful, can do curve-fits, and graph everything.


1: measure your curve and get some points. I created a file called ‘measured.dat’:

0  .0044
.1 .0123
.2 .0218
.3 .0382
.4 .0636
.5 .098
.6 .157
.7 .221
.8 .300
.9 .404
1 .495

2)  fit the curve. I use a cubic parabola: Ax3+Bx2+Cx+D

shell$ gnuplot
gnuplot> f(x)=a*x*x*x+b*x*x+c*x+d
gnuplot> fit f(x) './measured.dat' via a,b,c,d
Final set of parameters            Asymptotic Standard Error
=======================            ==========================a               = 0.142852         +/- 0.07749      (54.25%)
b               = 0.385536         +/- 0.1181       (30.63%)
c               = -0.035066        +/- 0.0493       (140.6%)
d               = 0.00833846       +/- 0.005414     (64.93%)
So now you have your coefficients: a,b,c,d

3) plug them into a graphing file. here’s mine:

# let's call this file "fit.gnuplot"
# you can keep the body of the file the same, and just
# modify a,b,c,d each time you run a new curve

#Cubic Parabola
#C=ax3 + bx2 + cx + d
# this trick limits the curve to x=0 through x=1.05
C(x)= (x>0 && x<1.05) ? B(x) : 1/0

# tangent slope, derivative of cantilever
# D = B'=3ax2 + 2bx + c

#focus (reflected) line slope (ts = tangent slope)
# y = 1/(TAN(2*(ATAN(1/ts))))
# incidence angle (0 = directly above,
#                  + means low in the sky
#                  - means high in the sky

# radians = degrees x PI / 180
# precalculate one, just showing the formula, not used
ideg= 0;
irad = ideg*pi/180

## slope of reflected ray
# focus y-int

## where will the graph be drawn?
set xrange [-.5:1.2]
set yrange [-.1:1.2]

## setup graphing
set multiplot
# offset (first ray will be ioff+istep)
# each ray will offset on x axis by this much
# arrow number
# tangent point
irad = ideg*pi/180

## how far to the left of the x-axis to extend arrows?
xp = -.35

## increment various counters
ar=ar+1; tp=tp+xstep

## downward ray
set arrow ar from tp,5 to tp,C(tp) lt rgb 'black'

## normal reflected ray
ideg=ideg+istep; irad = ideg*pi/180; ar=ar+1;
set arrow ar from tp,C(tp) to xp,F(D(tp))*xp+G(tp) lt rgb 'red'

## plus 1 step
ideg=ideg+istep; irad = ideg*pi/180; ar=ar+1;
set arrow ar from tp,C(tp) to xp,F(D(tp))*xp+G(tp) lt rgb 'black'

## plus another step
ideg=ideg+istep; irad = ideg*pi/180; ar=ar+1;
set arrow ar from tp,C(tp) to xp,F(D(tp))*xp+G(tp) lt rgb 'green'

## repeat this stanza as many times as you need
## next set
ideg=ioff; irad = ideg*pi/180
ar=ar+1; tp=tp+xstep
set arrow ar from tp,5 to tp,C(tp)  lt rgb 'black'   
ideg=ideg+istep; irad = ideg*pi/180; ar=ar+1;
set arrow ar from tp,C(tp) to xp,F(D(tp))*xp+G(tp) lt rgb 'red'
ideg=ideg+istep; irad = ideg*pi/180; ar=ar+1;
set arrow ar from tp,C(tp) to xp,F(D(tp))*xp+G(tp) lt rgb 'black'
ideg=ideg+istep; irad = ideg*pi/180; ar=ar+1;
set arrow ar from tp,C(tp) to xp,F(D(tp))*xp+G(tp) lt rgb 'green'

set label "  red: 1.5 degrees high\nblack: perfectly aimed\ngreen: 1.5 degrees low" at -.4,.2

set key at 1,.05
plot C(x) lt 2 title 'best fit line for measured sheet'
set key at 1,0
plot 'measured.dat' with points title 'measured points'

unset multiplot
pause -1

Now you have a graph definition that will do all the fancy arrows and such.

4) Show the graph:

shell$ gnuplot fit.gnuplot

Here’s what it makes:

You’ll need to duplicate the ‘repeat this stanza’ section once more for each point without a reflection on it.

Concetrating parabolic trough – part 1

Posted in i make things, solar on January 16, 2011 by makingthingswork

I’ve been looking into solar for water heating and (less so) power generation. I’ve decided that i like concentrating collectors since there’s less to insulate.

I did some tests and decided that while a parabolic collector is great, you can make a ‘good enough’ collector with far less precise construction techniques. I like that. I’m terrible at making things accurately, especially when they can be subjected to nature, and made from wood or other materials (not necessarily straight).

I’m a big fan of ‘coroplast’, aka corrugated plastic sheet. It costs about $15 for a 4×8 sheet of the stuff and it has some great qualities. Most of it is polypropylene, and can be heat welded if that’s what you’re after.

The concept is like this:

Take a piece of wood and attach some plastic sheet to it on one side.

Build a riser about 3′ out and put a pipe or other support on it, which will force the plastic sheet up into a bent shape.

Build an insulated collection mechanism on the top.

This has several key benefits:

  • Small surface to collect. Small surface means less materials and less surface to lose heat from
  • Insulation above collector. Convection is the dominant heat loss mechanism in these collectors, and by keeping all the hot air below insulation, you will lose a lot less
  • ease of construction. Not much to it at all

The main drawback is that you will have to aim it periodically. Thats another post.

After reading some engineering pages, i learned that a cantilever subjected to a strain at the end of it (like proposed above) will form a parabola-like shape. This is apparently well known in amateur radio circles as a ‘stressed parabola’. To get a good focus, you need a long focal length. For solar heat, i think you can get by with a looser focus.

I figured out the math to calculate the focus points for this kind of curve, and within a 10degree arc of aiming (eg if 0 deg is aimed directly at the sun, a +/- 5 deg range) it focuses all the light at a 7-8″ wide spot about 18″ off the origin point (eg x=0,y=0)

So i did a ton of calculations, sought some feedback, and was told it wouldn’t work. So i built a little one. I took a 2×4 piece of coroplast and leaned it on a char, then measured the height every 10 cm. I then did a curve-fit and plotted the focus. It was not anywhere near the same equation as was predicted by the engineering data i had, but the curve fit was spot on just the same, and it gave a 6″ focus! That’s great.

I then built a rig which would be a little more like what i would actually use:

The plastic is attached at 3 points: base, about 1/3 and at the top. It was minimally forced into place, but the middle support was pushed down a little, and i attached them in this order: bottom, middle, top. I should have fixed the top and bottom then attached the middle as ‘insurance’. As you will see later, forcing the middle throws off the curve-fit.

Basic theory:

Every point on a curve has a tangent line, which is the line that just ‘kisses’ the curve at that spot. When a ray of light hits the curve, it will reflect as if it had ‘bounced’ off the tangent line.

The Tangent can be calculated as the derivative of the curve, which is basic Calculus, but isn’t really very difficult to do.

So, to calculate the reflection slope, you do this:


ts is the tangent slope. irad is the offset of the inbound ray from vertical in radians.

You can then calculate the y-intercept of the line, and plot it.

Here are some graphs:

The colored lines should show ‘inbound’ (eg top to curve), but i didn’t plot them backwards. Too lazy.

This graph shows where the focus of the reflections would be. As you can see, it’s a pretty tight focus, however the curve does not match as well for this setup, so will need experimental verification.

I should note that the plastic will be coated with some sort of reflective coating (reflective mylar, etc)

PWM DC Motor control circuit

Posted in electronics, i make things on January 16, 2011 by makingthingswork

I’ve decided to start posting some of my makings again. Not sure what it is about January, but here we are.

I picked up a few 12v DC motors with the idea of making a variable speed controller for them. DC motors lend themselves to it, by doing PWM and/or lowering the voltage. I may eventually make a variable voltage driver, but in my experiments, PWM works better, since the higher voltage puts more current and therefore more torque while it’s on.

Here’s the circuit:

This circuit switches ground on/off. The motor/pump should be connected to the +voltage , and the motor ground to the MGND line. Should work with just about any input voltage. I’ve used it at +5 and +12v. Different voltage will need different values for R3, if you have an LED.

The output is out-of-phase with the PWM input. The PWM fet should be a fast switching type, and the other FET should be a power FET. I used IRFZ34N for the power fet, and VN1304 for the switching FET (ignore the part # in the schematic, that was just to get the correct pinout)

Quick description:

R1, Q3, D1, Q2 form a gate driver circuit for the Power FET. When the PWM pulses high, the power fet is turned off. This means the motor is out of phase (opposite) with the PWM input

R1 prevents too much power from going from +12v to GND when the gate is pulled low

D2 is a flywheel diode, to contain any back EMF generated by the motor.

R3, LED1 are just an indicator, and is optional. R3 just sets the current on the LED.

C1 is optional and may smooth the PWM signal out, if desired.

C2 will smooth out the load on the power source. Recommended to keep it.

I have tested this on 12v motors, and it works great! I do recommend a heatsink on the power fet.

I used an arduino to drive a PWM pulse, and used a potentiometer to vary the speed.