Posts Tagged ‘SRF05’

Arduino – Redefining the TV Remote

TVRemotes

We use them every day, but has no one got bored of pressing buttons on a stick, it’s far too much effort pressing buttons! Surely there are better ways to control a device? After doing some work with my Nikon camera using IR to control it, I wanted to do the same with other devices. Check out the video at the bottom of this post…

However, unlike the Nikon remote, my Samsung TV remote has many many buttons so each IR sequence sent from the remote will be different. This can be a problem when you want to decode the signals, which while not impossible I am lazy, so thankfully Ken Shirriff has built a library to do just that and while its built for TV remotes you can decode an IR signal to its raw pulses using it. Essentially the library senses IR and notes each pulse and gap between pulses, Kens library saves a lot of time and its well coded – I’ll cover the basics of it in a bit.

My idea is to capture the IR sequences and then using the Arduino send them by using different inputs other than buttons. My first idea is to use my SRF05 distance sensor (You can use any distance sensor) and the premise being that different distances from the sensor send different signals to the TV. So rather than pressing a button you just wave your hand above the sensor. Of course this is slightly limited but since I only have 5 channels (yep – only 5!) so it turned out to be quite feasible.

There are drawbacks to this of course – the main one being that you can only define so many actions in the sensors dectection range. But there is plenty of range to do the basics, power, sound and channel and by constantly measuring distances we can even say the direction of movement, up to down and vice versa, can have an effect on what signal to send. For example moving your hand closer to the sensor will change the channel down.

So first of all you may want to read some of my other tutorials/projects concerning IR and the SRF-05 and Sharp IR (it should also work well).
Arduino Nikon IR Intervalometer Camera Remote
SRF-05
– contains handy wiring diagram!
Arduino and Sharp GP2Y0A02 Infrared distance sensor

(Other Arduino projects and tutorials)

OK, next take a look at Ken Shirrifs IR library and guide here:
http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html

Arduino TV Remote Components

Arduino
Breadboard
IR Diode
3pin (NPN) Phototransistor/ IR receiver (
Radio Shack 276-640 IR receiver, Panasonic PNA4602, Vishay TSOP4838 – or just get one out an old mouse)
SRF-05 (or any distance measuring device e.g. Sharp IR GP2Y0A02)
Jumper wires

Oh and stating the obvious but you’ll also need a T.V with working remote to steal the signals from – course you can use other remotes (stereos etc..)

The circuits themselves are very very easy to build, an IR LED to pin 3, a IR receiver to pin 11 and the SRF-05 I’ve plugged into pins 2 and 4. I have all of them in one breadboard and it works very well (see below).

Using Kens Arduino TV Remote Library

If you download the library and then unzip it to your Arduino/Libaries directory (older versions, I think its Arduino/hardware/libaries). The library assumes that your phototransistor/ IR receiver is on digital pin 11 and your IR diode is on digital pin 3. Typically you want a IR receiver with a 38Khz range – they seem to work best for me.

How to get our TV infrared/ remote codes

First of all use Ken’s IRrecvDump example (should be in your examples menu) load this into your Arduino and begin to capture your remotes codes. My Samsung wasn’t recognised so I used the Raw codes – there’s plenty of documentation on Ken’s site for this – it’s really simple, even I could figure it out. You need to note how many pulses etc.. it decodes in the raw signal which helpfully is outputted e.g. Raw (68):

Now we process the codes slightly and put them in an array for each one now that we have our codes and the information we need to use them – since mine are in the raw format I need to clean them up slightly ready to be put in my code – just adding commas etc…

Now we can test the remote codes to make sure you can control your TV

Now using the IRsendDemo example, altering it my case to send the raw signal, we can test the codes to make sure that we can control the T.V – just use the basic sketch to send the codes which I edited slightly just to use an array for the raw code. You can check out the library files themselves to see the functions.

/*
 * IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
 * An IR LED must be connected to Arduino PWM pin 3.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include 

IRsend irsend;

// just added my own array for the raw signal
unsigned int powerOn[68] = {4450,4500,550,1700,500,1750,500,1750,500,600,550,600,500,600,550,600,500,600,550,1700,550,1700,550,1700,500,600,550,600,500,600,550,600,500,650,500,600,550,1700,500,650,500,600,550,600,500,600,550,600,500,600,550,1700,550,600,500,1700,550,1700,550,1700,550,1700,500,1750,500,1750,500};

void setup()
{
  Serial.begin(9600);
}

void loop() {

      // altered the code just to send/test my raw code
      irsend.sendRaw(powerOn,68,38);
      delay(100);

}

Add the distance sensor

This is actually the hardest bit and it’s not that hard really I just used my previous work and adapted it and wrote a few statements concerning the detected distance. You just have to spend some time debugging and getting your values right to ensure that your commands are only sent at the right time and that it doesn’t get confused. My code is still a little buggy if you’re not used to how to move your hand but it does work well once you’re used to it.

[ad#Google Ad in content]

/*
    http://luckylarry.co.uk
    Larrys alternative TV remote - oops no buttons!
    Sends signals to TV based upon sensor readings

    Makes use of Kens Shirriffs IRremote library
    An IR LED must be connected to Arduino PWM pin 3.
    Version 0.1 July, 2009
    Copyright 2009 Ken Shirriff
    http://arcfn.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
*/

#include
IRsend irsend;

const int numReadings = 5;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
int total = 0;                // the total of all readings
int average = 0;              // the average
int oldAverage = 0;           // the old average
int echoPin = 2;              // the SRF05's echo pin
int initPin = 4;              // the SRF05's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse
unsigned long distance = 0;   // variable for storing distance

// setup my arrays for each signal I want to send
unsigned int powerOn[68] = {4450,4500,550,1700,500,1750,500,1750,500,600,550,600,500,600,550,600,500,600,550,1700,550,1700,550,1700,500,600,550,600,500,600,550,600,500,650,500,600,550,1700,500,650,500,600,550,600,500,600,550,600,500,600,550,1700,550,600,500,1700,550,1700,550,1700,550,1700,500,1750,500,1750,500};
unsigned int soundUp[68] = {4450,4500,550,1700,550,1700,500,1750,500,600,550,600,500,600,550,600,500,600,550,1700,550,1700,550,1700,500,650,500,600,550,600,500,600,550,600,500,1750,500,1700,550,1700,550,600,500,600,550,600,500,600,550,600,500,600,550,600,550,600,500,1700,550,1700,550,1700,550,1700,500,1750,500};
unsigned int soundDown[68] = {4400,4550,500,1750,500,1700,550,1700,550,600,500,600,550,600,500,600,550,600,500,1750,500,1750,500,1700,550,600,500,650,500,600,550,600,500,600,550,1700,550,1700,500,600,550,1700,550,600,500,600,550,600,500,600,550,600,500,600,550,1700,550,600,500,1750,500,1750,500,1700,550,1700,550};
unsigned int channelUp[68] = {4400,4550,500,1700,550,1700,550,1700,550,600,500,600,550,600,500,600,550,600,500,1750,500,1700,550,1700,550,600,500,600,550,600,500,650,500,600,550,600,500,1700,550,600,550,600,500,1700,550,600,500,650,500,600,550,1700,500,600,550,1700,550,1700,550,600,500,1700,550,1700,550,1700,550};
unsigned int channelDown[68] = {4450,4500,500,1750,500,1750,500,1700,550,600,500,650,500,600,550,600,500,600,550,1700,500,1750,500,1750,500,600,550,600,500,600,550,600,500,600,550,600,500,650,500,600,550,600,500,1700,550,600,500,650,500,600,500,1750,500,1750,500,1750,500,1700,550,600,500,1750,500,1750,500,1700,550};

