Deprecated: Assigning the return value of new by reference is deprecated in /home/nicholas/public_html/ggnew/components/libraries/cmslib/spframework.php on line 101

Deprecated: Assigning the return value of new by reference is deprecated in /home/nicholas/public_html/ggnew/components/libraries/cmslib/spframework.php on line 644
Tutorial: Blinky Light
Tutorial: Blinky Light

Turn on your Propeller Platform and connect it to your computer. Grab an LED and breadboard and wire up the following circuit;

Blinky Circuit

The hookup wire connects to the socket marked 'GND' on the Propeller Platform, then to the shorter lead on the LED.  The longer lead on the LED connects to the resistor, then to P0 on the Propeller Platform.

The shorter lead on the LED is the negative lead. The longer lead is positive.  Current flows from power to ground, so the longer lead connects to the power source.

LED Ground vs. Positive

Now, open up the Propeller Tool.  Ready for your first program?  Here it is:

Your First Program

Take the program above, put it into the Propeller tool and hit F10.  It will quickly compile and load into the Propeller.  The code will blink an LED connected to P0 every other second (1/2 Hertz) forever.

Below is a video tutorial on the program;

Let's also break down each line to understand what it's doing

PUB main

Spin is organized into blocks;


These 2 blocks hold actual code.  If you're familiar with php or BASIC or C, these operate a lot like functions.  They have names (the name of this block is 'main'), and you can pass them values (function(passedval)).

The remaining blocks are not required for a valid program;


CON holds program constants.  If there's a constant you use throughout the program, the CON block allows you to change it once and it will be reflected every time you use it.  Here's an example of the blinky light program using a CON block;
Con Block


The VAR block holds program variables.  In the blinky lights program, I haven't used any system variables, but here's a version of blinky lights using variables
Var Block

There are 2 more blocks, DAT and OBJ, but we'll skip those for now - OBJ is useful when incorporating someone else's code, and DAT is another place to hold variables, it's also where you put assembly code (if you want to use it).

Anyway - back to our original program.  The next line is:

dira[0] :=1

Every pin on the Propeller can be set to an input or an output.  When the Prop boots up, each pin is set as an input, so we'll need to set P0 to an output. 

To set P0 as an output, we'll change the value of dira[0] to 1.  The := is an assignment operator in spin.  Can you guess how you'd set P10 to an output? dira[10] := 1.  You can also change a range of pins with a single command.  To change P0,P1,P2, and P3, just use dira[0..3] := 1.


repeat tells the Propeller to run a block of code multiple times.  You can specify a repeat condition (repeat i from 1 to 100) or just repeat forever by not specifying anything else.  This repeat block will repeat forever.

Code to be repeated is delineated by tabs.  Note the soft gray lines under the repeat block?  The Propeller Tool put those gray lines there to show you what code is in the repeat loop.

outa[0] := 1

outa tells the propeller to connect the pin to ground or to V+.  outa works just like dira, you can set the output of a single pin (outa[10] := 1) or a group of pins (outa[0..3] := 1).  Note that outa is only meaningful if the pin has been switched to an output with dira.

Further, the Propeller uses Tri-state logic.  Each pin can be connect to ground, V+, or set in a high impedance state. 

A high impedance state lets the pin sense whether it's connect to ground or V+ without changing the signal.  It lets multiple pins share the same data channel and do stuff like charlieplexing.

Connect the pin to ground with: 
dira[pin] := 1
outa[pin] :=0

Connect the pin to V+ with:
dira[pin] := 1
outa[pin] := 1

sense if the pin is connected to a low or high signal (high impedance state):
dira[pin] := 0 (default on bootup)
pinstatus := ina[pin]

ina works just like outa and dira, except it's read only.  If the pin is set as an input, ina will hold the current input value.

Your First Program, Continued >>


Other Stuff