Follow MaxBotix:
Ask A Question
|     Written By: Carl Myhre     |     Date Posted: 03-27-15     |
I2C MaxSonar Arduino TutorialA number of customers have asked if the Arduino supports talking to the I2C‑MaxSonar sensors over an I2C interface. The short answer is "Yes." The more technical answer is – be prepared to install a new library and learn a bit about digital electronics.

Never fear! I have worked to provide a clear and easy to follow path to get an I2C‑MaxSonar up and running on your Arduino. I have also provided some of the educational information related to this specific I2C implementation.

 

Note: Any I2C‑MaxSonar sensors shipped after 10/29/14 were updated to support faster speeds and work with the current Arduino Wire() library. This functionality, however, takes advantage of a timing slowdown in the current Wire() library. The recommended solution is to use the SoftI2CMaster library shown below.

Table of Contents

Why use a library? 

Use a library because the default Arduino Wire() library does not support changes in bus speed or feature proper clock stretching support. This makes the default library only marginally functional with an I2C‑MaxSonar because the I2C‑MaxSonar currently only supports bus speeds up to 50kHz (without clock stretching) or 400kHz (with clock stretching). Neither of these combinations are available without taking more direct control.

Thankfully, Peter Fleury and Bernhard Nebel were able to write an I2C library that uses a bit‑bang approach and has a nifty set of user defined parameters for greater flexibility in our interface. You can find the library here: https://github.com/felias-fogg/SoftI2CMaster. I am grateful for their work, as it means I did not have to write my own library.

Installing the I2C Library 

Go to: https://github.com/felias-fogg/SoftI2CMaster and download the I2C library.

Install the new library files <SoftI2CMaster.h> in the Arduino library folder.

To install the library, simply download the zip-file from GitHub, uncompress the zip file, rename the directory to SoftI2CMaster and move it into the library folder at
C:\Program Files (x86)\Arduino\libraries\

Please note that this file location applies to the 64 bit versions of the Windows Vista, Windows 7, and Windows 8 operating systems. For 32-bit operating systems, the location is
C:\Program Files\Arduino\libraries\

Load the library into the Arduino compiler.

To do this go to: SKETCH >> IMPORT LIBRARY >> ADD LIBRARY
When the library is added properly, you will see it on the library list menu, like shown in the image below.


You should now have the I2C library installed and you will be able to continue with the Arduino code examples. For more information on installing the I2C Arduino libraries visit http://playground.arduino.cc/Main/SoftwareI2CLibrary.

Loading the I2C‑MaxSonar Code 

First, copy the I2C‑MaxSonar code here into your Arduino Sketch software.

Save the code to your working directory.
The default location for Windows is %UserProfile%/My Documents/Arduino, but this may vary and you can generally use whatever is most comfortable for you.

Load the Arduino Code

Connect the Arduino to your PC and to the I2C-MaxSonar Sensor.
For wiring diagrams, please reference your datasheet here

NOTE: The code defaults to using the SDA and SCL outputs of the Arduino. Edit the corresponding #define statements if you want to use something else.

Compile and upload the code.

Start the Arduino Serial Monitor.

You should now have the I2C code running and reading the connected sensor.

Please note that the I2C‑MaxSonar Arduino code defaults to running 3 different code examples.

1. Address Polling Example (runs once) – The code will poll all of the available I2C addresses and look for connected I2C‑MaxSonar sensors. The rest of the code assumes that the I2C‑MaxSonar is located at its default address of 224.

2. Change Address Example (runs once) – The code will take a range reading at the default address of 224, change the sensor address to a new address of 222, take a range reading at the new address, then change back to the default address.

3. Read the Sensor Example (runs continuously) – The code will take a range reading from a connected I2C‑MaxSonar at the default address of 224 and output the range information that it received.

How the I2C‑MaxSonar Code Works 

I have included 3 functions (one for each command the sensor can do) and 3 different code examples (one for each type of activity a typical user will have to do). I do not go into every detail in this article. (You should read the code notes for that.) I have provided a brief description of what I did at each point.

These functions are included so you can easily talk to the I2C‑MaxSonar sensors. If you have other devices on the I2C bus, make the proper modifications to the code to run your other devices as needed.

The included functions and their related information are as follows:

A. start_sensor(addr)

Description: Uses the I2C library to start a sensor at the given address. You must start the sensor 100ms before requesting a range from it if you want the most recent information.

Parameters: addr: an even byte value corresponding to the 8-bit address of the sensor you want to command a range reading at. The default value for I2C‑MaxSonar sensors is 224.

Returns: errorlevel: defaults to FALSE (value of 0) when the communication is successfully completed. Set to TRUE (value of 1) if there was a communication error.

Note 1: Care should be taken to only operate one sensor at a time. Commanding more than one sensor to range at once may cause cross-talk (interference).

Note 2: addr is always an even value with the last bit being reserved for a Read/Write signal as defined by the I2C specification. Using an odd value will cause the address to automatically be rounded down to the next lowest even number when used.

B. read_sensor(addr)

Description: Uses the I2C library to read a sensor at the given address.

Parameters: addr: an even byte value corresponding to the 8-bit address you are reading from. The default value for I2C‑MaxSonar sensors is 224.

Returns: range: an int value corresponding to the distance found by the sensor (for the current I2C‑MaxSonar sensors this is a value between 20cm and 765cm). A value of “0” represents that the sensor could not be read and that there was an error in communication.

Notes: The I2C‑MaxSonar sensors are configured to report the last range reading stored in memory. Because of this, read_sensor() is called after at least 100ms has elapsed after running the function start_sensor(). Readings can be taken faster if absolutely required, but this will limit the maximum range of the sensor. (See the datasheet here for more details.)

C. change_address(oldaddr,newaddr)

Description: Uses the I2C library to change the sensor address at oldaddr to newaddr.

Parameters: oldaddr: an even byte value corresponding to the 8-bit address of the sensor you want to update to a new address. The default value for the I2C‑MaxSonar sensors is 224.

newaddr: an even byte value corresponding to the new 8-bit address of the sensor. A value of "0" is considered invalid and will be ignored.

Returns: errorlevel: defaults to FALSE (value of 0) when the communication is successfully completed. Set to TRUE (value of 1) if there was a communication error.

Note: For other devices operating on the I2C bus: It is recommended that you use addresses for the I2C‑MaxSonar that are below 240. This keeps the sensor away from the reserved address space.

Download the Files 

I2C‑MaxSonar Zip file
I2C‑MaxSonar text file
I2C‑MaxSonar Arduino file
Arduino IDE

Concluding Remarks

We hope this I2C tutorial and code example is a good introduction and helps to simplify a complex interface. If you have any questions regarding using an I2C‑MaxSonar with an Arduino, please email our technical support team by completing this Tech Support Form.

 
Author: Kathy Kostal  Date: 08-31-2016
Inc 5000 Maxbotix Logo Inc. Magazine Unveils 35th Annual List of America's Fastest Growing Private Companies–the Inc. 5000. MaxBotix Inc., Ranks No. 1752 on the 2016 Inc. 5000 with Three–Year Sales Growth of 213%.
Click here for full article.
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.