Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!


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

Marc de Vinck

I’m currently working full time as the Dexter F. Baker Professor of Practice in Creativity in the Masters of Engineering in Technical Entrepreneurship Program at Lehigh University. I’m also an avid product designer, kit maker, author, father, tinkerer, and member of the MAKE Technical Advisory board.


Related

Comments

  1. dan says:

    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<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);
    }else if (val>=175 && val< =250){ //if value is between 100 and 175
    digitalWrite(led5,LOW);
    digitalWrite(led4,HIGH); //turns on 4 LEDs
    digitalWrite(led3,HIGH);
    digitalWrite(led2,HIGH);
    digitalWrite(led1,HIGH);
    }else if (val>=100 && val< =175){ //if value is between 100 and 175
    digitalWrite(led5,LOW);
    digitalWrite(led4,LOW);
    digitalWrite(led3,HIGH); //turns on 3 LEDs
    digitalWrite(led2,HIGH);
    digitalWrite(led1,HIGH);
    }else if (val>=25 && val< =100){
    digitalWrite(led5,LOW);
    digitalWrite(led4,LOW);
    digitalWrite(led3,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led1,HIGH);
    }else if (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
    }

    1. Marc de Vinck says:

      @dan,

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

  2. µD says:

    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

    1. Marc de Vinck says:

      @µ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?

  3. fai says:

    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

    1. Marc de Vinck says:

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

  4. rsbohn says:

    // 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?

  5. µD says:

    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

    1. Marc de Vinck says:

      @µ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 ;)

  6. fai says:

    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

  7. Marc de Vinck says:

    @fai

    I was thinking size.

    Looks like some bit shifting to me!

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

  8. rsbohn says:

    //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;
    }

    1. Marc de Vinck says:

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

  9. SamF says:

    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

    1. Marc de Vinck says:

      @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.

  10. cassiano rabelo says:

    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

  11. Chris says:

    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.

    1. Marc de Vinck says:

      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

      1. Michael Gold says:

        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