Silicon ChipSerial I/O Controller & Analog Sampler - November 2005 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Broadband over Power Lines will blot out many radio services
  4. Feature: Broadband Over Power Lines: A Flawed Technology by Ross Tester
  5. Feature: Car Sound On The Cheap; Pt.1 by Julian Edgar
  6. Feature: Microbric: Robotics For Everyone! by Ross Tester
  7. Project: Studio Series Stereo Headphone Amplifier by Peter Smith
  8. Project: Build A MIDI Drum Kit by John Clarke
  9. Project: Serial I/O Controller & Analog Sampler by Trent Jackson
  10. Project: Delta XL02 Tower Loudspeaker System by Leo Simpson
  11. Feature: PICAXE In Schools; Pt.5 by Clive Seager
  12. Salvage It: Scrounging and (gulp) buying the bits by Julian Edgar
  13. Vintage Radio: Vintage radio clubs: 15 years old and still going strong by Rodney Champness
  14. Market Centre
  15. Advertising Index
  16. Book Store
  17. Outer Back Cover

This is only a preview of the November 2005 issue of Silicon Chip.

You can view 44 of the 112 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.

Articles in this series:
  • Car Sound On The Cheap; Pt.1 (November 2005)
  • Car Sound On The Cheap; Pt.1 (November 2005)
  • Car Sound On The Cheap; Pt.2 (December 2005)
  • Car Sound On The Cheap; Pt.2 (December 2005)
Items relevant to "Studio Series Stereo Headphone Amplifier":
  • 4-Output Universal Regulator PCB [18105151] (AUD $5.00)
  • PCB pattern for the Studio Series Preamplifier power supply (PDF download) [01109052] (Free)
  • Studio Series Headphone Amplifier PCB [01111051] (AUD $10.00)
  • PCB pattern for the Studio Series Headphone Amplifier (PDF download) [01111051] (Free)
Articles in this series:
  • Studio Series Stereo Preamplifier (October 2005)
  • Studio Series Stereo Preamplifier (October 2005)
  • Studio Series Stereo Headphone Amplifier (November 2005)
  • Studio Series Stereo Headphone Amplifier (November 2005)
  • Studio Series Remote Control Module (April 2006)
  • Studio Series Remote Control Module (April 2006)
Items relevant to "Build A MIDI Drum Kit":
  • PIC16F88-I/P programmed for the MIDI Drum Kit [drumkit16.hex] (Programmed Microcontroller, AUD $15.00)
  • PIC16F88 firmware and source code for the MIDI Drum Kit [drumkit16.hex] (Software, Free)
  • PCB patterns for the MIDI Drum Kit (PDF download) [01211051/2/3] (Free)
  • MIDI Drum Kit front panel artwork (PDF download) (Free)
Articles in this series:
  • Build A MIDI Drum Kit (November 2005)
  • Build A MIDI Drum Kit (November 2005)
  • Build A MIDI Drum Kit; Pt.2 (December 2005)
  • Build A MIDI Drum Kit; Pt.2 (December 2005)
  • Build A MIDI Drum Kit; Pt.3 (January 2006)
  • Build A MIDI Drum Kit; Pt.3 (January 2006)
  • Build A MIDI Drum Kit; Pt.4 (February 2006)
  • Build A MIDI Drum Kit; Pt.4 (February 2006)
Items relevant to "Serial I/O Controller & Analog Sampler":
  • PIC16F877A-I/P programmed for the Serial I/O Controller [IO.HEX] (Programmed Microcontroller, AUD $20.00)
  • PIC16F877A firmware (HEX), source code and PC software for the Serial I/O Controller (Free)
  • PCB pattern for the Serial I/O Controller (PDF download) [07111051] (Free)
Items relevant to "PICAXE In Schools; Pt.5":
  • PICAXE-08M BASIC source code for "PICAXE in Schools", part 5 (Software, Free)
