This is only a preview of the May 2018 issue of Silicon Chip. You can view 35 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "800W (+) Uninterruptible Power Supply (UPS)":
Items relevant to "Multi-use Frequency Switch":
Items relevant to "LTspice Simulation: Analysing/Optimising Audio Circuits":
Articles in this series:
Items relevant to "USB Port Protector – just in case!":
Items relevant to "12V Battery Balancer":
Items relevant to "El Cheapo Modules 16: 35-4400MHz frequency generator":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
Turn things on or off if they’re too fast ... or too slow... etc!
Deluxe
Frequency Switch
by John Clarke
Switch devices on or off according
to the frequency of just about
any sensor signal up to 10kHz.
So you can switch something on
or off if a sensor signal frequency
goes above or drops below a
figure which you can easily set.
Features
• Energises a relay when a signal goes above a
preset frequency and keeps it on until the signal drops below a second preset frequency
• Adjustable hysteresis can be used instead of
setting upper and lower frequencies
• Switching frequency can be from 1Hz up to
10kHz.
• Adjustable switching delay
• Two sets of 5A changeover relay contacts
• Easy pushbutton set-up
• Can be set up on the bench or in situ
• Threshold can be set using a signal
generator or frequency meter (eg, DMM)
• On-board signal frequency range indicators
• Power, threshold and relay-on LED indicators
T
feathering blades on suitably and much more.
here are many applications for
equipped turbines.
It is also much easier to set up
a device of this type. Just some
Of course, there are countless oth- than our previous Frequency Switch
of the things we thought of “off
er uses – you’re probably thinking of in June 2007 (siliconchip.com.au/
the top of our heads” include:
• Cutting power (or fuel) to a motor others that suit your particular appli- Article/2261) and the main reason for
cation.
that is that it is based on a PIC16F88
if it exceeds a certain speed
As long as it has, or can be fitted rather than the LM2917 frequency to
• Switching a fan on at low vehicle
speeds to provide improved cool- with a sensor, to provide a frequency voltage converter. (That first Frequenwhich varies with speed, temperature, cy Switch was quite tricky to set up!)
ing.
• Giving a warning to change gears flow etc, you can use our new Deluxe
when the engine RPM is approach- Frequency Switch. It can do all of this Setting the two frequencies
You need to set up two
ing the tacho red line.
frequencies, not one as you
• Switching from long
might have thought.
to short intake run- Specifications
Why do you need two
ners at a particular en- Supply voltage: 10-16V
frequencies? We need to set
gine RPM to optimise Supply current: 20mA with relay off; 60mA with relay on
two frequencies because if
power delivery.
Signal frequency range: 1Hz to 10kHz
the signal from your cho• Switching off a pump
sen sensor varies by even
if a flow meter records Signal amplitude: >1.4V peak-to-peak
a small amount at close to
the water flow is out- Threshold setting resolution: 20Hz at 10kHz; 1Hz at 2.27kHz; 0.2Hz at
the switching threshold,
side a specific range. 1kHz; 0.002Hz at 100Hz.
the relay would be con• Switching on an alarm Hysteresis: 0-50%
stantly chattering on and
if wind speed exceeds Switching delay: signal period plus 0-500ms
off – not good at all. So
a certain threshold.
Signal frequency bands: <10Hz, 10-100Hz, 100Hz-1kHz, 1-10kHz
we set an upper frequency
• Applying a brake or
36
Silicon Chip
Celebrating 30 Years
siliconchip.com.au
threshold above which the sensor signal must rise before the relay switches
on. And then we set a lower frequency threshold below which the sensor
signal must drop before the relay is
switched off.
You can set the two frequencies
close together or far apart.
Setting the frequencies is dead-easy
and there are several methods for doing it. The first method is to feed in
your wanted set frequency, say 500Hz,
from an oscillator or other source to
the sensor input and then press switch
S2. Then feed in the wanted lower frequency, say 400Hz, and then press S1.
The second method is arguably even
easier. You just set one frequency, say
500Hz, and then use an on-board trimpot (VR1) to set the hysteresis. This
will effectively set the lower frequency
(down to a minimum of 250Hz in this
example) and you can tweak it at the
time of installation.
If you don’t have an oscillator you
could use the real signal that you intend controlling the unit with, so long
as you can hold steady it at the required frequency/frequencies for long
enough to press the switch(es).
Alternatively, if that’s too difficult,
you actually can get the microcontroller to generate the wanted frequencies.
This second method is more involved
than the first and we will describe the
procedure later in this article.
Detection time and delay
You can also configure the unit with
a switching delay which is adjusted
with trimpot VR2 and can be set between zero and 500ms (ie, half a second). This ensures that if the signal
frequency only momentarily
crosses one of the thresholds,
it will not cause the relay to switch.
The input signal
frequency must remain at or beyond
the threshold for the
entire delay time
before any relay
switching will occur.
Each time the frequency crosses the
threshold, the delay time starts again.
Fig.1: This block diagram describes how the microcontroller measures frequency.
If you prefer switching to happen
immediately then set the response
time to zero (ie, VR2 fully anticlockwise).
LED indicators
To help in the set-up and installation procedures, we have included
indicator LEDs to show when an input signal is present and its frequency range:
• LED2 lights for frequencies between
0.5Hz and 10Hz;
• LED3 lights between 10Hz and
100Hz;
• LED4 between 100Hz and 1kHz;
• LED5 for frequencies between 1kHz
and 10kHz and all four LEDs light if
the frequency is above 10kHz.
Other LEDs show when the set
threshold frequency is reached and
whether the relay is on or off.
Relay options
The relay is a double-pole changeover (double throw) type (ie, DPDT)
which can switch one or two loads,
each up to 5A/48V (8A if you use the
specified relay from Altronics).
You have the option to get the relay
to switch on if the sensor signal rises
above the threshold frequency (set by
Here’s the complete frequency
switch, ready to mount inside its case (it
suits a UB3 jiffy box but could also be mounted inside
the equipment it is controlling if there is room).
siliconchip.com.au
Celebrating 30 Years
S2) and switch off if the sensor signal
drops below the threshold set by S1.
The alternative is to get the relay to
switch on if the sensor drops below
the lower threshold frequency (set by
switch S1) and switch off if the sensor signal rises above the threshold
frequency (set by switch S2). The second mode is activated by installing a
link at JP1 on the PCB.
Block diagram
Fig.1 (above) shows how the Deluxe
Frequency Switch monitors the signal
frequency. The PIC16F88 micro’s internal clock is derived from a 20MHz
crystal which is driven by an internal
oscillator amplifier.
The resulting 20MHz clock signal
is divided by four to produce a 5MHz
signal which drives an internal 16-bit
timer, TIMER1. This comprises two
8-bit cascaded timers, TIMER1H and
TIMER1L.
We have implemented an 8-bit overflow counter (OVER) in the unit’s firmware. That extends TIMER1 out to 24
bits, so it rolls over every 3.355 seconds [or 224÷5,000,000]. This equates
to an input frequency close to 0.3Hz.
Hence, the unit is designed to handle
signals from 1Hz and up.
The input signal is fed to pin 6,
which is also the Capture/Compare/
PWM (CCP) pin. The Capture module hardware in the micro is
configured so that
on each positive
signal transition
at this pin (ie,
low-to-high),
the values of
TIMER1H and
TIMER1L are
copied into the
CAPTURE1H
and CAPTURE1L registers and
an interrupt flag
is set.
This then trigMay 2018 37
Fig.2: the circuit is based around a PIC16F88-I/P, which measures the incoming frequency and energises the relay if
the frequency is above or below certain values and whether JP1 is present or not. It also has pre-settable response
times and hysteresis to prevent “chattering”. LEDs give you visual indication of the operation as well.
gers an interrupt handler routine
which copies the contents of the OVER
register into the CAPTURE OVER variable.
The timers and overflow counters are
then reset to zero, ready to count until the next input positive going edge.
The captured count represents the
number of pulses from the 5MHz clock
signal over the period between the two
positive input signal edges.
So for example, a 1Hz input signal
will have a one-second period between
each positive edge. The count value
stored will therefore be 5,000,000
(5M). At 1kHz, the period between
positive edges is 1ms and the captured
value will be 5000.
To calculate the frequency, all we
need to do is to perform the calculation F(Hz) = 5,000,000 ÷ value. Or we
can calculate the period as P(s) = value ÷ 5,000,000.
But in reality, the micro just has to
convert the upper and lower threshold settings to these same count units
and then compare the counter values
38
Silicon Chip
to those stored values, to determine
whether either threshold has been
crossed.
On-board frequency
generation
Where the microcontroller produces
an output frequency for you to measure during adjustment as per setup
method on page 41), pin 6 (CCP1) is
configured differently. Rather than being in Capture Mode, with pin 6 as an
input, it is used in Compare Mode and
pin 6 is an output.
TIMER1 is still driven with the
same 5MHz signal but the TIMER1L,
TIMER1H and OVER registers are preloaded with values calculated from
the frequency to be produced. Each
time OVER register overflows, the pin
6 output toggles and new values are
loaded into the TIMER1L, TIMER1H
and OVER registers.
Because pin 6 toggles each time
the counters overflow, the output frequency would be half what you might
expect based on the period value for
Celebrating 30 Years
the required frequency. So we need to
divide the period by two to give two
separate half periods.
This means there will be an error
whenever an odd period value is used,
since dividing it by two will yield a
remainder of one.
To solve this, and avoid the inaccuracy, two different pre-load values are
used. They are used alternately to load
into the TIMER1L and TIMER1H registers. So the duty cycle will not quite
be 50% but the frequency produced
TP1 voltage
Hysteresis
(adjusted
with VR1)
when setting
upper threshold
5V
3.75V
2.5V
1.25V
625mV
312.5mV
50%
43%
33%
20%
12%
6%
Table 1: Hysteresis setting versus
voltage at TP1.
siliconchip.com.au
will be accurate.
The values from each of the separate
period values are loaded into the TIMER1L, TIMER1H and OVER counters
alternately. At the same time, the output at pin 6 is changed in level.
For those interested, the values to
pre-load into TIMER1L, TIMER1H and
the OVER variable are calculated as
224 - (5,000,000 ÷ f (Hz)) ÷ 2, with the
alternative value being one higher in
cases where 5,000,000 ÷ f Hz is odd.
Circuit description
The full circuit shown in Fig.2
is based on microcontroller IC1, a
PIC16F88. This monitors the input
frequency, jumper state (JP1 and JP2),
switch state (S1 and S2) and trimpot
settings (VR1 and VR2). It also drives
the frequency LEDs (LED2-LED5),
threshold LED (LED6) and the relay
coil (RLY1) and its associated LED
(LED7) via NPN transistor Q2.
Power is fed in via CON1 and the
supply is nominally 12V DC. Diode
D1 provides reverse polarity protection and its cathode connects directly to the positive terminal of the relay
coil, applying a nominal 11.4V to it as
well as to the 5V regulator, REG1 and
it powers the rest of the circuit.
A 10F electrolytic capacitor is used
to filter the supply voltage and transients are clamped using a 16V zener
diode (ZD1), with the peak current limited by the series 47Ω resistor.
The supply is further filtered by another 10F capacitor and then REG1
reduces the 11.4V supply to 5V for IC1
and input conditioning transistor Q1.
The power LED, LED1, is connected
across the 5V supply with a 3.3kΩ series current-limiting resistor.
The input signal is fed into CON2
and it’s AC-coupled via a 10F capacitor and 10kΩ resistor to the base
of Q1. The 470pF capacitor filters any
transients while diode D2 clamps the
base voltage at -0.7V for negative excursions. Q1 inverts and amplifies the
signal, suitable for the capture compare input (CCP1) at pin 6 of IC1.
Frequency measurement
modes
When the micro is configured to generate frequencies for setting the upper
and lower thresholds, the output signal appears at pin 6 and TP3. For this
to work, there must be no input signal at CON2 and this means that Q1
is biased off and it will not load the
siliconchip.com.au
Fig.3: component layout for the Deluxe Frequency Switch with a matching photo
below. We suggest using an IC socket for IC1 – and make sure when you place
the connectors, their wiring access holes all point to the outside of the PCB.
output signal from pin 6.
20MHz crystal oscillator X1 is connected to IC1, between its CLKO and
CLKI pins, to allow for accurate and
wide-ranging frequency measurements. The MCLR-bar reset input is
tied to the 5V supply via a 10kΩ resistor to provide a power-on reset for
the microcontroller.
Internal pullup currents within IC1
hold the RB1 and RB2 inputs high
when switches S1 and S2 are not
pressed and similarly, are enabled for
the RB5 and RB6 inputs which are connected to jumpers JP1 and JP2. These
inputs are pulled low if a switch is
pressed or jumper plug inserted and
this can be sensed by IC1.
Output pins RA0 (17), RB4 (10), RB7
(13) and RA1 (18) drive signal indicators LED2-LED5 via 3.3kΩ currentlimiting resistors at around 1mA each.
Similarly, output RA4 (pin 3) drives
the threshold LED, LED6. The RB3
output (pin 9) switches transistor Q2
on when it goes high. This transistor
in turn switches on the relay. Diode
D3 quenches back-EMF from the coil
as Q2 is switched off.
Celebrating 30 Years
LED7 is also switched on when the
relay is powered. It’s wired across the
relay coil and uses a 10kΩ series resistor due to the higher voltage (11.4V).
It provides the same current to LED7
as for the other LEDs.
Trimpots VR1 and VR2 set the default hysteresis and delay time and
both are connected across the 5V supply, with their wipers connected to
analog inputs AN2 (pin 1) and AN3
(pin 2) respectively. The voltages at
these pins are converted to digital values using IC1’s inbuilt 10-bit analogto-digital converter (ADC). The 100nF
capacitors between each of these two
pins and ground provide a low-impedance source for the ADC during
conversions.
Construction
The Deluxe Frequency Switch is
built on a double-sided PCB coded 05104181 and measuring 102 x
58.5mm. It will fit in a plastic utility
box measuring 129 x 68 x 43mm.
Follow the overlay diagram, Fig.3,
when installing the parts. Fit the resistors first. The colour codes are shown
May 2018 39
overleaf but we recommend that you
use a digital multimeter (DMM) to
check the values before soldering them.
Diodes D1, D2, D3 and ZD1 are next
and these need to be inserted with
the correct polarity, with the striped
end (cathode, k) oriented as shown
in the overlay diagram. Diode D2 is
the 1N4148 type while D1 and D3 are
1N4004.
Parts list – Deluxe
Frequency Switch
1 double-sided PCB coded 05104181,
102 x 58.5mm
1 DPDT 12V DC coil relay (RLY1)
[Jaycar SY-4052 {5A},
Altronics S 4270A {8A}]
2 2-way screw terminals with 5.08mm
pin spacing(CON1,CON2)
2 3-way screw terminals with 5.08mm
pin spacing (CON3)
2 2-way pin headers with shorting
blocks (JP1,JP2)
1 18-pin DIL IC socket (for IC1)
1 20MHz crystal (X1)
2 SPST PCB-mount tactile pushbutton
switches (S1,S2)
[Jaycar SP0600, Altronics S 1120]
2 PC stakes (TP GND,TP3)
Semiconductors
1 PIC16F88-I/P microcontroller
programmed with 0510418A.HEX
(IC1)
1 LP2950ACZ-5.0 low dropout
regulator (REG1)
1 BC547 100mA NPN transistor (Q1)
1 BC337 500mA NPN transistor (Q2)
1 16V 1W zener diode (1N4745) (ZD1)
2 1N4004 1A diodes (D1,D3)
1 1N4148 signal diode (D2)
7 3mm LEDs (LED1-LED7)
Capacitors
1 100µF 25V PC electrolytic
2 10µF 16V PC electrolytic
1 10µF non-polarised (NP) PC
electrolytic
4 100nF 63V/100V MKT polyester
1 1nF 63V/100V MKT polyester
1 470pF ceramic
2 27pF NP0/C0G ceramic
Resistors & Potentiometers (all 1%,
0.25W)
1 1MΩ 1 100kΩ
4 10kΩ
6 3.3kΩ 1 1kΩ
1 47Ω
1 10kΩ vertical multi-turn trimpot,
3296W style (VR1)
1 10kΩ mini horizontal trimpot, 3386F
style (VR2)
40
Silicon Chip
We recommend using an IC socket for IC1. Take care with orientation
when installing the socket and when
inserting the IC. For the test points, we
used two PC stakes, one for TP GND
and the other for TP3. We left the remaining test points as bare pads so a
multimeter probe can be inserted.
Install the two 2-way pin headers
for JP1 and JP2 and then follow with
the capacitors.
The electrolytic types must be fitted with the polarity shown (long
lead to pad marked plus; the stripe
indicates the negative side) and note
that the 10F NP capacitor is non polarised and so can be installed either
way around.
Next, mount transistors Q1 and Q2
and also REG1. Take care not to mix
them up as they come in identical
packages.
Trimpots VR1 and VR2 are next to
be fitted. They may be marked as 103
instead of 10kΩ. Orient VR1 with the
adjusting screw as shown.
CON1 to CON3 can now be installed. CON1 and CON2 are 2-way
types and CON3 comprises two 3-way
screw connectors dovetailed together.
Fit all connectors with the wire entry
to the edge of the PCB.
Finally, the LEDs and relay RLY1 can
be installed. We placed the LEDs close
to the PCB, but they can be mounted
higher or mounted off the PCB if you
wish, connected with flying leads.
Although presented as a bare PCB,
the unit fits in a UB3 Jiffy box. In this
case, attach the PCB to the base of the
box using spacers. First, mark out and
drill 3mm holes for each of the corner
mounting holes.
You will also need to drill holes at
each end of the box for cable glands. A
gland at one end is used for the power
and signal wires while a gland at the
other end allows the relay contacts to
be wired up as required.
Set up
You have several options for setting
the unit up. You can set it up before installation using or an oscillator or the
actual signal source (if it can be held
steady enough) when you install it.
1) Oscillator method: Power the unit
up with a 12V power supply wired to
CON1. Connect the oscillator to CON2.
Set the signal amplitude to 2V peakto-peak or 0.7V RMS.
Set the oscillator to your desired upCelebrating 30 Years
Using a tacho signal
Say you are using the engine tacho signal to switch the relay if a certain engine
RPM is exceeded – say 6000 RPM.
If you have a 4-cylinder, 4-stroke engine,
6000 RPM = 100 revolutions per second.
Since this type of engine fires two cylinders
per crankshaft rotation, then the threshold
should be set to 200Hz [100 x 2].
per threshold frequency (eg, 500Hz)
and press S2. Then reduce the oscillator to set the lower threshold (eg,
400Hz) then press S1. That’s all there
is to it.
If you want to set a single threshold
frequency (ie, the upper threshold) and
use the hysteresis setting, fit a link to
JP2. Then adjust trimpot VR1 for the
required hysteresis (percentage) while
you monitor the voltage at TP1. Then
set the oscillator for the desired frequency and press S2.
Alternatively, if you want to set a
single threshold frequency at the lower threshold and use the hysteresis
setting for the upper threshold, fit a
link to JP2. Then adjust trimpot VR1
for the required hysteresis while you
monitor the voltage at TP1. Then set
the oscillator for the lower threshold
frequency and press S1.
Table 1 shows some the relationship
between the voltage at TP1 and the
percentage hysteresis. For example, if
you set VR1 to give 1.25V at TP1, the
hysteresis will be 20% and the resulting lower threshold frequency will be
20% lower than the frequency you set
with switch S2.
Note that you can also set the unit
with only one threshold frequency and
that will mean the relay will latch on
when the signal goes above the threshold and will stay on until the power
is turned off.
To set just a single threshold frequency, set the oscillator to the desired frequency and then press S2.
Then disconnect the signal from CON2
and wait until the signal LEDs all are
off. Then press S1 to set the lower frequency to zero.
No link is required at JP1 if you want
the relay to switch on as the frequency
rises above the threshold set by S2 (and
turns off when the frequency drops below that set by S1).
Alternatively, install JP1 if you want
the relay to switch on as the frequency falls below the threshold set by S1
(and turn off when the frequency rises
siliconchip.com.au
above the threshold set by S2).
lier) you need to adjust
trimpot VR2. You can
set the delay anywhere
between zero and half
a second.
If you don’t want
a delay set VR2 fully
anti-clockwise.
2) Frequency meter method: The
advantage of this approach is that you
don’t need an oscillator but you will need
a frequency meter or
oscilloscope to measInstallation
ure the frequency appearing at TP3.
Connect the 10-16V
To get into this
DC power source between the
mode, connect your
+12V and GND inputs at CON1.
frequency meter or
For automotive installations,
DMM between TP3
automotive-rated wire should
and GND.
be used and the +12V termiSwitch off power,
nal needs to connect to the
hold down both S1
switched side of the ignition.
The PCB is designed to fit into a UB3 Jiffy box, as shown here –
and S2 and then
That way, the unit only opbut it could also be “built in” to equipment it is controlling. You
switch on the powerates when the ignition is
may also be able to source the 10-16V DC from that equipment –
er. The micro then
switched on and the vehicle
as long as it isn’t turned off by the frequency switch!
produces a 100Hz
battery won’t go flat after long
signal at TP3.
Then remove JP1, use S1 & S2 to ob- periods of being parked.
To adjust this default frequency to
tain the lower threshold frequency, inThe easiest way to connect the GND
obtain your desired upper threshold,
sert JP1 again and press S1.
terminal in a vehicle is to wire it to the
press S1 and S2 until it reaches your
Alternatively, if you just want to set chassis using a crimped eyelet secured
target. S2 increases frequency, while
the upper threshold frequency with S2 to a convenient screw terminal.
S1 decreases frequency.
and have the hysteresis setting made
You may need to drill a separate hole
Short presses of the switches will
for the lower threshold as set by trim- in the chassis for this connection, or
alter the frequency at a slow rate. For
pot (VR2), then you must have a link utilise an existing earth connection.
faster changes, hold the switch down
fitted to JP2 before you start the proceWire CON2 to a suitable sensor. This
and the rate will change to a faster rate
dure. Similarly, you can set the lower can be the speedometer sensor, an ECU
after two seconds. Continue to depress
threshold with S1 and have the upper tachometer output, an injector or camthe switch for another two seconds and
threshold set by the hysteresis percent- shaft position sensor and so on. If you
the frequency will change at an even
age value as set by VR2.
haven’t already set the unit up, do so
faster rate.
Now turning off the power takes the as described above above.
This allows you to run through the
micro out of the mode whereby it proThe relay contacts are labelled Norentire frequency range in less than one
duces an output frequency at TP3. It mally Open (NO), Normally Closed
minute but still be able to do finer adthen reverts to normal operation, mon- (NC) and Common (COM).
justments with brief switch presses.
itoring the input frequency instead.
To switch power to a load, wire one
Having reached your target frequenThen fit a link to JP1 if you want the of its supply lines in series between
cy, insert JP1. Then press S2 to set the
relay to switch on as the frequency falls
either the COM and NO terminals (so
upper threshold frequency. Then rebelow the threshold set by S1 (and turns that it’s only powered when the relay
move JP1. Then press S1 to reduce
off when the frequency rises above that
is energised) or COM and NC terminals
the frequency to the lower threshold.
set by S2).
(so it’s switched off when the relay is
Then re-insert JP1 and press S1 to set
No link is required at JP1 if you want energised).
the lower frequency threshold.
the relay to switch on as the frequency
Note that the relay contact current
Note the two-step process to set each
rises above the threshold set by S2 (and rating is 5A for the Jaycar relay and 8A
frequency. In other words,with JP1 out,
turns off when the frequency drops be- for the Altronics relay (see parts list).
use S2 and S1 to adjust the frequency to
low that set by S1).
If a higher current is required, you
the wanted value, insert JP1 and press
If you want to configure the unit with can switch 12V DC to the coil of a largS2 to set the upper threshold.
a switching delay (as described ear- er relay using RLY1.
SC
Resistor Colour Codes
o
o
o
o
o
o
Qty. Value
1
1MΩ
1 100kΩ
4
10kΩ
6 3.3kΩ
1
1kΩ
1
47Ω
siliconchip.com.au
4-Band Code (1%)
brown black green brown
brown black yellow brown
brown black orange brown
orange orange red brown
brown black red brown
yellow violet black brown
5-Band Code (1%)
brown black black yellow brown
brown black black orange brown
brown black black red brown
orange orange black brown brown
brown black black brown brown
yellow violet black gold brown
Celebrating 30 Years
Small Capacitor Codes
Qty. Value
o
o
o
o
4
1
1
1
F
Code
100nF 0.1F
1nF 0.001F
470pF
27pF
-
EIA
Code
IEC
Code
104
102
470
27
100n
1n
470p
27p
May 2018 41
|