Introduction: Ultrasonic Distance Sensor Visualizer W/ Processing

this project uses processing to take data from an ultrasonic rangefinder on an arduino board and input that into variables in a processing sketch.

Step 1: Set Up Your Hardware

you will need:

ultrasonic rangefinder

arduino board


wire connect your rangefinder to GRD, 5V, and digital pin 10. the picture shows it set up with a breadboard that has an optional piezo buzzer wired as well, feel free to ignore that.

Step 2: Double Check Your Wiring Then Get Your Code Running

here is the arduino code:

const int pwPin = 10;

long pulse, inches, cm;

int measure = 0;

void setup() {



void loop() {

pinMode(pwPin, INPUT);

pulse = pulseIn(pwPin, HIGH);

inches = pulse/147;

cm = inches*2.54;

Serial.println(cm); measure = measure + 1;


Step 3: Get Your Processing Code Up and Running

first you will need to make sure you have the most updated form of processing and import the arduino libraries shown. (sketch-import library-add library-arduino(firmata)).

here is the code:

import processing.serial.*;
import cc.arduino.*;

int lf = 10; // Linefeed in ASCII

int value;

float numba = map(value, 12, 90, 10, 400);

String myString = null;

Serial myPort;

void setup(){


background (255);

size(800, 800);

myPort = new Serial(this, Serial.list()[0], 9600);

myString = myPort.readStringUntil(lf);


void draw(){

float numba = map(value, 12, 90, 10, 400);



drawCircle(width/2, height/2, numba);

branch(width/2, height, 180);

while (myPort.available()>0){

myString = myPort.readStringUntil(lf);

if (myString != null) {

myString = trim (myString);

value = int (myString);





void drawCircle(float x, float y, float numba){

ellipse (x, y, numba, numba);


drawCircle(x + numba/1.5, y, numba/2);

drawCircle(x - numba/1.5, y, numba/2);



void branch(float x, float y, float numba) {

line(x, y, x-numba, y-numba);

line(x, y, x+numba, y-numba);

if (numba>2){ branch(x-numba, y-numba, numba/2);

branch(x+numba, y-numba, numba/2);