SonarI2C – multiple HC-SR04 sensors on Arduino I2C

HC-SR04 octopus

HC-SR04 octopus on I2C plus one pin

I recently had my first encounter with ultrasonic distance sensors – specifically the Parallax Ping))) – while helping the Cresta Blanca Girl Scouts robotics club prep for the local RoboGames Fire Fighting challenge. This got me all interested in doing robot things again so I’ve dusted off the Seeed bot and I’m trying to make it more intelligent.
The first thing I observed was that you really want more than three of these on your bot. Right, left and forward just isn’t enough spatial awareness when you are navigating hallways and doors. Two on each of the four sides works much better. I also found that they use a pin each. And they cost $30. Each.
From China, you can get a pack of ten HC-SR04 sensors for under $12, with free shipping. These look similar and seem to perform a similar function, so I bought some to play around with. They all seem to work, but these guys use two pins each – one for trigger, the other for echo. So now I’m up to 16 digital pins, so unless I go to a Mega, I’m still stuck.
I2C bus would be the answer, but the bus itself is not fast enough for accurate echo measurement, and the only reference to an I2C library for these in the playground is broken. Time to write some code…

The concept behind the SonarI2C library is as follows:

Hardware:

  • Wire the Trigger pins to PCF8574(A) expander pins.
  • Wire the Echo pins to a wide OR/NOR logic tree, multiplexing them into a single hardware interrupt pin.

Software

  • Pick a sensor to trigger
  • attach an interrupt to the pin to watch for the beginning of the echo pulse
  • send the trigger pulse to the desired sensor, by flipping the appropriate expander pin on and off as quickly as possible. The spec sheet says 10µs but in fact it triggers on the falling edge, so longer is fine. I estimate the pulse in this case is about 240µs which seems to work fine.
  • the sensor does its thing and starts the echo pulse
  • the interrupt routine fires, notes the time, and attaches a new interrupt to watch for the end of the pulse
  • the sensor does its thing and ends the echo pulse
  • the interrupt routine fires, calculates the echo pulse duration, saves it and then clears the interrupt.

Each sensor is a class member and the class maintains them in a circular linked list and does all the housekeeping for you.

I developed this on a two unit breadboard setup, using a hand-knitted NOR gate, before I got myself some CD4078BE 8-input OR/NOR ICs. I then tested it on all 10 units using two PCF8574 and two daisy-chained CD4078BE. When that worked, I made myself a little prototype board supporting 8 units. Here’s the schematic and stuff for that.

Now I already use both of the interrupt pins on the Seeed Hercules controller for the motor encoders, so I’m still stuck there, but I already decided to make it a two-brain bot again, with the motor controller just keeping track of motors, so this will be controlled by the master unit. This time I’m using a cheap Pro Mini clone as the master, and will use EasyTransfer or similar to communicate between them.

octalsonarshield_schem
octalsonarshield_bb
20160424_153047
20160424_153028

Download the current SonarI2C library code on GitHub

Making a Custom Planishing Dolly

Completed Planishing Dolly

Completed Planishing Dolly

I needed a dolly with the correct profile to planish out the dents in a crumpled 1930s Ariel motorcycle front mudguard. As there is no such thing obtainable, I made my own.

I had one point on this old moonscape guard with a reasonably intact original profile. Using a piece of […]

Restoring a Vintage Valanced Mudguard – Part Two

Right rear patch tacked

Right rear patch tacked

It quickly became clear that it would not be possible to smooth out all the dents until the metal had an approximately consistent thickness. Some of the previous repairs consisted of overlay patches and lumpy welding. On the other hand, making accurate patches for a lumpy and distorted part was […]

Splitting Jumper Wires for Arduino

pi_gpio_hercules_2

A while ago I stocked up on jumper wires from Sparkfun and recently found while connecting an Arduino clone to a Raspberry Pi that the single straight header on both ends was not what was needed. Unlike the male breakaway headers, these female JST RE wire headers do not split up.

To solve this you […]

Restoring a Vintage Valanced Mudguard – Part One

Ariel Valanced Mudguard - as found

Ariel Valanced Mudguard – as found

Many years ago I traded some Ariel parts I didn’t need for some Ariel parts that I did need for my Ariel VB sidecar project. This project has been progressing intermittently since 1989. One of the parts I received was a valanced front mudguard (“fender” for our colonial […]