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
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.
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
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.
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
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)
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