Lesson 1

Blinking an LED 2.0: Programming Edition

If you did our LED project, you learned how to blink a light with a switch. In this project we will make the computer do the same thing.

Before we start programming, we need to plug in our LED and resistor. Take a look at the circuit board. The holes to the right side of the breadboard labeled P0 to P15 are called pins. Pins are places where we can tell the computer to take in or send out a one or zero. The one or zero is represented by an electrical voltage being on or off at the pin.

We'll be using pin 0 so plug the positive end of the LED into P0, and the negative side into the breadboard. Next plug a resistor in next to the LED and the other end into Vss, which is connected to the negative side of the battery. Your circuit should look like the picture above.

 
HIGH 0
LOW 0

Now we need to write the code to turn the light on and off. The words for "on" and "off" in PBasic are HIGH and LOW. The number after HIGH and LOW is the number of the pin we want to turn on and off, which is pin 0 in our case.

LED   PIN   0

HIGH LED
LOW LED

Sometimes it's easier to remember what pin we are using if we give it a name. To do this we write the name we want to use (LED in this case) followed by the word PIN and then the number of the pin we are using. We can now use the word LED in place of pin 0.

The word we use for pin 0 can be anything you want. We could call it "P0" or "zero" or even "monkeys." It doesn't matter. Be careful though! If the word you choose turns bold it's a special word, and you can't use it.

If you tried to run this, you probably wouldn't see anything happen. That's because the computer is blinking it faster than we can see.

LED PIN 0

HIGH LED
PAUSE 500
LOW LED
PAUSE 500

To see the light blinking, we need to tell it to wait for us with the word PAUSE.

The number after PAUSE is how long the computer will wait in milliseconds. A millisecond is 0.001 seconds. That's really fast! Can you figure out how long the pause will be in the code above?

This code will turn the light on and off once, but what if you want to do it a lot of times?

LED PIN 0

DO
HIGH LED
PAUSE 500
LOW LED
PAUSE 500
LOOP

To make the program repeat forever (or at least until we turn it off) we put the whole thing between the words DO and LOOP.

That's it! You have just written your first program. Try changing the pause time and pin to see what happens.