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

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:

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

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.


For more detail: Arduino & Processing – Getting values from SRF05 ultrasound sensor & serial port

Clip to Evernote

Leave a Comment

(Spamcheck Enabled)

Read previous post:
Arduino Wireless Irrigation System
SMS controlled Wireless Irrigation System using an Arduino

UPDATE 02/13/11 : Voting has started for the Sparkfun Microcontroller Contest. Please VOTE for me! UPDATE 02/26/11 : I won...

Scroll to top