Monday, February 17, 2014
Stupid Simple Tip #1
A rubber band and a pair of pliers make an quick and easy clamp for holding components while soldering.
Tuesday, January 21, 2014
Interface to Wireless Driveway Sensor
I have a driveway that is over 300 feet long and it is nice
to have some advance notice that someone is driving or walking in. Previously, I have used a very expensive IR
beam-break detector. It gave a lot of
false alarms and eventually failed due to a lightning strike. It also required that I run a very long cable
that could survive outdoors, which added more expense. It’s time to switch to a wireless sensor.
I found this inexpensive one at Harbor Freight ($17 with
coupon.) It is a simple, stand-alone
alarm with a receiver that just flashes some LEDs and emits a tone. It claims to work up to 400 feet and I verified that it works to at least 350 feet, which is good enough for my application. I created the circuit described below to
interface this to my Raspberry Pi based home alarm system.
The receiver can use 3 C-batteries or a 6V adapter (not
included) and I found that it would work fine on 5V. The simplest way to get a wired signal out of
the receiver is to connect to one of the LEDs.
This picture shows how I soldered a wire to the positive side of the
LED.
This will provide a very brief pulse of 5V, but I need to
simulate a normally open switch that connects to ground when triggered. Also, that signal needs to lasts for at least
a second to guarantee that the Raspberry Pi will see it when polling the GPIO
states.
I use a 555 timer IC in monostable configuration to provide
the longer pulse. The 555 is triggered
by a low pulse, so I also need an inverter.
I chose a 7404 IC because I have a stock of these reclaimed from salvage
many years ago. The output of the 555 is
a high pulse that lasts 2.2 seconds with the capacitor and resistor values in
this circuit. This is used to control an
NPN transistor that will provide a connection to ground as the signal output.
This circuit uses three wires to connect to the wireless receiver to provide power and read the LED state. Put it
all into a project box with some screw terminals and it is ready to
connect. I will find out over the next
several days how reliable this motion detector is. I am sure it will trigger when any deer come
by it. I am hoping that it doesn't produce
a lot of false alarms. Otherwise I
will probably end up disconnecting it.
Here is the interface circuit connected to the receiver. The white wire on the receiver is the antenna.
And here it is all put together.
Wednesday, January 15, 2014
Fail-Safe Circuit Using Discrete Logic Chips
When controlling a device that can present a danger to
people or property, it is important to include adequate safeguards, in both
software and hardware. I now have a
Raspberry Pi controlling the heater and circulation pump on my hot tub because
the existing controller failed. If the heater
is left turned on indefinitely or if it is turned on without the circulation
pump running, then bad things can happen.
It could produce scalding hot water or even a possible steam
explosion. Therefore, I have built in
multiple safeguards in both software and in hardware.
The system has two temperature probes: one in the water and one directly on the
outlet pipe from the heater. If the
software detects the heater temperature above a certain point it will enter a
failure mode and turn the heater off.
The software also assures that the circulation pump runs whenever the
heater is on. The hardware interface
uses discrete logic chips to add an additional layer of protection. An “and gate” is used to prevent the heater
relay from being enabled if the pump is not also enabled.
Another more complex circuit solves another problem. The software or the computer hardware could
fail, leaving the heater turned on indefinitely. A “clock failure detection” circuit is used
to handle this issue. For the heater
relay to remain turned on, a GPIO pin must be pulsed regularly. If this “heartbeat” is not detected, then the
failsafe circuit shown below will turn the relay off.
![]() |
| 555 in astable mode which produces a pulse every 2.079 seconds. |
Here are the components used:
- A clock source, provided by a 555 chip configured in astable mode, produces a pulse approximately once every two seconds. Many sources on-line describe how to use this very common chip. My configuration is shown here.
- An AND Gate, which does exactly what its name implies.
- An inverter, which changes a hi signal to low and a low signal to high.
- Four D-flops, which can be thought of as a single bit memory device. A D-flop will store the value on its data input line when the clock line is pulsed. It can also be set to one or cleared to zero using the PRESET and CLEAR inputs. These are active low, which means they should normally be kept high and briefly set to low to activate the preset or clear function. The Q pin is the output. An inverse of this is also provided but is not used in this circuit.
Here is the complete block diagram of the fail-safe circuit.
![]() |
| Fail-safe circuit |
I used TTL (5V) chips because I have a stock of these from
long ago.
- 555 – Monostable/Astable timer
- 7404 – Inverter (six on one chip)
- 7408 – two input AND Gate (four on one chip)
- 7474 – D-Flop (two on one chip)
Today it may be more appropriate to use CMOS (3V) chips and
equivalents to the TTL chips I used can easily be found.
Tuesday, January 14, 2014
Solid State Relays
I find solid state relays (SSR) very convenient, especially for controlling AC power lines. They typically include opto-isolation and zero crossing detection (explained later.) Also, they can be controlled with as little as 3V. This means that you can connect them directly to a GPIO pin. However, I still prefer to put a transistor in between to prevent drawing too much current from the GPIO.All these features do come at a price. The typical SSR that I use costs about $10. PC board mounted SSRs that handle small current can be as little as $1 each. The 40amp SSRs that I used to control the heater for my hot tub cost about $25. (I will describe that project in a later post.)
There is an important point you need to know about when using SSRs - the AC and DC versions are not interchangeable. When switching AC current, the relay should only turn on or off when the voltage is at zero during the AC cycle. This is what "zero crossing detection" does and it prevents a large surge from entering the device you are controlling. I have used a DC SSR to control a lamp and blown the bulb due to the lack of zero crossing detection. Also, if you use an AC SSR to control a DC load, it will turn on just fine, but it will never turn off since the SSR never detects the load voltage crossing zero. This has confused many a hobbyist and now you won't be one of them.
Tuesday, July 23, 2013
Running From an External Hard Drive
The SD card of
the Raspberry Pi is required for booting.
While you may not be able to boot from an external hard drive, moving
the root partition there will significantly speed up the Raspberry Pi. There are many uses where this configuration particularly
useful. File servers and media servers
are good examples. I use a system like this
for a MySQL database server.
There are a number of variations to the process, but basically, the steps are:
o Install OS on SD Card and boot
o Plug in external hard drive
o Partition and Format hard drive
o Copy system to hard drive
o Mount the new system and modify configuration to use the hard drive
I decided not to re-invent the wheel. The following instructions are just my variations on the instructions provided by Rattus here:
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=10914&p=129474&hilit=resizefs#p122476
Load SD Card with Raspbian in the usual way. Update the system software and firmware with the following three commands. This can take quite a while to complete.
apt-get update
apt-get upgrade
rpi-update
reboot
List the partitions This should show only the /boot and / partitions on the SD card.
fdisk –l
Plug external hard drive into USB and list the partitions again. This should show another HD probably at /dev/sda
Modify the partition table for the external drive.
fdisk /dev/sda
Delete partitions by entering “d” and then the number of the partition to delete. Delete all the partitions. Entering “p” will list the partitions.
At this point, you have a few options.
a) Create a single partition and move the root there. This will leave the swap file on the SD card.
b) Create a root partition that fills most of the drive and a small swap partition.
c) Create a partition for the OS, a small swap partition, and a large partition for application data.
There are a number of variations to the process, but basically, the steps are:
o Install OS on SD Card and boot
o Plug in external hard drive
o Partition and Format hard drive
o Copy system to hard drive
o Mount the new system and modify configuration to use the hard drive
I decided not to re-invent the wheel. The following instructions are just my variations on the instructions provided by Rattus here:
Note about the sudo command: All of the following must be done as root. You must put sudo before each of the commands. Or, you can follow the (not recommended, bad practice) habit I have and just enter sudo bash to run a shell as root and skip saying sudo all the time.
Load SD Card with Raspbian in the usual way. Update the system software and firmware with the following three commands. This can take quite a while to complete.
apt-get update
apt-get upgrade
rpi-update
reboot
List the partitions This should show only the /boot and / partitions on the SD card.
fdisk –l
Plug external hard drive into USB and list the partitions again. This should show another HD probably at /dev/sda
Modify the partition table for the external drive.
fdisk /dev/sda
Delete partitions by entering “d” and then the number of the partition to delete. Delete all the partitions. Entering “p” will list the partitions.
At this point, you have a few options.
a) Create a single partition and move the root there. This will leave the swap file on the SD card.
b) Create a root partition that fills most of the drive and a small swap partition.
c) Create a partition for the OS, a small swap partition, and a large partition for application data.
I
am using the third option. The following
instructions are fairly easy to change to use one of the other options. While still in the fdisk program, do these
steps.
- Create the root partition: I made mine 16GB. Enter “n” to create a partition, “p” to select primary partition and then “1” to select partition number one. Select the default start sector. Enter +16G to specify the size.
- Create the swap partition: Enter “n” to create a partition, “p” to
select primary partition and then “2” to select partition number two. Select the default start sector. Enter +2G to specify the size. Enter “t” to set the type of a partition. Enter “2” and then “82” to make this one a
swap partition.
- Create the data partition: Enter “n” to create a partition, “p” to select primary partition and then “3” to select partition number three. Select the default start sector and default end sector to fill up the rest of the disk.
- Enter “p” to list the partitions and verify
things are like you expect.
- Enter “w” to write the new partition table and exit.
Create (format) the data partition with an EXTv4 file system. This command takes a few minutes to finish. There is no need to format the first partition since we are going to do a raw copy of an existing file system over it anyway.
mkfs.ext4 /dev/sda3
Initialize the swap partition
mkswap /dev/sda2
Mount the new root partition.
mount /dev/sda1 /mnt
Use rsync to clone the root partition from the sdcard to the new partition on your hard disk, Note the exclude of /mnt. This takes a while.
rsync -avz --exclude '/mnt' / /mnt
Change the mount configuration file on the new root.
vi /mnt/etc/fstab
Change the root device /dev/mmcblk0p2 to be /dev/sda1
Add /dev/sda2 as a swap partition by adding this line.
/dev/sda2 none swap sw 0 0
Finally, stop the system from using the swap file that it normally uses.
rm /mnt/etc/rc?.d/*dphys-swapfile
And delete the old swap file.
rm /mnt/var/swap
Use your editor of choice (I use vi) to modify the boot configuration to use the new root partition. First make a backup copy.
cp /boot/cmdline.txt /boot/cmdline.orig
vi /boot/cmdline.txt Change /dev/mmcblk0p2 to be /dev/sda1
Now the system is ready to reboot. It should come up normally with the external hard drive mounted as root.
reboot
If the system fails, use another machine to copy /boot/cmdline.orig to /boot/cmdline.txt and then reboot again.
One problem that some people have run into is controllers that take too long to complete the USB discovery. This can be compensated for by adding delays in the /boot/cmdline.txt file with the bootdelay and rootdelay options.
- Create the root partition: I made mine 16GB. Enter “n” to create a partition, “p” to select primary partition and then “1” to select partition number one. Select the default start sector. Enter +16G to specify the size.
- Create the data partition: Enter “n” to create a partition, “p” to select primary partition and then “3” to select partition number three. Select the default start sector and default end sector to fill up the rest of the disk.
- Enter “w” to write the new partition table and exit.
Create (format) the data partition with an EXTv4 file system. This command takes a few minutes to finish. There is no need to format the first partition since we are going to do a raw copy of an existing file system over it anyway.
mkfs.ext4 /dev/sda3
Initialize the swap partition
mkswap /dev/sda2
Mount the new root partition.
mount /dev/sda1 /mnt
Use rsync to clone the root partition from the sdcard to the new partition on your hard disk, Note the exclude of /mnt. This takes a while.
rsync -avz --exclude '/mnt' / /mnt
Change the mount configuration file on the new root.
vi /mnt/etc/fstab
Change the root device /dev/mmcblk0p2 to be /dev/sda1
Add /dev/sda2 as a swap partition by adding this line.
/dev/sda2 none swap sw 0 0
Finally, stop the system from using the swap file that it normally uses.
rm /mnt/etc/rc?.d/*dphys-swapfile
And delete the old swap file.
rm /mnt/var/swap
Use your editor of choice (I use vi) to modify the boot configuration to use the new root partition. First make a backup copy.
cp /boot/cmdline.txt /boot/cmdline.orig
vi /boot/cmdline.txt Change /dev/mmcblk0p2 to be /dev/sda1
Now the system is ready to reboot. It should come up normally with the external hard drive mounted as root.
reboot
If the system fails, use another machine to copy /boot/cmdline.orig to /boot/cmdline.txt and then reboot again.
Saturday, July 13, 2013
Pulse Width Motor Control
My last project had me digging through old robotics parts to find an H-Bridge Motor Controller. I found one that had several TTL chips on the circuit as well and it reminded me of a trick some of you may find useful.
First, lets define pulse width modulation (PWM.) It refers to the duty cycle (how long the line is high vs how long it is low) of a pulse stream.

A duty cycle of 50% looks like this:

A duty cycle of 25% looks like this:

And 75% looks like this:
What use is this for controlling motors? If you have ever tried controlling a motor by simply turning it on and off, you quickly realize how little control you have. Your robot goes from stopped to zooming at full speed with nothing in between. If, however, you use PWM to power your motor, then it will be on at full power very briefly and then off, repeatedly. With PWM you can provide much finer motor control and have your robot move at whatever speed you like. The higher the duty cycle of the pulse stream, the more power your motor will output.
There are several ways to produce PWM signals from the Raspberry Pi. The simplest is to turn a GPIO pin on and off in a loop with delays controlling the pulse width. This is very CPU intensive and not always consistent. You can use libraries or device drivers to produce PWM from the GPIO that will be consistent and will use virtually no CPU. Or, you can use external hardware to produce PWM.
Here is how I have done it in the past. (I haven't built a robot using a Pi yet, but did it with simpler microcontrollers years ago.) Use six GPIO pins:
The four power bits control the duty cycle of a pulse stream and allow 16 different power levels. Below is a block diagram of the circuit I used to do this.
I used a 555 chip for the pulse generator, a 74161 counter, and a 7485 comparator. Other chips can be used to achieve similar results. I built my own H bridge circuit back then, but I recommend checking out some of the motor controller chips available now, such as the SN754410 chip.
First, lets define pulse width modulation (PWM.) It refers to the duty cycle (how long the line is high vs how long it is low) of a pulse stream.
A duty cycle of 50% looks like this:
A duty cycle of 25% looks like this:
And 75% looks like this:
What use is this for controlling motors? If you have ever tried controlling a motor by simply turning it on and off, you quickly realize how little control you have. Your robot goes from stopped to zooming at full speed with nothing in between. If, however, you use PWM to power your motor, then it will be on at full power very briefly and then off, repeatedly. With PWM you can provide much finer motor control and have your robot move at whatever speed you like. The higher the duty cycle of the pulse stream, the more power your motor will output.
There are several ways to produce PWM signals from the Raspberry Pi. The simplest is to turn a GPIO pin on and off in a loop with delays controlling the pulse width. This is very CPU intensive and not always consistent. You can use libraries or device drivers to produce PWM from the GPIO that will be consistent and will use virtually no CPU. Or, you can use external hardware to produce PWM.
Here is how I have done it in the past. (I haven't built a robot using a Pi yet, but did it with simpler microcontrollers years ago.) Use six GPIO pins:
- Enable (motor is on or off)
- Direction (forward or reverse)
- Power 0
- Power 1
- Power 2
- Power 3
The four power bits control the duty cycle of a pulse stream and allow 16 different power levels. Below is a block diagram of the circuit I used to do this.

I used a 555 chip for the pulse generator, a 74161 counter, and a 7485 comparator. Other chips can be used to achieve similar results. I built my own H bridge circuit back then, but I recommend checking out some of the motor controller chips available now, such as the SN754410 chip.
Gotta Know When to Fold 'em

For the past few months, I have burned up a lot of hobby time trying to build an interface to control the Orbit 58874 sprinkler control valve. These simply connect in-line with standard water hose connectors and cost about $20. However, they turned out to be quite complicated to control.
The valve connects with two wires. Sending a 24V pulse in one polarity will cause the valve to latch open. Sending it in the reserse polarity will cause it to latch closed. At least in theory. And, as a wise professor once told me: "In theory, practice and theory are the same. But in practice, they sure as hell aren't."
I had a 24V power supply, but it was a little too powerful. If you leave the valve powered for more than a second, you can burn out the solenoid coil. After melting one of my H-Bridge motor controllers, burning out several valves, and losing a couple of months, this project was officially no longer fun.
Fortunately, someone else has already gotten this working. If you are interested in this, check out http://rayshobby.net/ where you can find interfaces to these valves for Raspberry Pi and Arduino. I may just break down and buy his OpenSprinklerPi interface.But now it's time to throw in the towel and move on to other projects.
UPDATE: I was mistaken about the OpenSprinklerPi. It will only support standard valves. That's easy - apply voltage and they turn on. Stop and they turn off.
So I have returned to this and will be building an entirely new interface board for the sprinklers. It will probably include an H-Bridge to control output polarity and relays for each sprinkler.
Subscribe to:
Posts (Atom)






