Skip navigation

Distance Sensor PING)))

In this episode of How-To Tuesday from Make Magazine they take a look at the PING))) sensor which can measure distance using sonar. This setup may not work for all distance sensors since the PING))) is unique in that it connects to a digital pin. If your sensor connects to an analog pin you will need a different setup/sketch.

Materials you’ll need:

  • Arduino Duemilanove
  • PING))) Distance Sensor
  •  

    Arduino Sketch
    You can find this sketch under File > Examples > Sensors > Ping

    /* Ping))) Sensor
      
       This sketch reads a PING))) ultrasonic rangefinder and returns the
       distance to the closest object in range. To do this, it sends a pulse
       to the sensor to initiate a reading, then listens for a pulse 
       to return.  The length of the returning pulse is proportional to 
       the distance of the object from the sensor.
         
       The circuit:
    	* +V connection of the PING))) attached to +5V
    	* GND connection of the PING))) attached to ground
    	* SIG connection of the PING))) attached to digital pin 7
    
       http://www.arduino.cc/en/Tutorial/Ping
       
       created 3 Nov 2008
       by David A. Mellis
       modified 30 Jun 2009
       by Tom Igoe
    
     */
    
    // this constant won't change.  It's the pin number
    // of the sensor's output:
    const int pingPin = 7;
    
    void setup() {
      // initialize serial communication:
      Serial.begin(9600);
    }
    
    void loop()
    {
      // establish variables for duration of the ping, 
      // and the distance result in inches and centimeters:
      long duration, inches, cm;
    
      // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
      // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
      pinMode(pingPin, OUTPUT);
      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);
    
      // The same pin is used to read the signal from the PING))): a HIGH
      // pulse whose duration is the time (in microseconds) from the sending
      // of the ping to the reception of its echo off of an object.
      pinMode(pingPin, INPUT);
      duration = pulseIn(pingPin, HIGH);
    
      // convert the time into a distance
      inches = microsecondsToInches(duration);
      cm = microsecondsToCentimeters(duration);
      
      Serial.print(inches);
      Serial.print("in, ");
      Serial.print(cm);
      Serial.print("cm");
      Serial.println();
      
      delay(100);
    }
    
    long microsecondsToInches(long microseconds)
    {
      // According to Parallax's datasheet for the PING))), there are
      // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
      // second).  This gives the distance travelled by the ping, outbound
      // and return, so we divide by 2 to get the distance of the obstacle.
      // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
      return microseconds / 74 / 2;
    }
    
    long microsecondsToCentimeters(long microseconds)
    {
      // The speed of sound is 340 m/s or 29 microseconds per centimeter.
      // The ping travels out and back, so to find the distance of the
      // object we take half of the distance travelled.
      return microseconds / 29 / 2;
    }
    

     

    PING))) Schematic

     

    PING))) Illustration

     

    Photos

    LED & Potentiometer
    This example shows you how to obtain a value from a potentiometer using the Arduino’s analog pins. We then take this value and use it to adjust the brightness of an LED connected to digital pin 10. Note the pin you use for the LED must be labeled PWM.

    Materials you’ll need:

  • Arduino Duemilanove
  • 10k Potentiometer
  • 1K ohm resistor (brown, black, red)
  • LED
  •  

    Arduino Sketch

    /* 
     * Potentiometer LED Dimmer
     * ------------------------
     *
     * Dims a LED connected to digital pin 10 based on the value obtained 
     * from a 10k potentiometer connected to analog pin 0.
     *
     * Created January 2010
     * Matt Wiechec
     *
     */
    
    int potPin = 2;    // select the input pin for the potentiometer
    int ledPin = 10;   // select the pin for the LED
    int potVal = 0;    // variable to store the value coming from the sensor
    
    void setup() {
      pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
      Serial.begin(9600);       // for debugging via the Serial Monitor
    }
    
    void loop() {
      potVal = analogRead(potPin);            // read the value from the sensor
      potVal = map(potVal, 0, 1023, 0, 255);  // scale it to use it with the LED
      
      analogWrite(ledPin, potVal);            // set brightness
    
      Serial.print("Pot Val: ");
      Serial.println(potVal); 
    }
    

     

    LED/Potentiometer Schematic

     

    LED/Potentiometer Illustration

     

    Photos

    Photocells
    Just a quick post about hooking up a photocell to your arduino. This setup should work for other variable resistors (flex, force, etc).

    Materials you’ll need:

  • Arduino Duemilanove
  • Photocell
  • 10K ohm resistor (brown, black, orange)
  •  

    Arduino Sketch

    /* 
     * Photocell Reading
     * -----------------
     *
     * Read an analog value from a photocell (variable resistor)
     * connected to analog pin 0.
     *
     * Created January 2010
     * Matt Wiechec
     *
     */
    
    int photocellPin = 0;     // select the input pin for the photocell
    int photocellVal;         // variable to store the analog valie from the cell
    
    void setup(void) {
      Serial.begin(9600);     // for debugging via the Serial Monitor
    }
    
    void loop(void) {
      photocellVal = analogRead(photocellPin);  
      
      Serial.print("Photocell Val: ");
      Serial.println(photocellVal);     // the raw analog reading
    
      delay(100);
    }
    

     

    Photocell Schematic

     

    Photocell Illustration

     

    Photos

    Monski Pong

    This project comes from Tom Igoe’s book Making Things Talk. It’s a great example of setting up serial communication between the Arduino and Processing environments. You’ll build an old school Pong game using variable resistors as the controllers.

    Materials you’ll need:

  • Arduino Duemilanove
  • 2 variable resistor sensors (ie. force and flex)
  • 4 x 10K ohm resistor (brown, black, orange)
  • 2 x Push Button
  •  

    Arduino Sketch
    You’ll need to get your hands on the book for this one. Check your local library.

     

    Monski Pong Schematic

     

    Monski Pong Illustration

     

    Photos

    Arduino, LEDs, & Buttons

    In this How-To Tuesday video they take a look at a simple button setup which lets you turn an LED on/off. This time we hook up an LED without using the build in resistor on pin 13.

    Materials you’ll need:

  • Arduino Duemilanove
  • LED
  • 1K ohm resistor (brown, black, red)
  • 10K ohm resistor (brown, black, orange)
  • Push Button
  •  

    Arduino Sketch
    You can find this sketch under File > Examples > Digital > Button
    Note: I’ve made a slight change to the code on line 27, changing ledPin=13; to ledPin=10;

    /*
      Button
     
     Turns on and off a light emitting diode(LED) connected to digital  
     pin 13, when pressing a pushbutton attached to pin 7. 
     
     
     The circuit:
     * LED attached from pin 10 to ground 
     * pushbutton attached to pin 2 from +5V
     * 10K resistor attached to pin 2 from ground
     
     * Note: on most Arduinos there is already an LED on the board
     attached to pin 13.
     
     
     created 2005
     by DojoDave <http://www.0j0.org>
     modified 17 Jun 2009
     by Tom Igoe
     
      http://www.arduino.cc/en/Tutorial/Button
     */
    
    // constants won't change. They're used here to 
    // set pin numbers:
    const int buttonPin = 2;     // the number of the pushbutton pin
    const int ledPin =  10;      // the number of the LED pin
    
    // variables will change:
    int buttonState = 0;         // variable for reading the pushbutton status
    
    void setup() {
      // initialize the LED pin as an output:
      pinMode(ledPin, OUTPUT);      
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT);     
    }
    
    void loop(){
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
    
      // check if the pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (buttonState == HIGH) {     
        // turn LED on:    
        digitalWrite(ledPin, HIGH);  
      } 
      else {
        // turn LED off:
        digitalWrite(ledPin, LOW); 
      }
    }
    

     

    LED/Button Schematic

     

    LED/Button Illustration

     

    Photos

    Arduino 101 & LED Blink

    The How-To Tuesday series of videos from Make Magazine are a great place to start if you’re new to the Arduino platform. In this episode they take a look at the simplest of set ups – hooking up an LED to the arduino board and making it blink. You can also find alot of great information on the Arduino site.

    Materials you’ll need:

  • Arduino Duemilanove
  • LED
  •  

    Arduino Sketch
    You can find this sketch under File > Examples > Digital > Blink

    /*
      Blink
     
     Turns on an LED on for one second, then off for one second, repeatedly.
     
     The circuit:
     * LED connected from digital pin 13 to ground.
     
     * Note: On most Arduino boards, there is already an LED on the board
     connected to pin 13, so you don't need any extra components for this example.
     
     
     Created 1 June 2005
     By David Cuartielles
     
     http://arduino.cc/en/Tutorial/Blink
     
     based on an orginal by H. Barragan for the Wiring i/o board
     
     */
    
    int ledPin =  13;    // LED connected to digital pin 13
    
    // The setup() method runs once, when the sketch starts
    
    void setup()   {                
      // initialize the digital pin as an output:
      pinMode(ledPin, OUTPUT);     
    }
    
    // the loop() method runs over and over again,
    // as long as the Arduino has power
    
    void loop()                     
    {
      digitalWrite(ledPin, HIGH);   // set the LED on
      delay(1000);                  // wait for a second
      digitalWrite(ledPin, LOW);    // set the LED off
      delay(1000);                  // wait for a second
    }
    

     

    LED Schematic

     

    LED Illustration

     

    Photos

    Here’s something I just came by on youtube which has some very odd shots, which could work as loops.


    Watch as loop here

    Rationale

    Chase is a conceptual video based around the idea of a loop, but also inspired by cinematic single take shots (such as the title sequence of JCVD). The goal was to take this sequence further and create a feeling of confusion on the part of the audience. When watching the loop; your view should sway between being chased and doing the chasing. There is also a presence of a dream like state when you continuously watch the loop run, the seamless transitions are meant to loss the viewer in never being able to out run or catch up to the subject. To further this feeling there are unexplainable illusions throughout the scene, the subject in the shot seems to jump between locations while the camera follows a set path. Finally the SFX were kept very subtle, with no music or specifically distinct sounds, to eliminate predictable auditory cues when running the video in a loop.

     

    Some Inspiration

     

    Location







     

    Panorama

    Some slow motion experiments shot on Sheridan’s Sony HVR-V1U camera.

    Design Statement & Rationale

    My goal for the 10 second academy leader was to capture and create a visual narrative from existing objects – as opposed to superimposing the numbers. I thought that a pinball machine would give me a good opportunity to play with the sequencing of graphics that you would typically find on one. It also allowed me to use the different components of the table (ball, flippers, lights, etc.) to enhance the aesthetic I was looking for.

    While I did manage to get access to eight pinball machines, each with a variety of numbers, it proved impossible to just straight forward shoot and edit the sequence – a little After Effects compositing magic was necessary.

    The biggest challenge was controlling the visual distractions in the compositions while focusing the user’s attention on the numbers. After completing my first rough edit I found that there was too much irregularity between the sequence numbers to distinguish them from other elements on the screen. To solve this I used After Effects and Photoshop to control when each number lit up (each lights up exactly 10 frames into each second). I also timed other elements (balls rolling, flippers moving, lights flashing) to synchronize with the timing of the numbers. This created a consistency and rhythm that was easier for the viewer to understand. It also naturally drew the eye to the numbers through the use of motion.

    Finally the short intro and credits where designed to complete the experience of playing pinball – as in starting a game (using the plunger), and finishing (viewing the highscore).

     

    After Effects Insight