VEX Ultra Sonic Sensor

I was trying to code a ultrasonic sensor but I could not get my code to work. I looked online and on Ultrasonics - Software — FIRST Robotics Competition documentation and I could not get the code to work. If someone could help me with the code to get it to work it would be appreciated

Ultrasonic ultrasonic = new Ultrasonic(1, 2);

This is in our Robot Class above the robotinit()

Ultrasonic.setAutomaticMode(true);
ultrasonic.setEnabled(true); 

These 2 lines above are in robotinit()

System.out.println(ultrasonic.getRangeInches());

This line was in teleopInit()

The code would just display 0.0 even though we would put objects in front of it

we also made sure the wires were connected in dio slot 1 and 2 like in the wpilib

this is the diagram we followed

Welcome!

While it seems like you’re starting down the right track, the symptoms you report unfortunately don’t have a single possible root cause. To help narrow it down:

Do you have any info on the part number, or a link to the product that was purchased? Not all of them are exactly the same, and not all would work correctly with the roboRIO. The title says “VEX”, but I just want to confirm which one.

By having this info, people can help determine whether the sensor hardware is electrically compatible with the roboRIO, and whether the specifications align with the use-case.

This will print the reading once, right as you enable teleop. This isn’t necessarily “wrong” for initial testing, but keep it in mind as you construct your experiments.

Can you confirm which ports you actually used? The diagram indicates ports 0 and 1:

But the code you posted uses ports 1 and 2.

This information will help confirm whether the code has been written correctly (IE, aligned with your hardware and hookup assumptions).

The first line of code has a Capitalized U for Ultrasonic, while the second line has a lower-case u. Is this how your code is currently set up? Capitalization matters - you’ll want to make sure code functionally aligns with the examples in WPILib.

Can you describe the objects, and the distances you used? Ultrasonic sensors generally need large, flat surfaces, and have a certain minimum and maximum working range. Providing your test case will help people get an idea of what sorts of values should be expected in software.

4 Likes

https://www.vexrobotics.com/276-2155.html
This is the ultrasonic sensor we used

For the ports we did 1 and 2 Because that’s what the code did

when I tried using lowercase for the first line it said it was a static method

I incorrectly stated about the teleopinit() the print statement was in teleopPeriodic()

It’s probably worth posting a picture (or two) of your electrical connections. If you have the wrong ports, have input and output flipped, or have flipped either of the individual ports (GND and SIG), it will cause problems.

Ok. On further research, I concur. The static method from the Ultrasonic class should be used (not something specific to the particular object). This means there’s a small issue in the wpilib docs:

While it’s hard to say for certain there’s nothing wrong with the code, everything I’m hearing sounds like that’s headed in the right direction.

Agreed - photos of setup or more details might be able to eek out a solution. But I’m struggling to find anything fundamentally wrong with the setup described.

In cases like this, I usually start trying to run experiments to find the at-fault component. Running the sensor with a different microcontroller (arduino?), running the same code and roboRIO with a different sensor… something like that to help extract whether it’s a wiring or hardware issue, or if the issue lies with the software implementation.

While this might be a stretch - if you have access to an oscilloscope, it would make a great debug tool for a case like this.