Fads to Obsessions and Beyond...




Free domain for life, exceptional technical support, website transfer

Flash a LED ('Hello World' for PIC micro's)

Demonstrate the minimum circuit required to operate a PIC microcontroller - Use of In-Circuit Serial Programming (ICSP) with flashing a LED (the "hello world" example of PIC microcontrollers).

Microcontrollers are effectively a 'computer' on a single chip that contains a CPU, memory and programmable inputs/outputs (1). Consequentially, this provides an economical solution in the DIY situation to relatively easily digitally control a wide variety of devices and produce solutions to myriad problems, that a more traditional 'analog electronics' approach would require extensive electrical engineering knowledge and skill.

Various microcontroller families (PIC, ARM, AVR, etc) typically have onboard functionality such as analog-to-digital converters (ADC), Pulse Width Modulation (PWM), programmable timers, voltage comparators and general purpose input/output pins (GPIO) which with programming via EEPROM and flash enable sophisticated solutions with only relatively simple external ancillary circuitry. Such minimum external circuitry typically involves a suitable power supply, an oscillator and connections for the programming of the microcontroller.

Having choosen PIC microcontrollers (there are many sites comparing and contrasting the various microcontroller choices, I choose PIC mainly because of the extensive literature available) the next step is determining how to incorporate these components into a 'solution' (e.g. pump controller etc). A possibly easy option is to purchase a 'development board'. Such development boards include necessary minimum ancillary circuitry (power supply, programming connections, etc) and a variety of other typically required components (LCD screen, buttons etc).

The 'development board' approach (or even going with a variety of 'Ardinuo') makes it relatively easy to start using microcontrollers to accomplish 'electronic projects', however, in my opinion inhibits 'electronics' learning. Learning how to 'get a PIC working' from scratch is a valuable learning experience in itself, and since a wide variety of projects will only require a simple microcontroller circuit with the minimum support circuitry, enables DIY projects with the minimum components (i.e. in-expensive) and or custom made to whatever your particular requirements.

The minimum requirements for a PIC (PIC16 and PIC18 dealt with here) include a power supply, oscillator and programming connection (in-circuit serial programming ICSP). Power supply is easily provided by a simple linear voltage regulator (e.g. LM7805 supplied from a 'wall wart' or batteries). The oscillator depends upon the particular PIC, however, typically can be a simple RC network, a 'crystal' or an on-board internal oscillator, depending upon your circuit requirements. In-circuit serial programming (ICSP) is a straight-forward connection scheme/standard for programming PIC microcontrollers (requires a suitable 'programmer' e.g. PICkit2).

This minimum circuitry is demonstrated for a PIC (PIC18F248) with CCS C/PICkit2, using what I consider the simplest test application, flashing a LED (the electronics/microcontroller circuit analogous to the 'hello world' application of the 'programming world').


The minimum requirements for a PIC (PIC16 and PIC18 dealt with here) are:

  • Power Supply (generally 5V, but 3.3V also widely available)
  • Oscillator (RC network, external crystal oscillator)
  • In-circuit serial programming (ICSP)

Power Supply

A typical "wall-wart" power-supply is used (a surplus laptop charger in this case) in conjunction with a voltage regulator (LM7805) to provide the regulated 5V required by the PIC microcontroller.

The 'wall-wart' avoids resorting to the use of a transformer/rectifier with AC input etc (after which likely a LM7805 would be used in any case to provide load and line regulation). As a 'disclaimer', the DIY hobbyist should avoid dealing with AC power as a general rule.

Oscillator

An external crystal oscillator is used (X1 in the schematic) with associated capacitors C1 and C2. The PIC datasheet specifies a range of suitable capacitance values for C1 and C2 depending upon the oscillator frequency being used. Higher capacitance will increase the stability of the oscillator but will also increase the start-up time.

Note, the oscillator crystals can be either series or parallel cut. The PIC18FXX8 oscillator design requires the use of a parallel cut crystal.

In-circuit serial programming (ICSP)

I use a PICkit2 as the ICSP. Connection is as per the Microchip recommendations.

PIC microcontrollers can be serially programmed while in the end application circuit (i.e don't need to remove the component). This is simply done with two lines for clock and data and three other lines for power, ground and the programming voltage. This means the microcontroller can be programmed with the latest firmware and or updated as required. The Flash program memory is readable, writable and erasable during normal operation over the entire Vdd range.

Other

A resistor and diode is connected to pin 1 of the PIC. This is to take advantage of the PIC power-on reset (POR) circuitry, which requires MCLR (pin 1) connected directly (or through a resistor) to Vdd. The diode is to provide protection against incorrect voltage polarity.


Note: Image loading can be slow depending on server load.

  • PIC SchematicPIC Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    PIC Schematic - minimum circuit

    PIC Schematic - minimum circuit

This project did not require a PCB.

The construction was done using prototyping board. See the photographs and schematic diagram sections.

Qty Schematic Part-Reference Value Notes
Resistors
1R110K1/4W, 10% 
1R23301/4W, 10% 
Capacitors
2C1,C222pFCeramic 
1C30.33uF 
1C40.1uF 
Diodes
1D1Red LED 
1D21N-4001 
Integrated Circuits
1U1PIC18F248PIC microcontroller datasheet
1U27805Linear Voltage Regulator  datasheet
Miscellaeous
1J1CONN-H55-pin connector for ICSP
1SW1SW-SPDT 
1X110MHzCrystal Oscillator
Description Downloads
PIC - Bill of Materials Text File Download

Before connecting the PIC, check the supply voltages to ensure that the linear voltage regulator (or other power supply scheme) is supplying optimally 5V. Note (from datasheet), absolute maximum voltage on Vdd with respect to Vss is -0.3V to +7.5V.

Programming the PIC

The C code (using CCS compiler) to flash a LED connected to PORT B4 (pin 25) with a period of two (2) seconds is a follows:

Code Snippet 1:


  #include 

  void main() {
    setup_adc_ports(NO_ANALOGS);
    setup_adc(ADC_OFF);
    setup_spi(FALSE);
    setup_timer_0(RTCC_INTERNAL);
    setup_timer_1(T1_DISABLED);
    setup_timer_2(T2_DISABLED,0,1);
    setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
    
    output_b(0x00);         //clear port B
    do {
      output_toggle(PIN_B4);
      delay_ms(2000);
    } while (TRUE);  
  }   
   				

Code Snippet 2:


    #include <18F248.h>
    #device adc=16
    
    #FUSES NOWDT      //No Watch Dog Timer
    #FUSES WDT128     //Watch Dog Timer uses 1:128 Postscale
    #FUSES HS         //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
    #FUSES NOBROWNOUT //No brownout reset
    #FUSES NOLVP      //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
    
    #use delay(clock=10000000)   
   				

Once the circuit has been constructed as per the schematic (photographs of the circuit laid-out on breadboard given in the Photographs Section), the LED (D1) should flash with a period of two (2) seconds.

The circuit is the basic minimum required to enable a PIC microcontroller to operate after being programmed with a suitable .hex file. So this circuit was only laid out on a breadboard.

Before connecting the PIC, check the supply voltages to ensure that the linear voltage regulator (or other power supply scheme) is supplying optimally 5V. Note (from datasheet), absolute maximum voltage on Vdd with respect to Vss is -0.3V to +7.5V.

If the LED fails to flash with a period of two (2) seconds, carefully check your breadboard circuit against the schematic and the photographs in the Photographs Section. Look for "gotcha's" like the LED polarity (and for D2).

Also, when programming the PIC with .hex file, the PICkit2 will provide power to the circuit. In this particular simple circuit ('flashing a LED') this is not of concern. However, in a more complicated circuit, it may be required to insert a 'jumper link' or even a SPST switch to isolate the application circuit from pin 1 of the ICSP connector during programming of the PIC. This will avoid possible excessive current draw that could damage the PICkit2 programmer, and or adversely affect application circuit components/connected devices.

Comments/Questions

No comments yet.

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.

X

Sorry!

Only logged-in users with Silver Membership and above can download.


Silver Membership is only $2, so join now!