How-to Tuesday: Fun with the Arduino Starter Kit


This week I made a project with parts from the Arduino Starter Kit. I skipped over building the Proto Shield from the kit, since I made a how-to a while back. Otherwise, it's a simple build that doesn't require any soldering.

Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It's an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board. Arduino is open source! In addition to the genuine Arduino, resistors, buttons and other goodies, we've also tossed in our best selling Making Things Talk book. This starter kit will help get you started quickly without having to source a lot of parts and do a lot of shopping. Check out the "How To" for some helpful links to provide support and contact with the growing Arduino community.


Features


  • Now Shipping With the New Arduino Duemilanove
  • 1x Mini Breadboard
  • 1x 3 ft. USB Cable
  • 10x 1K Resistors
  • 2x 10k Resistors
  • 3x Red LEDs
  • 2x Green LEDs
  • 1X Superbright Blue LED
  • 1x Momentary Tactile Switch
  • 2x Interlink Force Sensing Resistors
  • 1x Protoshield Kit (unassembled)
  • Making Things Talk by Tom Igoe
  • 24" each of Red, Blue & Black AWG jumper wire
  • 9V Battery Case w/DC Plug (some assembly required)
  • Yes, even a 9V battery

Here is the code I used on the Arduino. It's NOT optimized. Why? Well, I thought this was the best way to write it so a beginner could really understand how it works. Think you can optimize it the best? Let's see what you got! Post the smallest, most efficient, version in the comments!


Suscribe to the MAKE podcast
| Download for iTunes

/*
--------------------------------------------

Make Magazine - Force Sensor Demo

This simple program visually shows the amount of force placed on the sensor
There are much more efficient ways to program, this way was chosen because
it is very easy to understand.

By Marc de Vinck - Licensed under Creative Commons....whatever.

--------------------------------------------
*/

// Here are the constants that we define prior to the program running

int forcePin = 2; // select the input pin for the force sensor
int val = 0; // variable to store the value coming in from the sensor

int led1=9; // defines "led1" as the number 9
int led2=10; // defines "led2" as the number 10
int led3=11; // defines "led3" as the number 11
int led4=12; // defines "led4" as the number 12
int led5=13; // defines "led5" as the number 13

// End of constant definitions

void setup() //run one time when the Arduino first powers up
{
Serial.begin(9600); //starts serial communication, only used for debgugging

pinMode(led1, OUTPUT); // remeber led1 = pin 9, this statement sets pin 9 to output only
pinMode(led2, OUTPUT); // remeber led2 = pin 10, this statement sets pin 10 to output only
pinMode(led3, OUTPUT); // remeber led3 = pin 11, this statement sets pin 11 to output only
pinMode(led4, OUTPUT); // remeber led4 = pin 12, this statement sets pin 12 to output only
pinMode(led5, OUTPUT); // remeber led5 = pin 13, this statement sets pin 13 to output only
}


void loop() //This next bit of code runs continuously
{

val = analogRead(forcePin); // read the value from the sensor

Serial.println(val,DEC); // print the value "val" of the sensor (used for debugging)

if (val>250){ //if the value is maxed out or greater than 250

// aternative code for the following -----for (i=1; i<6; i=i++); digitalwrite(led[i],HIGH)

digitalWrite(led5,HIGH); // turns on all 5 LEDs
digitalWrite(led4,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led1,HIGH);
delay(100); //slight delay to minimize flickering
}
else{
digitalWrite(led5,LOW); //turn off all 5 LEDs
digitalWrite(led4,LOW);
digitalWrite(led3,LOW);
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
}

if (val>=175 && val<=250){ //if value is between 100 and 175
digitalWrite(led4,HIGH); //turns on 4 LEDs
digitalWrite(led3,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led1,HIGH);
delay(100); //slight delay to minimize flickering
}
else{
digitalWrite(led4,LOW); //turns off 4 LEDs
digitalWrite(led3,LOW);
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
}


if (val>=100 && val<=175){ //if value is between 100 and 175
digitalWrite(led3,HIGH); //turns on 3 LEDs
digitalWrite(led2,HIGH);
digitalWrite(led1,HIGH);
delay(100); //slight delay to minimize flickering
}
else{
digitalWrite(led3,LOW); // you get the picture....
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
}

if (val>=25 && val<=100){
digitalWrite(led2,HIGH);
digitalWrite(led1,HIGH);
delay(100); //slight delay to minimize flickering
}
else{
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
}
if (val>=0 && val<=25){
digitalWrite(led1,HIGH);
delay(100); //slight delay to minimize flickering
}
else{
digitalWrite(led1,LOW);
}
}


In the Maker Shed:
Makershedsmall
IMG_5817copy.JPG
Arduino Starter Kit

More:
IMG_3111644.JPG
How-to Make a Proto Shield


