We can all agree that it is annoying when you are cycling and out of the sudden you find another cyclist trying to overtake you without signaling and in some cases, you might not even observe him and by accident hit him. So, this project can help with this problem by building an overtake warning system that will use an ultrasonic sensor to track incoming cyclists that plan to overtake you from the side, and it will use a vibration motor which will vibrate to warn you when this unannounced overtake is taking place. Also this can be useful for people like me who like to use earbuds or a headset while cycling.
Components required
To be able to do this project you will need the following components(Every components includes a link from where you can buy them from our website):
1 |
|
1 |
|
1 |
|
7 |
|
1 |
|
1 |
|
Lipo 18650 Battery |
1 |
3D Printed case |
1 |
Small 170 Connection Points Breadboard |
1 |
The files for the 3D Printed Case will be included in the article.
Guide
Prerequisites
You will need to install the Arduino IDE from this link, you use it to program your Arduino Nano.
You can install this version:
In the Arduino IDE you have to go in the Tools tab:
And change the board to the Arduino Nano like this:
And after you connect your board to your computer it will automatically find and select the communication port that it is using.
Hardware
Cable diagram
For powering the Microcontroller Board you can use a 18650 Battery Holder/ Charger from our website, note that you have to provide your own 18650 battery.
You will have to solder two pins on the battery holder like in the picture from below:
The 3D Printed Case
The 3D printed case
The 3D printed Lid
You can get the mesh files so you print the case at this link.
For the you have the option to attach the lid to the box using 4 M4*0.7 screws that are at least 6mm and up to 10 in length, or you can use duct tape for easier disassembly. And for attaching to the bike seat poll I would recommend either duck tape or a very strong double sided tape.
Code
For this project you have to set up the sensor from the code, and to make it easier for you I will break down the code for you, and I will also include the full code at the end of this breakdown.
Setting up the Variables
Variables store information like numbers or words, in our case we use them to easily move information around the code and also we can also use them to give names to the pins from the Arduino, this is going to make using them in the code much easier to use.
It doesn't matter where you initialise them, giving them a starting value, which is recommended every time you want to use them in the code for comparing to other values, for example.
When creating a variable you have to indicate which type of variable you want, depending on the programming languages these types might be different, but in our case “int” means integer and defines that the variable is an integer number.
Const means constant which means that we don't want the value from that variable to change.
After that we have to define the “Speed of Sound” and create two variables without giving initial values.
We are going to use them for the ultrasonic sensor, to be able to calculate the distance.
The ultrasonic sensor uses something similar to echolocation, but in our case we can use the time that it took the wave to hit the object and come back.
Setup phase
By default when creating a new project in the Arduino IDE you get 2 functions, setup and loop.
For the setup function we have to start the serial communication so we can get the distance printed in the terminal. We use this to test the sensor when we have the device connected to the computer from which we program it. Afterwards we are setting the relevant ultrasonic sensor pins to be an input or an output.
Main Function
For our main function we use the pregenerated loop function, which loops the code encapsulated by the loop function.
In the loop function we are getting the readings from our sensor and perform calculations so we can make sure that we are checking at all times if someone wants to do an overtake.
The first thing that we want to do is take readings from the ultrasonic sensor by telling it to send a pulse and tell us when it comes back using this part of code:
We have to use an equation to determine the “distance”, in that equation we take the time that it took the wave to travel the distance till the object and come back. We take the “distance” and we multiply it by the “speed of sound”, which we know is 343 m/s. We are going to use 0,034 because we want to get the distance in cm not in metres, lastly we have to divide by 2 to get only the “distance” from the sensor till the object.
Distance =(Duration * Speed of Sound)/ 2
In code we have to write it like this:
To print this in the terminal we use this code:
Now we have the part where we decide if someone wants to overtake us or not:
Here we take readings and if something within 2 metres is changing its distance compared to the sensor very fast, then we will take it as another cyclist that wants to overtake us, you can adjust the condition from the if to calibrate the sensor for you needs.
When that happens we will trigger our vibration motor. This motor uses analog values to determine its strength, by writing an analog value(PWM) of 255 to the vibration motor.
In my case I wanted the maximum value so I could use it for testing, but you can change it as you wish.
This is the full code:
const int trigPin = 9;
const int echoPin = 8;
const int pwmPin = 5;
int x=0;
int y=0;
#define Speed_of_Sound 0.034
long duration;
float distance;
void setup()
{
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pwmPin, OUTPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * Speed_of_Sound/2;
Serial.print("The distance is: ");
Serial.println(distance);
x=distance;
if(y-x>=90&&x<200)
{
Serial.print("Someone comes");
digitalWrite(pwmPin,HIGH);
delay(1000);
digitalWrite(pwmPin,LOW);
}
else
if(x<200)
y=x;
delay(1000);
}
You can access the full code at this link.
After you write or copy paste the code in the Arduino IDE you can upload it to the Microcontroller Board by pressing the upload button which looks like this: