Connecting Light Emitting Diodes to a PIC Microcontroller – Flowcode
Watch The Video Tutorial
Flashing an LED is more useful because with appropriate circuit changes like by the use of relays, this principle can be readily adapted to turning on and off almost any electrical device.
NOTE: The power supply is not shown here, the PIC should be connected to +5V(VDD) and Ground (VSS).
Pin 1 (MCLR) should be also connected to positive via a pull up resistor as shown on the circuit in the Reading Switches article.
As the internal oscillator is not used here, connect an external crystal oscillator on pin 13 and 14 as shown as well in the Reading Switches article.
As with most programming languages, our “Hello world” program here its just a simple switching ON and OFF(Flashing an LED with a delay on 1 sec) an LED connected to PORT B bit 0 of The PIC16F877A in common cathode
as shown on the figure above.
- Start a new project as show in the previous tutorial, select the PIC 16F877A. Go to the component toolbar and select LED
NB: LED Array: Select an array of LED at once (8 LEDs)
RGB LED: Select Tri-Colour LED (Red, green and Blue LED)
- To Change the LED Color, Select the LED on the panel and click on the … next to the “Ext Properties” options on the Properties Pane to open the LED Ext properties.
- Select the LED on the panel and click on the … next to the “Connections” options on the Properties Pane to open the LED connection properties. On the PORT drop down combo box, select PORTB and on the Bit drop down box, select Bit 0. Click Done.
Which value to Output to switch ON the LED?
To understand the concept, let us revise a bit the notion of decimal, binary and hexadecimal number.
In decimal numbering system, we have 10 numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.
In Hexadecimal, we have 16 numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F.
In binary system we have only 2 numbers: 0 and 1 and this is the basis of all digital systems with a 1 representing a high voltage such as +5V and 0 representing a low voltage such as 0V.
To program a PIC we can use any of these numbering systems. The figure below shows PIC16F877A pin configuration.
It is clear from the figure above, if we want to switch on an LED connected to pin B0 (pin 33), we must put a high voltage on that pin (common cathode) which is a “1” in binary, a “1” in decimal and also a “1” in hexadecimal. To switch it off, a “0” is needed.
To switch on LEDs connected to pins B4, B5, B6 and B7 (pins 37 to 40) while leaving off pins B0, B1, B2 and B3:
In binary we always start from right to left, with the least significant bit on the right and the most on the left, as B0 is the least significant bit, we will start with it:
The number to output in binary: 00001111 =15 in decimal and F in hexadecimal.
In flowcode to show the compiler that the number is in binary we start it with: “0b”, “0x” in hexadecimal and decimal we leave it the way it is.
So the correct way to write our binary number in flowcode will be: 0b00001111 or 0xF 0r just 15.
Let us continue with our project…
Repeat step 2 to insert another Output, double click to access its properties….
Select PORT B and put the value of 0 in the variable/value box. This is to put a value of 0 (low) to switch off the LED.
Repeat step 5 to insert another 1 second delay.
Start simulation and observe the LED switching on for 1 second and off for 1 second.
PIN 33 of the PIC (Pin RB0) also changes to red when the LED is on.
Download Flashing LED Flowcode Project