This is only a preview of the January 2004 issue of Silicon Chip. You can view 33 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 "Studio 350 Power Amplifier Module":
Items relevant to "High-Efficiency Power Supply For 1W Star LEDs":
Items relevant to "Antenna & RF Preamp For Weather Satellites":
Items relevant to "Lapel Microphone Adaptor For PA Systems":
Articles in this series:
|
PICAXE-18X
4-channel
datalogger
Pt.1: exploring the I2C bus
This PICAXE-18X Datalogger is a highly versatile 4-channel
data acquisition system. Based on one of the PICAXE series
of microcontrollers, it’s easy to use and reprogram, enabling
the end user to perform many different types of logging
experiments.
By CLIVE SEAGER
B
asically, this datalogger consists of four input
channels that can be sampled and stored (logged)
at user-defined intervals. One channel is dedicated for use with a digital temperature sensor, whereas the
remaining three can be used as analog or digital inputs.
Logging can be carried out at regularly spaced intervals
(typically one minute to several hours), or an optional
real-time clock (RTC) chip can be added to ensure accurate logging intervals over longer periods (once a week,
once a month, etc). Use of the RTC will be covered in
next month’s article.
Data is saved in an onboard EEPROM memory chip.
If desired, this memory chip can be upgraded for increased memory capacity. An optional memory expansion board can also be used to greatly increase memory
capacity.
Main Features
•
•
•
•
•
•
•
•
Low-cost design
Four logging channels
One dedicated digital temperature sensor channel
EEPROM data storage (easily expandable)
PICAXE micro means simple programming
3 x AA battery operation, low power consumption
Small footprint (approx. same size as 3 x AA cells)
Optional real-time clock with lithium battery backup
72 Silicon Chip
Once the “mission” is complete, data can be uploaded
for analysis on a computer. Data can also be displayed
(at the time of logging) on an optional liquid crystal
display (LCD) if desired.
The PICAXE-18X datalogger makes extensive use of
the I2C bus for communication between ICs. Therefore,
before proceeding any further let’s take a detailed look
at the principles of the I2C bus.
What is the I2C bus?
The Inter-Integrated-Circuit (I2C) bus was originally
developed by Philips for transferring data between
ICs at the PC board level. The physical interface of the
communication bus consists of just two lines – one for
the clock (SCL) and one for the data (SDA). These lines
are pulled high by resistors connected to the positive
rail – see Fig.1. A value of 4.7kΩ is commonly used for
these resistors, although the actual value used is not
that critical. When either the master or slave ICs want to
“transmit”, they signal their intent by pulling the lines
low (0V).
The IC that controls the bus is called the “master”. As
in this project, the master is often a microcontroller. The
other ICs connected to the bus are called “slaves”. There
can be more than one slave on the bus, but each slave
must have a different “address” so that it can be uniquely identified. In theory, up to 112 different addresses are
possible, but most practical applications would generally have 1-10 slave ICs.
A few interesting I2C slave devices include:
www.siliconchip.com.au
Table 1: Terms Used In this Article
IC
Integrated circuit or “chip”
Master
A microcontroller IC that ‘controls’ the
operation of a circuit
Slave
An IC that’s controlled by the master IC
Byte
A number between 0 and 255
Register
A memory location within the slave that
stores 1 byte of data
Register Address
An address that points to a particular
memory register
Block
Group of 256 registers
EEPROM IC
An IC that can store data (electrically eraseable programmable read only memory)
RTC IC
A slave IC that can maintain the date/time
(real-time clock)
24LC16B: a 2k EEPROM memory chip (Microchip)
DS1307: a real-time-clock chip (Dallas/Maxim)
PCF8574: an 8-bit input/output expander (Philips)
SP03: speech synthesiser module (Devantech)
Why use the I2C bus?
The I2C bus boasts the following advantages:
(1) Major semiconductor manufacturers produce lowcost I2C compatible ICs. The range of ICs available
is quite extensive: EEPROMs, real-time clocks, A-D
converters, D-A converters, PWM motor/fan controllers,
LED drivers, digital potentiometers, digital temperature
sensors, etc.
(2) Many of these ICs come in small 8-pin packages.
This makes the circuit design very straightforward.
(3) Multiple slave devices can be connected to the same
bus, using only two microcontroller pins.
(4) The bus design is very simple, using just two lines
and two resistors.
The disadvantages are as follows:
(1) The I2C bus communication protocol is quite complicated. However, by using PICAXE-based microcontroller systems (or similar), simple BASIC-style
commands can be used for all I2C data transfers. With
this method, very little technical knowledge of the bus
protocols is required.
(2) Each slave IC will have unique setup parameters (eg,
slave address) which must be extracted from the manufacturer’s datasheet. This is usually not too difficult
once you know what you’re looking for!
Slave configuration parameters
Although all I2C slave devices work in roughly the
same way, four parameters must be extracted from the
manufacturer’s data sheets for each type of device.
Parameter 1 – Slave Address: as already mentioned,
each slave IC on the I2C bus must have a unique address. This is not a problem when using different types
of ICs on the same bus, as most ICs have a different
default slave address.
The slave address is generally seven bits long, with
www.siliconchip.com.au
the 8th bit reserved to indicate whether the master
wishes to write to (1) or read from (0) the slave. A 10-bit
slave address is also possible but is rarely used and so is
not covered in this article.
For example, the data sheets for a particular device
might define its default address as “1010000x”, with
“1010000” being the address bits and “x” being the
read/write bit. In a PICAXE system, the state of the read/
write (8th) bit is automatically set or cleared by the
microcontroller as necessary for a read or a write operation.
To connect two or more of the same types of ICs (eg,
EEPROMS) on the same bus, an external addressing
scheme is often employed. In the case of the popular
24LCxx series of EEPROMs, three external address pins
(A2, A1 and A0) are provided. By connecting these pins
to V+ or 0V on your circuit design, up to eight parts can
be uniquely identified on the same bus.
For these ICs, the slave address is defined in the datasheets as “1010dddx”, where “d” is 1 or 0 depending on
the state of the external address pins A2 - A0.
Parameter 2 -Bus Speed (100kHz or 400kHz): the maximum bus speed for data transfer between the master
and slave is normally 400kHz. However, some parts will
only work up to 100kHz, and so the manufacturer’s data
sheet should be checked for each slave IC used. Note
that this is the maximum speed – all parts can be run at
the slower speed if desired.
Parameter 3 – Register Address Size (Byte or Word): all
data transfer from the master to the slave is a “write”,
and this means that a byte of data is transferred from
the master to a register within the slave IC. Conversely,
all data transfer from the slave to the master is a “read”.
Simpler slave devices have a maximum of 256 registers and so a “register address” of one byte length can
be used to identify the particular register of interest.
However larger devices, particularly memory EEPROMs,
have more that 256 registers and so may need a “word’
(2-byte) register address instead.
Fig.1: the I2C bus consists of just two signal lines,
SDA (serial data) and SCL (serial clock). Note the
pull-up resistors to the positive supply rail.
Fig.2: a Microchip 24LC16B
16kbit EEPROM IC is used for
data storage. It retains logged
data even when powered off.
January 2004 73
but only up 6 bytes from address 2 (10, 18, etc). If you
don’t follow this rule, you’ll overflow the 8-byte page
write boundary!
I2C in the PICAXE system
Hardware: all the PICAXE “X” parts (18X, 28X, 40X)
include an on-chip I2C communications port. Two pins
take on the SDA (serial data) and SCL (serial clock)
functions when any of the I2C BASIC commands are
used. For the PICAXE-18X system, SDA is leg 7 and SCL
is leg 10.
Software: communication with the slave device requires just three BASIC commands – i2cslave, readi2c
and writei2c.
i2cslave: the i2cslave command is used to set up the
slave parameters for each slave IC. The syntax is:
i2cslave slave_address, bus_speed, address_size
where slave_address is the address (eg, %10100000);
bus_speed is the keyword i2cfast (400kHz) or i2cslow
(100kHz); and address_size is the keyword i2cbyte or
i2cword as appropriate.
All the bits are in the package; you just have to assemble
the kit according to the instructions.
Parameter 4 – Page Write Buffer: all EEPROM memory
chips require a “write time” to save the data in the chip.
This is typically 5 or 10ms. When writing lots of data,
this can cause a significant delay. To help overcome this
issue, many ICs have a page write buffer that can accept
more than one byte at once (typically 8, 16 or 32 bytes)
so that all these bytes can be programmed at once. This
means, for instance, in the case of 8 bytes that you only
have one 10ms delay, rather than an 80ms delay.
Note that page writes can only start at a multiple of
the buffer size; they must not overflow the page buffer
size. In effect, this means (for an 8-byte buffer) that you
can write 8 bytes starting at address 0 (or 8 or 16, etc)
writei2c: the writei2c command is used to write data to
the slave. The syntax is:
writei2c start_address,(data,data,data,data…)
where start_address is the start address (byte or word as
appropriate); and data is bytes of data to be sent (either
fixed values or variable contents). Multiple bytes of
data can be sent at once but care should be taken not to
exceed the page buffer size.
readi2c: the readi2c command is used to read data back
from the slave into variables in the PICAXE. The syntax
is:
readi2c start_address,(variable, variable,…)
where start_address is the start address (byte or word as
appropriate); and variable is where the returned data is
stored in the master (b0, b1, b2, etc)
Example
To write the text “hello” (actually five bytes of data –
one byte for each letter) to a 24LC16B memory IC and
Fig.3: here’s how the light and temperature sensors connect to the PICAXE micro.
74 Silicon Chip
www.siliconchip.com.au
Table 2: EEPROM Comparison Chart
Device
Registers
Buffer
Slave
Speed
Address
24LC01B
128
8
%1010xxxx
i2cfast (400kHz)
i2cbyte
24LC02B
256
8
%1010xxxx
i2cfast (400kHz)
i2cbyte
24LC04B
512
16
%1010xxbx
i2cfast (400kHz)
i2cbyte
24LC08B
1k (1024)
16
%1010xbbx
i2cfast (400kHz)
i2cbyte
24LC16B
2k (2048)
16
%1010bbbx
i2cfast (400kHz)
i2cbyte
24LC32A
4k (4096)
32
%1010dddx
i2cfast (400kHz)
i2cword
24LC65
8k (8192)
64
%1010dddx
i2cfast (400kHz)
i2cword
24LC128
16k (16,384)
64
%1010dddx
i2cfast (400kHz)
i2cword
24LC256
32k (32,768)
64
%1010dddx
i2cfast (400kHz)
i2cword
24LC512
64 (65,536)
128
%1010dddx
i2cfast (400kHz)
i2cword
b = block address (internal to EEPROM); d = device address (configured by external pins A2, A1, A0); x = don’t care
then read it back into variables, the program would be:
i2cslave %10100000, i2cfast, i2cbyte
writei2c 0,(“hello”)
pause 10
readi2c 0,(b0,b1,b2,b3,b4)
‘set slave parameters
‘write the text
‘wait 10ms write time
‘read the data back again
Many projects involve the storage of data. This may
be data collected during a datalogging experiment or
preconfigured data built into the circuit at the time of
build (eg, messages in different languages to be displayed on an LCD screen). The PICAXE chips can generally store 128 or 256 bytes of data internally but some
projects may require much more than this, and so an
external memory storage IC is required.
External EEPROM (Electrically Erasable Programmable Read-Only Memory) ICs can be used to store large
amounts of data. Most EEPROMs store data in “blocks”
of 256 registers, each register storing one byte of data.
The simplest EEPROMs may only have one block of 256
registers, while more expensive EEPROMs can have up
to 256 blocks, giving a total of 256 x 256 = 65,536 (64k)
memory registers.
The 24LCxx series EEPROMs (see Fig.2) are probably the most commonly used I2C EEPROM devices.
Many manufacturers make these parts but we will only
consider Microchip brand parts in this article because
these tend to be readily available via mail order catalogs. These EEPROMs can be written to over one million times and the EEPROM also retains data when the
power is removed. Pin 7 of the IC is a write-enable pin
that can prevent the data being corrupted (keep the pin
high to prevent data being changed). Often, this pin is
connected to a microcontroller pin, so that the microcontroller can control when data can be written (pull
pin low to enable writes).
The cheapest EEPROMs (eg, Microchip parts ending
in the letter “B”) only use a single byte register address,
which by definition can only uniquely identify 256 registers. This means that the various blocks (if they exist)
must be identified in a different way. The 24LC16B has
eight blocks, the other EEPROMS have less (see Table 2).
www.siliconchip.com.au
The way these cheap EEPROMs overcome this address
problem is by merging the block address into the slave
address. This means, in effect, that a single 24LC16B
appears on the I2C bus as eight different “slaves”, each
slave having a unique address and containing 256 registers.
At first glance, this method of addressing seems rather
awkward but it does keep manufacturing costs to a
minimum. The down-side to this simplification is that
only one part can be used per bus (the external IC pins
A2-A0 are not actually physically connected within
Fig.4 – Datalogger Program
main:
high 5
'write protect EEPROM
for b1 = 0 to 59
'start for…next loop
high 3
low 5
'LED green
'write enable
readadc 0,b2
i2cslave %10100000, i2cfast, i2cbyte
writei2c b1,(b2)
pause 10
'read light value from 0
'set block 0 parameters
'write the value
'wait EEPROM write time
readtemp 7,b3
i2cslave %10100110, i2cfast, i2cbyte
writei2c b1,(b3)
pause 10
'read temp value from 7
'set block 3 parameters
'write the value
'wait EEPROM write time
high 5
low 3
'write protect EEPROM
'LED off
pause 60000
next b1
'wait 1 minute
'next loop
stop:
high 2
goto stop
'LED red
'loop forever
January 2004 75
Fig.5: the complete circuit diagram for the datalogger. Although shown here, the DS1307 (IC2) and piezo buzzer (PZ1) are
optional components not supplied with the the basic kit.
these cheaper ‘B’ parts).
The more expensive EEPROMS (24LC32 upwards)
use a word register address and so the block address can
be incorporated within the normal register word address. This means that the EEPROM appears on the I2C
bus as a single slave and so up to eight identical devices
can be connected to the bus by configuring the external
A2-A0 address pins accordingly. Using eight of the commonly available 24LC256 EEPROMs will give a huge
2Mb of memory!
Simple datalogger circuit
The program listing in Fig.4 shows how the 24LC16B
is used in a real-world application – in this case, as part
of the datalogger board. A DS18B20 digital temperature
sensor and a LDR light sensor are read once every minute and the results saved in the 24LC16B EEPROM. A
simplified portion of the datalogger circuit showing how
the sensors are connected is shown in Fig.3.
76 Silicon Chip
Light readings are saved in the first block (000) of the
memory, whereas temperature readings are saved in the
fourth block (011). A for...next loop is used to repeat the
process 60 times, and the for…next loop counter value
(b1) is used as the address to save the data within the
appropriate memory block.
Once the experiment is complete, the stored data
must be retrieved from EEPROM. Normally, this is
achieved by connecting the datalogger to a computer
and uploading the data. With the PICAXE-18X system,
this is easily achieved with the use of a “Wizard” built
into the PICAXE Programming Editor software. This
process is explained in detail later in this article.
Circuit details
Fig.5 shows the full PICAXE-18X Datalogger circuit.
Note that this circuit includes some optional components (eg, the DS1307 real-time clock) that will be covered in future articles. Table 3 shows the input/output
www.siliconchip.com.au
pin arrangement of the PICAXE-18X microcontroller.
Input sensors
The datalogger has four input channels, as follows:
Input 0 is normally used for a miniature light sensor
(LDR – Light Dependent Resistor). The miniature LDR is
connected via the two screw terminals in terminal block
CT6. This input is pre-configured as a potential divider
with a 10kΩ pull-down resistor.
Input 7 is pre-configured for use with a DS18B20 digital
temperature sensor. This is connected via terminal
block CT5. The flat side of the sensor faces down when
connecting the sensor into the terminal block. Digital
temperature sensors give precise readings in degrees
Celsius and so are much more accurate than traditional
thermistor-based circuits.
Inputs 1 and 2 are arranged for connection to your own
sensors (analog or digital). Each input pin and +V and
0V (GND) are connected to terminal blocks CT3 and
CT4. No pull-down resistors are present on the board
and so should be connected externally if required.
Memory
The datalogger is supplied with a single 24LC16B
EEPROM memory chip. This can store 2048 byte readings (eight blocks of 256 bytes). This usually enables
512 readings for each of the four sensors.
If desired, the memory capacity can be increased
by replacing this EEPROM with a 24LC256 EEPROM
(Part No. MIC050). This can store 32,768 bytes of data
(128 blocks of 256 bytes). Details on how to expand the
memory of the system will be covered in a future article.
Fig.6: follow this diagram closely when assembling
the board. Take care with the orientation of the 100µF
capacitor, the three ICs and the two LEDs.
between outputs 2 and 3. Switching output 2 high
and output 3 low will produce a green colour. Switching output 2 low and output 3 high will produce a red
colour.
Table 3: Input/Output Pin Configuration
Analog Input 0
LDR light sensor (CT6)
Power supply
Analog Input 1
Spare sensor input 1 (CT3)
The Datalogger is designed to run from a 3 x AA battery pack (3 x 1.5V = 4.5V with alkaline cells). If using
rechargeable cells, a 4 x AA pack should be used (4 x
1.2V = 4.8V).
The positive (red) wire should be connected to V+
on terminal block connector CT7. The negative (black)
wire should be connected to GND. When connecting the
wires it is recommended that the bare wire is bent back
over the insulation and then the screw tightened on
both. This gives a more secure connection.
If a plugpack is used, it must be a high-quality regulated type, with an output voltage between 4.5V to 5V
DC only. Unregulated plugpacks are unsuitable, as they
generate excessively high output voltages under lightload conditions.
Connection of a higher voltage source (eg, a 9V PP3
battery) or accidentally reversing the power supply connections will damage the ICs and digital temperature
sensor, as there is no on-board voltage regulation.
Analog Input 2
Spare sensor input 2 (CT4)
Digital Input 6
Datalink serial input
Digital Input 7
DS18B20 digital temperature sensor (CT5)
Output 0
Piezo sounder (optional - PZ1)
Output 1
I2C SDA
Output 2
Bi-colour LED (red)
Output 3
Bi-colour LED (green)
Output 4
I2C SCL
Output 5
EEPROM Write Enable (active low)
Output 6
Serial LCD (optional)
Output 7
Datalink serial output
Serial cable connections
The datalogger includes two sockets for connection to
a PC’s serial port via the appropriate cable (PICAXE part
AXE026). Socket CT1 (“Run”) is used for reprogramming the PICAXE chip, whereas socket CT2 (“Datalink”)
is for transferring mission data.
LED outputs
The datalogger has a bi-colour LED (LED2) connected
www.siliconchip.com.au
January 2004 77
Note that the green LED (LED1) is
connected to the square wave output of
the optional DS1307 RTC chip, not the
PICAXE chip. It will automatically flash
on and off every second when the DS1307
chip is inserted (and initialised by the
time/date wizard). Use of the DS1307 RTC
is covered in next month’s article.
Construction
All parts mount on a small, double-sided PC board. Refer to the silkscreen overlay printed on the top side of the board
Fig.7: to retrieve the logged data from EEPROM, you must first download a
as well as the overlay diagram (Fig.6) for
small BASIC program using the Datalink Wizard. This screen shot shows
component placement and orientation.
the Wizard’s default options.
To ease the assembly task, install the
smallest components first. Leave the connectors (CT1 CT8) and battery holder (BT1) until last.
Parts List
Assembly is quite straightforward, with attention to
the following important points:
1 PICAXE-18X Datalogger PC board
• Make sure that you have the notched (pin 1) end of
1 miniature LDR (connects to CT6)
the IC sockets oriented as shown.
1 32.768kHz miniature watch crystal (X1)
• The 2.5mm stereo sockets (CT1 & CT2) have align2 3.5mm stereo sockets (CT1, CT2)
ment pins on the underside that must be ‘clicked’ into
3 3-way terminal blocks (CT3, CT6)
position on the PC board prior to soldering.
2 2-way terminal blocks (CT6, CT7)
• The metal can of the crystal (X1) should be soldered
1 5-way right-angle header socket (CT8)
to the top of the PC board to secure it in position.
1 miniature pushbutton switch (S1)
• IC2, PZ1, BAT1 and CT9 are optional parts not re1 CR2032 cell holder (BAT1)
quired at this stage. Their use will be covered in future
1 3 x AA battery holder
articles.
1 18-pin IC socket
Using the Datalogger
2 8-pin IC sockets
To program the datalogger for a simple mission, begin
Semiconductors
by
launching the Programming Editor software (v3.5.1
1 PICAXE-18X microcontroller (IC1)
or
later).
Make sure that you are in PICAXE-18X mode
1 DS18B20 digital temperature sensor IC (connects
from
the
View
-> Options menu and type in the program
to CT5)
listed
in
Fig.4.
1 24LC16B EEPROM (IC3)
Connect the programming cable to the “Run” socket
1 5mm green LED (LED1)
and
apply power to the module. Now select PICAXE ->
1 5mm bi-colour (red & green) LED (LED2)
Run to download the program into the PICAXE chip.
Capacitors
The program starts immediately after the download,
1 100µF 16V PC electrolytic
recording temperature and light levels at one-minute in2 100nF 63V MKT polyester
tervals. The bi-colour LED flashes green as each reading
is taken. After one hour has elapsed (60 samples), the
Resistors (0.25W 1%)
red half of the bi-colour LED illuminates to indicate the
2 22kΩ
4 4.7kΩ
end of the mission.
3 10kΩ
2 470Ω
Also required (not in kit)
PICAXE Programming Editor Software (v3.5.1 or
later)
PICAXE download cable (Part No. AXE026)
3 x AA alkaline cells
Obtaining kits and software
Note: the design copyright for this project is owned
by Revolution Education Ltd. Complete kits (Part No.
AXE110) for this project are available from authorised
PICAXE distributors – see www.microzed.com.au or
phone MicroZed on (02) 6772 2777.
The PICAXE Programming Editor software can be
downloaded free of charge from www.picaxe.co.uk
or ordered on CD (Part No. BAS805) from your local
distributor.
78 Silicon Chip
Retrieving data from a mission
The Datalink communications utility within the
Programming Editor software is used to retrieve mission
data from the datalogger module. This utility saves the
data in CSV (comma-separated variable) formatted files,
which can later be opened with any spreadsheet application (eg, Microsoft Excel) for further analysis. The
utility also includes the option of automatically drawing
a graph of the data as it is uploaded.
To use the Datalink communications utility, a small
BASIC program must first be running in the PICAXE
microcontroller. This program reads the data from
EEPROM and transmits it (via the Datalink connector
and serial cable) to the computer, where it is processed
by the Datalink software utility.
This BASIC program can be automatically created
www.siliconchip.com.au
There’s no need to change the Wizard’s default options (see Fig.7). Simply click on the “OK” button to
download it to the datalogger module.
Note that the Datalink utility uses the standard
PICAXE programming cable to retrieve the data from the
datalogger module. However, the cable must be inserted
into the Datalink socket, not the PICAXE “Run” socket.
Using the Datalink utility
Wait until the datalogger mission is complete (status
LED red) before using the Datalink utility. When it’s
done, the procedure is as follows:
(1) Download the Datalink program via the Datalink
Wizard.
(2) Connect the PICAXE cable to the Datalink socket on
the datalogger.
(3) Select File -> New within the Datalink Window.
The data will then be uploaded, with the data simultaneously visible on screen. Once the data upload is
complete, choose the File -> Save As menu to save the
data as a CSV-formatted text file.
Summary
Fig.8: after running the Datalink Wizard, open the Datalink utility from the Programming Editor’s main menu (or
press F9). It’s then just a matter of selecting File -> New
and following the prompts to initiate the data upload.
and downloaded to the datalogger using the Datalink
Wizard. From the Programming Editor, select PICAXE ->
Wizard -> AXE110 Datalogger and choose the “Retrieve
Unknown Data” option to launch the Wizard.
www.siliconchip.com.au
The PICAXE system provides a very economical
method of implementing a high specification datalogging system. As the datalogging mission is programmed
by the end user, there is no limit to the function of the
system – the function of the datalogger can be easily
modified and customised as required.
Next month, we will show you how to add a DS1307
SC
real-time clock to the Datalogger!
About the Author
Clive Seager is the Technical Director of Revolution
Education Ltd, the developers of the PICAXE system.
January 2004 79
|