Many Ways to Blink 5: Ultrasonic Sensor to Computer

This tutorial turns your computer screen into one giant LED. You will be able to control the blink rate of you computer screen with the ultrasonic sensor. This is a little over kill :) but the point is to see your computer as an ouput block for the arduino. To do this we will use a new program called Processing. If you do not have it on your computer you should download and install it. Processing is the equivalent of Arduino but it works on the Computer as opposed to a microcontroller.

It was originally made to make it easier to do computer art visualizations and other computations.We will be using a prebuilt sketch that you can download from the tutorial page. This code uses the Processing equivalent of the Serial library to receive messages from the Arduino. The key thing about this though is that the Processing code uses the Serial port on the computer so you can NOT run the Serial Monitor at the same time as this code.

We will be sending messages from the Ardino to Processing in text. Whenever you are sending messages you run into a series of issues. The first one is how do does the computer know when you are done with a message. In human speech we know when someone is done talking when they pause. The computer does not have eyes :) and is much less sophisticated so it can not do this. Instead we must specifically tell the computer when the message is done. We do this with what is called a “termination character”. This is usually a single character that is put at the end of the message and the moment the computer sees this character it know that the message is complet and that it can process it. Any character can be used for this but it is best if it one that will never be used in a normal text. Conveniently is problem has been solved already. All of the character you see on this screen are part of the ascii character set. This defines the binary sequence the represent “a”, “b”, etc. They also included in the set characters that exist but are not displayed. One of these is the New Line (denoted “\n”) character or the number 13. This character tells the computer when to display character on the next line of the screen. You can think of it as what is done when you hit the Return key on your keyboard. Because this character is never displayed it makes a great termination character so that is what we will use for this tutorial.

Block Diagram

Light to Computer System


Code Steps

#include <Servo.h>
Servo myservo;  // create servo object to control a servo a maximum of eight servo objects  can be created
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
char Terminator = '\n';  


Serial.print("ON"); Serial.print(Terminator);
Serial.print("OFF"); Serial.print(Terminator);
myPort = new Serial(this, Serial.list()[0], 9600);


myPort = new Serial(this, Serial.list()[<your number>], 9600);
size (400, 400);

to the size of your screen. The run the program again. Now you have a one really expensive LED!

Full Code

      Adapted From: Analog Input by David Cuartielles and Tom Igoe
      Author: Malcolm Knapp
      Project: Ultrasonic Sensor to Computer
      Date: 4/10/14
      Version: 0.1
      Description: This code shows how to use a Ultrasonic Distance Sensor to control 
                   the blink rate of a computer screen.

    // ---------- included libraries ------------ 
    #include <Servo.h>
    #include <NewPing.h>

    // ---------- hardware pin defines  ----------- 
    int triggerPin = 12;      // select the pin ultrasonic trigger
    int echoPin = 11;      // select pint 

    // ---------- variable initialization  -----------
    int delayTime = 0;     //variable that holds the delay time in milliseconds
    int scaling = 1;
    unsigned int uS = 0;      // holds the time it took for the pulse to be recived
    unsigned int distance = 0; // holds the distance in centimeters
    int maxValue = 100;    // in centimeter
    int minValue = 0;      // in centimeter
    int maxDistance = 200;   
    char Terminator = '\n';  // in centimeters

    // ---------- library initialization  -----------  
    NewPing sonar(triggerPin, echoPin, maxDistance);

    void setup() {
      //---------- hardware declaration ----------

    void loop() {
      // Input
      delay(50);  // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
      uS =; // Send ping, get ping time in microseconds (uS).
      distance = uS / US_ROUNDTRIP_CM;  // convert time to distance
      // Debugging
      Serial.print("uS value: "); Serial.println(uS);
      Serial.print("Distance (cm): "); Serial.println(distance);

      // Processing 
      delayTime = map (distance, minValue, maxValue, 200, 1023);
      Serial.print ("Delay in milliseconds: "); Serial.println (delayTime);
      // Modes
      // None - put new modes here

      // Output   
      Serial.print("ON"); Serial.print(Terminator);
      Serial.print("OFF"); Serial.print(Terminator);