Friday, November 11, 2011

Using the Sparkfun Inventor's Kit to get started in Robotics

We are developing our own PORPOISE kit to make entry into the world of "Precision Oceanic Robotics Programming On and In the Sea Environment" as fun and easy to understand as possible.  To do that we are looking at other kits that make robotics simple and the one we have chosen to start with is the "Sparkfun Inventor's Kit" which can be purchased for about $100.

As I open the box I find that the slim manual included has friendly illustrations and writing. One of the things that impresses me is the provision of an "Arduino holder" that neatly fits the supplied breadboard and Arduino Uno circuit board. Laying the two boards into the holder is step 1.

Next, in a section called "Installing the IDE" (Integrated Development Environment)  the manual instructs us to download the software from and as I am at this moment working on a Windows XP system I opt for that and follow the instructions to install the open-source software, plug in my Arduino to the USB port and select the appropriate driver (this procedure can also be found in the first chapter of the  book Make: Arduino Bots and Gadgets which we also purchased from Sparkfun.)

Once we have gotten our green "on" LED  lit and the yellow LED at pin 13 blinking through the supplied "hello world" program we are ready to move to the third page, "A Small Programming Primer". 
The primer alerts us to the importance of C syntax: the use of // for single line comments, the use of /* and */ to start and end multi-line comments (all comments are ignored by the compiler and are essential road maps for remember what the code does by telling yourself and anybody else what the code means in plain english!).
{} curly brackets are used to define when a block of code starts and ends (this is similar to what you do when writing term papers in LaTex and a missing curly bracket can be the problem when something isn't compiling properly). And semi-colons ; are essential for ending a line in C.  If the semi-colon is missing the whole program can refuse to compile.

The primer then defines variables as "instructions to move numbers around in an intelligent way. Variables are used to do the moving".  With that said they define the most important variables as
int (integer), "the main workhorse, stores a number in 2 bytes (16 bits) with no decimal places;  long (long) for when integers aren't long enough (uses 4 bytes (32 bits) of RAM; boolean (boolean), a simple True or False variable that only uses one bit of RAM; float (float), used for floating point math, meaning decimals and takes 4 bytes;  and char (character) which stores one character using ASCII code and uses on byte (8 bits)."
They tell us that the Arduino handles strings (text) as an array of char's.

The next page introduces the Math Operators, Comparison Operators, Controle Structures, and Digital and Analog codes.

Math operators are well understood: =, +, -, * and /.  But the % sign is defined as a "modulo" which "gives the remainder when one number is divided by another (ex. 12 % 10 gives "2", i.e. 12 can be divided by 10 one time with a remainder of 2).

The comparison operators for logical comparisons are ==  which means "equal to" or "is".
!=  means "not equal to";  < and > are as they appear.

The basic control elements are the following two:
1)  if(condition){}
else if (condition){}
else {}
which "executes the code between the curly brackets  if the condition is true, and if not will test the else if condition and if that is also false the else code will execute."

2) for (int i = 0; i <
# repeats; 1++ ) {}
which is used "when you would like to repeat a chunk of code a number of times (can count up i++ or down i-- or use any variable."

Next come the basic  Digital commands:
1) pinMode(pin, mode); which is "used to set a pin's mode where pin is the pin number you would like to address from 0 to 19 and the mode can be either INPUT or OUTPUT,
2) digitalWrite(pin, value); where, once a pin is "set as an OUTPUT, it can be set to either HIGH (pulled to +5 volts) or LOW (pulled to ground).
3) int digitalRead(pin); where once a pin is set as an INPUT you return whether it is HIGH or LOW.

Finally the page talks about the tricks used to deal with Analog inputs and outputs using
1) int analogWrite(pin, value); using the Arduino pins that support pulse width modulation (3,5,6, 9,10,11), turning the pin on and off very quickly "making it act like an analog output" from 0v to 5 volts (using the numbers 0 as 0% and 255 as 100% of the duty cycle).
2) int analogRead(pin); which is used to read the pin voltages when set to input.
The primer gives the url for a full programming reference. It lets the reader know that the primer is targeted at people who have programming experience and lets the reader know that this section can be skipped if they want to dive into the circuit building, knowing they will "pick up most of it along the way".  These kind of gentle reminders help insure that students don't get intimidated and this is an approach we will take in our curriculum development too.

