This is only a preview of the September 2003 issue of Silicon Chip. You can view 24 of the 96 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 "Portable PIC Programmer":
Items relevant to "Current Clamp Meter Adaptor For DMMs":
Articles in this series:
Items relevant to "Digital Instrument Display For Cars, Pt.2":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
MORE FUN WITH THE PICAXE – PART 8
To sleep, perchance
to dream (or nap) . . .
(and PICAXE datalogging too!)
Quite aside from all their other
benefits, the inbuilt micro-power
standby features of Picaxes – send
ing them to sleep – can greatly
enhance battery life.
A new use for apparently “dead”
batteries, perhaps?
by Stan Swan
G
iven the prevalence of portable devices (refer June “SILICON C HIP ” editorial!) this
alone should win favour for projects
such as torches and data loggers used
away from a mains supply.
There are four distinct commands,
and several code techniques, to organise this.
PAUSE – introduces a program delay
in milliseconds.
Example: pause 100 = 100ms
Maximum delay is 65535ms (a
little over one minute) with about
1ms overhead. Of course, we’ve used
pause a lot in earlier articles for LED
flashing etc. No power saving.
WAIT – equivalent to pause, but with
larger units
Example: wait 10 = 10 seconds
Up to 65.5 seconds wait possible,
but again no power saving activated
NAP – Enters a low power short period mode.
Example: nap 3 yields a 144ms delay.
The eight nap period values (060 Silicon Chip
7), yield a duration given by the
formula:
Delay (in ms) = 2^time value x 18.
Hence Nap 0= 18ms, while nap
1= 36ms, nap 2= 72 ms , nap 3 =
144ms etc. Nap 7 = 2304ms (2.3
seconds).
SLEEP – Also enters a low power long
period mode (2.3 seconds units).
Example: Sleep 10 = ~23 seconds.
Although of high accuracy, resolution is lower and overall times
can amount to ±1% deviation (perhaps up to 30 seconds drift in an
hour.) The maximum sleep value,
of 65535 (being 256 x 256) extends
to days, but could be temperature
dependent. Hence don’t set your
alarm clock by this…
Other delays
Unusual delays, not catered for
above, can perhaps be organised by “do
nothing” nested loops that may run to
minutes. Hence a 100 x 100 = 10,000
count, could involve code looping
inside loops and be set up perhaps
for b0=1 to 100: for b1= 1 to 100: next
b1: next b0
A further alternative may be to
exploit the serin command, which patiently awaits the arrival of serial data
bytes – perhaps from a linked PC timer.
Rev. Ed’s AXE033 LCD display in fact
includes a DS1307 real time clock chip
offering this feature, allowing precise
intervals to be set.
OK – you know all about pause, so
let’s first put nap to work in yet another
LED flashing circuit.
Another one? As we’ve mentioned
before in this series, pulsed or flashing
LEDs attract attention, help identify the source and also save battery
power. And the PICAXE is perfect for
doing it! The classic 1970s LM3909
IC enjoyed decades of use in just this
field but that was – well – the 20th
century.
If the flash rate is fast enough
(>20Hz or so) human persistence of
vision comes into play as well and
www.siliconchip.com.au
the pulsing light “looks” to be just a
steady source.
Hence it’s “win win” – appearing
to be on but saving significant power
–and with today’s ultrabright white
LEDS, battery life can be hugely
extended – an important issue in
emergencies or even lesser developed
countries rural lighting.
Take note however – flash rates
around 7-10Hz are particularly irritating (they’ve even been used for riot
control) and in extreme cases may
bring on epileptic fits.
Power down
Rather than just have a short pause
between such LED flashes, it’s maybe
better to use the brief power down
features of nap. When devices are
being worked hard, such as the “over
driven” white LED here, this brief
cool-down spell may help to ease the
thermal stress on the LED (normally
limited to ~30mA but capable of withstanding 100mA pulses).
Recall that readadc command from
the “Door Minder” article (SILICON
CHIP March 2003)? This has been
further extended here so that the presented resistance from a 500kΩ pot
selects the flash sequences.
Such resistances could have been
set with a multi-position switch and
assorted resistors, or even maybe a
stepped push switch but the pot simplifies things nicely. Note the generous
program comments!
(Above): the circuit diagram and
protoboard layout for the first part of
this month’s PICAXE series – using
the various “slumber” commands
to save battery power in the LEDs.
This is used in conjunction with the
“LEDNAP” program overleaf.
As usual, the photo is just slightly
different from the protoboard layout
above (we’ve moved some components
for clarity in the drawing). The pulsed
white 5000mCd LED (from Jaycar)
used here stands out like a lighthouse
at night and is easily visible several
kilometres away.
www.siliconchip.com.au
September 2003 61
In contrast to catnaps, sleep is
intended for some serious PICAXE
downtime resting! During such a sleep,
power demands were found to drop to
the 100µA range, (although brief wake
up surges have been reported), hence
almost offer a new use for otherwise
“dead” batteries.
The 2.3 second unit (being the
upper value of nap7) implies a super
nap is invoked. The maximum sleep
LEDNAP.BAS
value (65535), extends to some days
and sequential sleeps could stretch to
(maybe) months.
For portable work, battery life may
ultimately limit the program duration
but a 1000mAh NiMH should last
10,000 hours (over a year) which may
compare with its shelf life!
Or a small photo-voltaic panel
could keep a rechargeable pack trickle-charged. These aspects are naturally
(Also downloadable from:
www.picaxe.orconhosting.net.nz/lednap.bas)
‘LEDNAP.BAS program for September “Silicon Chip” Picaxe-08 article. Ver 1.0 12/7/03
‘Potentiometer acts as multi position switch! Stan. SWAN = s.t.swan<at>massey.ac.nz
‘Values as set here allow 7 different flash types to be dialled up via the 500k pot
‘Much tweaking still possible-better reading b0 ranges,alter pot(Log?),R value etc
‘White LED (10mm ?) via BC547 with 470 Ohms from its base to Picaxe pin 2 via “NAP”.
‘Actual nap value depends on application - steady light,standby, rescue beacon etc
‘Even nap 0 has distinctive ~20 Hz flicker = ideal bike front for attention getting?
‘NB ~10Hz flicker is most irritating to many observers - may cause epileptic fit?!
‘On 3AA 4.5V supply & 470 base R,typical DSE Lux meter values (pulsed of course) ~
‘nap 0=35mA 340 Lux, nap 1=22mA 216 Lux, nap 2= 13mA 120 Lux, nap 3= 8mA 70 Lux
‘nap duration = 2^period x ~18ms , with period values 0-7 ( rolled over if beyond )
‘nap 0 ~18ms, nap 1 ~36ms, nap 2 ~72ms, nap 3 ~144ms, nap 7 ~ 2secs
‘Inbuilt loop o’head of course distorts M/S ratio. Red LED direct driven from pin 4
‘Program download from = www.picaxe.orcon.net.nz/lednap.bas Comments (‘) optional
‘————————————————————————————————————————
ledflash:
‘main adc read to set nap time or dim with pwm etc
readadc 1,b0
‘ADC read pin1 -via 500k pot(Log?) & 47k V.divider
b1= b0/22
'divide returned “nap” value so fits in 0-7 range
if b1=0 then redflash
'“08” readadc values are in 16 blocks 0-160,11 wide
if b1=1 then redpwm
'Could use as is,but division OK for just 7 values
if b1=7 then beacon
'1/2 Hz beacon mode nap 7 forced via b2 variable
'————————————————————————————————————————
whiteflash:
'routine for normal adjustable white flash rates
b2=b1-2
'tweak returned b1 rates,since too short as divided
high 2:pause 10:low 2
'LED pulse - somewhat overdriving via transistor
nap b2
'brief snooze to allow LED/BC547 cool down !
goto ledflash
'return to main pot. reading procedure
'————————————————————————————————————————
redflash:
'red led at pin 4 flash - rear bike light style
high 4:pause 10:low 4:nap 7
'pulsed ~ every 2 seconds - adjust to suit needs
goto ledflash
'return to main pot. reading procedure
'————————————————————————————————————————
redpwm:
'red led attractive pulsing effect
for b3=0 to 255 step 2
'loop so red LED has pleasing brightness increase
pwm 4,b3,1 :next b3
'PWM pin 4 LED one cycle at increasing pulse width
for b3=255 to 0 step -2
'loop to fade led out
pwm 4,b3,1
'PWM pin 2 led one cycle at decreasing pulse width
next b3:pause 300
'led displays a pleasing “heartbeat” effect !
goto ledflash
'return to main pot. reading procedure
'————————————————————————————————————————
beacon:
'battery life prolonging (weeks ?) beacon flash
high 2:pause 10:low 2
'brief led pulse - via transistor.Approx 1mA draw
nap 7
'~ 2 sec delay between pulses
goto ledflash
'return to main pot. reading procedure
62 Silicon Chip
an issue for portable applications such
as our data logger.
What – a Picaxe data logger?
Yes – although just a baby, the “08”
has a 64 bytes non volatile memory
(EEPROM). Any data (of values 0 –255)
can be stashed away here (in ascending locations 0,1,2. – 64), although
programs (stored downward 128,127
etc) share the same RAM and can be
overwritten if care is not taken!
Key commands used are write and
read, which store or retrieve during
a program run, much as you’d keep
a pencilled scratchpad of (say) items
during a stocktake.
The EEPROM (Electrically Erasable
Programmable Read Only Memory)
command is intended to “pre store”
values, either ASCII or data, so they
are available once the program gets
to work. No battery backup is needed
to hold this data, since the memory
is Flash RAM based. Here’s a simple
code snippet example –
EEPROM (13,7,19,69) ‘ pre stores bytes
13,7,19,69 at locations 0,1,2,3 if free
write b1 ‘ stores b1 value at memory location
staring from next available (4 here)
read b0 ‘ read b0 retrieves this value for
program use
Due to the “Von Neumann architecture” nature of the 08, its memory
can’t be easily extended, sadly ruling
out use of cheap I2C RAM chips. Even
though 64 bytes may seem too trivial to
exploit, it’s proven ideal for recording
voltage divider network values via the
“08” readadc command. Don’t get too
excited - only low resolution is possible, and an upper limit value of 160
applies, but some 16 unique values
may be detected and stored.
Data loggers of course allow valuable monitoring of such “real world”
values as temperature, earthquakes,
wind speeds, voltages, traffic, pH etc
- maybe too tedious or hazardous for
human recorders.
Educators may quibble but humans
are arguably better employed than
just watching dials and writing down
numbers and additionally we’re often
devious, lazy or deceitful – perhaps
“snoozing” when a key value arrives!
Direct computer analysis is eased with
machine gathering too.
OK, you’re convinced – but aren’t
such data loggers costly? Read on!
The deceptively simple Picaxe-08
www.siliconchip.com.au
Part two for
this month:
data logging
with the
PICAXE-08.
Here’s the
circuit
diagram and
protoboard
layout.
circuit (at right) and program (overleaf)
uses a 100kΩ/25oC NTC thermistor
as a temperature sensor in simple
automated application, with a sleep
period initially set to take readings
every minute for an hour. Since only
64 program bytes were available (the
other 64 being used for data of course)
refinements were limited, and the
readout technique a compromise.
But as the Excel graph shows, very
distinctive thermal environments
were easily logged, and applications
abound even as it stands!
Mmm – how about checking the
heating/cooling rate of a spa pool as a
guide to its insulation performance –
waterproof the sensor of course. Monitoring an air conditioner - the period to
bring rooms to a desired temperature
perhaps? Replace the NTC with an
LDR and note illumination changes
– room lighting /security etc. Check
actual voltages of discharging battery
packs for relative performance?
Incidentally, we’ll be extending this
design later with a Picaxe 18A (and
18X – due late 2003), offering higher
resolution, multi-channel and more
memory, so stay tuned.
To overcome the “08” lo-res limitation, an elegant ramping solution has
just been suggested. This sets up the
PWM command to feed incremental
pulsed PWM bursts into a capacitor
OK, this one is really different because it is built
on a mini protoboard, whereas the diagram above
is based on the standard protoboard we have used
throughout this series. Either is perfectly suitable
(especially if you already have one or other!). That’s
a 4V 20mA PV panel at top – rescued from a budget
garden lamp.
An Excel spreadsheet plot of the data obtained from our PICAXE
logger, in this case being used as a temperature logger. It shows the
readings over one hour in various locations – just to give you some
idea of what the PICAXE logger can be put to!
www.siliconchip.com.au
September 2003 63
DATALOG8.BAS
(Also downloadable from:
www.picaxe.orconhosting.net.nz/datalog8.bas)
and NTC/LDR until the preset digital
threshold is reached, at which point
values are recorded and “serout”
passed to be read (or graphed) on an
attached screen.
This naturally feeds live values rather than logged ones, but implies up to
three channels of hi-res data (values 0 –
255) could be simultaneously gathered
and PC stored. Check www.picaxe.
orconhosting.net.nz/datagath .bas for
the full program.
‘DATALOG program for September “Silicon Chip” Picaxe-08 article. Ver 1.02 24/7/03
‘Use with attached 100k thermistor etc pin 1. Via Stan.SWAN => s.t.swan<at>massey.ac.nz
‘When “08” powered up,any prior stored EEPROM values sent as pin 4 serial port data
‘Display this gathered data via any terminal program -LCD,BananaCom,F8,StampPlot etc.
‘If saved via a terminal program,the “.csv” data can of course be Excel graphed too
‘NB-Gives you 30secs to turn unit OFF before fresh storage begins & thus wiping old!
‘**** CARE - BE PROMPT ! REPROGRAMMING/RELOADING “08” TOTALLY WIPES DATA TOO ****.
‘As set up logs temp in 0-30 C range every min for 1 hr.WAIT more accurate than SLEEP?
‘Tweaking V divider network Rs may allow narrower temp range.Alter 47k to 100k maybe
‘Picaxe 18A should run this OK too, but give high res readings & store 256 values.Yah!
The circuit
‘————————————————————————————————————————
Son of a Picnik box!
‘Picaxe data storage value range 0-255,although only to 160 via “08” readadc of course.
Until now, all our circuits have
‘EEPROM builds up from location 0,but program builds down from 128. Just 61 bytes used
been based on a full-sized solderless
protoboard. And that’s what the dia‘Basic EEPROM syntax is ex. EEPROM (13,7,19,69 ) where bytes 13,7,19,69 EEPROM stored
gram overleaf shows. (We figure that
‘at locations 0,1,2,3. Code use READ & WRITE to access this data at these spots maybe
by now anyone experimenting with
‘To retain program simplicity & maximise number of readings, data can not be viewed as
PICAXEs would have one of these
‘gathered.In practise this should not be an issue, since data logger likely to be used
protoboards).
‘stand alone/ outdoors etc, then retrieved to display values back at an indoor PC etc.
But as you can see, there is an awful
‘N.B.SLEEP not 1:1,as unit=2.3 secs.Elapsed times ~x2 expected.By trial SLEEP 25=1 min
lot of unused real estate on that board.
‘Stored values are non volatile - thus no need battery backup connection once gathered
Let’s look at a couple of alternatives.
‘PIC makers (Microchip) say data retained in EEPROM >40 years unless overwritten.Bravo
A one-third size breadboard (sold
‘Typical (baby Wish board!) hardware setup pix=> www.picaxe.orcon.net.nz/datalog8.jpg
here in NZ as the Global EXP-325) just
‘With sample Excel graph resulting (1 hour run)=> www.picaxe.orcon.net.nz/datalog8.gif
fits everything, (including the 3.6V
‘ ****** Download this program via => www.picaxe.orcon.net.nz/datalog8.bas <= ******
NiCd battery – rescued from an old PC
‘————————————————————————————————————————
motherboard) we’ve become familiar
‘ASCII art schematic
Typical temp. readings/readadc values
with. It’ll snap shut in a sealed food
‘
- - - - - - -ve rail
0 Celsius
11
container for outdoor or submersed
applications too, and of course a sol‘ Piezo _____ Pin | |
4
21
dered version can be easily made too.
‘ Pin 0 ___ | 2 LED 47k
8
32
‘ ——— | |_______| |
12
43
Something more permanent?
‘ |Picaxe| |_______________|
16
53
So you want a permanent version?
‘ | 08 | Pin 1 ADC |
20
64
Time to transfer your circuit to, say, a
‘ ———
100k
24
75
PC board?
‘
|______Pin 4
NTC
28
86 etc
During recent time with the “08”,
‘ |||
serout
|
3-5 V
‘Usual 3 wire
+ + + + + + +ve supply
NOTE - Temp values approximate
‘prog.input
& may need better calibration
‘————————————————————————————————————————
‘READ/PLAYBACK ROUTINE
serout 4,n2400,(12,”Datalog “)
‘ASCII values 10=CR, 12=FF(=cls),13=LF, 44=comma
for b0= 0 to 63
‘stored data values readout to terminal or LCD
read b0,b1
‘polls & reads out stored eeprom values ( .csv)
serout 4,n2400,(44,#b1)
‘comma,then value <at> pin 4. LED to show data too?
next b0
‘read next stored EEPROM value out
wait 30
’30 secs “reading” delay -modify if too short etc
‘————————————————————————————————————————
‘WRITE/DATA LOGGING ROUTINE
for b0= 0 to 63
‘begin 64 data readings at time set by SLEEP
sound 0,(75,10)
‘Beep to alert data logging commencing
pulsout 2,500
‘brief flash from pin 2 LED indicates datalogging
readadc 1,b2
‘b2 has 16 blocks 11 wide (range 0-160),so 21 etc
write b0,b2
‘sequentially write values to EEPROM locations
sleep 25
’25x2.3secs ~1 min delay (+/- 1% )-alter to suit
next b0
‘Ex. Sleep 782 yields 64 x 1/2 hr =32 hrs data!
Here’s a stylised version of how a
‘Data gathering stops when 64 readings taken
permanent hookup on Veroboard or
‘————————————————————————————————————————
similar might work.
64 Silicon Chip
www.siliconchip.com.au
References and
parts suppliers . . .
(also refer to previous months articles)
1. Suitable NTC thermistor (R1895
100kΩ/25C), and PC prototyping
board (H5605), assorted resistors,
transistor & 500kΩ pot: Dick Smith
Electronics www.dse.com.au
2.White LEDs, battery (3.6V 70mAh
SB-1609 ~ A$9) and small piezo
sounder: Jaycar Electronics www.
jaycar.com.au
3. Humans perceive pulsing lights
under “flicker fusion frequency” laws
(Ferry-Porter etc) http://webvision.
med.utah.edu/temporal.html
4. Mini EXP-325 protoboard (~A$5):
www.globalspecialities.com
5. “Dataloggerama” insights are gained at
www.rogerfrost.com
6. Picaxe supplies via MicroZed
www.picaxe.com.au, with program
listings and diverse links www.picaxe.
orconhosting.net.nz
7. Thanks to Eltham Tech Centre and
Andrew Hornblow (Taranaki, NZ) for
DIY ideas.
and building on feedback from students, older hobbyists and emails,
it’s remained apparent that soldering
should be the last thing you do when
investigating such electronic circuits.
Maybe your design is unstable,
draws too much current, has wrong
colour LEDs or is even (duh!) too big
for the box! Once soldered up these
aspects may be a nightmare to alter
without tedious redesign or desolder-ing, risking ESD or heat damage.
Solderless “breadboard” proto-boards remain ideal for rapid PICAXE development (I’ll use them again
with the 18A), even though some of the
tweaking can be of course via software.
Naturally, once your design is finalised, if you want to keep it then
it’s standard practice to produce a
soldered version. If full PC board design costs and time are not justified,
consider several rapid cost-effective
approaches instead.
1. Many suppliers sell a pre-drilled
1/10th inch grid board, with copper
tracks matching a normal breadboard.
It’s almost “paint by numbers” to move
items from one to the other and solder
as you go, with the final result looking
convincingly professional.
2. VeroBoard (and all its variations).
The hobbyist’s standby of course and
is easily scored and cracked to size.
Considerable lateral thinking by NZ
“BrightSparks”, with minds refined
by crosswords, it transpires, have
produced soldered Vero versions to
suit many “08” circuits.
In the most general form (which even
includes a programming socket) only a
tight block of track needs removal but
a grander version even provides for
driver transistor placement. IC header
strip can be soldered in for flying leads
too. Recommended !
3. Copper clad “spider board”. Deep
cuts made quickly and neatly across
the copper produce ten sections (yeah,
we know spiders have eight legs,
not ten!) that suit top surface mount
soldering of components and an IC
socket, or even edge clipping of leads.
It perhaps best suits junior users who
SC
need WYSIWYG insights.
NEXT MONTH:
All new: The Picaxe 18A
Similarly, here’s a “Spider Board”
which you can make yourself with a
sharp knife and scrap of PC board.
www.siliconchip.com.au
Our baby Picaxe zero eight,
Will now take a rest, so its mates
Can show off their bytes,
With more circuit delights,
And greater PIC magic create!
September 2003 65
|