void setup() {
  // make the init pin an output:
  pinMode(initPin, OUTPUT);
  // make the echo pin an input:
  pinMode(echoPin, INPUT);
  // initialize the serial port:
  Serial.begin(9600);
}

 void loop() {

    // loop for a number of readings on the SRF-05 to get an average to smooth the results. Much like all my other examples
    for (index = 0; index<=numReadings;index++) {
      digitalWrite(initPin, LOW);
      delayMicroseconds(50);
      digitalWrite(initPin, HIGH);
      delayMicroseconds(50);
      digitalWrite(initPin, LOW);
      pulseTime = pulseIn(echoPin, HIGH);
      distance = pulseTime/58;
      total = total + distance;
      delay(10);
   }
    // store the previous reading
    oldAverage = average;
    // store the current reading
    average = total/numReadings;
    // debug to check for spikes in the sensor etc..
    Serial.println(average);

    // now the fun part...
    // if my distance is less than 5...
    if (average <= 5) {
      Serial.println("Power Off");
      // use Kens IR library to send my signal (array, number of items in array, Khz)
      irsend.sendRaw(powerOn,68,38);
      // these delays depend on how long it take my device to recognise the signal sent and to act - I don't want to send signals that aren't getting read etc..
      delay(5000);
      // otherwise if my hand is higher
    } else {
      // check to see if my hand is in the registered space above the sensor
      if (average <=20 && average >=10 && oldAverage >=10) {
        // the below statement is our sensitive the readings are so if the current and previous readings are different with a tolerance of +/- 1 we can look at the direction of movement
        if ((average != oldAverage)
        && (average+1 != oldAverage)
        && (average-1 != oldAverage)) {
          // if the current reading is higher than the previous, then my hand is moving upwards
          if (average > oldAverage) {
            Serial.println("Channel Up");
            irsend.sendRaw(channelUp,68,38);
            delay(2000);
          } else {
            // otherwise if it is below then my hand is moving downwards
            if (average < oldAverage && oldAverage <=20) {               Serial.println("Channel Down");               irsend.sendRaw(channelDown,68,38);               delay(2000);                        }                  }          // otherwise my hand must be stationary so check where it is.         } else {           // if my hand is stationary between 10 and 15 cms away from the sensor           if (average >= 10 && average <=15) {             Serial.println("Sound down");             irsend.sendRaw(soundDown,68,38);           } else {             // if my hand is a bit higher up...             if (average >= 16 && average <=20) {               Serial.println("Sound up");               irsend.sendRaw(soundUp,68,38);             }           }         }         }     }        // clear our index and total for the next reading just in case     if (index >= numReadings)  {
      index = 0;
      total = 0;
    }
}

[ad#Google Ad in content]

Arduino + Processing: Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 2: Visualising the Data

Arduino SRF 05 radar

This is where all the work is done to read an interpret the values from the servo and the sensor. If the readings are to erratic then you won’t have nice shapes. Also if you don’t allow enough time to the signals to be sent back then you’ll get false distance readings. So this code is only as good as your Arduino code and sensor setup.

Took me a few evenings to work this out to get it how I wanted, the hardest bit is the trigonometry involved which isn’t that difficult, everything else is done by loops and a few IF statements. I use the FOR loops alot because the all the elements can be displayed programmatically without having to write each rectangle, cirle and line to the screen with their own statement.

If you’re not familiar with Processing then head over to Processing.org. Processing is a tool/ IDE that lets you program and code graphics and animation, it’s free to use and pretty powerful – best part is it works hand in hand with Arduino in the same C/C++ style of code, letting us take data from Arduino and whatever’s plugged into it and then visualise it on screen, e.g. like a radar screen.

First we have to setup our variables, background and load in the serial port libraries to ensure we can read the data sent by the Arduino. We also have to include a function from the serial library called serialEvent() which listens for data being sent and allows us to read the data easily. We do some easy string work, splitting the serial port lines to get the servo position and sensor value.

Also we can setup the radar screen, drawing the text, measurements and display grid. With processing whats nearest the top of the draw() function is displayed first with everything subsequentally drawn on top of this. So our lines and text will be at the bottom of the function so it will always be visible. To draw the lines and measurements we use a FOR loop. The draw() function draws one frame, so we basically re-draw the frame 180 times – we use a couple of arrays to store the previous values to make it look continuous.

Now that we have those we can then begin to display the values on our screen. We use a FOR loop to loop through each item in our array, newValue and oldValue. These are setup to hold 181 items – 1 item per servo position with 1 extra just in case, we loop through these to constantly display the previous readings – if we were to use the servo position itself to iterate through the array then no previous data would be displayed since the servo position is always changing.

Thoughout we need to calculate the X and Y co-ordinates of each servo position and sensor distance reading. To get these values we use trigonometry using sine and cosine and converting the servo position to a radian using the sensor reading as the distance from the center from which to draw the point. To learn more about this and to save me writing it up check out this tutorial at Processing.org.

The Sketch
Below is the code used to create the radar screen, it has comments to help explain. Everything used is an in built function of the Processing language and you can find the reference for the functions that I use in the reference section on the Processing.org website. So methods such as fill(), stroke(), rect(), line() etc…

/*
luckylarry.co.uk
Radar Screen Visualisation for SRF-05
Maps out an area of what the SRF-05 sees from a top down view.
Takes and displays 2 readings, one left to right and one right to left.
Displays an average of the 2 readings
Displays motion alert if there is a large difference between the 2 values.
*/
import processing.serial.*;     // import serial library
Serial myPort;                  // declare a serial port
float x, y;                       // variable to store x and y co-ordinates for vertices
int radius = 350;               // set the radius of objects
int w = 300;                    // set an arbitary width value
int degree = 0;                 // servo position in degrees
int value = 0;                  // value from sensor
int motion = 0;                 // value to store which way the servo is panning
int[] newValue = new int[181];  // create an array to store each new sensor value for each servo position
int[] oldValue = new int[181];  // create an array to store the previous values.
PFont myFont;                   // setup fonts in Processing
int radarDist = 0;              // set value to configure Radar distance labels
int firstRun = 0;               // value to ignore triggering motion on the first 2 servo sweeps

/* create background and serial buffer */
void setup(){
  // setup the background size, colour and font.
  size(750, 450);
  background (0); // 0 = black
  myFont = createFont("verdana", 12);
  textFont(myFont);
  // setup the serial port and buffer
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('n');
}

/* draw the screen */
void draw(){
  fill(0);                              // set the following shapes to be black
  noStroke();                           // set the following shapes to have no outline
  ellipse(radius, radius, 750, 750);    // draw a circle with a width/ height = 750 with its center position (x and y) set by the radius
  rectMode(CENTER);                     // set the following rectangle to be drawn around its center
  rect(350,402,800,100);                // draw rectangle (x, y, width, height)
  if (degree >= 179) {                  // if at the far right then set motion = 1/ true we're about to go right to left
    motion = 1;                         // this changes the animation to run right to left
  }
  if (degree <= 1) {                    // if servo at 0 degrees then we're about to go left to right
    motion = 0;                         // this sets the animation to run left to right
  }
  /* setup the radar sweep */
  /*
  We use trigonmetry to create points around a circle.
  So the radius plus the cosine of the servo position converted to radians
  Since radians 0 start at 90 degrees we add 180 to make it start from the left
  Adding +1 (i) each time through the loops to move 1 degree matching the one degree of servo movement
  cos is for the x left to right value and sin calculates the y value
  since its a circle we plot our lines and vertices around the start point for everything will always be the center.
  */
  strokeWeight(7);                      // set the thickness of the lines
  if (motion == 0) {                    // if going left to right
    for (int i = 0; i <= 20; i++) {     // draw 20 lines with fading colour each 1 degree further round than the last
      stroke(0, (10*i), 0);             // set the stroke colour (Red, Green, Blue) base it on the the value of i
      line(radius, radius, radius + cos(radians(degree+(180+i)))*w, radius + sin(radians(degree+(180+i)))*w); // line(start x, start y, end x, end y)
    }
  } else {                              // if going right to left
    for (int i = 20; i >= 0; i--) {     // draw 20 lines with fading colour
      stroke(0,200-(10*i), 0);          // using standard RGB values, each between 0 and 255
      line(radius, radius, radius + cos(radians(degree+(180+i)))*w, radius + sin(radians(degree+(180+i)))*w);
    }
  }
  /* Setup the shapes made from the sensor values */
  noStroke();                           // no outline
  /* first sweep */
  fill(0,50,0);                         // set the fill colour of the shape (Red, Green, Blue)
  beginShape();                         // start drawing shape
    for (int i = 0; i < 180; i++) {     // for each degree in the array
      x = radius + cos(radians((180+i)))*((oldValue[i])); // create x coordinate
      y = radius + sin(radians((180+i)))*((oldValue[i])); // create y coordinate
      vertex(x, y);                     // plot vertices
    }
  endShape();                           // end shape
  /* second sweep */
  fill(0,110,0);
  beginShape();
    for (int i = 0; i < 180; i++) {
      x = radius + cos(radians((180+i)))*(newValue[i]);
      y = radius + sin(radians((180+i)))*(newValue[i]);
      vertex(x, y);
    }
  endShape();
  /* average */
  fill(0,170,0);
  beginShape();
    for (int i = 0; i < 180; i++) {
      x = radius + cos(radians((180+i)))*((newValue[i]+oldValue[i])/2); // create average
      y = radius + sin(radians((180+i)))*((newValue[i]+oldValue[i])/2);
      vertex(x, y);
    }
  endShape();
  /* if after first 2 sweeps, highlight motion with red circle*/
  if (firstRun >= 360) {
    stroke(150,0,0);
    strokeWeight(1);
    noFill();
      for (int i = 0; i < 180; i++) {
        if (oldValue[i] - newValue[i] > 35 || newValue[i] - oldValue[i] > 35) {
          x = radius + cos(radians((180+i)))*(newValue[i]);
          y = radius + sin(radians((180+i)))*(newValue[i]);
          ellipse(x, y, 10, 10);
        }
      }
  }
  /* set the radar distance rings and out put their values, 50, 100, 150 etc.. */
  for (int i = 0; i <=6; i++){
    noFill();
    strokeWeight(1);
    stroke(0, 255-(30*i), 0);
    ellipse(radius, radius, (100*i), (100*i));
    fill(0, 100, 0);
    noStroke();
    text(Integer.toString(radarDist+50), 380, (305-radarDist), 50, 50);
    radarDist+=50;
  }
  radarDist = 0;
  /* draw the grid lines on the radar every 30 degrees and write their values 180, 210, 240 etc.. */
  for (int i = 0; i <= 6; i++) {
    strokeWeight(1);
    stroke(0, 55, 0);
    line(radius, radius, radius + cos(radians(180+(30*i)))*w, radius + sin(radians(180+(30*i)))*w);
    fill(0, 55, 0);
    noStroke();
    if (180+(30*i) >= 300) {
      text(Integer.toString(180+(30*i)), (radius+10) + cos(radians(180+(30*i)))*(w+10), (radius+10) + sin(radians(180+(30*i)))*(w+10), 25,50);
    } else {
      text(Integer.toString(180+(30*i)), radius + cos(radians(180+(30*i)))*w, radius + sin(radians(180+(30*i)))*w, 60,40);
    }
  }
  /* Write information text and values. */
  noStroke();
  fill(0);
  rect(350,402,800,100);
  fill(0, 100, 0);
  text("Degrees: "+Integer.toString(degree), 100, 380, 100, 50);         // use Integet.toString to convert numeric to string as text() only outputs strings
  text("Distance: "+Integer.toString(value), 100, 400, 100, 50);         // text(string, x, y, width, height)
  text("Radar screen code at luckylarry.co.uk", 540, 380, 250, 50);
  fill(0);
  rect(70,60,150,100);
  fill(0, 100, 0);
  text("Screen Key:", 100, 50, 150, 50);
  fill(0,50,0);
  rect(30,53,10,10);
  text("First sweep", 115, 70, 150, 50);
  fill(0,110,0);
  rect(30,73,10,10);
  text("Second sweep", 115, 90, 150, 50);
  fill(0,170,0);
  rect(30,93,10,10);
  text("Average", 115, 110, 150, 50);
  noFill();
  stroke(150,0,0);
  strokeWeight(1);
  ellipse(29, 113, 10, 10);
  fill(150,0,0);
  text("Motion", 115, 130, 150, 50);
}

/* get values from serial port */
void serialEvent (Serial myPort) {
  String xString = myPort.readStringUntil('n');  // read the serial port until a new line
    if (xString != null) {  // if theres data in between the new lines
        xString = trim(xString); // get rid of any whitespace just in case
        String getX = xString.substring(1, xString.indexOf("V")); // get the value of the servo position
        String getV = xString.substring(xString.indexOf("V")+1, xString.length()); // get the value of the sensor reading
        degree = Integer.parseInt(getX); // set the values to variables
        value = Integer.parseInt(getV);
        oldValue[degree] = newValue[degree]; // store the values in the arrays.
        newValue[degree] = value;
        /* sets a counter to allow for the first 2 sweeps of the servo */
        firstRun++;
        if (firstRun > 360) {
          firstRun = 360; // keep the value at 360
        }
  }
}

[ad#Google Ad in content]

The end result
Here’s a video below of it all working! There is an issue to bare in mind with the SRF-05 and thats that it works best as a static range finder, it emits a conical wave which will bounce back off of anything small or large and has a wide detection field of vision so it may pick up something that’s not directly in front of it. As you can see by the below image the detection range is so wide that it doesn’t truly see all the gaps as its detected something else in its field of vision.

sonar-topdown

If there are less objects it works fine… time to look at other sensors I think – anyway here’s the video below just showing it working on my screen.

Part 1: Setting up the Circuit and Outputting Values
Part 3: Visualising the Data from Sharp Infrared Range Finder

Arduino + Processing – Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 1: Setting up the Circuit and Outputting Values

arduino servo SRF05

First things first, we need to build our circuit. This is the easy bit! We’ll be using the Arduino to control a servo that will rotate our sensor around 180 degrees. The Arduino will then send the value from the distance sensor along with the current angle of the servo to the serial port.

Before proceeding please take a moment to check out some of my other work with the SRF-05 and servos if you’re unfamiliar with either.
Arduino SRF-05 Tutorials
Arduino Servo Tutorials

I’m building this with the SRF-05 ultrasonic range finder/ distance sensor, but because this has a fairly wide field of detection it’s not very precise – I think I’ll end up trying a different range finder maybe an IR one as the SRF-05 works best as a static sensor/ detector, anyway…

Arduino Radar Parts list

SRF05 Ultrasonic range finder
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
1x Servo (has to need no more than 5v supply)
You’ll also need some way to mount the sensor to the servo.

Arduino Radar Servo Circuit

Straight forward, we have the Arduino providing power to the breadboard and we have the servo and the SRF-05 sharing this power. Then we have the servo output pin going to Arduino digital pin 9 and the SRF-05 pins going to digital pin 2 and 3. You’ll notice that in my pictures I have 2 servos – I’m just using the bottom one of the pair to rotate the sensor round. On your servo you’ll need to figure out a way to mount the sensor on to the servo wheel – I used a lot of blu-tac! You’ll also see I’ve mounted my sensor vertically so that the when the servo moves there’ll be less interference with recieving values – placing the sensor horisontally seemed to give differences of up to and sometimes over 5cm between the first and second readings.

My servos do tend to move a bit so I’ve used more blu-tak/ modelling clay to hold them down and in place – if the servos move other than the way they’re meant to then it means dodgy readings.

SRF05 pin layout
arduino-servo-SRF05
Simple rig to rotate sensor 180 degrees

Arduino SRF05 Radar Sketch

The hardest bit – rotate the servo from left to right, then right to left and for every degree of movement take a series of readings and send them to the serial port. We’ll want to produce an average reading value for consistancy. Unfortunately with this ultrasound sensor we have to be quite slow to make sure we’re getting accurate values and we have to allow time for the signal to come back each time and register in order to produce the average value.

We do the rotation using a for loop to count to 180 and for each iteration we move the servo by +1 or -1 depending on which way we’re going – if you’ve hacked your servos then you can do a full 360 loop. During this loop we do another FOR loop to count to 10/ numReadings and for each iteration we add the distance measured to the total and after 10 readings we get our average by dividing the total by the number of readings. Then reset the total and the counter to start again for the next servo position. Finally before finishing the  the FOR loop for the servo we output the servo position and average reading to the serial port each with a preceeding character for us to later use to identify the values when reading the serial port in Processing. The last line is using println which will start a new line for the next set of values – each reading has its own line in the serial buffer makign it much easier to get our values back out.

/*
luckylarry.co.uk
Radar Screen Visualisation for SRF-05
Sends sensor readings for every degree moved by the servo
values sent to serial port to be picked up by Processing
*/
#include             // include the standard servo library
Servo leftRightServo;         // set a variable to map the servo
int leftRightPos = 0;         // set a variable to store the servo position
const int numReadings = 10;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
int total = 0;                // the total of all readings
int average = 0;              // the average
int echoPin = 2;              // the SRF05's echo pin
int initPin = 3;              // the SRF05's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse
unsigned long distance = 0;   // variable for storing distance

/* setup the pins, servo and serial port */
void setup() {
  leftRightServo.attach(9);
  // make the init pin an output:
  pinMode(initPin, OUTPUT);
  // make the echo pin an input:
  pinMode(echoPin, INPUT);
  // initialize the serial port:
  Serial.begin(9600);
} 

/* begin rotating the servo and getting sensor values */
void loop() {
  for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) {  // going left to right.
    leftRightServo.write(leftRightPos);
      for (index = 0; index<=numReadings;index++) {            // take x number of readings from the sensor and average them
        digitalWrite(initPin, LOW);
        delayMicroseconds(50);
        digitalWrite(initPin, HIGH);                           // send signal
        delayMicroseconds(50);                                 // wait 50 microseconds for it to return
        digitalWrite(initPin, LOW);                            // close signal
        pulseTime = pulseIn(echoPin, HIGH);                    // calculate time for signal to return
        distance = pulseTime/58;                               // convert to centimetres
        total = total + distance;                              // update total
        delay(10);
      }
    average = total/numReadings;                               // create average reading

    if (index >= numReadings)  {                               // reset the counts when at the last item of the array
      index = 0;
      total = 0;
    }
    Serial.print("X");                                         // print leading X to mark the following value as degrees
    Serial.print(leftRightPos);                                // current servo position
    Serial.print("V");                                         // preceeding character to separate values
    Serial.println(average);                                   // average of sensor readings
  }
  /*
  start going right to left after we got to 180 degrees
  same code as above
  */
  for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) {  // going right to left
    leftRightServo.write(leftRightPos);
    for (index = 0; index<=numReadings;index++) {
      digitalWrite(initPin, LOW);
      delayMicroseconds(50);
      digitalWrite(initPin, HIGH);
      delayMicroseconds(50);
      digitalWrite(initPin, LOW);
      pulseTime = pulseIn(echoPin, HIGH);
      distance = pulseTime/58;
      total = total + distance;
      delay(10);
    }
    average = total/numReadings;
    if (index >= numReadings)  {
      index = 0;
      total = 0;
    }
    Serial.print("X");
    Serial.print(leftRightPos);
    Serial.print("V");
    Serial.println(average);
   }
}

[ad#Google Ad in content]

Part 2: Visualising the Data
Part 3: Visualising the Data from Sharp Infrared Range Finder

Arduino – Basic Theremin meets Processing!

Arduino piano theremin

My last theremin involved a small speaker. Now I’ve replaced the speaker with my PC, using processing to pick up the values from the SRF05 ultrasound sonar distance sensor and play different notes accordingly – which gives multiple possibilities and far better sounds.

So to start with check out my previous theremin, the circuit is the same except for removing the speaker and the Arduino code now prints values to the serial port instead of outputing directly to a speaker. The only thing that I’m doing here that is really new is using the minim libary for Processing, built by this chap (thankyou). What this allows us is to assign a sound file to a variable in Processing and then gives us functions to start/stop the sound.

In my processing code you’ll see that I’ve got 12 sounds, one for each chromatic note and at the moment they’re from a piano. To get sounds and samples to use you can sign up to freesound.org. Mine I got here from ‘pinkyfinger’ and once you’ve decided which sounds you want to use then the rest is fairly easy – the more samples and octaves the better. I’ve used the piano set here just so I can hear the different notes and I’ve only 12 notes (chromatic scale) but eventually Iwould like to expand this to say 48 notes.

When you get your sounds I’ve found it’s easiest to use .wavs but minim will allow you to use WAV, AIFF, AU, SND, and MP3 files. Also I store my sounds in the root of the folder where I’m saving my processing sketch.

Ok, lets start with the Arduino parts, circuit and sketch.

Arduino Theremin Parts

SRF05 Ultrasonic range finder
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board (you can actually do this without a breadboard)
Jumper/ Connector wires

The Theremin Arduino Circuit

Basically the same as before just without the speaker on it. For more info on the SRF05 checkout my past stuff here. In this instance Processing is going to do all the work, so the circuit just needs to pass values from the SRF05 to Arduino, which in turn passes values to my PC via the USB cable.

piano-theremin

The Arduino Theremin Sketch

Very cut down version, it takes the value as before and converts it to a distance and then just prints it to a new line in on the serial port.

// written at: luckylarry.co.uk
// very easy Theremin combined with processing
// sketch prints out distance to the serial port
// processing picks up value and plays notes accordingly
// no annoying speaker drone! :)

// setup pins and variables for SRF05 sonar device
int echoPin = 2;                                // SRF05 echo pin (digital 2)
int initPin = 3;                                // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds
unsigned long distance = 0;                     // variable for storing the distance (cm) we'll use distance as a switch for the speaker

//setup
void setup() {

  pinMode(initPin, OUTPUT);                     // set init pin 3 as output
  pinMode(echoPin, INPUT);                      // set echo pin 2 as input
  Serial.begin(9600);                           // start the serial port

} 

// execute
void loop() {
  digitalWrite(initPin, HIGH);                  // send 10 microsecond pulse
  delayMicroseconds(10);                        // wait 10 microseconds before turning off
  digitalWrite(initPin, LOW);                   // stop sending the pulse
  pulseTime = pulseIn(echoPin, HIGH);           // Look for a return pulse, it should be high as the pulse goes low-high-low
  distance = pulseTime/58;                      // convert the pulse into distance (cm)

  // make the sound.
  // check the distance, if over 50cm make no sound - send no signal
  if (distance > 50) {
    Serial.println(distance);                   // print the distance value to the serial port
    delay(50);                                  // delay for 50 milliseconds before starting again...
  }
}

[ad#Google Ad in content]
Next up we have processing, which will look for values on the serial port, so first upload the Arduino code to your board and test it works. Next start up Processing to finish off the code. If like me you’re fairly new to processing there’s lots of info to be had at: http://www.arduino.cc/playground/Interfacing/Processing

The Processing Theremin Sketch

With your code now running on the Arduino board we now need processing to pick up these values. Previously I had something of an issue getting these values and reading them. But in hindsight I was being a bit dim when I realised I could just look in the serial port for when a new line (n) was printed which would tell me that a new value was about to be sent. Of course thats providing your Arduino sketch is using Serial.println() (print line) instead of Serial.print() which just chucks every value into one long string – and if you look at my previous attempts with processing you can laugh at how I started splitting this huge string up etc… Oh hindsight is a wonderful thing, anyway, I digress, below is the processing sketch:

// written at: luckylarry.co.uk
// very easy Theremin combined with processing
// sketch prints out distance to the serial port
// processing picks up value and plays notes accordingly
// no annoying speaker drone! :) replace the sounds and delays with
// your own...

import processing.serial.*;                // import serial library so we can read the serial port
import ddf.minim.*;                        // import minim library

// define the serial port
Serial myPort;          

// define minim variables:
// here we say that variable A is an audiosample etc...
Minim minim;
AudioSample GSharp;
AudioSample A;
AudioSample Bb;
AudioSample B;
AudioSample C;
AudioSample CSharp;
AudioSample D;
AudioSample Eb;
AudioSample E;
AudioSample F;
AudioSample FSharp;
AudioSample G;

// setup
void setup () {

  // set up the variables, loading in the sound files from your project folder
  // which should be the same place as where you save this sketch
  // details on using minim and audioSample are here: http://code.compartmental.net/tools/minim/manual-audiosample/
  minim = new Minim(this);
  GSharp = minim.loadSample("GSharp.wav", 2048);
  A = minim.loadSample("A.wav", 2048);
  Bb = minim.loadSample("Bb.wav", 2048);
  B = minim.loadSample("B.wav", 2048);
  C = minim.loadSample("C.wav", 2048);
  CSharp = minim.loadSample("CSharp.wav", 2048);
  D = minim.loadSample("D.wav", 2048);
  Eb = minim.loadSample("Eb.wav", 2048);
  E = minim.loadSample("E.wav", 2048);
  F = minim.loadSample("F.wav", 2048);
  FSharp = minim.loadSample("FSharp.wav", 2048);
  G = minim.loadSample("G.wav", 2048);

  // List serial ports, saves us trying to figure out which COM we're using.
  println(Serial.list());
  // Open the active port - providing you've only got one sending serial data (which you should)
  myPort = new Serial(this, Serial.list()[1], 9600);
  // don’t read the serial buffer until we see a new line - this is genius and simple compared with my last efforts
  myPort.bufferUntil('n');
}

void draw() {
  // we need to declare the draw function even though we're not using it!!
}

void serialEvent (Serial myPort) {
  // get the string from the serial buffer - gets all chars until the next line break...
  String bufferString = myPort.readStringUntil('n');

  if (bufferString != null) {
    // get rid of any whitespace - sometimes the serial buffer can have blanks etc.. in the string
    bufferString = trim(bufferString);
    // convert the value to an int - we're only sending numbers over the serial port so parsing it to an int shouldn't ever be an issue.
    float inByte = float(bufferString);
    int pulse = int(bufferString);         // declare a variable to hold our value.
    println(pulse);                        // for debug print the value so we can check it.

    // remember that our pulse is in CM so if its less than 5cm then do this etc... else do this... else do this.. for as many sound samples
    if ( pulse < 5 ) {
      GSharp.trigger();
      delay(25);
    }
    else if ( pulse < 8 ) {
      A.trigger();
      delay(25);
    }
    else if ( pulse < 11 ) {
      Bb.trigger();
      delay(25);
    }
    else if ( pulse < 14 ) {
      B.trigger();
      delay(25);
    }
    else if ( pulse < 17 ) {
      C.trigger();
      delay(25);
    }
    else if ( pulse < 20 ) {
      CSharp.trigger();
      delay(25);
    }
    else if ( pulse < 23 )  {
      D.trigger();
      delay(25);
    }
    else if ( pulse < 26 ) {
      Eb.trigger();
      delay(25);
    }
    else if ( pulse < 29 ) {
      E.trigger();
      delay(25);
    }
    else if ( pulse < 32 ) {
      F.trigger();
      delay(25);
    }
    else if ( pulse < 35 ) {
      FSharp.trigger();
      delay(25);
    }
    else if ( pulse < 38 ) {
      G.trigger();
      delay(25);
    }
    else if ( pulse > 50 ) {
      // if the distance is greater than 50cm then play nothing
    }

  } // end if there's a value in the serial bufferstring

}   // end void serialevent()

[ad#Google Ad in content]

Arduino – A Basic Theremin

basic theremin

Theremins are cool. Fact. You may also have heard of them as either aetherphone/ etherophone or termenvox/ thereminvox. Essentially its an electronic music instrument that plays a certain note depending on the position of your hand and its distance from an antenna.

Building a very basic one with Arduino is easy – easier than my basic Arduino drum machine. Very easy. Basically we can replicate one by building a circuit of a speaker and a distance sensor, such as my favourite the SRF05 ultrasound thingy. We calculate the distance and the nearer you get the difference in note/frequency emitted from the speaker.

You’re going to need only a few parts and not much code. You can read more about my applications of the SRF05 along with sample code here: Arduino SRF05 Projects. If you can’t be arsed with reading any of that then no worries all is revealed below…

Arduino Theremin Parts List

1x speaker
SRF05 Ultrasonic range finder
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires

Arduino Theremin Circuit

Very simple,we have a speaker with the negative running to Arduino’s GND, and it’s positive going to digital pin 6 on the Arduino board. The code will tell the speaker to turn on and off rapidly based on the objects distance from the ultrasound sensor. This rapid turning on and off of the speaker is what will generate different sounds (like my drum machine). The only other part of the circuit is the SRF05 distance/ proximity sensor. It has a positive and negative connected to the Arduino and then 2 pins for trigger and echoing an ultrasound wave both going to digital pins 2 and 3 respectively on the Arduino board.

basic-theremin-circuit

Theremin Arduino Sketch

To save the annoyance of those around you I’ve added in a small statement that basically says if your hand is not within 30 cm of the sensor then don’t play a sound. But otherwise basically we measure distance constantly and when an object is detected we convert the distance in to a value for which to use in the oscillation of the speaker to generate different pitchs and frequencies.

// written at: luckylarry.co.uk
// very easy Theremin

// setup pins and variables for SRF05 sonar device
int echoPin = 2;                                // SRF05 echo pin (digital 2)
int initPin = 3;                                // SRF05 trigger pin (digital 3)
int speakerPin = 6;                             // Speaker pin
unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds
unsigned long distance = 0;                     // variable for storing the distance (cm) we'll use distance as a switch for the speaker
unsigned long soundDelay = 0;                   // variable for storing the deay needed for the pitch

//setup
void setup() {

  pinMode(speakerPin, OUTPUT);                  // sets pin 6 as output
  pinMode(initPin, OUTPUT);                     // set init pin 3 as output
  pinMode(echoPin, INPUT);                      // set echo pin 2 as input

 } 

// execute
void loop() {
  digitalWrite(initPin, HIGH);                  // send 10 microsecond pulse
  delayMicroseconds(10);                        // wait 10 microseconds before turning off
  digitalWrite(initPin, LOW);                   // stop sending the pulse
  pulseTime = pulseIn(echoPin, HIGH);           // Look for a return pulse, it should be high as the pulse goes low-high-low
  distance = pulseTime/58;                      // convert the pulse into distance (cm)
  soundDelay = pulseTime/3;                     // alter this variable to alter the pitch of the sound emitted

  // make the sound.
  // check the distance, if over 30cm make no sound
  if (distance < 30) {
  digitalWrite(speakerPin, HIGH);
  delayMicroseconds(soundDelay);
  digitalWrite(speakerPin, LOW);
  delayMicroseconds(soundDelay);
  }
}

[ad#Google Ad in content]

Pretty cool for no parts - but of course the sound quality isn't great. I guess you could add a switch in the circuit so you could manually start and stop it to produce clearer notes.

Obstacle avoidance Arduino robot – build your own larryBot

larryBotv06

So after 5 previous versions that had various flaws, I now have an Arduino robot that actually works and although basic is very cheap – although there a probably a few more flaws so please point them out to me but this is a good start on how to make your own robot.

In order to catch up, please see my previous posts below, describing the problems that the other 5 versions had, how the h-bridge chip works and using the SRF05 ultrasound distance sensor.

larryBot – Arduino robot versions 0.1 to 0.5 lessons learned
Control a DC motor with Arduino and L293D chip

Arduino SRF05 Distance Sensor

Now that you’re up to speed, lets start by fixing the flaws in the previous version, this was the case that my motors were drawing way too much current and the L293D chip from ST Micro couldn’t output enough current for each motor.

So, I replaced the chip with the snappy named ‘SN754410‘ from Texas Instruments. This has EXACTLY the same 16 pin layout as the L293D chip and all of the same features except that it can output 1.2 amps per channel rather than the now tiny 0.6 amps of the L293D. Pin configuration diagram below is the same for the L293D as it is for the SN754410, I recommend the SN754410 Arduino comination.

L293D Pin layout

Great I’ve now got more current to my motors, but their stall current is still at over 2 amps, I could add a heatsink to the chip and pass more current through it, but instead I got some more efficient motors than the Mabuchi FA-130’s that came with the Tamiya gearbox. These motors are made by Solarbotics and are their RM3 series which fit perfectly, can handle 4 times the voltage but use a fraction of the current – typically at 9v they use just over 1 amp. Perfect.

Having corrected this, larryBot v0.6 was go! I still faced a lack of power to the DC motors – either because my batteries were running low or not able to supply the current. But since my new motors could run up to 12 volts (instead of the puny 3v of the originals) I decided to use a 9v battery to power them instead of my 4 AA’s.

Watching larryBot move is great, even on carpet and with the tank tracks 9 times out of ten he can climb small obstacles or has enough traction to shunt them out the way. Anyway enough waffling – here’s how he’s made…

The Arduino Robot Tracked Chassis

You could use anything you want really – construction sets, your own custom fabricated chassis etc… But since I’m cheap I managed to get a pile of foamboard for my chassis. I can waste and reuse as much of this as I want so its no problem if I make a mistake or want to improve it. Also in theory this leads to rapid prototyping, so when I do decide to fabricate a chassis I know exactly where the best places are for holes, mounts etc…

The robot chassis parts and tools:

Small Phillips/ cross-head screwdriver
Gluegun
Craft knife
Pencil
Ruler
Assorted nuts and bolts – A good set of M series nuts and bolts
Foamboard 5mm thick – 1 A4 sheet is plenty
Tamiya gearbox 70097 – assembled in mode A
Tamiya track and wheel set 70100
Elastic bands (normally dropped by the postie)

Sizing up the robot base

First of all the size of our chassis design is dictated by a few things. The axle length: our tank tracks need about 5mm clearance so the space on the axle is roughly 65mm wide that I can mount on. Next we have the length of the tracks and how many wheels will be used, I kept my track footprint small so my chassis length didn’t need to be much bigger than the gearbox. Which leads on to gearbox positioning – the Tamiya gearbox I have is roughly 75mm in length and the shape of the tracks will dictate where to position the gearbox as the driving wheels are attached to this. The final consideration of course is mounting all the sensors, battery packs, breadboard and the Arduino board.

In my attempts so far I have a base that is just longer than twice the length of the gearbox (175mm) which gives me space at the front for sensors and space at the back for batteries. I then mount a smaller piece of foamboard on top of this that then houses the gearbox and spaces it far enough above the running wheels for the tracks at the bottom – also giving enough tension in the tracks for them not to slip off (unlike larryBot v0.4). From here I can continue to bolt on additional structures to position the breadboard and so on.

So using this knowledge you should be able to size up and cut the foamboard to the dimensions you need – a craft knife will be more than enough to cut this board. To make the holes needed for your screws and bolts just use a small Phillips/ Cross-head screwdriver to bodge a hole through – it won’t take any effort, then you can drive the screws through this guiding hole. If you have washers then use them but the foamboard seems to be able to support all the hardware fine.

Attaching the running wheels and tank tracks

First mark out the position of where you want your wheels, very important as you don’t want them wonky!

larryBotv06-base

To mount the running wheel axles on to the chassis I used a couple of small hexagonal bolts for each side of the axle and then used the glue gun to fix them to the chassis – the best way to do this is to put the bolts on to the axle, use a small amount of glue to hold the bolts in place and then use a shit load of glue over the bolts to secure them properly.

When adding the wheels to the axles, don’t push them all the way on as these axles are slightly shorter than the Tamiya gearbox which will cause you problems with the tracks.

Mounting the Tamiya gearbox, DC motors, sensors, breadboard, Arduino and batteries

To attach the gearbox I just used the screws supplied with the gearbox and bolted this to my smaller piece foamboard. I then in turn bolted this to the main chassis using 4 long bolts and a series of spacers and nuts in between the layers to given the correct spacing and adjustment for my drive wheels.

For the SRF05 distance sensor I just used some blu-tack/ modelling plasticine to hold it in place for now.

The breadboard I mounted above the gearbox, which for this I just fixed it on top of 4 long bolts which then in turn attached the gearbox base. The Arduino board currently then sits on the breadboard held on by the multitude of wires running from it and the power supply cable.

And for the batteries, since I scrapped using the 4xAA’s to power the motor I only had to worry about two 9V batteries, 1 of which was my DC power supply for the board. I fixed them to the chassis just using an elastic band, since I’d want to get to them easily enough.

larryBotv06-finished

Simple Arduino Robot Circuit

The parts list doesn’t differ much from my other tutorials for motors and L293D. But I did find it was troublesome to get the parts from the same supplier, so be aware that you may need to look at multiple suppliers and postage may get expensive.

Robotic parts list

2 x Solarbotics RM3 motors
SN754410 motor driver chip
SRF05 Ultrasonic distance sensor
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
2x 220nF multilayer ceramic capacitor (Y5V)
2 x 50V 10uF Capacitor (although I’ve not used them here)
2.1 mm coaxial DC jack
2 x PP3 9Volt battery
PP3 9Volt Connector
9Volt battery holder

larryBotv06-board

You can see that the circuit is pretty simple, nothing actually that fancy, I have the SRF05 using the +5v, GND and digital pins 12 and 13. The SN754410 then uses the digital pins 9 and 10 to control each channel – these can use PWM to do speed control, then there are the switch pins on the h-bridge that go to digital pins 3,4,5 and 6. The spare GND is used to join the GND connection between the motor power and Arduino power supply. Here are the instructions for the 9v battery DC supply. If you want to use the extra 50v 10 uF capacitors then these sit on the power supply for pins 8 and 16 on the SN751140 respectively.

larryBotv06-SN754410-layout

Arduino Robot Code

Nothing much has changed from the larryBot v0.1-0.5 sketch except that I’ve altered the detection distances as I have a much faster response time from the robot.

const int numOfReadings = 10;                   // number of readings to take/ items in the array
int readings[numOfReadings];                    // stores the distance readings in an array
int arrayIndex = 0;                             // arrayIndex of the current item in the array
int total = 0;                                  // stores the cumlative total
int averageDistance = 0;                        // stores the average value

// setup pins and variables for SRF05 sonar device

int echoPin = 12;                               // SRF05 echo pin (digital 2)
int initPin = 13;                               // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds
unsigned long distance = 0;                     // variable for storing the distance (cm)

int motor1Pin1 = 3;                             // pin 2 on L293D
int motor1Pin2 = 4;                             // pin 7 on L293D
int enable1Pin = 9;                             // pin 1 on L293D
int motor2Pin1 = 5;                             // pin 10 on L293D
int motor2Pin2 = 6;                             // pin  15 on L293D
int enable2Pin = 10;                            // pin 9 on L293D

void setup() {
  // set the motor pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enable2Pin, OUTPUT);
  // set enablePins high so that motor can turn on:
  digitalWrite(enable1Pin, HIGH);
  digitalWrite(enable2Pin, HIGH);

  pinMode(initPin, OUTPUT);                     // set init pin 3 as output
  pinMode(echoPin, INPUT);                      // set echo pin 2 as input

  // create array loop to iterate over every item in the array

  for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  digitalWrite(initPin, HIGH);                  // send 10 microsecond pulse
  delayMicroseconds(10);                                // wait 10 microseconds before turning off
  digitalWrite(initPin, LOW);                   // stop sending the pulse
  pulseTime = pulseIn(echoPin, HIGH);           // Look for a return pulse, it should be high as the pulse goes low-high-low
  distance = pulseTime/58;                      // Distance = pulse time / 58 to convert to cm.
  total= total - readings[arrayIndex];          // subtract the last distance
  readings[arrayIndex] = distance;              // add distance reading to array
  total= total + readings[arrayIndex];          // add the reading to the total
  arrayIndex = arrayIndex + 1;                  // go to the next item in the array                                 

  // At the end of the array (10 items) then start again
  if (arrayIndex >= numOfReadings)  {
    arrayIndex = 0;
  }

  averageDistance = total / numOfReadings;      // calculate the average distance
  delay(10);

  // check the average distance and move accordingly

  if (averageDistance <= 10){
    // go backwards
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);    

  } 

  if (averageDistance <= 25 && averageDistance > 10) {
    // turn
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
  }
  if (averageDistance > 25)   {
    // go forward
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);     

  }
}

[ad#Google Ad in content]

Some problems you may face – if like my you don’ t have a spare 9V battery connector to hand check this connection if nothing is happening – I used blu-tack to hold my wires in place so it’s a bit temperamental.
Check that your motor wires are properly in contact with the motor terminals if you haven’t soldered them again using some blu-tack or tape is handy for getting a good connection.
Motor’s are under strain – your tracks are too tight.
Tracks come away from the wheels – check your tracks are not too loose and that your running wheels are in line with the drive wheels – the Tamiya gearbox is slightly wider than the Tamiya track and wheel set axles.

larryBotv06-frontback

I’ve gotten a fairly cheap robot that avoids obstacles, next plan is to extend it to sense various things – for instance detect motion and move towards it, or a light/ heat source. The robot costs are quite high if you factor in the Arduino board and if you don’t have any of the parts – but this can be broken down and used for many other projects so you’ll get a lot of reuse out of these bits, but I reckon that the total cost is around £70-80 in total, so fairly cheap when compared to other bots. Of course if you don’t want tracks (?) then you can just use wheels instead, Tamiya do also make wheels that will fit the gearbox.

Just in case you have trouble getting parts, here’s a small list of people that can supply the various bits – although none of them will have the full set. Shipping from the states is an option, but check the shipping costs as it may negate the cost savings. Please let me know of other sources, the list is in no particular order.

Sparkfun – USA: motor controller and Tamiya parts
Pololu – USA: Tamiya parts and motors
Techbotics – UK: Tamiya parts – just about cheaper than getting parts from Sparkfun/ Pololu in the USA
Active robots – UK: motors, SRF05 but generally overpriced on everything
Rapid Electronics – UK/EU/USA: most component parts and hardware
Farnell – UK/EU/USA: SN754410 chip and most components but shit for orders if your billing and delivery addresses are separate
Mouser – UK/EU/USA: SN754410 chip and most components
SK Pang – UK: SN754410 chip but dodgy VAT calculations (charges tax on shipping as well) few other parts here.

If you need an Arduino board, I reliably found a seller on ebay from Hong Kong that will sell and ship you aboard for far less than paying for it the UK – downside is it takes about a week to arrive.

larryBot – Arduino robot versions 0.1 to 0.5 lessons learned

larryBotv01

So I’ve decided to build a robot using Arduino to control the sensors. Here’s my progress so far – I am hoping that my mistakes here and over the coming posts will guide people in more detail about what to avoid. Also I won’t provide a parts list until I have a working robot…

The purpose of my early version of larryBot is to create a basic robot that can be controlled with Arduino and that can be made from minimal materials and costs – I aimed with the Arduino included for this cost to be about £60-70 in total depending on what you can scrounge and providing you have the tools and some parts already. Also I wanted to make a robot from scratch rather than hacking together existing things, like modifying a RC car chassis for example. So far all larryBots aim is to do is to avoid obstacles and move around them.

First off I looked at using the L293D chip to control 2 DC motors and this would operate my steering and I would use the SRF05 ultrasound range finder as a proximity sensor. I had no problem with this circuit when I first started until today.

My first consideration was the chassis, now I figure that most people like me have spent the money on the Arduino board, which is worth every penny, and you probably have a few motors and bits with which to do this. However, like me, you looked at chassis and what to build one from to mount all the electronics. Theres the Tamiya erector set (if you can get it in the UK), Meccano, Lego and of course specific robot chassis’s not to mention designing your own and getting it made.

All very expensive (unless you have it already) and requires effort. I am lazy and karma happened to bless me with finding a stash of 5mm thick foamboard for me to use. If you aren’t aware foamboard is used for modelling and mounting work for display – if you need any then get a design agency to come and quote for work at your company and keep their foamboard :)

Anyway, the foamboard is excellent for my uses so far as my robots aren’t massive and their weight is minimal. It also allows me to rapidly prototype a chassis and layout without any costs – you can just push screws and bolts through to mount motors and use a glue gun to join parts. Another bonus is that you only need a craft knife to cut it.

To connect everything I just brought a set of various nuts and bolts to use along with a glue gun and thats it.

My circuit throughout has basically remained the same, I use a 6v power supply to power the L293D and the motors, and a 9V DC power supply to power the Arduino, which controls the L293D and reads the Ultrasound sensor. My changes so far have been on the chassis, motors, movement and gearing. The sketch for these versions has also remained the same which you can see detailed below:

larryBot v0.1-05 Arduino Robot Sketch

const int numOfReadings = 10;                   // number of readings to take/ items in the array
int readings[numOfReadings];                    // stores the distance readings in an array
int arrayIndex = 0;                             // arrayIndex of the current item in the array
int total = 0;                                  // stores the cumlative total
int averageDistance = 0;                        // stores the average value

// setup pins and variables for SRF05 sonar device

int echoPin = 12;                               // SRF05 echo pin (digital 2)
int initPin = 13;                               // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds
unsigned long distance = 0;                     // variable for storing the distance (cm)

int motor1Pin1 = 3;                             // pin 2 on L293D
int motor1Pin2 = 4;                             // pin 7 on L293D
int enable1Pin = 9;                             // pin 1 on L293D
int motor2Pin1 = 5;                             // pin 10 on L293D
int motor2Pin2 = 6;                             // pin  15 on L293D
int enable2Pin = 10;                            // pin 9 on L293D

void setup() {
  // set the motor pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(enable2Pin, OUTPUT);
  // set enablePins high so that motor can turn on:
  digitalWrite(enable1Pin, HIGH);
  digitalWrite(enable2Pin, HIGH);

  pinMode(initPin, OUTPUT);                     // set init pin 3 as output
  pinMode(echoPin, INPUT);                      // set echo pin 2 as input

  // create array loop to iterate over every item in the array

  for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  digitalWrite(initPin, HIGH);                  // send 10 microsecond pulse
  delayMicroseconds(10);                                // wait 10 microseconds before turning off
  digitalWrite(initPin, LOW);                   // stop sending the pulse
  pulseTime = pulseIn(echoPin, HIGH);           // Look for a return pulse, it should be high as the pulse goes low-high-low
  distance = pulseTime/58;                      // Distance = pulse time / 58 to convert to cm.
  total= total - readings[arrayIndex];          // subtract the last distance
  readings[arrayIndex] = distance;              // add distance reading to array
  total= total + readings[arrayIndex];          // add the reading to the total
  arrayIndex = arrayIndex + 1;                  // go to the next item in the array                                 

  // At the end of the array (10 items) then start again
  if (arrayIndex >= numOfReadings)  {
    arrayIndex = 0;
  }

  averageDistance = total / numOfReadings;      // calculate the average distance
  delay(10);

  // check the average distance and move accordingly

  if (averageDistance <= 25){
    // go backwards
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);    

  } 

  if (averageDistance <= 45 && averageDistance > 25) {
    // turn
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
  }
  if (averageDistance > 45)   {
    // go forward
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);     

  }
}

[ad#Google Ad in content]

I’ve gone down the route so far of using differential steering, this means that to turn left 1 motor goes forward while the other goes in reverse. I did look at using a servo to mimic an RC cars steering but this added to complexity which for now I want to avoid.

larryBot v0.1 – 0.2

This version had two rear mounted wheels that turned either way via the L293D motor driver giving me differential steering. I also mounted a single axle at the front with 2 wheels on this. At first this one moved fine on a smooth floor but then I noticed the motor mounted wheels were slipping so I bonded them to the axle using some araldite (strong glue). This was going fine but then I noticed that when used on a different surface larryBot couldn’t turn as effectively.

larryBot v0.3

So I figured that perhaps it was the fact that the steering would be limited by having 2 front wheels, so I removed these and built in a single wheel at the front for the bot to pivot on to aid steering. Saddly this wasn’t happening and larryBot still had problems on carpet. Next I looked at the motors, essentially they were just tranferring power to the wheels via a worm gear. This isn’t really any good to get any decent torque out of the motors, so perhaps a better gear system would help me out here. I also figured that realistically wheels may not be that great for the best transport, so I looked instead at getting some tracks.

larryBot 0.4

To keep costs down I spent a while looking for a small gearbox system and track set. Logically and costwise I arrived at the decision to get a Tamiya gearbox (Tamiya part No. 70097) with motors included and a Tamiya track and wheel set (70100), the total for which including postage was about £25 you can order from the states but there are a few places in the UK that you can order from too.

I created a long wheel base much like a standard tank and mounted everything on to a new chassis cut from foamboard – nothing complicated, just a rectangle with some bolts to hold the gear box on. To add the axles to the chassis, I used some nuts and glued them on to the chassis with the glue gun to create a way to hold the axles in place.

This worked out great – larryBot conquered the carpet! Hooray! But now with the gearbox he was too fast and kept throwing his tracks off the wheels when ever he turned so he never seemed to run for more than a few seconds – no where near as long as the previous versions. Back now to rethink the wheel base and track setup.

larryBot v0.5

So today I made a new chassis, much shorter wheel base and the tracks are much tighter, tested it with the motors wired up to the battery directly and larryBot now turns fine!

I added in the circuit board and larryBot started to run BUT… the motors began to cut out frequently and the L293d chip began to get very hot. Hmmmm…. It turns out that the motors supplied with the Tamiya gear box which are Mabuchi FA-130’s draw up to 2.2A stall current per motor! the L293D chip can only deliver a maximum of 0.6A to the motor which is barely enough to freely run the motor and with tracks on this will run the motor for less than 5 seconds before cutting out

So now it looks like I have 2 choices and I think I will do both – firstly I’ll look at a new motor driver chip which can handle higher current loads and secondly I’m going to replace the gearbox motors with a motor which has a much lower stall current than the huge 2A! This will also have a nice bonus of making my batteries last longer as well as my components – perhaps also allowing me to use just 1 power supply.

Now I need new motors/ control chip

Now I need new motors/ control chip, he

Arduino & Processing – Getting values from SRF05 ultrasound sensor & serial port

arduino processing

I’ve started to delve into Processing and passing values between Processing and Arduino. If you’re wondering what Processing is, basically its an open source programming language for vizualising data that can interface with Arduino either by reading values/ pins or by setting them. Just remember that they are 2 very different things and require 2 different sketches!

This is very cool if you want to display information from Arduino in a graphical way, or if you want to have a physical input device for your computer, for instance a Flash application that takes inputs from switches and potentiometers.

So anyway, it’s really very easy to get this installed and working, just do the following steps here: http://www.arduino.cc/playground/Interfacing/Processing

Now, I’ve had a bit of an issue getting values from my SRF05 from Arduino to Processing. In Arduino it works fine and the serial port shows the values correctly but in Processing I couldn’t get the values. Some people suggest using Firmata but the trouble with this is that it has very few of the functions that I need for example delayMicroseconds() is not available when using Firmata.

Also I saw alot of people trying to get this working using different mode settings for the SRF05 but to be honest I like my code so I want to use that – I use separate echo and trigger pins rather than a singular pin. I reckon this may also help you out should you be having problems getting other values.

Another issue I ran across was converting serial port output to an integer, it just didn’t like this at all so I found a quick solution into fixing this. Although this introduces another error which we have to catch but now I think I have a set of code that works pretty well for what I need.

So first of all take a look at my Arduino SRF05 tutorial.

Arduino SRF05 Sketch

Now you’ve got the circuit and parts use the following sketch on the Arduino board:

const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int echoPin = 2;             // the SRF05's echo pin
int initPin = 3;             // the SRF05's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse
unsigned long distance = 0;  // variable for storing the distance

void setup() {

  // make the init pin an output:
  pinMode(initPin, OUTPUT);
  // make the echo pin an input:
  pinMode(echoPin, INPUT);
  // initialize the serial port:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
     readings[thisReading] = 0;
  }
  // set the serial port to begin logging values which Processing will later read.
  Serial.begin(9600);
}

void loop() {
  // send the sensor a 10microsecond pulse:
  digitalWrite(initPin, HIGH);
  delayMicroseconds(50);
  digitalWrite(initPin, LOW);

  // wait for the pulse to return. The pulse
  // goes from low to HIGH to low, so we specify
  // that we want a HIGH-going pulse below:

  // converts the microsecond time to distance in CM
  pulseTime = pulseIn(echoPin, HIGH);
  distance = pulseTime/58;

  // subtract the last reading:
  total= total - readings[index];
  // read from the sensor:
  readings[index] = distance;
  // add the reading to the total:
  total= total + readings[index];
  // advance to the next position in the array:
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)  {
    // ...wrap around to the beginning:
    index = 0;
  }
  // calculate the average:
  average = total / numReadings;  

  // print out an A character to set the start of the value and help us
  // cast this value to a string and integer later on in Processing.
  Serial.print('A');

  // now write out our value from the sensor.
  Serial.print(average);
  // delay must be the same as the delay in the processing code
  delay(100);
}

[ad#Google Ad in content]

It’s not too different from the previous tutorial, just gets rid of the LED and the main thing to note is the serial port output at the bottom. We add an A to mark the beginning of the value in the serial output and we use this later on to help cast ou value as a string and then integer, for some reason trying to pass the serial value as an integer or trying to parse it as such fails.

SRF05 Processing Sketch

Now for the Processing code we just need this bit and that’s it – we’re just looking in the log file here to see the values being read correctly. Basically we do a few checks to make sure we have no null values, that the output we’re reading is of expected length and has the correct identifying symbol at the front, you don’t have to use ‘A’, this can actually be anything you want.

// import the serial libary for Processing
import processing.serial.*; 

// define a new port object
Serial port;

// setup the port, this referes to the port object above
// COM8 is my USB port that I use, your's maybe anythign like COM1, etc..
// 9600 is the baud rating (how much data is transferred over a set time)
// it is important to make sure your Arduino is outputting to the same baud rating!!

void setup(){
     port = new Serial(this, "COM8", 9600);
}

// begin our output for Processing
void draw(){

  // delay 100 milliseconds - this must be the same delay used in the Arduino sketch.
  delay(100);

  // create a string to store the value from reading the serial port
  String serialBuffer = port.readString();

  // check if the value is not null, that the value is greater that 2 and no more than 4 characters
  if (serialBuffer != null && serialBuffer.length() >= 2 && serialBuffer.length() <= 4) {

    // check that the first character in the string is A
    // we add A to our output to cast the serial value to a string type.
    // Its also handy as the A is doubled up as a marker for a new value in the serial log
    if (serialBuffer.indexOf("A") == 0) {
      // if this is true then remove A
      serialBuffer =  serialBuffer.substring(1,serialBuffer.length());
    }
    // double check to make sure there are no more A's in our string - otherwise we can't convert to a number
    if (serialBuffer.indexOf("A") == -1) {
      // convert the string to an integer
      int i = Integer.parseInt(serialBuffer);
      // print our integer
      println(i);
    }
  }

}

[ad#Google Ad in content]

Make sure...

Make sure that your serial port is set to the same baud rating in both the Arduino and Processing sketch.

Make sure that the delay used in both executable methods matches.

Make sure that the correct port is being read.

Arduino – Sonic range finder with SRF05

SRF05

A guide to using the SRF05 Distance Sensor with Arduino in order to calculate distances from objects. In this case I’m also altering the output of an LED with PWM according to how close an object is to the sensor. So the nearer you are the brighter the LED.

So if we start with the SRF05, it’s an IC that works by sending an ultrasound pulse at around 40Khz. It then waits and listens for the pulse to echo back, calculating the time taken in microseconds (1 microsecond = 1.0 × 10-6 seconds). You can trigger a pulse as fast as 20 times a second and it can determine objects up to 3 metres away and as near as 3cm. It needs a 5V power supply to run.

Adding the SRF05 to the Arduino is very easy, only 4 pins to worry about. Power, Ground, Trigger and Echo. Since it needs 5V and Arduino provides 5V I’m obviously going to use this to power it. Below is a diagram of my SRF05, showing the pins. There are 2 sets of 5 pins, 1 set you can use, the other is for programming the PIC chip so don’t touch them!

SRF05 pin layout

SRF05 Arduino Components

220 Ohm resistor (Red, Red, Brown, Gold)
SRF05 Ultrasonic range finder
LED
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
Optional 9V DC power supply or use the USB power for the Arduino

Arduino SRF05 Circuit

Very, very simple circuit, I’ve used the breadboard to share the GND connection and to add the LED which I could probably have done with out the breadboard. You’ll see the most complex thing is the code later on.

Arduino-SRF05

SRF05 Arduino Distance Sensor sketch

All the work is done here, I’ve added code that averages the distance readings to remove some of the jitter in the results as the SRF05 is calculating distances very rapidly and there can be a lot of fluctuation. Also I convert the time in microseconds to distance by dividing the time by 58.

Why 58? Well because if you take the time in microseconds for a pulse to be sent and received e.g. for 1 meter it takes about 5764 microseconds – at least from my wall anyway. If I divide this time by the distance in cm in I will get 57.64 so I just round this up – you can calculate distance in any other unit with this method.

Here I’ve also decided that for every cm under 255 my LED will get 1 step brighter. I’ve been lazy here for the sake of the sensors 3 metre range I didn’t see the point in making this any more complicated. Otherwise I would calculate the brightness on the percentile of proximity out of total range.

[ad#Google Ad in content]

// written at: luckylarry.co.uk
// variables to take x number of readings and then average them
// to remove the jitter/noise from the SRF05 sonar readings

const int numOfReadings = 10;                   // number of readings to take/ items in the array
int readings[numOfReadings];                    // stores the distance readings in an array
int arrayIndex = 0;                             // arrayIndex of the current item in the array
int total = 0;                                  // stores the cumlative total
int averageDistance = 0;                        // stores the average value

// setup pins and variables for SRF05 sonar device

int echoPin = 2;                                // SRF05 echo pin (digital 2)
int initPin = 3;                                // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds
unsigned long distance = 0;                     // variable for storing the distance (cm)

// setup pins/values for LED

int redLEDPin = 9;                              // Red LED, connected to digital PWM pin 9
int redLEDValue = 0;                            // stores the value of brightness for the LED (0 = fully off, 255 = fully on)

//setup

void setup() {

  pinMode(redLEDPin, OUTPUT);                   // sets pin 9 as output
  pinMode(initPin, OUTPUT);                     // set init pin 3 as output
  pinMode(echoPin, INPUT);                      // set echo pin 2 as input

  // create array loop to iterate over every item in the array

  for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
 }
// initialize the serial port, lets you view the
 // distances being pinged if connected to computer
     Serial.begin(9600);
 } 

// execute
void loop() {
digitalWrite(initPin, HIGH);                    // send 10 microsecond pulse
delayMicroseconds(10);                  // wait 10 microseconds before turning off
digitalWrite(initPin, LOW);                     // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH);             // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58;                        // Distance = pulse time / 58 to convert to cm.
 total= total - readings[arrayIndex];           // subtract the last distance
readings[arrayIndex] = distance;                // add distance reading to array
total= total + readings[arrayIndex];            // add the reading to the total
arrayIndex = arrayIndex + 1;                    // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings)  {
    arrayIndex = 0;
  }

  averageDistance = total / numOfReadings;      // calculate the average distance

  // if the distance is less than 255cm then change the brightness of the LED

  if (averageDistance < 255) {
    redLEDValue = 255 - averageDistance;        // this means the smaller the distance the brighterthe LED.
  }

  analogWrite(redLEDPin, redLEDValue);          // Write current value to LED pins
  Serial.println(averageDistance, DEC);         // print out the average distance to the debugger
  delay(100);                                   // wait 100 milli seconds before looping again

}

[ad#Google Ad in content]

Well this is going to make the sensor for a robot methinks. I'll alter this to control a servo so turn left or right when blocked, or perhaps to alter the speed of the motors. Or maybe I'll just give myself bat like senses, maybe even fight crime! Anyway below is the quick video of it in action: