Follow MaxBotix:
Ask A Question
|     Written By: Tom Bonar     |     Date Posted: 04-30-2014    |
The Arduino micro-controller is one of the most popular development boards for electronics enthusiasts. With the ability to control components such as buzzers, LED’s, servos, motors, and LCD’s, Arduinos have become the go-to selection for users that are looking to start into electronics, firmware coding, or automation. So with all these item types at your finger tips, what good is it to have control over so many components, if there are no sensors to trigger different functions in the Arduinos firmware?

The MaxBotix Inc., MaxSonar sensor line has become a very popular sensor for operation with the Arduino micro-controller. With three simple interfaces, it is easy to connect a MaxSonar to an Arduino.

In this article, I will be providing: links to components needed, wiring diagrams for all outputs, and Arduino sketch files for all compatible MaxSonar sensors. With that being said, let’s get coding!

Table of Contents


List of Materials Needed

Setting Up the Arduino

Wiring Pictures


Arduino Coding Part 1

    Useful Information
    Arduino Comments
    Code the Input & Variables

Arduino Coding Part 2

    Code Setup
    Code Section to Read Sensor

Arduino Coding Part 3

    Code Debug Section
    Code Void Loop
    Review the Code

Code Downloads

    LV-MaxSonar Code Downloads
    XL-MaxSonar Code Downloads
    HR-MaxSonar Code Downloads

Arduino Coding Part 1

Useful Information

Breadboard - A thin plastic board used to hold electronic components (transistors, resistors, chips, etc.) that are wired together. Used to develop prototypes of electronic circuits, the boards can be reused for future jobs. An image of how the internal nodes are connect is viewable here

ADC - Analog to Digital Converter. This takes a voltage and outputs it as bits. Typically as a 10-bit format (0 to 1024), some ADC's have other scales, so please reference the ADC datasheet.

Code Definitions

INPUT - Interface pin is acting as an input. The micro-controller will report or measure this pin if brought "HIGH" to voltages greater than 3.0VDC. This is used for reading the Time of Flight output of the sensor.

variable - A word that the Arduino will refer to later. This can be a simple word such as "inches". This is used to store a value that changes.

void - Section of code that performs a specific task. This is used to break up Arduino code into smaller more manageable sections.

analogRead - Tells the Arduino to read the Analog In pins. These pins are read using a 10-bit ADC.

pulseIn - Tell the Arduino to read the PWM (Pulse Width Modulation) pins as an input. These pins are read as length in uS.

Serial.print() - Outputs the information to the computer or display that is reading the Arduino. Made up of several sections.
   ("xyz") - Any text inside of the quotation signs are output to the display
   (xyz) - when there are no quotations, this data is for range data you want to output. i.e. mm, inches

Serial.println() - this tells the Arduino that this is the end of the current line and the next output will be the start of a new line.
   If this is not put in, all the data output by the Arduino will show up on one continuous line of text that can look very unorganized.
   This can be also used to have information such as (xyz) or ("xyz").

delay() - this tells the Arduino to wait a length of time in milliseconds before performing the next task

Top of the Page

Arduino Comments

Comments are symbols that are inserted in code to tell the processor to ignore that line or section. In the Arduino coding platform, there are two comment types.

The first comment type is symbolized like this: //    This tells the processor ignore everything that follows this symbol on the current line. As soon as a new line is started, the processor will start looking at the data again. This works well if a single line is being removed or a note is being written about a section of code.

The second comment type is for ignoring large portions of code. To start ignoring a section the comment is /* and to stop ignoring a section the comment is */. This type of coding works well for removing sections of code that are not being used, sections being diagnosed and debugged, or at the start of the code for useful documentation such as: purpose, date written, and the development platform.

Top of the Page

Code the Input & Variables

This section covers how to write a piece of code to read the sensor's output into the Arduino.

1.) Open the Arduino Sketch software on your computer
     If you would like the code pre-written to start from, jump to the Code Examples section
2.) Optional - Write several lines of code that say: code use, part number being used, platform, and the
     date started
     This allows the code written to be a base code for other projects. An example is seen below.
First Arduino Code
Used to Read MaxSonar MB1013
Written for Arduino Uno
Reading Pin# output (# being the pin for PW, AN, or TX)
Started MM/DD/YY
3.) Code the Arduino's input
     3a.) If the Analog Voltage is being read, this section will look like this  ‑  const int anPin = 0;
     ba.) If the Pulse Width is being read, this section will look like this      ‑  const int pwPin1 = 3;
4.) Code any variables used within the code
     4a.) For Analog Voltage, this section will look like  ‑  long anVolt, mm, inches;
     4b.) For Pulse Width, this section will look like      ‑  long sensor, mm, inches;
5.) Save the current file.
     Typically a good idea to save the file as PN_Output_Use. An example is below

Top of the Page

Author: Cody Carlson  Date: 08-02-2016
Raspberry Pi 2 board MaxSonar sensors offer a variety of outputs including TTL serial data. This tutorial guides you through the process of setting up your Raspberry Pi 3 with a MaxBotix sensor. Click here for full article.
Author: Scott Wielenberg  Date: 07-26-2016
Full Horn Housing MaxBotix offers an expanded range of packaging options for many of our sensors. Each option provides unique benefits to certain mounting integrations. This article provides a brief overview of each option.
Click here for full article.
Author: Scott Wielenberg  Date: 07-18-2016
Sensor is Tested When providing support, our technical support team may determine that further testing at our facility is the best way to help resolve the issue that you are facing. At this point, they will start the Return Merchandise Authorization (RMA) process. This article will explain what you can expect as your ultrasonic sensor travels through our RMA process.
Click here for full article.
Author: Scott Wielenberg  Date: 07-11-2016
Typical Wall Pipe Many customers have requested the option to mount an ultrasonic sensor in a pipe. During the testing and development cycle, we discovered a number of considerations and requirements that must be met for the application to be successful. When all of these are met, a user may be able to achieve the desired level of success for measuring the liquid level inside of a pipe.
Click here for full article.
Author: Jenney Grover  Date: 06-28-2016
one of the options for outputs On April 19th, we welcomed our supporters to join us for the Grand Opening of the Build Out. Bob and Nita Gross gave a tour of the build out and their vision for the space. We continue to be in awe of the support from our community, our employees, our distributors, and our customers. Thank you for the many years of support, and we look forward to serving you in the years to come.
Click here for full article.
Signup for notification of our exciting new products and periodic new letters. We are excited to provide the latest information from MaxBotix Inc.