Programming and writing about it.

echo $RANDOM

Tag: Arduino

Exploring Arduino: Arduino + Ethernet + DHCP with EtherTen

In the last post on Arduino, I briefly wrote about my experiments with setting up a wireless communication between a host computer and an Arduino board using XBee. Here, we explore talking to the world of the Internet(s!) using Arduino. Time for giving your Arduino an IP address! Instead of using an Ethernet shield on an Arduino UNO, I thought the EtherTen might be a better idea to try, since it has onboard Ethernet. I am glad, I chose Etherten for various reasons!

EtherTen

Arduino Webserver

Hook up your Arduino to your computer and plug in the Ethernet cable into the board, with the other end connected to a router. If you see the Webserver example (under File -> Examples -> Ethernet) shipped with the Arduino IDE, you will see that it uses the Arduino Ethernet library.

It however requires you to set up a static IP address for your Arduino. However, in a multi-user home/university network, there is a fair chance that the IP addresses are allocated dynamically, hence I would like to use DHCP in my Webserver sketch. Now, that said: using dynamic IP address for a web server isn’t the greatest of ideas technically. But, we shall not go there in this exercise.

I used Georg Kaindl’s Arduino DHCP library to obtain the IP address dynamically and integrated with the Web server example. Here is the complete sketch. Once, you compile and load the sketch to your board, and open the Serial Monitor, you should see something like this:


Attempting to obtain a DHCP lease...
A DHCP lease has been obtained.
My IP address is 10.0.0.97
Gateway IP address is 10.0.0.138
DNS IP address is 10.0.0.138

Now, go to your web browser, and type in: 10.0.0.97:8080 and you should see:

Arduino sends the sensor readings from the Analog pins 0-5 (I don't have any sensor connected to this pins, so they are just random readings)

Now, let us use the telnet  program to send a request to our Arduino:

Telnet session

Resources and misc.

That’s it for this time. (My previous posts on Arduino.)

Advertisement

Exploring Arduino: XBee and Sharp IR Distance Sensor

I have been exploring the Arduino since my last post. I haven’t gone around doing something very useful yet. However, I have been able to pick up the bits & pieces and more excitingly getting a hang of the engineering aspects of things. In a small mini-project, I need to have a wireless communication setup. I decided to use XBee for this and this tutorial worked great for me.

XBee Shield

XBee Shield with XBee Series 1 module + Arduino UNO

XBee USB Explorer

XBee USB Explorer + XBee S1 Module

XBee shield stacked on the top of the Arduino UNO. I had to use stacking headers to get a lift, since the XBee shield was protruding onto the USB connector of the UNO.

XBee Shield on the Arduino UNO (using stackable headers to create some vertical room)

The Sharp IR distance sensor connected to the UNO to transmit sensor data over XBee:

XBee "powered" Arduino UNO with the Sharp IR Distance Sensor attached

I hooked up the Sharp IR distance sensor to transmit the distance over XBee to my explorer module. This post helped me get very accurate readings from the distance sensor.

Arduino sketch

Here is the Arduino sketch:

/*
XBee Controller
xbee_controller.pde
*/

/* Sensors*/
int IRpin=1; /* IR Distance Sensor */

/*Actuators*/
int ledPin=13;

/* Read the incoming command*/
int command;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
 }

void loop()
{

   while (Serial.available()>0)
    {

      command = Serial.read();

      if (command==49) //1
        { /* following code is taken from the blog post linked above*/
          /* Read the IR sensor data and send */
          float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
          float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
          Serial.println(distance);                       // print the distance
         }

      if (command == 50) //2
         {
           /*Let there be light!*/
           digitalWrite(ledPin,HIGH);

         }
    }

}

Communicating with the “remote” XBee

On your host with the XBee explorere connected, open a serial terminal to /dev/ttyUSB0 either using screen (screen /dev/ttyUSB 9600) or using ‘minicom’.
If you send a ‘1’, you should get the IR sensor data and if you send a ‘2’, the LED will light up.

As you can see from above sketch, I intend to extend this sketch to respond to more commands and hence activate different actuator or send any other sensor data.

Exploring Arduino: Beginnings on Arch Linux

I ordered the Arduino starter kit from Robot Gear which came with the Arduino UNO board and few other essentials – LEDs, Miniature breadboard, Connecting wires, etc. The generic instructions for Linux mentioned in the Arduino playground worked for me without any problems. You will also have to set the permissions for the normal user in Arch Linux to be able to access the serial ports. Please see the Arch Wiki page on Arduino here. I was blinking a LED in no time!

Arduino programming

My first impression of looking at Arduino code is that its based on C/C++, which it is (See here). The Foundations page looks a handy starting point. Personally, I found this booklet provided by Oomlout a very handy reference, you can just hook up the board and start tinkering around with the notes by your side and pretty much learn the basics.

Development Environment

For now, I am just using the Arduino SDK from here. I intend to switch to a relatively mouse free environment right after this post by following the tips on the Arch Wiki above and also here.

Next, I describe my first real experiment with the Arduino board.

Blinking  LEDs

This is a Arduino UNO connected to a miniature breadboard on which you can see a Yellow and a Red LED connected to two digital pins on the UNO via two 330 Ohm resistors. (Excuse the tissue paper base :-) ).

Arduino UNO connected with 2 LEDs on a miniature breadboard

Circuit
The schematic is the same as in CIRC-01, but for two LEDs. So basically you would have one wire going from Pin 12 to one LED, and from Pin 13 to another. Each of them would be connected to the Ground via a 330 Ohm resistor.

Arduino code

What we have here is very simple. The two LEDs shall blink alternately with a delay, which you can specify. This is the code for the above Arduino circuit:

/* Multiple LED Blinking program
  Amit Saha

*/  

// constants won't change. Used here to 
// set pin numbers:
const int numPins = 2;
const int ledPin [] =  {12,13};      // the number of the LED pins

int interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // Iterate over each of the pins and set them as output
  for(int i=0;i<numPins;i++)
    pinMode(ledPin[i], OUTPUT);      
}

/* Loop until death */
void loop()
{ 
  for(int i=0;i<numPins;i++)
  {    
    digitalWrite(ledPin[i],HIGH);
    delay(interval); 
    digitalWrite(ledPin[i],LOW);
    delay(interval);        
  } 
}

The basic manuals for Arduino programming shall tell you that the setup() function is executed when you upload your sketch to the board and the loop() function shall continue indefinitely till the power is not plugged out of the board.

The two LEDs, denoted by the integer variable numPins, are connected to the Digital pins 12 and 13 on the UNO board and this is specified by the ledPin[] array. The pins are specified in OUTPUT mode using pinMode() function. That completes the setup() portion of the code.

In the loop ( ) function, what we do is just iterate over each of the LEDs, set to HIGH, wait using delay( ), set it to LOW and do the same for the all the LEDs serially. Once you Verify and Upload your code to the board, you should see the LEDs blinking ON and OFF, one after the other.

As a next step, if you have more LEDs at your disposal, you may want to experiment with CIRC-02.