Using the ultrasound

We’ve just tried to use the ultrasound (sonar) for the first time with V5. I’ve found online help about the correct order to plug the 2 cables in etc and the code… but we haven’t got it working. I made it print the distance on the screen, but it just gives 0.00mm. We are coding in C++ and I just wrote a simple if greater than x mm then run motors, else hold code.

Several questions…

  1. What setting does each cable need under the devices setup on the cortex? ie: digital input, digital output high or digital output low.
  2. Is there a way on the cortex or in VCS to see live feedback of the values as we could with robotc?
  3. Any other advice?

You can’t see the live feedback through VCS, since it doesn’t have any way to retrieve data from the robot. You could, however, write to an SD card and graph the output.

Thought as much, hence getting value to display on the controller. I’m just not sure it is working or which part isn’t working I should say. Distance value just isn’t changing.

Take a look at our range-finding curriculum activity. The code is not C++, but the logic of hooking up and using the ultrasonic rangefinder is the same no matter what the language is.

Cheers, I did find this already while hunting for answers and it was very useful, I plugged into CD ports with correct order. Coded a basic drive until within x mm code, either the robot didn’t move or didn’t stop depending on the settings for the 3-wire ports.
Is there anything that needs setting on the cortex or does the code set that once it’s running? Maybe I shouldn’t have changed them, but I toggled the port setting before running the programs and different settings to seem to change what was happening.

No, it just works. Make sure that the rangefinder is not too close to the floor (it can get pings back from the floor).

Also, you might try hooking your robot up with Robot Mesh Studio, populate the device panel on the far right hand side with the ports you are using for rangefinder, then click the “Connect” button. When the Connect program is running on your Cortex, the rangefinder value will display in the device panel. This is also useful for debugging other sensors and motors. Good luck!

Thanks, I’m going to try a clean program tomorrow. I was adding it to an already complete code, so maybe something isn’t working well together.

So I got it working, but only by plugging it in the oppersite way to your instruction pictures. Orange in A and yellow in B.

The picture is wrong, but the text is correct. I’ll fix the picture, thanks for letting us know. The text says:

Plugging the range finder into the Brain requires some care. The 3-wire ports on a V5 brain are labeled A through H. The two cables of the range finder have to be plugged into adjacent ports in the pairs AB, CD, EF, or GH.  You cannot use other combinations, as, for example, BC, AD and FG will not work. The Input needs to be plugged into the higher of the pair, and the Output into the lower. Like this:

A, C, E, G - Output cable

B, D, F, H - Input cable

If you put the cables in the reverse order, the range finder will not work.
1 Like

Cheers, That’s what made me rethink the connections. I saw the text versus the picture.