Connecting a MaxBotix Ultrasonic Sensor to a Raspberry Pi
By: Armin Samii
Tags: Raspberry Pi, Sensors, Maxbotix, Prototyping, Electronics

A quick tutorial to get you started with a MaxBotix Ultrasonic Sensor and a Raspberry Pi. These ultrasonic sensors are an inexpensive way to detect the distance to the nearest object in the line of sight.

If you’re here, you may have already tried following the MaxBotix tutorial and found that your data is garbled. Rather than the data looking like “R102” representing 102 inches, you’ll see something like “+\X04\X02\X04”. The issue is that some sensors write TTL data, and others write RS232 data. The Raspberry Pi’s RxD serial pin cannot read RS232 data — it’s inverted, and no, you can’t just flip the bits in software! You’ll need to flip the bits in hardware. Here’s how to do that, plus some level-shifting to drive your ultrasonic sensor at 5V.

This is a tutorial for beginners. You will need basic soldering skills and the ability to edit files on the command line.

Bill of Materials

This tutorial is based on the following materials, though it will apply to any combination of Pi and MaxBotix sensors.

Step 1: Configure the Raspberry Pi

You need to enable UART to read the serial pins. If you have a Raspberry Pi Model 3 or 4, you’ll also need to switch bluetooth from using UART to using the slower MiniUART.

To do this, edit /boot/config.txt as a superuser and add the following lines to the end, then reboot:

enable_uart=1

dtoverlay=pi3-miniuart-bt

The second line is not necessary unless you have a Raspberry Pi 3 or 4, but won’t hurt anything if you have a lower version.

Step 2: Basic Connection

Connect the Ground, VCC, and TxD from the Ultrasonic Sensor to the Raspberry Pi:

The black wire is ground, the red wire is 3.3V VCC, and the yellow wire is the serial data.

Make sure you don’t connect VCC to the 5V pins on your Pi — that will destroy your Pi! We’ll get to how to power your sensor with 5V in Step 5.

To test the connection, run the following command:

minicom -b 9600 -o -D /dev/serial0

You should see garbled data. If your data starts with an R, followed by 3 numbers, you don’t need to invert — you have a Maxbotix sensor that outputs TTL data that doesn’t require inversion. Skip Step 3. If you see garbled data, hooray! Let’s ungarble it.

Step 3: Invert data with a NOT gate

If you’re getting garbled data, it’s likely because your MaxBotix sensor sends a 0V signal to indicate a digital 1, and a 3.3V signal to indicate a digital 0. We need to invert these to match what the Raspberry Pi expects: 0V to indicate 0, and 3.3V to indicate 1.

You can buy specialized hardware for this (e.g. a MAX232), but that takes up more space and costs more than a transistor plus a couple resistors. This is the easiest and most inexpensive method:

Building a NOT gate with a single NPN transistor

We’ve added a transistor and two 1kΩ resistors. Here’s how each pin of the transistor is wired to create a NOT gate:

1. Base (middle pin)

Connect to a 1kΩ resistor which is then connected to the output from the MaxBotix sensor. This is the input signal which we want to invert.

2. Collector (left pin)

Connect to a 1kΩ resistor, and then to the 3.3V VCC rail. This is also the inverted output, so connect it to the Pi (the orange wire).

3. Emitter (right pin)

Connect to ground

Here’s how it works: when the Base is held low (at 0V), the path of least resistance from VCC is through the orange wire to the Pi, so the Pi receives a 1 when the MaxBotix outputs a 0.

In contrast, when the Base is held high (at 3.3V), the path of least resistance from VCC is through the emitter, going to Ground, so Pi receives a 0.

You’ve now built a NOT gate! Plug in your pi and re-run the command:

minicom -b 9600 -o -D /dev/serial0

You should now see reasonable data coming through, in the form of an R, then three digits. That represents the distance in inches from the sensor.

Step 4: Increase to 5V

If you look at your sensor’s datasheet, you’ll see better performance at 5V than 3.3V. If you want to power the sensor at 5V, you’ll need to ensure the serial pins are getting no more than 3.3V or you could destroy them.

To accomplish this, use a logic level converter. This will convert your 5V signal into a 3.3V equivalent.

Here, the top of the breadboard is at 5V (represented by the pink wire), and the bottom section is at 3.3V (represented by the red wire).

The MaxBotix sensor uses the 5V rail, then the level shifter converts that to 3.3V. The orange wire remains at 3.3V.

Once again, plug it in and test. The output will be no different than before, but you’ll be getting better performance from your sensor.

Summary

We’ve taken a variant of an RS232 signal from a MaxBotix sensor, level-shifted it down from 5V to 3.3V, inverted the signal with a NOT gate, and fed that into the Pi.