Silicon ChipCrazy Cricket Or Freaky Frog - June 2012 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: What's next on the automotive wish list?
  4. New Microcontrollers: Feature-Laden, Fast & Furious by Nicholas Vinen
  5. Review: WiNRADiO Excalibur WR-G31DDC HF Receiver by Maurie Findlay
  6. Project: Crazy Cricket Or Freaky Frog by John Clarke
  7. Project: Wideband Oxygen Sensor Controller Mk.2, Pt.1 by John Clarke
  8. Project: Mix-It: An Easy-To Build 4-Channel Mixer by Nicholas Vinen
  9. Project: PIC/AVR Programming Adaptor Board; Pt.2 by Nicholas Vinen
  10. Review: Agilent’s 35670A Dynamic Signal Analyser by Allan Linton-Smith
  11. Vintage Radio: John de Hass & his Philips vintage radio collection by Rodney Champness
  12. PartShop
  13. Order Form
  14. Market Centre
  15. Advertising Index
  16. Outer Back Cover

This is only a preview of the June 2012 issue of Silicon Chip.

You can view 28 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 "Crazy Cricket Or Freaky Frog":
  • Crazy Cricket/Freaky Frog PCB [08109121] (AUD $10.00)
  • PIC12F675-I/P programmed for the Crazy Cricket/Freaky Frog [0810912A.HEX] (Programmed Microcontroller, AUD $10.00)
  • Firmware (ASM and HEX) files for the Crazy Cricket/Freaky Frog project [0810912A] (Software, Free)
  • Crazy Cricket/Freaky Frog PCB pattern (PDF download) [08109121] (Free)
Items relevant to "Wideband Oxygen Sensor Controller Mk.2, Pt.1":
  • Wideband Oxygen Controller Mk.2 Display PCB [05106122] (AUD $7.50)
  • Wideband Oxygen Controller Mk.2 PCB [05106121] (AUD $15.00)
  • PIC16F1507-I/P programmed for the Wideband Oxygen Sensor Controller Mk2 [0510612A.HEX] (Programmed Microcontroller, AUD $10.00)
  • PIC16F88-E/P programmed for the Wideband Oxygen Sensor Controller Mk2 Display [0510612B.HEX] (Programmed Microcontroller, AUD $15.00)
  • Firmware (ASM and HEX) files for the Wideband Oxygen Sensor Controller Mk2 [0510612A/B] (Software, Free)
  • Wideband Oxygen Controller Mk.2 Display PCB pattern (PDF download) [05106122] (Free)
  • Wideband Oxygen Controller Mk.2 PCB pattern (PDF download) [05106121] (Free)
  • Wideband Oxygen Controller Mk.2 panel artwork (PDF download) (Free)
Articles in this series:
  • Wideband Oxygen Sensor Controller Mk.2, Pt.1 (June 2012)
  • Wideband Oxygen Sensor Controller Mk.2, Pt.1 (June 2012)
  • Wideband Oxygen Sensor Controller Mk.2, Pt.2 (July 2012)
  • Wideband Oxygen Sensor Controller Mk.2, Pt.2 (July 2012)
  • Wideband Oxygen Sensor Controller Mk.2, Pt.3 (August 2012)
  • Wideband Oxygen Sensor Controller Mk.2, Pt.3 (August 2012)
Items relevant to "Mix-It: An Easy-To Build 4-Channel Mixer":
  • Mix-It! 4 Channel Mixer PCB [01106121] (AUD $15.00)
  • Mix-It! 4 Channel Mixer PCB pattern (PDF download) [01106121] (Free)
Items relevant to "PIC/AVR Programming Adaptor Board; Pt.2":
  • PIC/AVR Programming Adaptor Board PCB [24105121] (AUD $20.00)
  • PIC/AVR Programming Adaptor Board PCB pattern (PDF download) [24105121] (Free)
Articles in this series:
  • PIC/AVR Programming Adaptor Board; Pt.1 (May 2012)
  • PIC/AVR Programming Adaptor Board; Pt.1 (May 2012)
  • PIC/AVR Programming Adaptor Board; Pt.2 (June 2012)
  • PIC/AVR Programming Adaptor Board; Pt.2 (June 2012)

Purchase a printed copy of this issue for $10.00.

