An Arduino Pro Mini ultra-sonic beeping thingamajiggy

The other day I received an email from an associate, who inquired if I was familiar with the Polaroid sx70 SONAR module. I replied with a simple NO, but I do have experience working with low-cost ultrasonic sensors like the Parallax’s PING)))™ ultrasonic sensor, Maxbotix ultrasonic rangefinder and the Devantech SRF02. Sonar or ultrasonic sensors are often used in mobile robotics for obstacle avoidance, navigation, map building and in interactive art installations where you need to detect an object in a room or space. Ultrasonic range sensors work by emitting a short burst of ultrasonic sound, which is reflected by objects in front of the device and returned to the detector. The receiver amplifier sends these reflected signals (echoes) to a micro-controller which times them to determine how far away the objects are, in other words measures the distance.

So, in the spirit of teaching I decided to write a post on the Arduino Pro Mini and the Parallax Ultrasonic sensor. I went the extra mile and added audio feedback via a 8-ohm mini speaker, visual feedback via a BlinkM Led and included an external power source.

You will need the following components to build an ultra-sonic beeping thingamajiggy:

  • Arduino Pro Mini 328 – 5V/16MHz
  • Break Away Headers – Straight
  • FTDI Basic Breakout – 5V or FTDI Cable 5V
  • BlinkM RGB Led
  • Parallax’s PING)))™ ultrasonic sensor
  • 8-ohm mini speaker
  • Basic breadboard
  • Enclosed 4 “AA” battery holder
  • Jumper wire
  • Lets start by addressing the Arduino Pro Mini.
    The Arduino Pro Mini is intended for advanced users who require flexibility, low-cost, and small size. Please note that there are two versions of the board, one that operates at 5V (like most Arduino boards), and one that operates at 3.3V. Both boards come with the minimum of components (no on-board USB or pin headers) to keep the cost down. The Arduino Site has an overview on the Arduino Pro Mini, I suggest you at least review the page to get familiar with the Inputs and Outputs of the board. The Arduino Pro Mini is programmed using a FTDI Cable or in my case a FTDI Basic Breakout board.

    Since we are using a breadboard to prototype the ultra-sonic beeping thingamajiggy, I suggest you solder the break away headers to the Arduino, in fact, I always suggest creating connectors for sensors rather than just soldering them to the Arduino. A quick soldering tip is to stick the headers into a breadboard and place the Arduino board on top. Then solder all headers. Make sure you also solder headers to Analog 4 and 5, which you will find on the board near the ATmega328 chip. You should now have something that resembles this:

    Now, lets hook up some power. This part is very important, you don’t want to fry your board so pay close attention. Locate The power pins on your Arduino Pro Mini:

    * RAW. For supplying a raw (unregulated) voltage to the board.
    * VCC. The regulated 3.3 or 5 volt supply.
    * GND. Ground pins.

    The VCC pin on the Arduino Pro Mini is bypassing the regulator and you should NOT CONNECT ANY POWER SUPPLY ABOVE 5 VOLTS to this pin. In most cases you will use the RAW pin to connect a power supply (up to 12V) in need of regulation.

    The enclosed 4 “AA” battery holder from Radio Shack, has a voltage of 6 volts, therefore I have no other choice but to connect it to the unregulated RAW pin.

    ONLY USE THE VCC PIN IF YOU ARE SURE YOU HAVE A REGULATED 5 VOLT POWER SOURCE.

    I took the battery enclosure and added a 2 pin screw terminal to the end of the wires to make the connection to the breadboard easier.

    Make sure the battery enclosure switch is on the OFF state and then connect the power supply to the breadboard.
    Add the ground (black) wire from the battery enclosure to the blue rail of the breadboard.
    Add the power (red) wire from the battery enclosure to the red rail of the breadboard.

    Grab some jumper wires and connect a wire from the blue rail of the breadboard to the GND pin on the Arduino (green wire in image below). Connect a wire from the red rail of the breadboard to the RAW pin on the Arduino (blue wire in image below).

    Next lets connect the ultrasonic sensor.

    You will notice the sensor only has 3 pins, the 5V pin, the GND pin and the SIG (signal) pin. You can easily place the senor onto your breadboard and wire it up. I had some extra Male/Female jumper wires, so I utilized them to free up some space on the breadboard.
    Connect the GND pin of the sensor to the blue rail on the breadboard (black wire in image below).
    Connect the 5V pin of the sensor to the red rail on the breadboard (red wire in image below).
    Connect the SIG pin of the sensor to pin 7 of the Arduino (blue wire in image below).

    If connecting the sensor is all you cared about, you could stop here. All you have left to do is connect the FTDI Cable or FTDI Basic breakout board to the Arduino Pro Mini. Open up the Arduino IDE:
    Select Tools -> Serial Port
    and select the correct usb port (mac) /dev/tty.usbserial-XXXXXXXX.
    Select Tools->Board
    and select Lilypad Arduino w/Atmega328 (I’m running Arduino IDE 018 and for some unknown reason this selection is the only one which enables an upload without errors).
    Select File-> Examples->Sensors->Ping
    and upload the sketch.
    Your done! If you open up the Serial Monitor, it will display the distance being detected from the sensor in inches and cm’s.

    Moving forward, lets add the BlinkM RGB Led. If you read my previous post on the BlinkM you will be familiar with the process. If not, grab your BlinkM and lets get familar with it.

    The BlinkM is a networkable and programmable full-color RGB LED. It has a small AVR micro-controller on board to allow a user to digitally control the LED over a simple I2C interface. The Arduino will communicate with the BlinkM, using the Wire library so we don’t have to worry about the details of I2C. If you haven’t already checked out the BlinkM support page please do so, it has a bunch of perks you should be aware of. Take a look at the print on the board itself. You will notice PWR and I2C printed directly on the board, take mental note because that’s what we will be interfacing with shortly. BlinkM needs two wires for power and two for data. We will be connecting to the Arduino analog pins 4 & 5 which double as the I2C data signal (“SDA”) and clock signal (“SCL”), respectively.

    Place the BlinkM onto your breadboard and connect a wire (red wire in image below) from the red rail on the breadboard to the positive (+) symbol printed on the BlinkM. Connect a wire (brown wire in image below) from the blue rail on the breadboard to the ground (-) symbol printed on the BlinkM.

    Proceed by connecting a wire (blue wire in image below) to analog 4 of the Arduino Pro Mini. Connect the other end of the wire to the ‘d’ (I2C SDA) symbol printed on the BlinkM.
    Connect another wire (green wire in image below) to analog 5 of the Arduino Pro Mini. Connect the other end of the wire to the ‘c’ (I2C SCL) symbol printed on the BlinkM.

    Next, lets hook up the 8 ohm mini speaker for audio feedback.
    Simply connect the black wire from the speaker to the blue rail of the breadboard. Connect the red wire from the speaker to digital pin 7 on the Arduino Pro Mini.

    And there you have it. We have completed our simple circuit for an ultra-sonic beeping thingamajiggy.
    Lets move onto the code.

    The first thing you are going to want to do is connect the FTDI Cable or FTDI Basic breakout board to the Arduino Pro Mini. There are pros and cons to the FTDI Cable vs the FTDI Basic breakout board . The breakout board has TX and RX LEDs that allow you to actually see serial traffic on the LEDs to verify if the board is working, but the board requires a miniB cable. The FTDI Cable is well protected against the elements, but is large and cannot be embedded into a project as easily. The FTDI Basic breakout board uses DTR to cause a hardware reset where the FTDI Cable uses the RTS signal.

    I prefer to use the FTDI Basic breakout board to connect to the Arduino pro Mini. Anyhow, connect either the FTDI Cable or Basic breakout board to the Arduino and open up the Arduino IDE.
    Select Tools -> Serial Port
    and select the correct usb port (mac) /dev/tty.usbserial-XXXXXXXX.
    Select Tools->Board
    and select Lilypad Arduino w/Atmega328.
    Select File->New
    and copy the following code to the sketch:
    *NOTE you can download my source below*

    /*
    Code under (cc) by Manuel Gonzalez, www.codingcolor.com
    http://creativecommons.org/license/cc-gpl
    Digital pin 7 Parallax Ultrasonic Range Finder
    Digital Pin 4 8-ohm mini speaker
    Analog pins 4 (SDA),5(SCL) I2C communication with BlinkM
    */

    #include <Wire.h>
    #include "BlinkM_funcs.h"



    const int pingPin = 7;
    const int speakerPin = 4;
    const int blinkm_addr = 9;//address to blinkM
    byte r,g,b;

    void setup() {
     // Serial.begin(9600);
      pinMode(speakerPin,OUTPUT);
     
      BlinkM_beginWithPower();
      BlinkM_stopScript(blinkm_addr);//stop the BlinkM
      BlinkM_setFadeSpeed(blinkm_addr, 255);//set up fade param
      BlinkM_fadeToRGB(0,0x00,0x00,0x00);//fade all to black
    }

    void loop()
    {

      long duration, inches;
      pinMode(pingPin, OUTPUT);
      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);

      pinMode(pingPin, INPUT);
      duration = pulseIn(pingPin, HIGH);

      // convert the time into a distance
      inches = microsecondsToInches(duration);
      /*
      Serial.print(inches);
      Serial.print("in, ");
      Serial.println();
      */

      displayColor(inches);
      delay(100);
    }

    void displayColor(long inches)
    {
      if(inches < 12)
      {
         r = 255;
         g = 0;
         b = 0;
        beep(speakerPin, 2500, 500);
      }else {
         r = 9;
         g = 249;
         b = 17;
      }
     
       BlinkM_fadeToRGB(0, r,  g,  b);
    }
    void beep(int targetPin, long frequency, long length) {
     
      long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
      long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
     for (long i=0; i < numCycles; i++){
        digitalWrite(targetPin,HIGH);
        delayMicroseconds(delayValue);
        digitalWrite(targetPin,LOW);
        delayMicroseconds(delayValue);
      }
    }

    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;
    }

    Take note of the include.
    #include "BlinkM_funcs.h"
    It’s a BlinkM Arduino library written by Tod E. Kurt. You will need the BlinkM_funcs.h library which is also availbe in my download.

    Upload the code to your Arduino Pro Mini disconnect the FTDI Cable or Basic breakout board and turn the power on.

    If all went well, you should have an ultra-sonic beeping thingamajiggy.

    Enjoy!
    Download Source:
    Ultra-Sonic beeping thingamajiggy (305)

    6 thoughts on “An Arduino Pro Mini ultra-sonic beeping thingamajiggy

    • June 21, 2012 at 6:37 am
      Permalink

      your cording is so help full to me.but
      in this cord ,there is
      “duration = pulseIn(pingPin, HIGH);”
      so i want to know what is the meaning of
      “pulseIn”

      thanks….

      Reply
      • February 1, 2013 at 1:48 pm
        Permalink

        ‘pluseIn” is a method which returns a value, in this case duration is set to the returned value. You will find the method in the BlinkM library.

        Reply
    • June 25, 2012 at 4:28 am
      Permalink

      can you please give me this program in MICRO-C language. in the
      “duration = pulseIn(pingPin, HIGH);”

      what is the meaning of
      “pulseIn(pingPin, HIGH)”

      Reply
    • July 28, 2012 at 11:24 am
      Permalink

      What is the maximum distance?

      Reply
      • August 15, 2012 at 4:03 pm
        Permalink

        It depends on the Ultra-sonic sensor you are using. The one I mentioned provides precise, non-contact distance measurements within a 2 cm to 3 m range.

        Reply

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    %d bloggers like this: