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 3

Code the Debug Section

The debugging section of Arduino code is great for reading the sensor's output on a computer, as well as diagnosing functions that are not operating properly. For example if there is an LED that is supposed to change color when the sensor reports different range readings, and is not changing, the debug section will report the range reading taken from the sensor. If the sensor's range is not changing, this section of code will show the range reading as steps are taken to see if sensor position or the code is incorrect.

void print_range (){
  Serial.print("S1");
  Serial.print("=");
  Serial.print(mm);
  Serial.print(" ");
  Serial.println(inches);
}

Top of the Page

Code the void loop

The “void loop” section is the area of code in which the Arduino will continually cycle through commands. By making smaller void sections, such as “read_sensor” or “print_range”, the code becomes much more manageble to code. For this section the code simply looks like this.

void loop () {
  read_sensor();
  print_range();
  delay(100);
}

Because the sensor does not update every mS, the Delay slows down the Arduino so it only pulls a new range reading at set intervals. I typically set this interval to the same interval that the sensor refreshes, in this case 100mS.

Top of the Page

Review the Code

Now that several sections have been coded, it is best to verify that all the code is appropriately setup, and ready for uploading to the Arduino. If you wish to review the line information, the complete code for pulse Width and Analog Voltage are below.

The first way to do this is simply click the check mark icon in the top left corner of the Arduino IDE. This will verify all the code is properly written. Any errors will be displayed in the bottom of the IDE software in orange text

The second way to verify code, if copied from this example, is to compare it to the code below.

Analog Voltage

const int anPin = 0;
long anVolt, mm, inches;

void setup() {
  Serial.begin(9600);
}

void read_sensor(){
  anVolt = analogRead(anPin);
  mm = anVolt * 5;
  inches = mm/25.4;
}

void print_range(){
  Serial.print("S1");
  Serial.print("=");
  Serial.print(mm);
  Serial.print(" ");
  Serial.println(inches);
}

void loop() {
  read_sensor();
  print_range();
  delay(100);
}

Pulse Width

const int pwPin1 = 3;
long sensor, mm, inches;

void setup() {
  Serial.begin(9600);
  pinMode(pwPin1, INPUT);
}

void read_sensor (){
  sensor = pulseIn(pwPin1, HIGH);
  mm = sensor;
  inches = mm/25.4;
}

void print_range(){
  Serial.print("S1");
  Serial.print("=");
  Serial.print(mm);
  Serial.print(" ");
  Serial.println(inches);
}

void loop() {
  read_sensor();
  print_range();
  delay(100);
}

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.