POF 02 Hello world

Purpose

Getting started with JeeNodes and setting up the software and the hardware to make them "do stuff".

Goal

To blink a LED connected to a JeeNode with your own code:

What You'll Need

Introduction

This Hello World project is "barely a POF". It doesn't strictly need the foam base, since nothing is going to be plugged into the JeeNode port headers. Nevertheless, it's listed as first project because it goes through all the preliminaries required to work with POFs. Might as well set up everything right, right?

We're going to set up four things here: you (yes, you!), the software, the hardware, and your code.

1. You (Yes, You!)

This is just to make sure we're on the same page. Let's see...

  • you're pretty curious what this "physical computing" is all about
  • you've got an embedded micro-controller called a JeeNode
  • you want it to "do stuff", meaning: you want to hook up sensors, lights, actuators, motors, etc.
  • you're starting from scratch, or you want to start over from scratch to get everything working right
  • you've got some experience with software, hardware, or electronics but perhaps not with all of them
  • you've got a PC/Mac and know how to use it and how to install software and get it working
  • you're willing to get your hands dirty and dive in - at the deep end!

Is that more or less you? Great - let's get started then!

2. The Software

To make things happen on a JeeNode, it needs software. In the case of a micro-controllers unit (MCU), it's often called firmware. So what we want to do is write software for the JeeNode, and then somehow get it running on that JeeNode. Alas, an MCU is not very powerful compared to a PC/Mac, and it doesn't have the ability to develop that software on it. Instead, we need to develop the software on the PC/Mac, and then somehow transfer something to the MCU to make it perform the requested tasks.

That's what the Arduino IDE is for. It's an integrated development environment (IDE) which lets you write and store code for the MCU in the form of projects, which are called "sketches" in Arduino-speak. Your code has to be written in the "C" and/or "C++" programming language. In this project, some sample code to blink our LED has already been written for you, it looks like this:

#define LED_PIN 9
void setup () {
    pinMode(LED_PIN, OUTPUT);
}
void loop () {
    digitalWrite(LED_PIN, 1);
    delay(500);
    digitalWrite(LED_PIN, 0);
    delay(500);
}

Once we enter this "source" code in the Arduino IDE, we can "compile" it. This is the process of checking that all our instructions are valid and precise, and then transforming these instructions into a form which the MCU can understand. Each brand of MCU has a different "machine language", and needs a different compiler. In the case of JeeNodes and most other Arduino-compatible boards, the MCU is an Atmel AVR ATmega, which happens to be what the Arduino IDE is set up for. Running the validity and preciseness checks is done with the "Run" command in the Arduino IDE.

The next step will be to get the generated machine code from your PC/Mac to the MCU in the JeeNode. This process is called "uploading", and requires a connection between your computer and the JeeNode - usually a USB cable these days. But now we're getting into hardware territory, and we're not ready for that just yet!

So let's get that Arduino IDE working on your PC/Mac.

Luckily, this is a fairly standard software installation. And it's fully documented, for Windows, Mac OS X, and Linux: http://arduino.cc/en/Guide/HomePage - so go ahead and follow those instructions. I'll wait :)

Back?

Good. There is one issue to be careful about, and that is the Arduino IDE's configuration of Board type and Serial Port. Both are chosen from the Tools menu, and will remain as configured until you change them again. For the JeeNode, use board type "Arduino Duemilanove or Nano w/ ATmega328", and the proper serial / COM port for your unit. Be careful if you have multiple units connected at the same time, or you'll upload code to the wrong one!

You'll need to familiarize yourself with the Arduino IDE, so one thing you should do right now is to try and create a new sketch with the above source code. Then try running it. If all is well, you should see something like this - the exact numbers are not important:

Binary sketch size: 888 bytes (of a 30720 byte maximum)

Got that? Good, you're all set. And the best part is that you won't ever have to repeat this again - your computer is now set up to develop and generate embedded code for Arduino's and JeeNodes.

3. The Hardware

These are the pieces that need to be connected together. A USB cable, a USB-BUB interface, two JeePlugs, a JeeNode, a red LED, and foam board. Details about setting up the base are described on the POF 01 Foam base construction page:

The first step is to push the JeeNode upside down onto the foam base, as shown here - I call it the "happy feet" position:

Then hook up the USB-BUB and the USB cable, note that these too need to be placed upside down:

The last step is to connected the LED. LEDs are polarized, so they have to be connected the right way around. In this case, the long lead should be on the left:

Just push the leads in, the gold-plated contacts will make a good connection. Here's a close-up:

4. Your Code

There is very little code in this POF, as already shown. Here it is again:

#define LED_PIN 9
void setup () {
    pinMode(LED_PIN, OUTPUT);
}
void loop () {
    digitalWrite(LED_PIN, 1);
    delay(500);
    digitalWrite(LED_PIN, 0);
    delay(500);
}

The structure of this code is the same as all Arduino sketches: a setup() function which gets called once, and a loop() function which gets called continuously afterwards. There is no magic at all here, main() is simply defined as follows inside the Arduino's wrapper code:

int main () {
    init();
    setup();
    for (;;)
        loop();
    return 0;
}

The init() code is also part of the Arduino wrapper code, it sets up the Serial port object and a clock timer based on TIMER #0 for use with millis() and delay().

The "B1" pin on the JeeNode is bit 1 of ATmega port B and corresponds to what Arduino calls digital pin 9.

The pinMode() and digitalWrite() calls are again wrappers from the Arduino environment. There is extensive documentation on the web, see the Arduino site.

What's Next

You now have a development environment which works for Arduino hardware and a variety of other Arduino-compatible boards, including the JeeNode.

You've hooked up a JeeNode with a LED attached to it, and you've made it do specific "stuff" by uploading the above sketch to it.

You could now try a few things out, such as altering the delays to make the LED blink differently. Or write more code, to make the LED send out morse signals. Or even swap the LED for an infrared type, and send out remote control signals to change channels on your TV.

This is just the beginning, and you're now all set to explore the world of Physical Computing - enjoy!

DSC_0813.jpg (10.9 KB) per, 2010-09-11 07:50

DSC_0814.jpg (8.8 KB) per, 2010-09-11 07:50

DSC_0808.jpg (8 KB) per, 2010-09-11 07:50

DSC_0809.jpg (9 KB) per, 2010-09-11 07:50

DSC_0810.jpg (6.5 KB) per, 2010-09-11 07:50

DSC_0811.jpg (10.1 KB) per, 2010-09-11 07:50