Articles in this series:
  • What’s this? Free PC Boards for Schools? (May 2005)
  • What’s this? Free PC Boards for Schools? (May 2005)
  • PICAXE In Schools, Pt.2 (June 2005)
  • PICAXE In Schools, Pt.2 (June 2005)
  • PICAXE In Schools, Pt.3 (July 2005)
  • PICAXE In Schools, Pt.3 (July 2005)
  • PICAXE In Schools, Pt.4 (September 2005)
  • PICAXE In Schools, Pt.4 (September 2005)
  • PICAXE In Schools; Pt.5 (November 2005)
  • PICAXE In Schools; Pt.5 (November 2005)

Purchase a printed copy of this issue for $10.00.

By TRENT JACKSON S erial I/O controller & analog sampler Looking for an easy-to-drive I/O controller? This unit connects to the serial port of your PC and can be programmed to switch relays, dependent on voltage, resistance, temperature and digital inputs. It also includes comprehensive system timers to control the relays and you can set the system up to operate as a PLC-style controller. PC-based serial I/O controllers and analog samplers are hardly new. However, this 10-bit unit has some special features that are normally hard to come by in a DIY unit, including closed loop control (as in a thermostat), spreadsheet logging, programmable I/O logic control, and temperature and light 70  Silicon Chip sensing. It also includes real-time system timers that can be used to control two on-board relays. Speaking of relays, you can also define “less than” or “greater than” values in the software to control them. This can be done for any of the input variables – temperature, LDR resist- ance, analog voltage inputs and digital inputs. For example, you could set one of the relays to switch on if the temperature goes above say 20°C and this could then control a fan or some other item of equipment. In short, there are lots of possibilities, especially as the unit can also be directly interfaced to other CMOS circuits. You can also quite easily create your own software to control the heart of this project which is (of course) a microcontroller – in this case a PIC! This particular PIC is a 40-pin 16F877A “power plant”, boasting 8K of flash memory, 256 bytes of RAM, eight analog inputs, 256 bytes of EEPROM memory and lots of other features. In operation, the PIC communicates with your PC via a serial port. You can siliconchip.com.au a preset period ranging from 1-999 seconds. In effect, it’s basically a mini low-speed oscilloscope – see Fig.1. The output port can be directly written to by clicking the D0-D9 output lines on the software interface (to set the data value) and then clicking the <Write Data To Port> button. Alternatively, the data value can be entered by directly typing it in, in either decimal or hexadecimal format. It’s the PC software that really makes this project. However, this software would be of little use without the PIC microcontroller – it generates all the analog and digital ports and converts all the data on these ports into a serial data stream for the PC. Logging as well Fig.1: the Serial I/O Controller is controlled by Windows-based software. This is the main GUI (graphical user interface) – check out “The PC Software At A Glance” section for a rundown on all the functions. Features & Specifications Rugged 10-Bit Digital Input Port (0-16V) Temperature Accuracy: ±1°C 0-5V & 0-25V Analog Voltage Inputs On-Board System Timers Plus Buzzer Temperature & Light Sensing (LDR) Inputs 32-bit Windows-Based Software High-Current 10-Bit Digital Output Port Full Function SpreadSheet Logger Closed Loop Control Using Two Relays CRO-Style Analog Graph Plotter Serial Interface (2400 bits/s, Inverted) Combination I/O “AND OR” Logic connect it to the PC using either a serial cable or a USB-to-serial adaptor. It’s worth noting that there are not many DIY serial-based 10-bit I/O controllers “out there”, most being parallel port-based. The downside to using the serial port is that it is a bit slower. However, given the fact that all data is updated about four times a second, it isn’t all that bad. Faster speeds can be obtained if you click on the “Dedicated Fast Update” option in the software. In this mode, the variable that you select is updated at a rate of about 20 times a second. Once again, any of the data elements can use this mode, including temperature, analog inputs, digital inputs and LDR resistance value. The serial rate is 2400 bits/s, which is fast enough for siliconchip.com.au the job and works reliably. The system can also be set up to function as a “PLC-style” controller. You can program it via the accompanying Windows software to accept a certain decimal value from the input port and in turn write a pre-defined value to the output port. With 1024 combinations to play with, there’s lots of logic control that can be used for your applications. The input values, along with temperature and other analog values, can be assigned to control either of the two onboard relays. A buzzer is also included – eg, to sound a warning when certain preset thresholds are exceeded. Also included is an analog plotter. This can be set to plot a graph of the data element that you select over The data logging side of things can handle up to 1000 samples. You can set the sampling time anywhere from 1-999 seconds, which means that this unit is suitable for long-term data acquisition. All analog inputs have 10-bit resolution, which equates to a step size of about 5mV (ie, 5/1023). As a result, the temperature readout is capable of displaying 0.5°C changes. The first analog input has a range of 0-5V and the second 0-25V (with a resolution of 25mV). Provision has also been made for measuring resistance and there is an on-board LDR that changes resistance according to the light level present. You could replace this LDR with some other resistive device if desired. As stated previously, thresholds for this variable can also be set in the software to control the relays. Circuit details Fig.2 shows the block diagram of the I/O Controller, while Fig.3 shows the full circuit details. The circuit is dominated by the PIC16F877A microcontroller, which is clocked at 4MHz using crystal X1 and two 22pF loading capacitors. This particular microcontroller was chosen mainly for its pin count, rather than for its 8K of program code space. In fact, the code occupies a mere 1K of memory for this project! All but two pins on the 40-pin PIC micro are used, including four for A/D inputs, 20 for I/O lines, two for serial data transmission, two to control the relays, one for a buzzer and two for the TX/RX lines. Of course, November 2005  71 and RC7 (pins 25 & 26). At all times, the software in the PC calls the shots. The PIC micro “sits back” and waits for a command on the serial port with the format “Sync Byte, Function” – see the Control Code panel for further details This makes it very easy for custom software to be developed for this project and in fact, the whole idea was for it to be as universal as possible. A complete list of all the function commands is provided with this article. Other circuits could easily be adapted to interface to this controller, including PICAXE circuits. The microcontroller software for this project was written using PIC Basic Pro, which is a true compiled high level BASIC language with similar commands to the PICAXE. The source code will be available as a free download from the SILICON CHIP website at www. siliconchip.com.au. Fig.2: the PIC microcontroller (IC1) dominates the hardware side of the I/O Controller. It generates all the analog and digital ports and converts all the data on these ports into a serial data stream for the PC. this could all have been done with (say) an 18-pin PIC16F628 plus a few discrete ICs. However, the 16F877A microcontroller has a price tag of just $10, so why bother? As shown in Fig.3, the output port consists of two ULN2003 Darlington open-collector driver ICs (IC3 & IC4), along with 10 LEDs (LEDs5-14) and their associated 330W current-limiting resistors. The LEDs draw about 10mA each, which is well within the capabilities of the ULN2003 drivers. In fact, these drivers can provide sink currents of up to 500mA per line (although this must be derated when more than one line is active), so there’s still plenty of “headroom” to connect your own “goodies” to this port. The LEDs hanging off this port actually have two functions. First, they give an obvious indication as to the status of the data lines (ie, which bits are set). And second, they act as pullups for the open collector outputs of IC3 & IC4. As a result, this port can be directly interfaced with other logic circuits (both TTL and CMOS). Note that because of the LEDs, the outputs are no longer open collector. 10-bit input Want a 10-bit input as well? No 72  Silicon Chip problem – that’s provided by lines D0D9 on ports RD0-RD7 and RC0-RC1. These lines feature zener diode clamping (ZD1-ZD10), which means that up to 16VDC can be applied. In practice, 3.5-16V is the valid range for a logic high, while 0-2V is the valid range for logic lows. Values between these two ranges can be read as either high or low, depending on where the threshold is. As shown, a 1kW resistor, a 100nF capacitor, a 5.1V zener diode and a 100kW pull-up resistor are connected to each input line. The zener diodes clamp input voltages that are greater than 5.1V, while the 100nF capacitors and associated 1kW resistors act as filters. The 100kW resistors have two functions. First, they act as pull-down resistors when the input lines are floating and second, they discharge the 100nF capacitors when the logic states change, so that the RC time-constants remain valid. Data communication is achieved via the serial port and an RS232 interface based on IC2, a MAX232 serial data buffer. The external transmit (TX) and receive (RX) signals are on pins 13 & 14 of IC2 respectively, while the PIC micro communicates via ports RC6 A/D converters Four out of the eight available 10bit A/D converters inside the PIC are used in this circuit – one for sensing temperature via an LM335Z precision temperature sensor, one for measuring the resistance of an LDR (or some other variable resistor) and two for measuring the 0-5V and 0-25V analog voltage inputs. The relevant inputs are ports AN0AN3 on the microcontroller (pins 2-5). The LM335Z temperature sensor is connected to AN0, the LDR to AN1, the 0-5V analog input to AN2 and the 0-25V analog input to AN3. All calibration for these measurements is done via the Windows software provided. For example, temperature measurements are accurate to ±1°C after proper calibration. Considering that the A/D converter is 10-bit and the LM335Z output varies by 10mV/°C , this figure is to be expected. Calibration for the 0-25V input is achieved using both the software and a 100kW trimpot (VR1). This trimpot, and its associated 330kW resistor function as an adjustable voltage divider. In practice, VR1 is adjusted to provide a division ratio of 5:1. The best way to do this is to apply a precise 12.5V to the input, run the PC software and adjust VR1 until you read 12.5V on the screen. Further fine tuning can then be done via software. Properly calibrated, this input is siliconchip.com.au siliconchip.com.au November 2005  73 Fig.3: this is the full circuit diagram of the Serial I/O Controller. The PIC microcontroller (IC1) accepts the analog and digital inputs and generates outputs to drive the relays (via Q1 & Q2) and the digital output port (via Darlington arrays IC3 & IC4). It also interfaces with the serial port via a MAX232 serial data buffer (IC2). THE PC SOFTWARE AT A GLANCE T HE SOFTWARE for the I/O Controller has been tested with Windows XP and Windows 98SE but should also work with Windows 95, Windows 98 and Windows 2000. It has four main interfaces: (1) the main GUI (designated Multi Function IO Controller); (2) Data Logging; (3) IO Logic Control; and (4) Alarms & Timers. The last three are accessed via the main interface by clicking on the menu items. Most of the control is retained within the main GUI. This has a host of options which range from displaying the values of all the data elements, a graphical analog plotter, closed loop control, and input and output data control. In addition, there are a number of command buttons in the bottom righthand corner that perform various tasks (all of which are selfexplanatory). The first step in getting it working is to find an available Comm (serial) port on your PC and click the “Connect” button”. A communications link will then be established. After that, it’s simply a matter of setting up the system to perform the required task. The accompanying breakout boxes give further details. THE INPUT VALUES (ie, Temperature, LDR Resistance and Analog Input 1 & 2 voltages) are clearly displayed here. DATA CAN BE WRITTEN to the 10-bit output port by clicking on the D0-D9 “LEDs” or by entering in a decimal or hex value and then clicking the <Write Data To Port> button. CLOSED LOOP CONTROL: this section lets you control either relay 1 or relay 2 (or both), according to an input variable (voltage, resistance or temperature). Here, for example, relay 1 has been set up to switch on if Analog Input 1 is less than 2.75V and to switch off if Analog Input 2 goes above 3.5V. Similarly, relay 2 switches on if the temperature is less than 20°C and off if the temperature rises above 22°C (ie, it could be used as a thermostat). However, you have to be careful not to overlap conditions – eg, setting relay 1 to switch on of Analog Input 1 is greater than 3V and off if it goes above 2V would cause erratic operation by toggling the relay on and off continuously. Note that all data is automatically saved when you exit the software. 74  Silicon Chip THIS SECTION lets you select which input variable to plot. Clicking the down arrow lets you choose which input variable to use for closed loop control. ANALOG CRO-STYLE GRAPH PLOTTER: this nifty little window shows the history of any input variable that you select by plotting a graph. You can define the update rate anywhere between 1-999 seconds. Alternatively, for faster speeds, you can select the fast update mode option that’s located right at the bottom of the window. In this mode, the plotter is updated at about 20 times a second. Make sure you remember to enable this function by placing a tick in the “ON” box. Plotter scaling is fully automatic for each element. siliconchip.com.au DATA LOGGING: you name it you can log it – Temperature, LDR resistance, Analog Input channel 1 or 2, 10-bit input data . . . the choice is yours! What’s more, you can set the logging interval from 1-999 seconds and up to 1000 individual logs can be recorded. There’s also a facility to automatically save logged data at a specified interval, to retain data in the event of power failure. The “auto-log” file is stored in the program’s applications folder and can be opened in any text editor. Other features include: save, open and print capabilities; logging with or without date or time; the ability to auto-clear the log sheet after 1000 samples have been recorded; and the ability to change the input variable on the fly. The log sheet scrolls automatically as new data is logged. Once the log is full, a red “LED” flashes to indicate that you need to save or clear the log so that new data can be recorded – just click on the “Clear Log” button or select “Start New” under the logger menu. THE COMBINATION IO LOGIC CONTROL dialog allows you to set the unit up to function as PLC-style controller. To assign values, just type the data in (in decimal or hex format), or click on the data lines. Do this for the input & output values and click on “Add To List”. You can save, open and print all logic scripts. And there are a few options that allow you to manipulate the logic – invert, XOR and latch. With latching, the output remains in its last state until a new input condition has been detected. Without latching enabled, the output port will be cleared once the input value has been removed. After you create or open a script, be sure to enable it by ticking the “Enable Logic Script” box. When a script is active, a virtual red “LED” next to the SILICON CHIP logo flashes. The response time is about 250ms for normal update all data mode and 100ms for dedicated fast update sampling mode. THE ALARMS & TIMERS INTERFACE has a vast array of timer parameters & buzzer alarm settings, most of them self-explanatory. The main thing to keep in mind is that the time format for the timers is 24-hour mode and must include all digits – ie, trailing zeros. All settings are automatically saved to a configuration file each time the program closes. These settings are then automatically loaded each time the program executes at start up. Deselecting the “Use Date” option allows the timers to be cycled on a daily basis (rather than just the programmed dates). The buzzer alarm conditions are located at the very bottom and you can use any data element you wish. There are three separate conditions – make sure that none of them overlap. siliconchip.com.au November 2005  75 Fig.4: install the parts on the PC board as shown here but don’t install the PIC microcontroller until after the power supply has been tested (see text). Take care to ensure that all polarised parts are correctly oriented and be sure to take the usual precautions against static electricity when handling the ICs. Note that the relays are capable of switching low voltages (up to about 50V DC) only. Table 1: Resistor Colour Codes o o o o o o o o o o o No. 1 1 1 11 3 3 1 10 13 1 76  Silicon Chip Value 470kW 390kW 330kW 100kW 10kW 4.7kW 2.2kW 1kW 330W 180W 4-Band Code (1%) yellow violet yellow brown orange white yellow brown orange orange yellow brown brown black yellow brown brown black orange brown yellow violet red brown red red red brown brown black red brown orange orange brown brown brown grey brown brown 5-Band Code (1%) yellow violet black orange brown orange white black orange brown orange orange black orange brown brown black black orange brown brown black black red brown yellow violet black brown brown red red black brown brown brown black black brown brown orange orange black black brown brown grey black black brown siliconchip.com.au very stable and quite accurate. In fact, you could use it as a second meter for measuring DC voltages up to 25V. Diodes D4-D7 provide over-voltage protection for each of the four input channels (within reason). They do this by clamping the input voltage to the supply rail if it rises above 5.6V. The 100nF MKT capacitors on the inputs are rated at 100V, while the input impedance is a respectable 390kW. Basically, the two analog inputs should be able to cope with inputs up to about 50V DC. Diodes D4-D7 also eliminate any positive or negative-going spikes due to pulsed DC inputs. ADCs in PIC micros aren’t very tolerant when it comes to glitches on the input being measured, so it’s necessary to eliminate these. Ports RE0 & RE1 on IC1 are used to control the on-board relays via buffer transistor stages Q1 & Q2. A 1N4001 diode is included across each relay coil to protect the driver transistors from any back-EMF that may be generated when the relays switch off. LEDs 1 & 2 indicate the status of the relays – ie, LED1 is on when Relay1 is on and LED2 is on when Relay2 is on. Port RE2 is used to switch the buzzer via transistor buffer Q3. This buzzer can be used to warn of certain conditions, as set via the software. For example, it could be set to turn on if the ambient temperature rises above a certain level. Alternatively, it could by programmed to sound if the analog voltage reading on Ch1 falls below a critical threshold. It could even be set up so that it sounds only when a combination of two or more variable conditions are breached. Port RA4 is used to pulse LED3 to indicate TX/RX activity. This occurs in real-time and if this LED isn’t flashing, then it’s likely that there’s no communications link between the controller board and the PC. It doesn’t tell you where the fault is though – it could be on either side (or on both). Temperature sensing Let’s now see how the LM335Z temperature senor operates. At 0°C, this device produces 2.73V between its centre pin and ground. This voltage increases by 10mV for each 1°C temperature increase. As shown, the LM335Z is forwardbiased via a 4.7kW resistor, which siliconchip.com.au Fig.5(a): here’s how to connect a relay to any of the 10 digital output lines (D0-D10). The diode must be soldered directly across the relay terminals (be sure to get it the right way around). Note that you must remove the indicator LED from the output line (otherwise the reverse voltage rating of the LED will be exceeded). Fig.5(b): if the current required to drive the relay is more than the ULN2003 can handle, a transistor buffer can be added as shown here. This circuit will switch at least 500mA. Note that you must remove the indicator LED from the output line (otherwise the reverse voltage rating of the LED will be exceeded). Both these circuits can be powered from an external 12V regulated plugpack supply. ensures enough current to produce a reasonably accurate measurement. A 100pF capacitor and 470kW resistor filter the output voltage from the sensor, which reduces toggling of the least significant bit that is common to most ADCs – ie, when the voltage level is right on the threshold of a step. This effect is further cancelled out via the software. The calibration can be tweaked by changing the value of the 470kW resistor. A higher value will increase the temperature reading and vice versa. If you play around with the calibration enough, you could probably obtain ±0.5°C accuracy. provide some hysteresis across the 10kW resistor. Note that this input can be calibrated as well and as mentioned earlier, you can use it to measure other resistive devices as well. Be careful though – there’s no input protection. Light sensing Construction LDR1 performs the light sensing operation. It’s wired in parallel with a 100kW resistor, to bring its low-light level value down to around 90kW. The resulting voltage across the series 10kW resistor is proportional to the light level and this is applied to port AN1. In the software, the 10kW resistor is treated as part of a voltage divider. Once the voltage across it is known, the LDR’s resistance can be calculated to give a value in ohms (the parallel 100kW resistor is taken into consideration for this calculation). The two 1mF tantalum capacitors connected to AN1 ensure stability and Construction is quite straightforward, with all parts mounted on a single PC board coded 07111051 (161 x 160mm). Fig.4 shows the assembly details. Begin by installing the 34 wire links. Some of these are quite long and are close together, so make sure they don’t short together. Note also that two of the links go under the microcontroller (IC1). Once these are in, install the resistors in the locations shown. Table 1 shows the resistor colour codes but you should also check each value on your multimeter before installing it. Follow these with Power supply Power for the circuit is derived from a 9-15V DC supply (eg, a plugpack), with diode D1 providing reverse polarity protection. This feeds regulator REG1, which delivers a +5V supply. The associated 220mF capacitor on the input and the 10mF and 100nF capacitors on the +5V rail provide filtering and decoupling. November 2005  77 CONTROL FUNCTION CODES W ANT TO WRITE your own control programs? Here’s how a basic rundown on how the control codes work, together with a list of all the codes that control the system. During normal operation, the PIC “sits back” and waits for a command. When a valid command is received, it’s immediately executed and the PIC then goes back to its main internal loop and waits for another command to be issued (Fig.7). The first byte of data that the PIC expects to receive is an “a” in the data string. This “a” is the sync byte and is commonly used to indicate the start of a string. It also greatly reduces errors in the transmission, while also reducing the amount of code FUNCTION COMMAND STRING required in reading the actual data. Read Temperature “a” + CHR$(1) As stated in the article, it’s easy to interface other circuits to this controller. Read LDR “a” + CHR$(2) Due to its simplicity, you could even use Read Analog Ch1 “a” + CHR$(3) an 8-pin PICAXE micro to control it, in turn giving a huge number of I/O pins to Read Analog Ch2 “a” + CHR$(4) play with. Toggle Relays “a” + CHR$(5) The first step is to issue the controller Activate Buzzer “a” + CHR$(6) with a command string, telling it what function you want processed. Depending on Read 10-Bit Inputs “a” + CHR$(7) the function, you then issue another comWrite Port Data “a” + CHR$(8) mand to define the action to be taken. For example, if you want the “Read Temperature” function, then 50ms after you issue this command, it will to read the voltage at the output of the LM335Z temperature sensor. Following A/D conversion, this value will then be output as a serial data string to the serial port. On the other hand, if you issue the “Toggle Relays” command, it will then wait for another command to tell it which relay to toggle (Relay1 or Relay2), followed by the state of the relay – ie, ON or OFF. In the case of a data string, it’s either a 0 or a 1. After A Command Is Sent Now let’s see what happens after a command has been sent. We’ll take each command in turn. READ Temperature Command – at least 50ms after this command has been issued, the following string will be sent out: [“a” + #VCC Step Value + “<at>”] As previously mentioned, the “a” is the sync byte and – in the case of the controller talking back – it also sends out an end of string sync byte. The #VCC Step Value will be a decimal number ranging from 0-1023 which equates to 10-bit binary. At the receiving end, you will need to convert this value into temperature (as in the PC software provided). The analog converters are all 10-bit, so the step size is 4.887mV. The decimal value represents the number of steps. The best way to go about converting these values is to use tables rather than calculations. Temperature, LDR, Analog Channel 1 & 2, Input Port Commands – at least 50ms after this command has been issued, the following string will be sent out: [“a” + #Decimal Value + “<at>”] Toggle Relays Command – at least 50ms after you have issued the select function command, you must then issue this string to define which relay is to be used and its state (ON or OFF): [“a” + CHR(Relay Number) + CHR(State)] Note that the “Relay Number” variable must be in the range of 1-2, while the “State” variable must be either a 1 or 0 (all in ASCII). Activate Buzzer Command – at least 50m after you have issued the select function command, you must then issue this string defining the state of the buzzer (ie, ON or OFF, 1 or 0): [“a” + CHR(State)] Write Port Data Command – at least 50ms after you have issued the select function command, you must then issue this string defining the value of the output port (range 0-1023): [“a” + CHR(Port Value)] 78  Silicon Chip Fig.5: the calibration dialog lets you enter offset values, to accurately calibrate the four analog input channels. Once you enter in an offset, the related element is updated instantly. You then click on “OK” to save these settings to disk, which are automatically restored the next time you run the program. the diodes and LEDs, taking care to ensure these parts are installed with the correct polarity. The capacitors can go in next, again taking care with the polarity of the tantalum and electrolytic types. These can then be followed with the two miniature relays, the buzzer, the screw terminal blocks and the IC sockets. Now install the three BC548 transistors (Q1-Q3), followed by the LM335Z temperature sensor. This sensor comes in a similar package to the transistors so don’t get them mixed up. The LM7805 regulator is mounted with its metal tab flat against the PC board. To do this, first bend its leads down through 90° about 4mm from its body, then secure it to the board using an M3 x 6mm machine screw and nut and solder its leads. The 4MHz crystal is also mounted flat against the PC board – just bend its leads through 90° and push it all the way down onto the board before soldering its leads. It’s then secured in place using a wire loop which is soldered at either end to the PC board and also to the top of the metal case (this also connects the case to ground). Trimpot VR1 and the LDR can now be installed. The latter can be mounted about 10mm proud of the PC board, with its leads sleeved in spaghetti insulation so that they don’t short together. That done, you can install the three 16-pin ICs into their sockets, making sure the MAX232 chip is siliconchip.com.au Par t s Lis t 1 PC board, code 07111051, 161 x 160mm 11 PC-mount 3-way screw terminal blocks, 5mm spacing 1 PC-mount 2-way screw terminal block, 5mm spacing 2 SPDT PC-mount 5V DC Mini relays 1 mini PC-mount piezo buzzer 1 100kW horizontal trimpot (VR1) 1 light dependent resistor (LDR1) (Jaycar RD-3480, DSE Z-4801, Altronics Z-1619, or equivalent) 1 4MHz crystal (X1) 1 PC-mount DB9F connector 1 serial cable 1 500mm-length tinned copper wire (for links) 3 16-pin IC sockets 1 40-pin IC socket 5 M3 x 6mm screws 1 M3 nut 4 M3 x 12mm tapped spacers 1 set of self-adhesive labels Fig.7: this is the basic flowchart for the PIC software. In operation, the instructions are executed sequentially, after which the microcontroller returns to the “Main Wait For Command Loop”. used for IC2. Check that these devices are all oriented correctly and be sure to observe the usual precautions to prevent damage from static electricity. Initial checks All parts should now be in place except for the PIC microcontroller (IC1) – that’s left until the power supply has been checked out. First, check the assembly carefully to ensure that all parts are in their correct locations and that all polarised parts are correctly oriented. That done, apply power and check the voltage at the output terminal of the 7805 regulator – you should get a reading that’s close to +5V with respect to ground. This same voltage should also be present on pins 11 & 32 of IC1’s socket. If all is well, switch off and install siliconchip.com.au IC1 (notched end towards IC3 & IC4). You’re now ready to put your new I/O Controller through its paces. Don’t forget to set VR1 as described earlier. To drive the system, you can either use the Windows-based software or you can write your own control programs. The Windows-based software is easy to drive – just take a look at the accompanying screen grabs and the explanatory notes. For those who wish to write their own software, the control codes are listed in a separate panel. Finally, note that the on-board relays are suitable for switching lowvoltages only – eg, up to about 50V DC. DO NOT use them to switch mains voltages (ie, 240VAC). The board is not designed to do this and it would SC be much too dangerous. Semiconductors 1 PIC16F877A microcontroller programmed with IO.hex (IC1) 1 MAX232 serial transceiver (IC2) 2 ULN2003 Darlington arrays (IC3, IC4) 3 BC548 NPN transistors (Q1-Q3) 1 LM335Z temperature sensor (TS1) 1 LM7805 voltage regulator (REG1) 3 1N4001 diodes (D1-D3) 4 1N914 diodes (D4-D7) 10 5.1V 0.5W zener diodes (ZD1-ZD10) 2 5mm red LEDs (LED1, LED3) 2 5mm green LEDs (LED2, LED4) 10 3mm red LEDs (LED5-LED14) Capacitors 1 220mF 25V electrolytic 6 10mF 16V electrolytic 2 1mF 16V tantalum 16 100nF 100V MKT polyester 1 100pF 50V ceramic 2 22pF 50V ceramic Resistors (0.25W, 1%) 1 470kW 4 4.7kW 1 390kW 1 2.2kW 1 330kW 10 1kW 11 100kW 13 330W 2 10kW 1 180W November 2005  79