-
1How It Works
The HC-SR04 ultrasonic sensor principles of operation are well documented on the internet. A good example is at ElecronicWings
Below is their diagram of how it works.
The Octosonar takes advantage of something that is not clear from this diagram. The 10us trigger pulse length is a minimum value. The trigger is actually on the falling edge (high to low transition). The sensor does not care if you keep TRIG high for much, much longer than 10us.
The Octosonar keeps the TRIG pin high when the sensor is not in use, and low when it is in use - by default 50ms "low" and 350ms "high" (750ms "high" on the OctosonarX2). It gives each sensor 50ms "low" to take its measurement and also uses that "low" signal to enable a dedicated tri-state buffer to pass the ECHO pin signal to a hardware interrupt pin on the Arduino.
The TRIG pins are driven by a pin-expander chip on the I2C bus, with each active pin being made "low" in rotation. While I2C does not give good enough timing accuracy for echo measurement it works fine for this selection rotation function. Timing is taken by hardware interrupt routines, so the start and end of the ECHO pulse can be measured accurately, even if your Arduino code was doing something else at the time.
The results are stored away for your code to retrieve later when it looks for it.
-
2Software Installation
If you are using version 1.6.2 or later of the Arduino software (IDE), you can use the Library Manager to install this library:
- In the Arduino IDE, open the "Sketch" menu, select "Include Library", then "Manage Libraries...".
- Search for "Octosonar".
- Click the Octosonar entry in the list.
- Click "Install".
If this does not work, you can manually install the library:
- Download the latest release archive from GitHub and decompress it.
- Rename the folder "Octosonar-1.2.0" to "Octosonar".
- Move the "Octosonar" folder into the "libraries" directory inside your Arduino sketchbook directory. You can view your sketchbook location by opening the "File" menu and selecting "Preferences" in the Arduino IDE. If there is not already a "libraries" folder in that location, you should make the folder yourself.
- After installing the library, restart the Arduino IDE.
(note - the above instructions adapted from a Pololu readme)
-
3Wiring Up the Octosonar
The connections on the Octosonar are clearly labelled. I'll try and run up a wiring diagram later, but for now the following list of connections should get you there.
To connect to Arduino
Octosonar Pin Uno Pin
DefaultDescription 5V 5V 5V supply for the HC-SR04 sensors. If you have a 5V Arduino (e.g. Uno) you can skip this connection and bridge the V1 solder jumper on the back to use the VCC suply for this purpose. INT D2 The hardware interrupt pin you select in your code. SCL A5 The I2C SCL pin or your controller. SDA A4 The I2C SDA pin or your controller. VCC 5V The voltage power used by your controller. This will be either 5V (Arduino Uno) or 3.3V (Raspberry Pi, Arduino Pro 328 - 3.3V etc).
IMPORTANT: if you have a 3.3V controller, DO NOT SOLDER V1GND GND Ground Solder Jumpers on the back A0, A1, A2 allow you to change the I2C address from the default 0x38
The V1 solder jumper is used to connect the VCC and 5V paths together - which saves you a wire if VCC and 5V are the same thing on your controller.
To connect to HC-SR04, the eight connectors are marked VTEG matching the pins on the HC-SR04. If you are not using all the ports you can disable the inactive ports in software which will give more frequent polling of the active sensors. The ports are numbered S0 thru S7 (S was for "sensor") and are spaced to allow molex locking headers to be used.
-
4Wiring Up the OctosonarX2
The OctosonarX2 is a lot busier than the Octosonar but the connections to the Arduino are exactly the same - so see above for that. The only difference is that the V1 and address solder jumpers are on the front of the board.
The OctosonarX2 base address is 0x20
There is a big difference on the HC-SR04 connectors.
- The are numbered H0 thru HF - H is for hexadecimal
- They are all spaced 0.1" apart to fit normal headers vertically or horizontally.
- They are arranged in blocks of 4
- Each block has an extra V and G pin at one end.
- Each block has VTEG labelled at one end - this applies to the four pins above/below it in a column.
While you can connect 16 individual 4-pin cables and drive 16 sensors, this is optimized to drive 12 sensors on 4 "trimount brackets" using only 8 cables. This is a major rats-nest reduction.
To connect to the trimounts use one cable to connect the V pin next to the block and the adjacent 3 T pins, and another cable to connect the G pin next to the block and the adjacent 3 E pins.
Be careful connecting the cables the right way around at the other end. As the 4-pin cables are usually colored red-black-yellow-white, I use a red for the V line and then put the other cable in the other way around i.e. white for G. That way if you get them flipped you don't connect 5V to ground.
-
5Coding
A full current listing of the Octosonar class and methods is in the readme file at https://github.com/arielnh56/OctoSonar
An important consideration is to make sure that you are coding your arduino as a "finite state machine". The class relies on your loop() being fast and pretty regular. If you have code with a bunch of delay() calls all over it you will get poor performance. Finite state machine is not something I'm going to get deeply into here, but essentially whenever you want to wait for something in your code - don't. make a note of when you want to wait till, then move on. On the next loop() cycle you check to see if the time is up. If it is is, then do the thing you were waiting for, if it isn't, then move on. The simplest example I've seen of this is the Arduino button debounce tutorial.
Take a look at the OctoSonarTest example (or OctoSonarX2Test)
/* OctoSonarX2Test * * This is a basic test for the OctoSonar functionality * outputs range in mm of 8 sensors to serial monitor * serial text at 115200 baud * * Copyright (c) 2018 Alastair Young */ #include "OctoSonar.h" #define SONAR_ADDR 0x38 #define SONAR_INT 2 #define ACTIVE_SONARS 0xFF OctoSonar myOcto(SONAR_ADDR, SONAR_INT); void setup() { Serial.begin(115200); myOcto.begin(ACTIVE_SONARS); // initialize bus, pins etc } uint32_t last_print = 0; void loop() { OctoSonar::doSonar(); // call every cycle, OctoSonar handles the spacing if (last_print + 200 < millis()) { // serial output every 200ms last_print = millis(); for (uint8_t i = 0; i < 8; i++) { Serial.print(myOcto.read(i)); Serial.print(" "); } Serial.println(); } }
- #include "OctoSonar.h" // this includes the library
- #define SONAR_ADDR 0x38 // this sets the address of the board
- #define SONAR_INT 2 // this selects the hardware interrupt pin
- #define ACTIVE_SONARS 0xFF // this is a bitmap for the active sensors. There are 16 bits as this supports both the Octosonar and OctosonarX2. Turning off inactive sensors allows the active sensors to be polled more frequently.
- OctoSonar myOcto(SONAR_ADDR, SONAR_INT); // this creates our "myOcto" Octosonar object, in this case at address 0x38 and pin 2. In theory you can have more than one board, on separate interrupt pins, but I haven't tested it.
- myOcto.begin(ACTIVE_SONARS); // call this from setup(). it initializes the board and sets the initial active mask.
- OctoSonar::doSonar(); // call this every loop(). The faster your loop() the quicker your sensors will cycle. This just moves on to the next sensor if the current sensor is "done" - the actual distance measuement happens in the background in interrupt routines.
- myOcto.read(i); // this reads the value last measured by sensor "i". By default this is in mm. As this is not going to update every loop() just check it as often as makes sense for your application. This still uses floating point math so is "expensive" and will slow down your loop() if you do it every time.
-
6Movie!
This is an old video showing the V1 board in action. The performance of the V2 is similar. The V1 board did not handle "lost echo" conditions well, particularly with some HC-SR04 clones. Yes, I know, I need a newer video :-)
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.