DesignSpark Electrical Logolinkedin
Menu Search
Ask a Question

Using PicKit demo board to drive numerous LEDs

I am designing several PIC-based modules for use on my upcoming Model Railway layout which is still in the planning stage. In order to programme the PIC chips, I decided to purchase a PicKit3 programmer. Research showed that a package called ‘Debug Express’ was available (665-7416) which included a demo-board holding a 44-pin surface mount (SM) PIC chip. This board offers over 30 i/o pins for use.

As the ‘Express’ kit wasn’t a lot more expensive than the PicKit alone, it was decided to purchase this, as the opportunity to drive lots of controllable (as opposed to ‘always on’) or ‘flashing’ LEDs from one programmable device had some appeal.

Time passed, and ideas for the roles the various LEDs would assume evolved.
Some uses of ‘flashing’ or switching LEDs on a model railway layout include:

  • Emergency vehicle blue lights
  • Roadworks amber lights
  • Road traffic lights
  • Belisha beacons
  • Level crossing beacons
  • Street lights
  • Platform lights
  • Arc welding
  • TV flickering
  • Pub garden lights
  • Church / Castle floodlights
  • Advertisements
  • Shop window displays
  • Bus lighting
  • Office lighting
  • House lighting

It became clear that the on-off periods of various LEDs would be vastly different, but the PIC could handle this requirement with ease as it has 3 timers available for use by application software.

The demo board itself has no high-current drivers, and thus also no protection against short-circuits caused by wiring faults, even if it could supply all the current required by lots of LEDs (which it can’t).

Work began in designing a PCB to interface the demo board to the outside world of ‘Trainville’.
A design decision taken early on was to make this device appeal to other users as well.
Several features have thus been incorporated in order to widen the appeal of this board.

The demo board usefully has rows of pin header holes which can be used to both connect and mount a daughter board (à la Arduino Shields). This approach was taken, but the concept turned on its head by making the demo board the ‘shield’ which fits atop the interface board, as the latter is larger in size. The demo board was cut in half, as the ‘development area’ of this wasn’t needed.

At that time, news was emerging of low-priced PCB manufacture from Asia. The ‘special offers’ naturally led the PCB design to fit within the 100 x 100mm form-factor offered at attractive prices from these companies. (In the event, each 2-layer, PTH, solder-resisted & silk-screened board cost about £2 – somewhat less than a piece of Veroboard the same size).

The specification for the interface board was now nearing completion: ULN2803 (714-1167) drivers and connectors around the outside of the board, with the PIC board in the centre. This left a quandary: what to place in the middle of the board underneath where the demo board sits? Whatever went there would have to be very low profile – DIP ICs in sockets was out – in order not to impede fitting the demo-board ‘shield’ on top. In the end, the decision was made to replicate exactly what was on the demo board; a surface mount (SM) PIC and a few other components (a variable resistor and a push button). This means that the interface board can be assembled as a completely stand-alone board if required.

During this period of design brainstorming, further ‘requirements’ from ‘Trainville’ entered the equation:

  • Funicular Railway / Cable Car
  • Narrowboat crossing an aqueduct
  • Realistic (as is possible) day/night layout illumination

The first two functions require a stoppable, reversible DC motor, whilst the third requires the use of a high power driver fed from a PWM source within the PIC. The Narrowboat motor needs to be reversible as room doesn’t allow for a circular route, and turning a model boat round to head back is out of the question. The devious, but practical solution, is to simply tow it back silently under the cover of darkness, ready for tomorrow’s excursion.

These features, combined with some of the lighting effects mentioned earlier, require that the PIC is temporally aware – it ‘knows’ the time in ‘Trainville’. Thus, it can switch off office lights at night, switch off house lights during the day, stop the Funicular Railway at tea time (all tourist attractions in the UK close at 5pm, right?), etc. etc. Again, the PIC with its timers can do all these things.

The PWM signal is taken to a STP36NF06L MOSFET (486-5671) capable of switching more than enough LEDs to control the layout ‘daylight’ overhead illumination – this is likely to be 12v LED tape. In the event, space permitted another of these MOSFETs to be added for switching a large number of street lamps.