The fourth section is small "Electronics Primer" that points out that "no previous electronic experience is required to have fun with this kit." We will include such a disclaimer in our program too.

The section has pictures and descriptions identifying LEDs, Diodes, Resistors, Transistors, Hobby Servos, DC Motors, Piezo elements, ICs (Integrated Circuits), Pushbuttons, Potentiometers and Photo Resistors, explaining what each does and things to watch out for along with web resources for more details.  It also shows the resistor color code and talks about lead clipping. Lead clipping is important for easy use of components on the arduino and breadboard. The recommendation is that LED positive leads are climpped to 10mm (3/8") and negative leads to 7mm (9/32") (note that the negative lead, if you cut the positive one too short and get confused, is the side of the LED that has the squarish indentation on the plastic bulb bottom; to easily determine which side that is if you can't easily see it with your eyes or feel it with your fingers, just place the LED on its side on a smooth surface with one lead on the bottom and one on top and it will roll if it is placed on the positive side and rest if it is placed on the negative side), resistors have their leads bent down to 90 degrees and clipped to 6 mm (1/4").

On page 8 the first Circuit assignment begins; creating a blinking LED.  One of the truly wonderful thing about the Sparkfun Inventor's Kit is the provision of "Layout Sheets" that are pinned to the breadboard and show exactly where to insert wires and components, providing a pattern you can perforate.  We will include something similar in our PORPOISE kit. This makes assembling the electronics for prototyping foolproof, much like patterns for knitting or sewing did for countless hobbyists who wanted to learn those arts.  The provision of patterns for "sewing the electronics on to the board" seems like a great way to invite young women into robotics, particularly those who may have grown up with patterns for garment making. In the open source tradition that Arduino products are famous for, the layouts and all code and schematics are offered free of charge on the internet. This didn't deter us from buying the kit, in fact knowing we could have it for free actually made us that more likely to buy the kit, happy to  let somebody else do the labor. It is the proper psychology of commerce and it brings repeat customers. People are happy to pay a premium for a service if they know that nobody is trying to hustle them or constrain their options.

Similarly we will make our PORPOISE kit schematics and all information open-source and free of charge for the same reason, and we know that those who have the money will thus support our labor time putting kits together for them but that those who don't have the money can substitute their own labor. This is a win-win.

In Circuit-01 the four two pin headers that hold the pattern to the breadboard need to be cut from the strips provided; snapping with fingers or with pliers didn't work so well for me; an exacto knife seems to be the best way to get the job done. I recommend you pin the pattern down first before placing your components. I didn't at first and then I had to remove my components to get the pattern to line up properly. Also, you will find it easier to poke a hole in the pattern where you want to place a component with a pin header, or, even better, one of the hard wire pins,  before placing the component because the act of perforating the pattern can bend your component leads.

The wire connecting the 5V power output to the + rail of the breadboard has no functionality yet, so don't get confused by it.  It is there for later modifications.  only the wire from Arduino pin 13 to the positive side of the LED on the breadboard is relevant; the negative side of the LED goes to the 330 ohm resistor which in turn is connected to the negative rail of the breadboard which goes to the Power Gnd pin of the arduino.

When you hook it up with the USB cable to your computer and load the Blink program the LED you put on the breadboard will blink at the same rate as the tiny yellow LED (labeled L) on the arduino.  You know the program is uploading to the board because the little rectangular yellow LEDs on the board with the labels TX (Transmit) and RX (Receive) blink rapidly.  Then the little rectangular LED labeled L and the one you put on the breadboard start blinking.

The rate of the LEDs is determined by the Blink program, which is the  first "hello world" arduino sketch which comes with the Arduino software (Load it from Files>Examples>1.Basics>Blink; you don't have to type anything in).

Most Arduino sketches to do the basic things you want your robot to do are already written and available on line thanks to the open source community. You can learn to program by simply playing around with parameters and seeing what happens. Through trial and error at first and logic later, you can make your board do almost anything it is capable of doing.

In this first circuit, for example, if you change the "delay" parameters under void loop () you can change the blinking rate.  It is set to delay(1000) when you load the example.  Increase it to delay(2000) if you want to make it blink twice as slow and delay(500) if you want to make it blink twice as fast.   G' head and try it, really! I set the digitalWrite(13,HIGH); delay(5000) and left the digitalWrite(13,LOW); at delay(1000); and what I got was a 5 second on light and a one second off light.  Fun.

Once you've played around with these parameters you should feel rather proud and accomplished.  You did what most robot professionals spend their time doing -- changing values.  That will be most of the programming you need to do as a novice roboticist. The codes are all out there; you will need to modify them by changing values so they do what you specifically want.  As Newton said, in Science "we stand on the shoulders of giants" and there is no need to go back and re-invent the wheel.
(A word of caution, though -- remember to COMMENT any changes you make so you can figure out what you did in case something goes wrong! When I changed the digitalWrite delay to 5000 I changed the comment in the sketch to "wait for 5 seconds" instead of "wait for 1 second").

  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
int ledPin = 13; //LED connected to digital pin 13

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(ledPin, OUTPUT);    

void loop() {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);              // (seconds * 1000)
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);              // (seconds * 1000)

Another fun thing to try is changing the pin number.  I changed the int ledPin line to
int ledPin = 8; //LED connected to digital pin 8
When I compiled the code the little rectangular yellow LED on the arduino stopped blinking, but when I connected the positive lead wire from the breadboard LED to pin 8 on the arduino it started blinking.

The manual explained how to change the blink time by changing the lines in the code to:

void loop() {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(time on);              // (seconds * 1000)
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(time off);              // (seconds * 1000)

I made a mistake that you might make too: I took them literally and typed the above code in to my sketch.  The result, upon compiling, was an error message. Of course there is no such value as "time on" or "time off" -- we hadn't declared them as variables. What the manual meant was to put in a "time on" number (1000 or 2000 or 500 or whatever) and a number for how long you want the LED to be off for "time off", and the comment explains that whatever you put in, the computer interprets as "number of seconds you desire times 1000" meaning that the value 5000 is 5 seconds.  This may sound obvious, but I know there are students who, like me, might take what is written literally at first, not knowing what is supposed to be typed in verbatim, and what is serving as a placeholder.

The next mod the manual instructs us to do is to control the brightness of the LED. For this we use analog signals.    You replace the LED to pin 9 (also changing the wire) so that it reads int ledPin = 9
then you change digitalWrite to analogWrite, and instead of using HIGH for full on and LOW for full off you set a numeric value where 0 is off and 255 is full brightness. I  put in this code:

void loop() {
  analogWrite(ledPin, 50);   // set the LED to low brightness between 0 (off) and 255 (full on)
  delay(1000);              // (seconds * 1000)
  analogWrite(ledPin, 255);    // set the LED to full on
  delay(1000);              // (seconds * 1000)

After compiling you get a dim LED then a bright one.

Finally, in this tutorial, they instruct you to upload the Fading Program at File>Examples>3.Analog>Fading.

 This example shows how to fade an LED using the analogWrite() function.

 The circuit:
 * LED attached from digital pin 9 to ground.
 Created 1 Nov 2008
 By David A. Mellis
 Modified 17 June 2009
 By Tom Igoe
 This example code is in the public domain.
int ledPin = 9;    // LED connected to digital pin 9

void setup()  {
  // nothing happens in setup

void loop()  {
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect   

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect   

The result is that your breadboard LED does a nice slow fade out from bright to off and then back up to bright. Play around with the delay values to see what they do. I set the first delay to 500 and when the bulb was fully lit it sat there at full brightness for a long time. I set the second delay to 100 so between the fade out and the new fade in things happened fairly quickly.

So that's Circuit-01. Great fun. Now on to Circuit-02.

No comments:

Post a Comment