Recent Entries

Comments

Oldest comments listed first.

Posted by: dan on December 2, 2008 at 10:44 AM

equivalent if statement

The following takes the code above and integrates else if statements and moves the delay to the bottom since it was the same code in each if statement. I think should work the same way.

if (val>250){ //if the value is maxed out or greater than 250

// aternative code for the following -----for (i=1; i=175 && val=100 && val=25 && val=0 && val<=25){
digitalWrite(led5,LOW);
digitalWrite(led4,LOW);
digitalWrite(led3,LOW);
digitalWrite(led2,LOW);
digitalWrite(led1,HIGH);
}
else{
digitalWrite(led5,LOW);
digitalWrite(led4,LOW);
digitalWrite(led3,LOW);
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
}
delay(100); //slight delay to minimize flickering
}


Posted by: Marc de Vinck on December 2, 2008 at 11:19 AM

@dan,

Thanks for the input. There are ways to really optimize this code......it's a good start.


Posted by: µD on December 2, 2008 at 10:51 AM

Here's my attempt at making your code a little more general and without all there repetitions (DRY: http://en.wikipedia.org/wiki/DRY)

I'll also give you an advice: indent your code, it'll be MUCH MORE readable

So, enough words here's: http://pastebin.com/mfbdba76 the code

Haven't tested it 'cause I don't have an arduino (yet), should work though


Posted by: Marc de Vinck on December 2, 2008 at 11:15 AM

@µD

Can't get the file? Email me, and I will fix the link.

I know it is repetitive, but the idea is to show how it works.

Repetition: http://www.happychild.org.uk/acc/tpr/amz/1198rept.htm

I have a very optimized code that works great...I just want to see what others can come up with before I post mine. I think my version can be further optimized, and I am waiting for a true Arduino Guru to post some amazing code. Anyone?


Posted by: fai on December 2, 2008 at 12:08 PM

code optimization

speed or size?

changing the leds to array would do both then in pseudocode assuming max, high mid, mid , low mid, low, zero

if max
11111 > lights
elseif high mid
01111 > lights
elseif mid
00111 > lights
elseif low mid
00011 > lights
elseif low
00001 > lights
else
00000 > lights
end

delay 100

repeat


Posted by: Marc de Vinck on December 2, 2008 at 12:12 PM

Love it, getting closer to what I was thinking....Thanks for the feedback.


Posted by: rsbohn on December 2, 2008 at 12:10 PM

turn the LED on

// modifications by rsbohn
// http://fundamental.antville.org
// This is meant to be instructional
// (fully optimized code would write to PORTB directly)

// adjust delay_time to minimize flicker
int delay_time = 100;
void all_off() {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
}

void loop() {
all_off();
val = analogRead(forcePin);

// turn on LEDs based on the force value
if (val > 0) digitalWrite(led1, HIGH);
if (val >= 25) digitalWrite(led2, HIGH);
if (val >= 100) digitalWrite(led3, HIGH);
if (val >= 175) digitalWrite(led4, HIGH);
if (val >= 250) digitalWrite(led5, HIGH);

delay(100);
}

// Does LED1 ever go off? Does LED5 ever go on?


Posted by: µD on December 2, 2008 at 12:54 PM

Here's a new link:
http://rafb.net/p/xpSxGX17.html

rsbohn is way simpler and more straightforward than mine, though mine is more general


Posted by: Marc de Vinck on December 2, 2008 at 1:04 PM

@µD

I'll have to try your code. Thanks.

Oh, an I plan on covering the serial port in a later build....not just for debugging ;)


Posted by: fai on December 2, 2008 at 1:17 PM

code optimization

speed or size?

changing the leds to array would do both then in pseudocode assuming max, high mid, mid , low mid, low, zero

if max
11111 > lights
elseif high mid
01111 > lights
elseif mid
00111 > lights
elseif low mid
00011 > lights
elseif low
00001 > lights
else
00000 > lights
end

delay 100

repeat


Posted by: Marc de Vinck on December 2, 2008 at 4:34 PM

@fai

I was thinking size.

Looks like some bit shifting to me!

That would work well, and it would be really small.


Posted by: rsbohn on December 3, 2008 at 4:36 AM

//how about this:

int gate[] = {10, 20, 50, 100, 200, 32767};
void setup() {DDRB = 0b00111110;}
void loop() {
int dval = 0;
int rval = analogRead(2);
for (int x = 0; rval > gate[x]; x++) {
dval = dval * 2 + 1;
}
PORTB=dval*2;
}


Posted by: Marc de Vinck on December 3, 2008 at 4:39 AM

OK, I HAVE to try this! You may be the winner!


Posted by: SamF on February 4, 2009 at 1:25 PM

Confusion