Thus the design of the hardware continued, but in a rather unusual way. The usual arrangement is to draw a schematic diagram, extract a netlist, place the components, and route the track traces. This wasn’t going to work for this board; the size limit meant that as little of the board as possible must be occupied with just copper tracks – they need to be as short as possible, and crucially, avoid convoluted routes. The approach taken was to lay out the board components and tracks to give the most efficient layout and construct a matching schematic diagram in tandem. The resulting outcome was pleasing, workable and acceptable. This design approach, however, isn’t for the faint-hearted or inexperienced designer! Don’t try this at home.

The motors will require limit switches to prevent overrun, so some i/o pins are left as logic-level inputs/outputs, with limiting and pull-up resistors. These pins can also trigger MP3 player sound modules, to create localised sound to match time-based activities, e.g. the narrowboat engine.

Analysis of the SN754410 (052-7704) motor control chip, and some of the lighting effects, where two LEDs operate in pairs with only one-or-the-other alight, resulted in the need for either a large(ish) number of PIC pin allocations or the use of inverting logic. The latter approach was taken, in order not to ‘waste’ PIC pins when there is an alternative method available. A 4069 hex-inverter CMOS chip (052-7596) fulfils this need.

Along with ‘housekeeping’ items, such as a voltage regulator, and ICSP connector, the hardware design is complete. The ULN2803 common-cathode pin is bought out to the connectors – not needed for LEDs, but the ULN2803 can drive relay coils too.
The common 0v is also brought out on all connectors, so that local PSUs for LED current can be used if required.
Several jumpers are provided so that LEDs may be powered from the on-board 5v regulator for testing purposes.

The picture shows the finished board along with (schematically) the devices it drives.

The various groups of driven devices are designated zones in the picture as follows:

  • A – PWM-driven high-current, high-voltage output for layout illumination
  • B – 8 LEDs driven via ULN2803 buffer
  • C – 8 LEDs driven via ULN2803 buffer. 6 of these will be for 2 traffic lights
  • D – High-current, high-voltage output for multiple LEDs for street lighting
  • E – 4 pairs of complimentary LEDs for alternate flashing applications driven via ULN buffer
  • F – 8 logic-level digital i/o pins for switches, etc. These could drive LEDs via external drivers
  • G – 2 forward/stop/reverse DC motors

The firmware for the PIC is in assembler, as a good deal of ‘bit-banging’ is required by this application.

The PIC runs at 32KHz using its internal oscillator – timing isn’t that critical, and this saves i/o pins.

One of the timers is configured to interrupt the processor at 10Hz. This interrupt is used to increment a counter, and it is the value of this counter which controls the flash rate of the alternating lights, and also the traffic light sequencing. This is probably faster than it should be, but should be slow enough to look acceptable – running at the correct speed would be too slow for an onlooker to notice that they are changing. The counter is also used for the ‘time of day’ calculations.

Another of the timers is used to generate the PWM signal for dimming the layout illumination. This is configured to run at 120Hz with 8-bit resolution. This could be reduced to 60Hz with 9-bit resolution if the perceived flicker isn’t too annoying – this will be decided when the lighting is in-situ.

Using the clock speed configured above, a ’24-hour day’ in ‘Trainville’ takes about 20 minutes of real time. This should be enough time for onlookers to enjoy an entire sequence of temporal events without fatigue and boredom becoming an issue.

Comments and observations are always welcome - find me on LinkedIn or via email.

I'm a freelance PCB Designer, producing designs in my spare/hobby time in addition to my full time role as a software developer. I have been designing PCBs since the 1970s using both manual (black and red/blue tapes) for many years, followed by CAD-based design using Racal-Redac, Protel, OrCad, Eagle and finally Design Spark. Most of the hobby boards I have designed are for my own use, and for sharing with other members of MERG (www.merg.org.uk) and are related to Model Railway control.

6 Nov 2018, 15:28