Archive

Archive for the ‘Arduino’ Category

Blink using Arduino

December 17, 2011 Leave a comment
Introduction
OK you’ve gotten yourArduino set up and also figured out how to use the software to send sketches tothe board. Next step is to start writing your own sketches. We’ll start offeasy by just modifying something that already works.
To start we will venturedeep into the Blink sketch, looking at each line and trying to understand whatits doing.
Then we will starthacking the sketch!
Blinkie

The sketch itself is inthe text input area of the Arduino software. Sketches are written in text, justlike a document. When you select Compile/Verifyfrom the menu, the Arduino software looks over the document andtranslates it to Arduino-machine-language – which is not human-readable but iseasy for the Arduino to understand.
Sketches themselves arewritten in C, which is a programming language that is very popular andpowerful. It takes a bit of getting used to but we will go through theseexamples slowly.
/*
 * Blink
 *
 * Thebasic Arduino example.  Turns on an LEDon for one second,
 * thenoff for one second, and so on…  We usepin 13 because,
 *depending on your Arduino board, it has either a built-in LED
 * or abuilt-in resistor so that you need only an LED.
 *
 *http://www.arduino.cc/en/Tutorial/Blink
 */
intledPin = 13;                // LED connected todigital pin 13
voidsetup()                    // run once, when thesketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}
voidloop()                     // run over and overagain
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
Comments
Lets examine this sketchin detail starting with the first section:
/*
 * Blink
 *
 * Thebasic Arduino example.  Turns on an LEDon for one second,
 * thenoff for one second, and so on…  We usepin 13 because,
 *depending on your Arduino board, it has either a built-in LED
 * or abuilt-in resistor so that you need only an LED.
 *
 *http://www.arduino.cc/en/Tutorial/Blink
 */
This is a comment, it is text that is not used by the Arduino,its only there to help humans like us understand whats going on. You can tellif something is a comment because there is a /* at the beginning and a */ at the end. Anything between the /* and */ isignored by the Arduino. In this example the person who wrote the commentdecided to make it look pretty and add *’s down the side but this isn’tnecessary.
Comments are very useful and I strongly encourage every sketch you make have acomment in the beginning with information like who wrote it, when you wrote itand what its supposed to do.
Variables
Lets look at the nextline:
intledPin = 13;                // LED connected todigital pin 13
This is the first lineof actual instruction code. It is also extremely unlike English (or any otherhuman language). The first part we can easily understand is the part to theright, which is also a comment. Turns out if you want to make a small comment,you can use // as well as /* */. // is often used for short, one line comments.
The rest of the line,the stuff before the //, is what is called a statement, which is basically like a computerizedsentence. Much like human sentances end with a . (period), all computersentences end with a ; (semicolon)
OK all we have left isthe statement itself, which turns out to be a sentence telling the computerthat we would like it to create a box named ledPin and to put the number 13 in that box. If youremember your math, you may recall that the box is also known as variable.

box-type
box-name
=
stuff-to-put-in-box
int
ledPin
=
13


The first part of thissentence is int, whichis short for integer which is a fancy way of saying whole number 
The second part of this sentence is ledPin which is the name of the box
The third part is an =, which basically says that the variable (box) namedledPin should start out equaling whatever is after the =
The fourth part is 13, a whole number (integer) which is assigned to
 ledPin
Procedures
Lets move on to the nextsection 
voidsetup()                    // run once, when thesketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}
OK we’ve got twocomments, each starting with //. We understand comments already so lets skipthat.
We also see in the middle there is a statement, we know its a statement becauseit ends with a
 ; (semicolon) however there’s a whole bunch more stuff before andafter it.
This bunch of code is an example of a
 procedure, a procedure is a collection of statements, itsused to group statements together so that we can refer to them all with onename. Its just like a procedure that we use to perform a task step by step.
returned value
procedure name
(input values)
{ statements }
void
setup
()
pinMode(ledPin, OUTPUT); }

