Project 3 was not only a great success, but a fun journey where I had to tinker with both hardware and software to achieve the desired outcome. This project had 2 major requirements:
- To be able to simultaneously read data from 3 ultrasonic sensors.
- To create brackets for the sensors to be securely housed in.
I initially worked on a separate Raspberry Pi, and started the project by trying to read a singular sensor data. For that, I downloaded and installed the WiringPi library using the tutorial on the SparkFun website. I used sudo apt-get to get the library into my local machine and ran the build file to set it up.
Once it was ready to go, I tested if the WiringPi library worked by building an executable file from the blink.c given in the examples folder of WiringPi directory. After confirming that both the library and GPIO pins were working without any problems, it was the time to get to write the code and build the circuit for the ultrasonic sensors.
As we can see from the picture of the sensor, it is quite simple to connect it to a Raspberry Pi. VCC is power, and it goes to a 5V pin on Raspberry Pi’s GPIO pins. Trig and Echo are wires to transfer data. Trig is for sending an ultrasonic signal into the environment that the sensor is in, and Echo is for receiving the signal back. After that, it is very easy to calculate the distance of the signal from the amount of time it took for the signal to travel from and to the sensor. The final pin is GND, which goes to a Ground pin on Raspberry Pi. One caveat with Raspberry Pi is that it only houses 2 pins for 5V, meaning that you need to use a breadboard or PCB of some sort to feed all 3 sensors with power.
Here are the pins I used to connect the sensors to the Raspberry Pi:
5,6,27,22,10,9, 5V Power, Ground.
I ended up soldering the all of the wires to a breadboard and used it as an intermediary to ensure cleanliness and organization of the cables. Here is how the final product looks like:
After installing the library, making sure it worked, building the circuit, it was the time to write the code. I took most of the code from the example given on Moodle and modified it to have it work with all three sensors. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#define TRIG 21
#define ECHO 22
#define TRIG2 2
#define ECHO2 3
#define TRIG3 12
#define ECHO3 13
void setup() {
wiringPiSetup();
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(TRIG2, OUTPUT);
pinMode(ECHO2, INPUT);
pinMode(TRIG3, OUTPUT);
pinMode(ECHO3, INPUT);
//TRIG pin must start LOW
digitalWrite(TRIG, LOW);
delay(30);
}
int getCM() {
//Send trig pulse
digitalWrite(TRIG, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG, LOW);
//Wait for echo start
while(digitalRead(ECHO) == LOW);
//Wait for echo end
long startTime = micros();
while(digitalRead(ECHO) == HIGH);
long travelTime = micros() - startTime;
//Get distance in cm
int distance = travelTime / 58;
return distance;
}
int getCM2() {
//Send trig pulse
digitalWrite(TRIG2, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG2, LOW);
//Wait for echo start
while(digitalRead(ECHO2) == LOW);
//Wait for echo end
long startTime = micros();
while(digitalRead(ECHO2) == HIGH);
long travelTime = micros() - startTime;
//Get distance in cm
int distance2 = travelTime / 58;
return distance2;
}
int getCM3() {
//Send trig pulse
digitalWrite(TRIG3, HIGH);
delayMicroseconds(20);
digitalWrite(TRIG3, LOW);
//Wait for echo start
while(digitalRead(ECHO3) == LOW);
//Wait for echo end
long startTime = micros();
while(digitalRead(ECHO3) == HIGH);
long travelTime = micros() - startTime;
//Get distance in cm
int distance3 = travelTime / 58;
return distance3;
}
int main(void) {
setup();
printf("Distance 1: %dcm\n", getCM());
printf("Distance 2: %dcm\n", getCM2());
printf("Distance 3: %dcm\n", getCM3());
return 0;
}
In this code above, we first import the required libraries (WiringPi), then assign number values to variables that we will be using with the TRIG and ECHO pins using DEFINE. These number values are the WiringPi numbers for the GPIO pins assigned to them on Raspberry Pi. After that, we set up those pins by specifying whether they are input or output. After that’s done we have a seperate getCM function for each sensor, namely getCM, getCM2, and getCM3. They all return the respective distance values of their sensors. Finally, in the main function, we print out each value by calling each respective function.
This concludes the electronic part of this project. Now I will be mentioning the casing for the sensors.
I used AutoDesk Inventor to 3D model my casing brackets. But before starting modeling, I needed to find the measurements and dimensions for the HC-SR04 sensor. I found it by searching it on Google. After that, it was the time to start designing the casing bracket on Inventor. One thing I kept in mind was the screw placement on the Mbot. They needed to align with the screw ports designed on my bracket. One major problem was the front of the Mbot did not have any screw ports, so I had to adjust my design to fit the closest screw ports on the front end, which was relatively further ahead than the other sides of the Mbot. Here is my 3D model:
After the design process ended, I printed the bracket using the 3D printers available in the Danforth Tech building. I used PLA filament for the printing as it was the only available material in hand. However, although my brackets were aligning with the Mbot well, they were loose and needed to have support in order to say in place more securely. For that reason, I went to the Wood Lab and made special support pieces for the brackets with the help of the ETAD Wood Lab TA. Here what the end product looks like:
Challenges:
- Getting the WiringPi library to work.
- Unexpected stopping of the sensors before soldering the breadboard, costed me additional 5+ hours but got fixed after soldering.
- Learning how to 3D model.
- Learning how to 3D print.
- My bracket being loose, having to support it with additional material
In conclusion, it was a very fun and educational project.
References:
[1] “RPi: HC-SR04 Ultrasonic Sensor mini-project,” try { work(); } finally { code(); }, Jul. 16, 2013. https://ninedof.wordpress.com/2013/07/16/rpi-hc-sr04-ultrasonic-sensor-mini-project/ (accessed Dec. 07, 2023).