Tutorial 02 – Running LED’s

The running LED’s tutorial demonstrates the use of the various program labels, program layout and a few basic commands. It uses the PICAXE School Experimenter Board circuit diagram layout and configuration.

Tutorial Task 
Switch the 3 LED’s on for 200ms and then off for 200ms in sequence starting on the red LED then the yellow LED and then the green LED. If one LED goes off the next LED must go on at the same time.  When the green LED is switched off the sequence must start at the beginning again and continue indefinitely until the power is switched off.

Tutorial Solution 
Open a new program window in the PICAXE Programming Editor for your Running LED’s program. In the tutorial we are going to use all three LED’s and from the circuit diagram we know that the red LED is on port 0,the yellow LED is on port 1 and the green LED is on port 2. It is confusing to remember on which port is what colour LED. So we are going to give the ports names that we can easily understand in the program.

Enter the initiation code for your program by starting of with a Label (“init:”) to identify and provide a marker with an address. If you need to initialise the program again from with in the code this Label provides an address to jump to.

The symbol command is used to give the output ports names that we can understand in the program without having to look it up.

init:                                                              ;Program initiation Label
         symbol RED_LED = 0                     ;Rename port 0 to RED_LED
         symbol YELLOW_LED = 1             ;Rename port 1 to YELLOW_LED
         symbol GREEN_LED = 2                ;Rename port 2 to GREEN_LED

That takes care of the initiation and we can move on to the main program. Identify the main program code are by entering a “main:” label. This label also provides us with an address to jump to from within the program code.

main:                                                           ;Label called main, the start of the main program

Type in the following code for the main program:

            high RED_LED                              ;Switch on the red LED
            pause 200                                      ;Pause 0.2 seconds (200ms)
            low RED_LED                               ;Switch off the red LED
            high YELLOW_LED                     ;Switch on the yellow LED
            pause 200                                      ;Pause 0.2 seconds (200ms)
            low YELLOW_LED                       ;Switch off the yellow LED
            high GREEN_LED                        ;Switch on the green LED
            pause 200                                      ;Pause 0.2 seconds (200ms)
            low GREEN_LED                          ;Switch off the yellow LED
            goto main                                       ;Loopback to the beginning
            End                                                 ;The end of the main program


The high command outputs a high (1) with a voltage of 5V (4.5 in our case) on the port following the command and a low command outputs a (0) with a 0V voltage. If we look at the circuit diagram we see that a high voltage on the output port will switch on the LED’s and a low voltage will switch them off. 

Starting off with the red LED we switch it on and pauses the program for 200ms. The LED needs some time to switch on and display its light. If we switch it on and then off again in the next command we will not see any light because the LED will already be switched off again before it had time to light up. Comment out the pause 200 command to verify the statement. After the pause we switch the red LED off and directly after that we switch the yellow LED on so these two actions will as one.

The rest of the program is just a repetition with other LED’s. The goto command loop the program execution back to the “main:” label address or marker and the whole process start again and will continue indefinitely.

It is good practice to end the main program with a End command. The program will never get to the end command but it keep things structured and if you later change the code in the main program space there will we an end at the end to stop the program from going into the sub procedure program space.

You can download the program file, Tutorial 2 – Running LED’ s.zip by right clicking on it and selecting ‘save link as’ from the popup menu.