Hi, thanks for the tutorial.

I'm a little confused about how you're getting a value for the voltage at pin 2. With the force resistor, you are changing the resistance between pin 2 and ground, but I don't see that this would change the voltage, only the current.

When I made a similar circuit (before I saw this), I used a force sensor, but I made a "voltage splitting" circuit: a force resistor between ground and pin 2, and a fixed resistor between pin 2 and +5V. This way, if the resistance between ground and 2 was low, the voltage would be low, and if it was high the voltage would be high.

I guess you're somehow doing the same thing, but I don't quite understand how. Any light would be great!

Thanks,
Sam


Posted by: Marc de Vinck on February 6, 2009 at 8:39 PM

@SamF

Did you try running the code and looking at the debug window? It will show you the numerical range the Arduino is reading from the pin. (see: Serial.println(val,DEC)) It's not a high/low read, it's read of the variable resistance that the pin is receiving.

By hooking the sensor up to a pin and ground you will get a range of values (0-1023) Just one of the great things about Arduino.


Posted by: cassiano rabelo on March 2, 2009 at 6:47 PM

Hello. I'm new to Arduino and electronics in general, so I hope I can get some help. I'm getting a huge variation in the readings from the force sensing resistor and as a side effect the lights are flickering quite a lot. Much more than in the tutorial movie.

Here is a snippet from my debug window when doing:
val = analogRead(forcePin);
Serial.println(val,DEC);

Output:
240, 960, 108, 23, 649, 767, 87, 1, 278, 969, 282, 18, 137, 924

I'm not touching the sensor at all, but if I press it, it goes down to zero and stays like that. Is this huge variation when not in use normal?

Thanks a lot,
Cassiano Rabelo


Posted by: Chris on March 29, 2009 at 9:03 AM

....Help!?

I tried setting this up with a phototransistor.... NOTHING WORKS! I got the 5 pack of phototransistors from radioshack and the work..... but the code doesn't work for it .. when I remove the resistor the leds all turn on and when the resistor is in the 1st one stays on. Oh, and it cant be a problem with uploading, I can do that easily. Oh, yeah and another thing are you connecting the variable resistor to GND and DIGITAL pin 2 or ANALOG pin2...


Thanks for the help. reply soon.
Chris.


Posted by: Marc de Vinck on March 29, 2009 at 9:33 AM

Chris,

You are trying to make a different project, so the code isn't likely to work. I used a force sensor, not a phototransistor. I would be happy to help, but you will need to post a schematic, or clearer description, of what you are building.

You might want to check out this link for some ideas about phototransistors:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234177936


Posted by: Michael Gold on May 25, 2009 at 8:23 PM

Works just fine here...

Marc,

Just came from the (Love) Shack with my 5pk of assorted photocells, hooked everything up following the video (I used analog pin 2 on my Arduino Duemilanove for the photocell) and yes, got the same initial result as Chris: One LED lit. Then with some measurements using a multimeter (since for whatever reason RS saw fit to not document the photocells' resistance ranges on the package) as well as having gone through the photoresistor examples 5-3 and 5-4 in the Getting Started with Arduino book, I discovered that the resistance on the photocells increases with the *absence* of light. So, running the demo code, when I cover the photoresistor with my hand, then the LEDs do indeed light up.

With that, I am pleased to report that your demo code which was intended for use with the pressure sensor also works swimmingly without modification with the RS Cadmium-Sulfide Photocell 5-pack (RS part #276-1657)...

Pics:
http://img196.imageshack.us/img196/2678/ldrduino.jpg
(The setup: photocell at the right of the protoboard)

http://img39.imageshack.us/img39/1994/ldrduinoaction.jpg
(Covering the photocell, I don't think I will ever be a hand-model)

Thanks for this demo!

Michael


Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television




Check out more videos from MAKE.

Maker SHED

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter



    MAKE Archives

    Make: Money

    Make: Science Room
    Subscribe to MAKE Magazine!

    Make: Online editors and authors!

    Gareth BranwynGareth Branwyn
    Editor-in-Chief


    Phillip TorronePhillip Torrone
    Senior Editor
    | Web | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter


    John BaichtalJohn Baichtal
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Current Podcast

    itunesdl.gif Behind the Scenes at MAKE and CRAFT In January, many of the remote MAKE/CRAFT team members (myself included) convened at the Maker Media headquarters at O'Reilly Media in Sebastopol, California. Take a look behind the scenes of your favorite DIY publications as Goli Mohammadi gives us... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    Sign up for the Make: Newsletter

    Our Make: Newsletter covers news from maker Media, has original columns, Shed deals, and more! You can also read the archives of past issues.


     



    MAKE Fascination video series brought to you by Dow

    Make: Education
    MAKE: en Español MAKE: Japan
    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog