Learning to Program the Arduino

Listing 4. Fade an LED On/Off Using a Flex Sensor

/* Flex sensor + LED
/* Analog Input
* Demonstrates analog input by reading an analog sensor
* on analog pin 0 and turning on and off a light emitting
* diode (LED) connected to digital pin 13.
* The amount of time the LED will be on and off depends
* on the value obtained by analogREAD().
* Created by David Cuartielles
* Modified 16 Jun 2009
* By Tom Igoe
* http://arduino.cc/en/Tutorial/AnalogInput
* Modified 16 July, 2011
* By Amit Saha
* Current code was tested with a Flex sensor
int sensorPin = 0; /*Flex sensor pin */
int ledPin = 13; /* LED pin*/

void setup() {
  pinMode(ledPin, OUTPUT);

void loop() {

  int loop=1;
  float sensorValues=0.0,delaytime;
  for(loop=1;loop <=10 ;loop++)
     sensorValues = sensorValues + analogRead(sensorPin);

  //Use the average as a delay value
  delaytime = sensorValues/10;

  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin, LOW);


The flex sensor has two pins: one end should be connected to the +5V input, and the other pin should be connected to the analog pin 0 and farther on to the ground via a 10 kOhm resistor.

Serial Communication

All the sketches so far have made use of the Arduino library calls to read and write to the Arduino pins. You haven't directly made use of the serial communication between the host computer and the Arduino board. First, I'll describe how to write a basic client/server style program.

The "server" program is a sketch that lives on the Arduino board waiting for serial data to be available (an integer in this case) and sends back the number by adding 1 to it to the "client".

Listing 5. Example of a Server Sketch

/* Serial communication demo: +1*/

/* Server program
Amit Saha*/

int number;

void setup()
  // Open the serial connection, 9600 baud

void loop()
  // Get the data "packet"
  // Wait for some data to arrive
  if (Serial.available()>1) {

      Serial.println(number + 1);


The client side of the program is written in Processing using its serial library (Listing 6).

Listing 6. Example of a Serial Client Sketch

/*Client for Serial communication*/
/* Amit Saha */

import processing.serial.*;

Serial myPort; // The serial port

// initial variables:
int i = 1; // counter
char inData;

void setup () {

size(400, 350); // window size

// List all the available serial ports

// Pick up the first port, since I usually have
// just the Arduino connected.
// Make sure the correct port is selected here.
myPort = new Serial(this, Serial.list()[0], 9600);

// set initial background:

background(255); }

void draw () {


  if (myPort.available() > 0) {
    inData = (char)myPort.read(); // Typecast it to the corresponding
                                  // character for the
                                  // ASCII value


void serialEvent () {

To run the client, download the processing IDE and create a new sketch with the code as shown in Listing 5. Run this code after you have uploaded the server sketch (Listing 5) to the Arduino.

Controlling a Servo

Now, let's make things move with your Arduino code. For this example, let's control the movement of a servo—that is, start and stop the servo and control its speed and rotation. Three wires protrude out of the servo: control (white/yellow), power (red) and ground (black/brown). First, set up the circuit such that the the control wire is connected to the Arduino's digital pin 2, the red wire is connected to the Arduino's 5V input and the black wire to the ground. Now, upload the sketch (Listing 7) to your Arduino. You also optionally may connect an LED to the digital pin 13 (in the same way you connected a single LED earlier), which will turn on or off depending on whether the servo is rotating.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Really interesting

marksen's picture

Arduino is a really intersting project because its way to use are limitless. It can be adapted to many things and it's quite easy to program.

I love the Arduino! Thanks

Pit's picture

I love the Arduino! Thanks for the interesting article.

Processing, C/C++ and Arduino

SomeTom's picture

...a language that looks very similar to C and is based on Processing.

Well, I'd rather say it's a language that looks like Processing (Arduino is using the Processing IDE) but is based on C/C++. There's AVR's version of gcc under the hood, while Processing is running on top of Java.
Although the simplified C/C++ inside the IDE is looking mostly like Processing, Arduino really is not Processing at all. If you used to play around with Processing before you tried to code some Arduino you might make some weird experiences, as it really seems to be the same - until you try to try to initialize arrays or do some string operations.
After hours of really weird behaviour, I looked a little deeper into the docs and was quite surprised to find out that I had unknowingly been running Java-like code through a C-compiler for half a day :)
The funniest thing about it was that it actually worked most of the time...

Thank you for the comment.

Amit Saha's picture

Thank you for the comment. Perhaps, "based on Processing" is not very true.

However to me, the language looks "very" C. And yes as you pointed out, its really gcc-avr down below as also pointed out in the later parts of the article. My experience with Processing is not beyond a basic level, so I came to Arduino "from" C, rather than processing.


Great article. Just a brief

Fabpicca's picture

Great article.
Just a brief observation.
Arduino doesn't stand for "good friend" in Italian (I'm Italian). As far as I know Arduino is the name of the cafè Arduino inventors where used to meet.

But Wikipedia says so?

AmitSaha's picture

Hello: Thanks for your comment. I am sorry if I got it wrong, But Wikipedia has this: "Arduino" is an Italian masculine first name, meaning "strong friend". The English version of the name is "Hardwin".(From: http://en.wikipedia.org/wiki/Arduino#History)


You're mistaken.

Anonymous's picture

Amit is a Hebrew name which has the meaning "Friend" but it's not correct to say that Amit is the Hebrew word for friend. Just because the name's origin is Italian doesn't mean it's a current Italian word for something.

Name of Cafe, Name of King

MoChaMan69's picture

The name Arduino comes from the cafe the founder(s) frequented where they thought of the project . Cafe Arduino takes it's name from medievel king that briefly ruled the area . I can't find my source right now but it's something like King of Ardo , therefore he was an Arduino , hence Cafe Arduino , hence the Arduino Project . Much of this comes from the IEEE article a few months ago .

Unfortunately, the lack of research in the first sentence makes me doubt the entire 5 page article . I'll glance at it later anyway in case there's something useful . - MC

I'm from Italy, therefore I

From_Italy's picture

I'm from Italy, therefore I can give a definitive answer. Arduino is the name of a bar in Ivrea (Piedmont, Italy) frequented by the (Italian) creators of the open source framework. The bar was named after Arduin of Ivrea, first King of Italy from 1002 to 1017. By the way, Arduino is not a common name nowadays in Italy


Roland's picture

The IO pins can't drive much: 5v and 40ma each, not to exceed 120ma total. You need a tiny relay to do anything more than power an LED, and even there you need to limit the current to prevent burnout. I have had luck with tiny reed relays connected directly to the IO pins. See www.elexp.com p/n 22RD5, with 5v/500ohm coils. These draw only 10ma. Buy more than you need and test them all. Two of the 12 I ordered had open coils. And put a diode across any relay coils. The Arduino already has pull-up/pull-down diodes on all the pins, which is why the AD pins read around 2.5v if not connected. Remember, when you stop applying any power to a coil, the magnetic field collapses which produces a brief voltage spike. Also, any of the above reed relays can also be used as magnetic sensors to detect an open window, for instance. Just glue a small magnet nearby.

Figures 2 and 3

OP's picture

The LEDs shown in figures 2 and 3 are shorted as illustrated. The edge rows on most (if not all) breadboards are buses.

Ground wires and tty

SomeTom's picture

Right. Also the ground wire of the rightmost LED does not connect anywhere. It should be in the same column as the LED it is supposed to ground.
If you are using a recent version of Arduino (and a newer original board) you are more likely to find /dev/ttyUSB0 that /dev/ttyACM0. (Just a typo, but /dev/tttyACM0 won't do any good at all).