To better understandprocedures, lets use an analogy to the kinds of procedures we’re used to
clean catwash thecat(dirty cat)                    // a procedure forwashing the cat
{
        turn on the shower.
        find the cat.
        grab the cat.
        put cat under shower.
        wait 3 minutes.                                     // wait for cat to get clean.
        release cat.
}
This is a procedure forwashing the cat. The name of the procedure is wash the cat, it uses a dirty cat as the input and returns a clean cat upon success. There are two brackets, an openbracket { and a closed bracket }, thats are used to indicate the beginning andend of the procedure. Inside the procedure are a bunch of statements,indicating the correct procedure for washing a cat. If you perform all of thestatements then you should be able to turn a dirty cat into a clean cat.
Looking again at theprocedure, we see that it is named setup and it has no input values and it returns void. Now you’re probably asking yourself “whatisvoid?” Well thats a computer-scientist way ofsaying nothing. That is, this procedure doesnt returnanything. (That doesnt mean it doesn’t do anything, just that it doesn’t have atangible number or whatever, to show when its complete)
voidsetup()                    // run once, when thesketch starts
There is one statment inthis procedure,
pinMode(ledPin,OUTPUT);      // sets the digital pinas output
We’ll return to thisstatement in detail later, suffice to say it is a way of telling the Arduinowhat we would like to do with one of the physical pins on the main processorchip.
Procedure calls
We’re onto the nextbunch of text.
voidloop()                     // run over and overagain
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}
Using our now well-honedtechnique we recognize that the text to the right is all comments. We alsorecognize another procedure, this one called loopwhich also has no inputs or output. This procedure has multiplestatements, one after the other.
We’re going to skip thefirst statement for now and go straight to statement #2.
The second and fourthstatements are the same, and have something to do with a delay. This statement is very similar to the”wait 3 minutes.” command in our cat-washing procedure. Thisstatement says “Dear Arduino. Stop what you’re doing for a short amount oftime. Thanks!” 
To do this, the statement performs a
 procedure call. (We will use the phrasing calls aprocedure). Basically, we wantthe Arduino to take a break but don’t quite know how to do it, lucky for us,someone else wrote a procedure called delay which we can call upon to do the work for us. Kind of like if we needto do our taxes and we dont know how, we call upon an accountant to do it forus, giving them the paperwork input and getting tax return as the result.

procedure name
(input values)
;
delay
(1000)
;

This means thatsomewhere out there, there’s a procedure something like this: 
voiddelay(numberof milliseconds
{
  “Dear Arduino. Stopwhat you’re doing for (number of milliseconds)amount of time. Thanks!”
}
(Of course, this exampleis not proper code)
Turns out this delay procedure works pretty well, and all we have todo is tell it how many milliseconds (1/1000th of a second) to wait and it willdo the job for us.
Returning to the firststatment, we see that it is also a procedure call. This time for some procedurecalled digitalWrite. We’ll also skip this one in detail for a bit,except to explain that its turning a pin on the Arduino chip on and off, andthat pin is powering the LED so in essence its turning the LED on and off.
Special Procedures -Setup() and Loop()
I do want to mentionquickly here that the two procedures we’ve mentioned so far are extra-specialin that when the Arduino first wakes up after being reset, it always does whatsin the setup procedure first. Then it does whatever is in the loop procedure over and over and over…forever! Orat least until you turn it off.
Modifying the example
Now that we’ve analyzedthe entire program it’s time to make some changes. In your Arduino software,change the number in the delay procedure calls to 500 (from 1000) as shown
voidloop()                     // run over and overagain
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(500);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(500);                  // waits for a second
}
If you try to save thesketch, you’ll get the warning that it’s read-only.

 Not a big deal, you can save it under a new name, such as MyBlink



Once the Arduino hasbeen updated with the new sketch you should see a faster-blinking light thanbefore
If the LED is notblinking faster, check:
·                 Did you make the changesto the delay procedure call to make it 500?
·                 Did the compile/verifycomplete successfully? (should look like the screenshot above)
·                 Did the upload completesuccessfully? (should look like the screenshot above)
Categories: Arduino

Basics of arduino-Getting started

December 17, 2011 Leave a comment
Introduction

This lesson won’t teach any electronics, really. Its more for making sure that everything is setup and ready for the future lessons. It will verify the Arduino is working as intended and that the computer you are using is compatible.
Do you have everything you need?

For this lesson you will need some stuff! Make sure you have this stuff or you will be unable to complete this lesson
ImageDescriptionDistributor

Assembled Arduino board, preferrably a Diecimila or Duemilanove (or whatever the latest version is) but NG is OK too

Adafruit

$35

USB Cable. Standard A-B cable is required. Any length is OK.

Adafruit

