Skip to content

Autonomous Systems

AI, Algorithms, Robotics (HelloRob | Berea College)

Menu
  • Home
  • Blog Guidelines
  • Schedule
Menu

P3H Hardware – Ultrasonic Sensor

Posted on December 7, 2023

Breakdown of Code

To start working on the ultrasonic sensor code, I had to add the wiringPi library to the raspberry pi. In the beginning of the code, you would add #include <wiringPi.h> to call onto that specific library. Using #define you can define which GPIO pins you will be using. I used wiringPi pin 4 for the trig pin which would be, “#define TRIG 4” so now the code knows which pin the TRIG pin of the ultrasonic sensor is connected to on the raspberry pi.  Setting the GPIO pins to either output or input (echo pin = input ; trig pin = output) in my code I did “pinMode(TRIG, OUTPUT” and this tells the raspberry pi that wiringPi pin 4 needs to be set as an output source. Last general code is to create a function to get the distance from the sensor, and in my code I used “getCM(int echoPin)” which all of the echo pins are set as an input source.

General Circuit

Simplified breakdown of the circuit: all trig pins are connected to a single GPIO pin on the raspberry pi, All ground pins from the sensors are connected to the same ground pin on the raspberry pi, All VCC/power pins are connected to the same 5v pin on raspberry pi. I used three 1k Ohm resistors and three 1.2k ohm resistors for the echo pins. I connected the echo pins to wiringPi pins 5, 6, and 2 on the raspberry pi.

Ultrasonic Sensor Mounts

For the ultrasonic sensor mounts, I used white PLA 3D print filament. 

General measurements / dimensions I got using a digital caliper:

The general dimensions that are listed in the photo below are specific to the mbot I was using because most of the robots have different screw positioning and base plates, so this dimensions may not be suitable for all robots/mbots.

Challenges

The only challenge I had during the hardware project 3 was not calling wiringPiSetup() to initialize the wiringPi library. A simple mistake because at first I was using setup() which is still usable father down the code, but if wiringPiSetup or  wiringPiSetupGpio is not being called, then the rest of the code will not function properly because although you defined which pins are being used, raspberry pi GPIO and wiringPi pins have different numbers. At first I was using GPIO 23, 24, etc. When using wiringPi library, those pin numbers are correct, but not correct because GPIO pin 23 is also wiringPi pin 4. During the early stages I kept using GPIO pin numbers and not wiringPi pin numbers, and after changing the pin numbers, the code functioned properly.

References

Raspberry Pi GPIO Pinout. (n.d.). Pinout.xyz. https://pinout.xyz/

Raspberry gPIo – SparkFun Learn. (n.d.). Learn.sparkfun.com. Retrieved December 7, 2023, from

https://learn.sparkfun.com/tutorials/raspberry-gpio/introduction

RPi: HC-SR04 Ultrasonic Sensor mini-project. (2013, July 16). Try { Work(); } Finally { Code(); }. https://ninedof.wordpress.com/2013/07/16/rpi-hc-sr04-ultrasonic-sensor-mini-project/

‌

simeon

Recent Posts

  • Dancing the Hokey Pokey
  • Python Wall-Following Project
  • From Static to Dynamic: Translating Wall Follower Code from C++ to Python
  • Final Project Reflection Blog – Hardware
  • Enhanced Obstacle Avoidance with Ultrasonic Sensors on MBOT
  • San Francisco Demands Apple and Google Delete AI ‘Nudify’ Apps From App Stores
  • A Humanoid Company Backed by Eric Trump Is Preparing Its Robots for War
  • Why Apple Sued OpenAI, New York Takes on Data Centers, and What to Know about Cyclosporiasis
  • Here’s Why Anthropic Is Pushing States to Regulate AI Faster
  • Please Stop Making Me Opt Out of AI
©2026 Autonomous Systems | Design: Newspaperly WordPress Theme