Crazy Cricket . . . or Freaky Frog! Love the sound of crickets and frogs (and who doesn’t)? Maybe you will revise your judgement after exposure to Crazy or Freaky – the (very) pesky cricket and equally annoying grenouille. D esigned to imitate the chirping noise of a cricket or the gentle croaking of a frog, Crazy/Freaky loves to sing in the dark and happily chirps/croaks away, much to the annoyance of others. When disturbed by light, he immediately shuts up, remaining stealthy and silent. He keeps his location secret until conditions become favourable when he begins to chirp again. To make life simple, we’ll just refer to Crazy – but remember every other time you turn him on he becomes Freaky. He’s sneaky! Crazy does not immediately begin to chirp when darkness falls. He may wait a second or two or he may delay his singing for up to 40s. By this time you may think he has (thankfully) moved away. But start to chirp (he eventually will) and you will then know that Crazy is a very happy little insect. Call him pesky, call him annoying but we just call him Crazy. 26  Silicon Chip You may think that this behaviour is just like any ordinary cricket or frog, but naturally Crazy is different. Ordinary crickets make sounds to establish their territory or attract a mate. And their chirping sounds are produced by rubbing a coarse section of one wing against a scraper located on the other wing. This process is called stridulation. Crazy does not stridulate! Nor does he need to attract a mate (well, not that we’ve noticed). However, he does claim his territory. This territorial claim remains until he is discovered whereupon his final fate remains uncertain. There may be search-and-destroy missions to locate Crazy but he is very elusive. One thing against him is that his eyes glint in the dark and this may reveal his position. More than likely though, his eyes will terrify the unwary. by John (Chirpy) Clarke While ordinary crickets are made from biological materials, Crazy is an all-electronic insect manufactured from numerous elements including silicon, iron, copper, carbon and silica. He also incorporates man-made plastics in his construction that are rather difficult to pronounce for a cricket. When attempting to pronounce his material make-up he is sometimes heard expressing just the word “chip”. It’s derived from the longer expression “silicon chip”. Whether this expression sets Crazy apart as being more highly evolved than his biological counterparts is unknown. As Crazy says, he does include a silicon “chip” in his make up. In this design the chip is a PIC microcontroller and that vastly simplifies his circuitry. Just as crickets evolve in nature, this makes this new design an evolutionary improvement over the previous but ever popular “Clifford the Cricket” from December 1994. In that circuit a siliconchip.com.au Easy to build but hard to ignore – Crazy Cricket, shown here in 3D, chirps away in the dark and flashes his LED eyes . . . until you turn the lights on. Then he shuts up until it gets dark again. We’ve shown Crazy here with resistor “legs” coming from the underside of the PCB – while this is perfectly acceptable, they could just as easily come from the top side and bent over the edge of the board. Or indeed, they could have been made from tinned copper wire. The PIC micro is also programmed with Crazy’s alter-ego, Freaky Frog. Each is selected in an alternate fashion on each supply power-up. CMOS hex inverter was used instead. Further improvements over the previous 1994 design include reduced component count, smaller and more compact construction and significantly lower current drain. This low current allows the use of a lithium 3V cell. That’s in contrast to the 1994 version that used a rather large 9V battery. That battery acted more like a convict’s ball and chain, with the weight often restricting Clifford from his annual winter migration northward to a warmer climate. The 1994 chirping sound was rather limited and comprised a 2kHz tone modulated at 160Hz and at 25Hz. This didn’t simulate a real cricket. He’s real (almost)! For this latest version, we wanted Crazy to sound more realistic, so the sounds made by Crazy are based upon a real cricket’s chirping. Typically, a cricket produces three close-together chirps each separated by silence – then an even longer silence, before repeating these triplet chirps. Fig.1 shows a typical cricket chirping waveform. Each individual chirp comprises a tone of about 4kHz that lasts for around 50ms. The spacing between each chirp is also around 50ms. A much wider spacing is between each triplet at around 250ms. As expected, without arms (he has six legs!) a cricket does not have an accurate timepiece to set these periods precisely and so these periods do vary a little. Fig.1: typically, a cricket produces three close together chirps each separated by a silent space, then a wider spacing of no sound before repeating these triplet chirps. The scope grab on the left is a close-up of the drive waveform fed to the piezo sounder. Channels 1&2 (yellow and green) are at either end of the piezo while the mauve trace shows the difference – that is, the full 6V across the piezo while that on the right shows one burst of cricket sound. siliconchip.com.au June 2012  27 J1 POWER 100nF 470k 4 3V LITHIUM BUTTON CELL K D1 1N4004  A 1 Vdd GP2 GP3/MC GP4 LDR 6 IC1 PIC12F675 GP1 GP5 GP0 100 5 PIEZO TRANSDUCER 3 2 7 330 330 A  K A  K LED1 LED2 Vss 8 SC 2012 CRAZY CRICKET/FREAKY FROG K A LEDS 1N4004 A K Fig.2. the circuit is very simple with just a single, cheap PIC microcontroller (IC1) and a few other components. IC1 monitors the LDR that in turn monitors the ambient light. IC1 also drives the piezo transducer that emits all the chirping noise and the LEDs flash while ever Crazy chirps. The tone of the chirp, however, does not appear to vary by any noticeable degree. Crazy simulates the cricket chirp by producing the three 4kHz chirps separated by the longer spacing. When reproducing this waveform, we found that a 50ms chirp with 50ms gap for each chirp triplet tended to sound more like an umpires whistle (NOT a cricket umpire . . .) than a cricket! Clearly there is a difference between a real cricket’s stridulation and a generated waveform driving a piezo transducer. In order to sound more realistic, the simulated chirps were reduced to 20ms wide with 20ms gaps between them. The standard cricket 250ms spacing between the three chirps, however, is incorporated into Crazy’s voice. Variations As mentioned, a cricket does not produce precise periods in its chirping. To simulate this variation, Crazy has his chirping periods varying randomly over a limited range. The variations are weighted so that the 20ms and 250ms periods are more common compared to rarer wider and narrower periods. The variations in the periods provide a more natural cadence to Crazy’s chirping. The variations prevent the simulated cricket sound from being too regular, relentless and artificial. Physical appearance Crazy is made up using a small PCB (printed circuit board) with the components mounted onto this. Most 28  Silicon Chip parts are mounted on the top of the PCB including the cell holder and eyes, made from 3mm diameter red LEDs. The piezo transducer that produces the cricket sound is slung beneath the PCB. Legs; six in all, are fashioned from spare resistors – or you could use tinned copper wire. The circuit As shown in Fig.2, Crazy’s circuitry is very simple, comprising a PIC microcontroller, IC1 and just a few associated components. It’s powered by a 3V lithium cell, switched via a jumper link JP1. The jumper is removed when Crazy is not used to save any power draw from the cell. The circuit does not draw much current anyway – typically only 3µA when Crazy is dormant in lighted conditions. Current drain while chirping is 1mA. Diode D1 is included as a safety measure to prevent damage to IC1 should the cell be connected incorrectly somehow. This could happen if the cell holder is installed the wrong way round. If the polarity is wrong, diode D1 will shunt the reverse current. If the cell holder is installed correctly, then because of the way the CR2032 cell is made, there is no way that it can be inserted back-to-front. At least that is true for the particular cell holder we used. IC1’s power supply is bypassed with a 100nF capacitor and IC1 runs using its internal 4MHz oscillator. When Crazy is dormant and awaiting darkness, this oscillator is shut down (put into sleep mode) to save power. A low frequency watchdog timer is set running to waken IC1 approximately each half second. During the woken period, IC1 checks the ambient light level from the light dependent resistor (LDR1). Normally, IC1’s GP1 output is set high (3V) and so there is no current flow through the 470kΩ resistor and the LDR. Again, this is done to minimise current drawn from the 3V cell. When IC1 is awake, it sets output GP1 low (0V) and the LDR forms a voltage divider in conjunction with the 470kΩ resistor across the 3V supply. The voltage across LDR1 is monitored at input GP3. In darkness, the LDR resistance is high (above 1MΩ) so the voltage at input GP3 is more than 2V due to the voltage divider action of the LDR and the 470kΩ resistor. This voltage is detected as a high level by IC1. The high level tells IC1 that Crazy is in the dark. With bright light, the LDR will drop in resistance, down to around 10kΩ, which produces a low level at input GP3. IC1 recognises this as Crazy being located in a lighted area. Output GP1 is only held low for a short duration, sufficient for ambient light readings from the LDR. GP1 then returns high to save power. Software solutions Note that the GP3 input in many projects is often configured as the MCLR input (master clear), which allows the microcontroller to have an external power on reset. However, for our circuit we need to use this as a general purpose input for monitoring the LDR. When MCLR is set up as an input, the MCLR operation is switched to an internal connection within the microcontroller so the master clear power-on-reset function is not lost. One disadvantage of using this as a general purpose input is that it is not a Schmitt trigger input. The lack of a Schmitt trigger input at GP3 can mean that, at a particular ambient light level, the input to GP3 could be read as either a high or low input level by IC1’s software. At this threshold, Crazy could produce strange sets of chirping as IC1’s software switches on and off the chirping, undecided as to the ambient light level. We solve this by making sure that once Crazy is switched on (in darkness), he is not switched off until the siliconchip.com.au PIEZO TRANSDUCER UNDER PCB LED1 K IC1 D1 PIC12F675 470k 100nF 4004 LEGS + LDR1 PIEZO 100 JP1 330 CR2032 BUTTON CELL HOLDER 330 LEGS LED2 A K © 2012 A Fig.3: all parts mount on the PCB. Take care that the cell holder, IC1, D1 and the LEDs are oriented correctly. The piezo is under the board. The six legs can be any value resistor or even lengths of tinned copper wire. Note the turned-back and soldered safety “feet” in the photo above. PIEZO ambient light reaches a significantly higher level. This difference in level is called hysteresis. Hysteresis is implemented by pulsing the GP1 output momentarily high when checking for a high ambient light level. High ambient light means that the LDR resistance is low, so the GP3 input is a low voltage. The momentary high pulse level effectively raises the GP3 voltage slightly since this pulse is filtered due to the internal capacitance at the GP3 input of 50pF or less. The raised voltage means that the LDR is required to have a lower resistance (ie have more light shining on it) to bring the GP3 voltage low enough for a low input reading by IC1. The second disadvantage of using the MCLR pin as a general purpose input is that there can be a problem when programming the microcontroller. This problem occurs when the internal oscillator is also used to run the microcontroller (which we do). We solve this problem in the software and the solution is discussed later under the ‘programming’ subheading. Output drivers Outputs GP0, GP2, GP4 and GP5 on IC1 are used to drive the LEDs and piezo transducer. The piezo transducer is driven via both the GP2 and GP4 outputs. When output GP2 is high, GP4 output is low and when output GP2 is taken low, output GP4 is taken high. This provides a full 3V peak square wave drive to the transducer. A 100Ω resistor limits peak current siliconchip.com.au at the switching of the outputs. LED1 and LED2 are independently driven via outputs GP5 and GP0 respectively, via 330Ω resistors. These LEDs are driven for short bursts while Crazy is producing a tone. Only one LED is driven at one time to limit the peak current drawn from the battery, to extend its life. Construction Crazy is constructed on a PCB coded 08109121, measuring 30 x 65mm. He is presented as a bare PCB with wire legs upon which to stand. Check the PCB for any problems such as undrilled holes or breaks in the tracks. Faults are unlikely since PCBs these days are generally of excellent quality, particularly if you are using a board supplied by SILICON CHIP or any of the kit suppliers. Fig.3 shows the PCB overlay. Begin construction by installing the resistors, using a multimeter to check the value of each before inserting into the PCB. You might note that for this project we have also shown the individual resistor colours on the PCB overlay. As mentioned earlier, the legs can be either spare resistors or lengths of tinned copper wire. We prefer resistors but please yourself! Of course, the resistor values for the legs doesn’t matter to anyone except, perhaps, Crazy (would you like it if you had six different legs?). Diode D1 can now be installed, taking care to orient correctly. The 100nF capacitor can be soldered in next and it can be positioned either way round. Then solder in the 2-way pin header along with the cell holder – make sure the plus terminal is oriented toward diode D1 on the PCB. LED1 and LED2 are mounted raised off the PCB by about 10mm. The leads can be bent so that each LED sits horizontally and faces outward toward their corner of the PCB. Make sure the longer lead of each LED (the anode) is inserted in the “A” position on the PCB. The LDR is mounted about 5mm above the PCB surface and sits horizontally. Whether you use resistors or wire for the legs, they should be cut to about 35mm long, with a small loop formed on the outer ends so that the wire end is not sharp. These loops can be filled with solder. Bend the legs so that Crazy can stand upright. The piezo transducer is mounted on the underside of the PCB supported on TO-220 insulating bushes used as spacers and secured with short M2 screws and nuts. The wires can be soldered to the underside of the PCB (the positions are marked ‘piezo’) or brought around to the top of the PCB and soldered in the normal way. Heatshrink tubing over the wires to the PCB will help prevent the wires from breaking off. While the piezo will probably come with red and black wires, indicating that it is polarised, in this case it Freaky Frog Crazy has an alter-ego (or should that be alternate ego?), Freaky Frog, who produces frog “knee-deep” sounds instead of cricket sounds. If you prefer frogs to crickets or tire of Crazy and want a change, then replace all references in this article to Crazy with Freaky. Freaky has a different cadence to Crazy and produces a set of 10 chirps 10ms long with 2ms gaps. This is followed by a 30ms gap and then another set of 3-chirps 10ms long with 2ms gaps. The 10/3 sets are separated by between 200 and 1200ms that varies irregularly. The frequency of the chirps is set at around 2kHz. Both Crazy and Freaky are in the PIC program – each time you turn it on, the alternate program runs. June 2012  29 Parts list – Crazy Cricket/ Freaky Frog 1 PCB coded 08109121, 30 x 65mm (available from SILICON CHIP for $10 plus p&p – see pp 96-97) 1 20mm button cell holder (Jaycar PH-9238, Altronics S 5056) 1 CR2032 Lithium cell (3V) 1 30mm diameter piezo transducer (Jaycar AB-3440, Altronics S 6140) 1 LDR 10kΩ light dependent resistor (Altronics Z 1621; Jaycar RD-3480) (LDR1) 2 TO-220 insulating bushes 1 DIL8 socket 2 M2 x 8mm screws with nuts 1 2-way pin header (2.54mm pin spacing) with jumper shunt (J1) 1 25mm length of 2mm heatshrink tubing Semiconductors 1 PIC12F675-I/P programmed with 0810612A.hex (IC1) 1 1N4004 diode (D1) 2 3mm high brightness red LEDs (LED1,LED2) Capacitors 1 100nF 63V or 100V MKT polyester Resistors (0.25W, 1%) 1 470kΩ 2 330Ω 1 100Ω 6 resistors for legs or 250mm 0.7mm tinned copper wire doesn’t matter – either wire can be soldered to either “piezo” position. Note that if you intend to program the PIC yourself, hex file 0810612A. hex can be downloaded from the SILICON CHIP website. Also see the section under programming for details about how to do this. Solder in either the IC or the IC socket, making sure it is oriented correctly. If using a socket, place the IC in it now – watch out that you don’t bend the pins! Now install the CR2032 cell in its holder and place the jumper link onto the 2-way header (JP1). If all is well, the LEDs will momentarily flash after about 3s to acknowledge power has been connected. An acknowledgement by a brief flashing of the LEDs also occurs when a low light level is detected. Low light can be simulated by covering over the LDR. Crazy will then begin chirping after a delay of about 10 seconds, providing the low light level remains. 30  Silicon Chip From then on, Crazy will randomly vary his waiting period before chirping begins at the onset of darkness. grammed, it will begin executing its program. A typical program initially sets up the microcontroller with the general purpose (GP) lines set as inputs Modifications or outputs (I/O). This conflicts with the Crazy has a loud chirp so that he will programmer needing to use the clock be heard effectively even if hidden in and data programming I/O lines for a dark cupboard. If you require less program verification. volume, then change the 100Ω resistor This problem does not happen if the in series with the piezo transducer to MCLR pin is set as the external MCLR a higher value such as 4.7kΩ or 10kΩ input because the programmer then for a nominal reduction in perceived has control over the microcontroller, volume by about 50%. Higher values stopping it from executing the proagain will give even less volume. grammed code. The light level threshold can be Note also that in order to run the altered by changing the 470kΩ resis- code, the microcontroller has to have tor in series with the LDR. A lower the internal oscillator configured resistance value (say 100kΩ) will have instead of an external crystal, RC or Crazy chirping at a higher ambient external clock oscillator. light level. By contrast, increasing the The programming problem is solved resistance value will mean that Crazy in the software provided by including will need a darker light level before he a three second delay at the start of the begins chirping. program. This delay is before the I/O lines are set as inputs or outputs. The Programming I/O lines therefore remain as high imIf you are programming the mi- pedance inputs while the programmer crocontroller yourself, you may be verifies the internally programmed presented with a warning by the pro- code using the clock and data programgrammer stating that programming is ming lines. not supported when both the MCLR is A warning from the programmer will set as a general purpose input and with still be issued but the microcontroller the internal oscillator set. can be programmed successfully and However, you will be able to pro- correctly verified by the programmer. gram the microcontroller successfully, Note that the PIC12F675 also needs ignoring the warning. That’s because special programming due to the fact any problems associated with this that it has an oscillator calibration configuration is already solved by a value (oscal) that is held within the software solution. Read on if you want PICs memory. This calibration value more details. is individually programmed into each As mentioned, we set MCLR as a PIC by the manufacturer and provides general purpose input and utilise the a value that allows setting of the PIC internal oscillator within IC1. This to run at an accurate 4MHz rate using can present problems for a program- the internal oscillator. mer during the process of verifying This value must be read before erasthe software code after programming. ure and programming so that it can The problem lies in the fact that as be included with the rest of the code soon as the microcontroller is pro- during programming. If this procedure is not done, then the oscillator frequency could be offfrequency. That will have an effect on Crazy’s chirp. Most PIC programmers will automatically cater for this oscal value – but it is worthwhile checking if your programmer correctly handles this, especially if you have difficulties. Finally, be aware that the PIC12F675 requires a 5V supply for programming, even Fig.4: if you see this warning (or similar) when though it happily runs at 3V attempting to program the PIC, simply ignore it in the circuit. (ie, just press OK). SC siliconchip.com.au