Or any computer supply store

$5

9V DC power plug with 2.1mm barrel plug, positive tip

Optional

Adafruit

Any electronics supply store (Best Buy, Radio Shack, etc.)

$10

4 Rubber bumpers

Optional

These are included in Arduinos purchased from the Adafruit shop, otherwise any hardware store should have them.

$1

Preparing the Arduino!

Take your Arduino out of its protective bag. Look at it and make sure it looks kinda like this:


Diecimila Arduino

Or like this:


NG Arduino

If there’s anything missing or really wrong, make sure to contact the store you bought it from. For example, here is an Arduino that is missing the two round silver capacitors!

OK, now that you are satisfied that your Arduino looks good, put the rubber bumpers on the bottom of the board. This will protect your Arduino from spills and dirty tables. If your table is made out of metal, this is essential!


Download Driver

You’ll want to grab the USB drivers before plugging in the Arduino for the first time.

First, download the USB VCP driver from the FTDI website

I strongly suggest visiting the site and downloading the most recent drivers, as of Sept. 2007 the following links are the most recent.
Windows XP/2000/Vista
Mac OS X PPC
Mac OS X Intel, v 10.4 or higher
Linux v2.4 or lower kernel only!

If you have a Linux computer, and you have a 2.6 kernel, the driver is built-in and you are good to go. You can always run uname -a to get the kernel version.

Then extract the driver bundle onto the Desktop.

Under Windows use Unzip or rightclick and select “Extract All…”


Power up! (USB)

Now we are ready for the moment of truth, it’s time to plug your Arduino in and power it up. The most common way to do this is to plug one end of the USB cable into the Arduino and the other end into a computer. The computer will then power the Arduino.

The jumper-setting step is only for Diecimila and OLDER arduinos! Make sure the Power Jumper is set correctly. Right next to the USB jack, there is a jumper with 3 pins. If the jumper is on the two pins nearest USB jack, that means you are planning to power the Arduino via the USB cable. If it’s on the two pins nearer the DC Jack then it means you are planning to power the Arduino using a 9V battery or wall adapter.

You’ll want it set as shown in the picture above.

Make sure your cable is a A-B cable. One end should be thin, rectangular. The other end should be square.

Plug the thin end into your computer

Make sure that the USB cable is plugged in directly to a computer port. Sometimes monitors or keyboards have a USB port you can plug into. Most of the time this is fine, but I strongly suggest you plug it directly into the computer as that will eliminate any possible problems. Same goes for USB hubs.
Later on, once you’ve verified you can power the Arduino and upload sketches no problem, then you can try plugging it into other ports.

Plug the square end into your Arduino

You should get a small green light on the right side of the Arduino, as shown here.

And a few blinking orange lights, on the left side, as shown in this video (download mp4 here)

If you’re plugging it into a Mac or Linux machine, you may not get as much blinking from the orange lights. The video is from plugging into a Windows computer.

If no lights or blinking occurs, double check:
Is the USB cable plugged into the computer and into the Arduino?
Is the computer on?
Is the jumper set correctly?
Try another USB port, USB cable, and computer

If you still can’t get it working, your Arduino may be faulty.

Next its time to install the driver! Follow these links for instructions for each of the supported operating systems
Windows
Mac
Linux
Power up! (9V DC, optional!)

Another way to power up the Arduino is to plug in a battery or wall adapter into the DC jack.

Verify that you have a 9V DC 100-500mA power adapter, with a 2.1mm barrel plug and positive tip. If the box doesn’t have any information about the adapter, you can look for these clues

Make sure the symbol near the bottom of the label is present. It means that the outside of the plug is negative and the inside is positive. A center-negative plug will not work with the Arduino.

To verify the DC plug is the right shape, just try plugging it in. If it doesn’t fit or is wobbly, it’s the wrong kind.

You can learn how to test wall adapters using a multimeter here.

The jumper-setting step is only for Diecimila and OLDER arduinos! Make sure the Power Jumper is set correctly. The jumper should be set so that it connects the two pins close to the DC jack

Plug in the adapter and verify you get the green light!

If not, double check:
Is the DC adapter plugged in?
Is the DC adapter the right kind? Check voltage, polarity, plug size, etc.
Is the jumper set correctly?
Try another adapter.

If you still can’t get it working, your Arduino may be faulty.

Categories: Arduino