Gyro & UltraSonic Help

Can someone help me out? I am trying some new things with autonomous and i was wondering if i can make our robot move forward using the ultrasonic sensor and turn right 90 degrees with the gyro? Im also trying to make a button set a certain angle with our 3 position solenoid with the gyro reading? Can someone point me in the right direction? Thanks!

The short answer is yes.

I’m not a programmer, so i can’t give you much, but here’s a high-level view:

The Ultrasonic (e.g., Maxbotix) returns distance. Maxbotix is an analog voltage*, you read it in, do a multiplication and get a distance in, say, inches. At the start, you measure the distance several times (you want several readings the same, so you know they are good) and then move the drivetrain until the distance is what you want. If you need to move a large distance, or move quickly, you may need some kind of PID control (probably P alone is plenty) to avoid overshooting your target distance.

The Gyro can be used to measure any angle. Again, take several measurements to get a stable reading with the robot standing still, then command a turn until you reach your desired angle. Again, PID control to avoid overshooting.


  1. You can also use odometery to measure distance - specifically, a sensor on your drivetrain can report its movement, and using that number you can calculate distance quite accurately.

  2. PID is not to be feared. In this case, the “error” is the distance remaining, and as that gets smaller you use it to make the motors go slower. So, as the distance (or angle) remaining gets smaller, the robot moves more slowly so you can stop where you need to. (Imagine going exactly 7 feet at full speed - think you can stop accurately from full speed? No. So, PID, but for this only P is really necessary)

*3. Maxbotix sensors also have other outputs, I find the analog easiest to use, but you may think differently and that’s OK.

Good luck!

Ok thanks ill try to figure out the code now that i know how they work. Thanks! Do you know how to do the conversion for the ultrasonic sensor?

Also how far out can the sensor detect

I think the sensor goes out to 255 inches, but YOU need to look up the specs on whatever sensor you have.

At 255 inches, my sensor delivers 2.550 volts. Surely you can do the math youself?

I’m not entirely sure what sensor you’re using, but this site is fantastic if you’re using a MaxBotix ultrasonic sensor. If you’re getting inconsistent values, you can try sending the data to a moving average or Kalman filter to smooth it.